ImGraph
 All Classes Functions Variables Enumerations Pages
MatrixViewer.h
1 //IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
2 
3 // By downloading, copying, installing or using the software you agree to this license.
4 // If you do not agree to this license, do not download, install,
5 // copy or use the software.
6 
7 
8 // License Agreement
9 // For Open Source Computer Vision Library
10 
11 //Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
12 //Copyright (C) 2008-2010, Willow Garage Inc., all rights reserved.
13 //Third party copyrights are property of their respective owners.
14 
15 //Redistribution and use in source and binary forms, with or without modification,
16 //are permitted provided that the following conditions are met:
17 
18 // * Redistribution's of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
20 
21 // * Redistribution's in binary form must reproduce the above copyright notice,
22 // this list of conditions and the following disclaimer in the documentation
23 // and/or other materials provided with the distribution.
24 
25 // * The name of the copyright holders may not be used to endorse or promote products
26 // derived from this software without specific prior written permission.
27 
28 //This software is provided by the copyright holders and contributors "as is" and
29 //any express or implied warranties, including, but not limited to, the implied
30 //warranties of merchantability and fitness for a particular purpose are disclaimed.
31 //In no event shall the Intel Corporation or contributors be liable for any direct,
32 //indirect, incidental, special, exemplary, or consequential damages
33 //(including, but not limited to, procurement of substitute goods or services;
34 //loss of use, data, or profits; or business interruption) however caused
35 //and on any theory of liability, whether in contract, strict liability,
36 //or tort (including negligence or otherwise) arising in any way out of
37 //the use of this software, even if advised of the possibility of such damage.
38 
39 //--------------------Google Code 2010 -- Yannick Verdie--------------------//
40 #ifndef __OPENCV_HIGHGUI_QT_H__
41 #define __OPENCV_HIGHGUI_QT_H__
42 
43 #ifdef _WIN32
44 #pragma warning(disable:4503)
45 #pragma warning(push)
46 #pragma warning(disable:4996 4251 4275 4800)
47 #endif
48 #include "opencv2/core/utility.hpp"
49 #include "opencv2/highgui/highgui_c.h"
50 #include "opencv2/imgproc.hpp"
51 
52 #include <QAbstractEventDispatcher>
53 #include <QApplication>
54 #include <QFile>
55 #include <QPushButton>
56 #include <QGraphicsView>
57 #include <QSizePolicy>
58 #include <QInputDialog>
59 #include <QBoxLayout>
60 #include <QSettings>
61 #include <qtimer.h>
62 #include <QtConcurrent/QtConcurrentRun>
63 #include <QWaitCondition>
64 #include <QKeyEvent>
65 #include <QMetaObject>
66 #include <QPointer>
67 #include <QSlider>
68 #include <QLabel>
69 #include <QIODevice>
70 #include <QShortcut>
71 #include <QStatusBar>
72 #include <QVarLengthArray>
73 #include <QFileInfo>
74 #include <QDate>
75 #include <QFileDialog>
76 #include <QToolBar>
77 #include <QAction>
78 #include <QPushButton>
79 #include <QCheckBox>
80 #include <QRadioButton>
81 #include <QButtonGroup>
82 #include <QMenu>
83 #include <QDialog>
84 
85 #include <boost/thread/mutex.hpp>
86 #include <boost/thread/recursive_mutex.hpp>
87 #include <boost/thread/condition_variable.hpp>
88 #ifdef _WIN32
89 #pragma warning(pop)
90 #endif
91 
92 class MatrixViewer;
93 class DefaultViewPort;
94 
95 //start private enum
96 enum { CV_MODE_NORMAL = 0, CV_MODE_OPENGL = 1 };
97 
98 //we can change the keyboard shortcuts from here !
99 enum {
100  shortcut_zoom_normal = Qt::CTRL + Qt::Key_Z,
101  shortcut_zoom_imgRegion = Qt::CTRL + Qt::Key_X,
102  shortcut_save_img = Qt::CTRL + Qt::Key_S,
103  shortcut_edit_pen_img = Qt::CTRL + Qt::Key_E,
104  shortcut_properties_win = Qt::CTRL + Qt::Key_P,
105  shortcut_zoom_in = Qt::CTRL + Qt::Key_Plus,//QKeySequence(QKeySequence::ZoomIn),
106  shortcut_zoom_out = Qt::CTRL + Qt::Key_Minus,//QKeySequence(QKeySequence::ZoomOut),
107  shortcut_panning_left = Qt::CTRL + Qt::Key_Left,
108  shortcut_panning_right = Qt::CTRL + Qt::Key_Right,
109  shortcut_panning_up = Qt::CTRL + Qt::Key_Up,
110  shortcut_panning_down = Qt::CTRL + Qt::Key_Down,
111  shortcut_always_top = Qt::CTRL + Qt::Key_T
112 };
113 
114 //end enum
115 #define _WINDOW_MATRIX_CREATION_MODE 0x00010000
116 
117 class ToolsWindow : public QWidget
118 {
119  Q_OBJECT
120 public:
121  ToolsWindow(QString name, MatrixViewer* parent);
122  ~ToolsWindow();
123 
124  void addWidget(QWidget* obj);
125  void linkWithMatrix(MatrixViewer* mView);
126 
127 protected:
128  void closeEvent(QCloseEvent * e);
129  void showEvent(QShowEvent * event);
130  void hideEvent(QHideEvent * event);
131 
132  MatrixViewer* _parent;
133 
134  QPushButton* _updateMatrix;
135  QComboBox* _matrixTypes;
136  QComboBox* _matrixVals;
137  QLineEdit* _rows;
138  QLineEdit* _cols;
139  QLineEdit* _channels;
140 
141  QPushButton* color_choose;
142  QLineEdit* pencilSize;
143 
144  QPointer<QBoxLayout> myLayout;
145 
146  public slots:
147  void updateMatrix();
148 };
149 
150 #define __ACT_IMGRAPH_LOAD 0
151 #define __ACT_IMGRAPH_SAVE __ACT_IMGRAPH_LOAD +1
152 #define __ACT_IMGRAPH_LEFT __ACT_IMGRAPH_SAVE +1
153 #define __ACT_IMGRAPH_RIGHT __ACT_IMGRAPH_LEFT +1
154 #define __ACT_IMGRAPH_UP __ACT_IMGRAPH_RIGHT +1
155 #define __ACT_IMGRAPH_DOWN __ACT_IMGRAPH_UP +1
156 #define __ACT_IMGRAPH_ZOOM_X1 __ACT_IMGRAPH_DOWN +1
157 #define __ACT_IMGRAPH_ZOOM_IN __ACT_IMGRAPH_ZOOM_X1 +1
158 #define __ACT_IMGRAPH_ZOOM_OUT __ACT_IMGRAPH_ZOOM_IN +1
159 #define __ACT_IMGRAPH_PEN_EDIT __ACT_IMGRAPH_ZOOM_OUT +1
160 #define __ACT_IMGRAPH_ONTOP __ACT_IMGRAPH_PEN_EDIT +1
161 
162 class MatrixViewer : public QDialog
163 {
164  Q_OBJECT;
165 
166 public:
167  MatrixViewer(QString arg2, int flag = CV_WINDOW_NORMAL);
168  ~MatrixViewer();
169 
170  void writeSettings();
171  void readSettings();
172 
173  int getPropWindow();
174  void setPropWindow(int flags);
175 
176  bool isPencil_mode() const { return pencil_mode; };
177 
178  void toggleFullScreen(int flags);
179 
180  void updateImage(cv::Mat arr);
181  cv::Mat getMatrix();
182 
183  void displayInfo(QString text, int delayms);
184  void displayStatusBar(QString text, int delayms);
185 
186  void enablePropertiesButton();
187 
188  void setViewportSize(QSize size);
189 
190  DefaultViewPort* view(){ return myView; };
191 
192  //parameters (will be save/load)
193  int param_flags;
194  int param_gui_mode;
195  int param_creation_mode;
196 
197  QPointer<QBoxLayout> myGlobalLayout; //All the widget (toolbar, view, LayoutBar, ...) are attached to it
198 
199  QVector<QAction*> vect_QActions;
200 
201  QPointer<QStatusBar> myStatusBar;
202  QPointer<QToolBar> myToolBar;
203  QPointer<QLabel> myStatusBar_msg;
204 
205  public slots:
206  void changePenSize();
207 protected:
208  virtual void keyPressEvent(QKeyEvent* event);
209 
210  ToolsWindow* myTools;
211 private:
212  bool isOnTop;
213  bool pencil_mode;
214  int mode_display; //opengl or native
215  DefaultViewPort* myView;
216 
217  QVector<QShortcut*> vect_QShortcuts;
218 
219  void createActions();
220  void createShortcuts();
221  void createView();
222  void createGlobalLayout();
223  ToolsWindow* createParameterWindow();
224 
225  void hideTools();
226  void showTools();
227  QSize getAvailableSize();
228 
229  private slots:
230  void displayPropertiesWin();
231  void switchEditingImg();
232  void chooseColor();
233  void switchOnTop();
234 };
235 
236 
237 enum _typemouse_event { mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3 };
238 static const int tableMouseButtons[][3] = {
239  { CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP }, //mouse_up
240  { CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN }, //mouse_down
241  { CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK }, //mouse_dbclick
242  { CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE } //mouse_move
243 };
244 
245 
246 class ViewPort
247 {
248 public:
249  virtual ~ViewPort() {}
250 
251  virtual QWidget* getWidget() = 0;
252 
253  virtual void writeSettings(QSettings& settings) = 0;
254  virtual void readSettings(QSettings& settings) = 0;
255 
256  virtual void updateImage(const cv::Mat arr) = 0;
257 
258  virtual void startDisplayInfo(QString text, int delayms) = 0;
259 
260  virtual void setSize(QSize size_) = 0;
261 };
262 
263 
264 
265 
266 class DefaultViewPort : public QGraphicsView, public ViewPort
267 {
268  Q_OBJECT
269 
270 public:
271  boost::recursive_mutex _mtx; // explicit mutex declaration
272 
273  DefaultViewPort(MatrixViewer* centralWidget);
274  ~DefaultViewPort();
275 
276  QWidget* getWidget();
277 
278  void writeSettings(QSettings& settings);
279  void readSettings(QSettings& settings);
280 
281  double getRatio();
282  void setRatio(int flags);
283 
284  void updateImage(const cv::Mat arr);
285 
286  void startDisplayInfo(QString text, int delayms);
287 
288  void setSize(QSize size_);
289 
290  void updateViewport(){ viewport()->update(); };
291 
292  public slots:
293  //reference:
294  //http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming
295  //http://doc.qt.nokia.com/4.6/gestures-imagegestures-imagewidget-cpp.html
296 
297  void siftWindowOnLeft();
298  void siftWindowOnRight();
299  void siftWindowOnUp();
300  void siftWindowOnDown();
301 
302  void resetZoom();
303  void imgRegion();
304  void ZoomIn();
305  void ZoomOut();
306 
307  void loadMatrix();
308  void saveView();
309  QColor getPenColor() const { return myPenColor; }
310  void setPenColor(QColor val) { myPenColor = val; }
311  float getPenSize() const { return myPenWidth; }
312  void setPenSize(float newSize) { myPenWidth = newSize; }
313 
314  void updateImage();
315  cv::Mat getMatrix();
316 protected:
317  void contextMenuEvent(QContextMenuEvent* event);
318  void resizeEvent(QResizeEvent* event);
319  void paintEvent(QPaintEvent* paintEventInfo);
320  void wheelEvent(QWheelEvent* event);
321  void mouseMoveEvent(QMouseEvent* event);
322  void mousePressEvent(QMouseEvent* event);
323  void mouseReleaseEvent(QMouseEvent* event);
324  void mouseDoubleClickEvent(QMouseEvent* event);
325 
326  void drawLineTo(const QPointF &endPoint);
327  QPoint toImgCoord(QPointF src);
328 
329 private:
330  QPointF lastPoint;
331 
332  QColor myPenColor;
333  float myPenWidth;
334 
335  //parameters (will be save/load)
336  QTransform param_matrixWorld;
337 
338  cv::Mat image2Draw_mat;
339  cv::Mat image_copy;
340  QImage image2Draw_qt;
341  int nbChannelOriginImage;
342 
343  void scaleView(qreal scaleFactor, QPointF center);
344  void moveView(QPointF delta);
345 
346  QPoint mouseCoordinate;
347  QPoint positionGrabbing;
348  QRect positionCorners;
349  QTransform matrixWorld_inv;
350  float ratioX, ratioY;
351 
352  bool isSameSize(IplImage* img1, IplImage* img2);
353 
354  QSize sizeHint() const;
355  QPointer<MatrixViewer> centralWidget;
356  QPointer<QTimer> timerDisplay;
357  bool drawInfo;
358  QString infoText;
359  QRectF target;
360  bool labelsShown;
361 
362  QPoint pixelEdit;
363  int canalEdit;
364  bool updateEdits;
365  QLineEdit *imgEditPixel_R;
366  QLineEdit *imgEditPixel_G;
367  QLineEdit *imgEditPixel_B;
368  QLineEdit *imgEditPixel_A;
369 
370  void drawInstructions(QPainter *painter);
371  void drawViewOverview(QPainter *painter);
372  void drawImgRegion(QPainter *painter);
373  void draw2D(QPainter *painter);
374  void drawStatusBar();
375  void controlImagePosition();
376  void icvmouseProcessing(QPointF pt, int cv_event, int flags);
377 
378  private slots:
379  void stopDisplayInfo();
380 };
381 
382 #endif
Definition: MatrixViewer.h:266
Definition: MatrixViewer.h:162
Definition: MatrixViewer.h:246
Definition: MatrixViewer.h:117