00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "PixmapWidget.hpp"
00024
00025 #include <QPaintEvent>
00026 #include <QPainter>
00027 #include <QMouseEvent>
00028 #include <QKeyEvent>
00029
00030 #ifndef QT_NO_DEBUG
00031 #include <iostream>
00032 #endif
00033
00034 PixmapWidget :: PixmapWidget(QImage* image, QWidget* parent)
00035 :QWidget(parent),
00036 img(image),
00037 clicked(false)
00038 {
00039
00040 this->setMinimumSize( img->width(), img->height() );
00041
00042
00043
00044 this->setFocusPolicy ( Qt::ClickFocus );
00045
00046 #ifndef QT_NO_DEBUG
00047 std::cout << "PixmapWidget created" << std::endl;
00048 #endif
00049 }
00050
00051 PixmapWidget :: ~PixmapWidget(void)
00052 {
00053
00054 #ifndef QT_NO_DEBUG
00055 std::cout << "PixmapWidget deleted" << std::endl;
00056 #endif
00057 }
00058
00059
00060 void PixmapWidget ::mousePressEvent(QMouseEvent* event)
00061 {
00062 #ifndef QT_NO_DEBUG
00063 std::cout << "\tPixmapWidget:: Mouse pressed (" << event->pos().x() << ";" << event->pos().y() << ")" << std::endl;
00064 #endif
00065
00066 if ( event->button() == Qt::LeftButton )
00067 {
00068 startMousePosition = event->pos();
00069 actualMousePosition = event->pos();
00070 clicked = true;
00071
00072 repaint();
00073 }
00074 else if ( event->button() == Qt::RightButton )
00075 {
00076 emit unzoom();
00077 }
00078 }
00079
00080 void PixmapWidget :: mouseMoveEvent(QMouseEvent* event)
00081 {
00082 if ( clicked )
00083 {
00084 #ifndef QT_NO_DEBUG
00085 std::cout << "\tPixmapWidget:: Mouse pressed" << std::endl;
00086 #endif
00087
00088 actualMousePosition = event->pos();
00089
00090 repaint();
00091 }
00092 }
00093
00094 void PixmapWidget :: mouseReleaseEvent(QMouseEvent* event)
00095 {
00096 #ifndef QT_NO_DEBUG
00097 std::cout << "\tPixmapWidget:: Mouse released (" << event->pos().x() << ";" << event->pos().y() << ")" << std::endl;
00098 #endif
00099
00100 if ( clicked == true )
00101 {
00102 clicked = false;
00103
00104
00105 if ( startMousePosition != event->pos())
00106 {
00107 emit zoom(startMousePosition, event->pos());
00108 }
00109
00110 repaint();
00111 }
00112 }
00113
00114 void PixmapWidget :: keyReleaseEvent(QKeyEvent* event)
00115 {
00116 #ifndef QT_NO_DEBUG
00117 std::cout << "\tPixmapWidget:: Key released (" << std::hex << event->key() << std::dec << ")" << std::endl;
00118 #endif
00119
00120 if ( event->key() == Qt::Key_Escape )
00121 {
00122 clicked = false;
00123
00124 repaint();
00125 }
00126 }
00127
00128 void PixmapWidget :: paintEvent(QPaintEvent* )
00129 {
00130 QPainter painter(this);
00131
00132
00133
00134
00135 painter.drawPixmap(0, 0, QPixmap::fromImage(*img));
00136
00137 if ( clicked )
00138 {
00139 painter.setPen(Qt::white);
00140 painter.drawRect(startMousePosition.x(), startMousePosition.y(), actualMousePosition.x() - startMousePosition.x(), actualMousePosition.y() - startMousePosition.y());
00141 }
00142 }