Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/highgui/src/window_QT.h
16337 views
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
#include "precomp.hpp"
44
45
#ifndef _DEBUG
46
#define QT_NO_DEBUG_OUTPUT
47
#endif
48
49
#if defined( HAVE_QT_OPENGL )
50
#include <QtOpenGL>
51
#include <QGLWidget>
52
#endif
53
54
#include <QAbstractEventDispatcher>
55
#include <QApplication>
56
#include <QFile>
57
#include <QPushButton>
58
#include <QGraphicsView>
59
#include <QSizePolicy>
60
#include <QInputDialog>
61
#include <QBoxLayout>
62
#include <QSettings>
63
#include <qtimer.h>
64
#include <QtConcurrentRun>
65
#include <QWaitCondition>
66
#include <QKeyEvent>
67
#include <QMetaObject>
68
#include <QPointer>
69
#include <QSlider>
70
#include <QLabel>
71
#include <QIODevice>
72
#include <QShortcut>
73
#include <QStatusBar>
74
#include <QVarLengthArray>
75
#include <QFileInfo>
76
#include <QDate>
77
#include <QFileDialog>
78
#include <QToolBar>
79
80
#include <QAction>
81
#include <QCheckBox>
82
#include <QRadioButton>
83
#include <QButtonGroup>
84
#include <QMenu>
85
#include <QTest>
86
87
//start private enum
88
enum { CV_MODE_NORMAL = 0, CV_MODE_OPENGL = 1 };
89
90
//we can change the keyboard shortcuts from here !
91
enum { shortcut_zoom_normal = Qt::CTRL + Qt::Key_Z,
92
shortcut_zoom_imgRegion = Qt::CTRL + Qt::Key_X,
93
shortcut_save_img = Qt::CTRL + Qt::Key_S,
94
shortcut_properties_win = Qt::CTRL + Qt::Key_P,
95
shortcut_zoom_in = Qt::CTRL + Qt::Key_Plus,//QKeySequence(QKeySequence::ZoomIn),
96
shortcut_zoom_out = Qt::CTRL + Qt::Key_Minus,//QKeySequence(QKeySequence::ZoomOut),
97
shortcut_panning_left = Qt::CTRL + Qt::Key_Left,
98
shortcut_panning_right = Qt::CTRL + Qt::Key_Right,
99
shortcut_panning_up = Qt::CTRL + Qt::Key_Up,
100
shortcut_panning_down = Qt::CTRL + Qt::Key_Down
101
};
102
//end enum
103
104
class CvWindow;
105
class ViewPort;
106
107
108
class GuiReceiver : public QObject
109
{
110
Q_OBJECT
111
112
public:
113
GuiReceiver();
114
~GuiReceiver();
115
116
int start();
117
void isLastWindow();
118
119
bool bTimeOut;
120
QTimer* timer;
121
122
public slots:
123
void createWindow( QString name, int flags = 0 );
124
void destroyWindow(QString name);
125
void destroyAllWindow();
126
void addSlider(QString trackbar_name, QString window_name, void* value, int count, void* on_change);
127
void addSlider2(QString trackbar_name, QString window_name, void* value, int count, void* on_change, void *userdata);
128
void moveWindow(QString name, int x, int y);
129
void resizeWindow(QString name, int width, int height);
130
void showImage(QString name, void* arr);
131
void displayInfo( QString name, QString text, int delayms );
132
void displayStatusBar( QString name, QString text, int delayms );
133
void timeOut();
134
void toggleFullScreen(QString name, double flags );
135
CvRect getWindowRect(QString name);
136
double isFullScreen(QString name);
137
double getPropWindow(QString name);
138
void setPropWindow(QString name, double flags );
139
void setWindowTitle(QString name, QString title);
140
double getWindowVisible(QString name);
141
double getRatioWindow(QString name);
142
void setRatioWindow(QString name, double arg2 );
143
void saveWindowParameters(QString name);
144
void loadWindowParameters(QString name);
145
void putText(void* arg1, QString text, QPoint org, void* font);
146
void addButton(QString button_name, int button_type, int initial_button_state , void* on_change, void* userdata);
147
void enablePropertiesButtonEachWindow();
148
149
void setOpenGlDrawCallback(QString name, void* callback, void* userdata);
150
void setOpenGlContext(QString name);
151
void updateWindow(QString name);
152
double isOpenGl(QString name);
153
154
private:
155
int nb_windows;
156
bool doesExternalQAppExist;
157
};
158
159
160
enum typeBar { type_CvTrackbar = 0, type_CvButtonbar = 1 };
161
class CvBar : public QHBoxLayout
162
{
163
public:
164
typeBar type;
165
QString name_bar;
166
QPointer<QWidget> myparent;
167
};
168
169
170
class CvButtonbar : public CvBar
171
{
172
Q_OBJECT
173
public:
174
CvButtonbar(QWidget* arg, QString bar_name);
175
176
void addButton(QString button_name, CvButtonCallback call, void* userdata, int button_type, int initial_button_state);
177
178
private:
179
void setLabel();
180
181
QPointer<QLabel> label;
182
QPointer<QButtonGroup> group_button;
183
};
184
185
186
class CvPushButton : public QPushButton
187
{
188
Q_OBJECT
189
public:
190
CvPushButton(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata);
191
192
private:
193
CvButtonbar* myparent;
194
QString button_name ;
195
CvButtonCallback callback;
196
void* userdata;
197
198
private slots:
199
void callCallBack(bool);
200
};
201
202
203
class CvCheckBox : public QCheckBox
204
{
205
Q_OBJECT
206
public:
207
CvCheckBox(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata, int initial_button_state);
208
209
private:
210
CvButtonbar* myparent;
211
QString button_name ;
212
CvButtonCallback callback;
213
void* userdata;
214
215
private slots:
216
void callCallBack(bool);
217
};
218
219
220
class CvRadioButton : public QRadioButton
221
{
222
Q_OBJECT
223
public:
224
CvRadioButton(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata, int initial_button_state);
225
226
private:
227
CvButtonbar* myparent;
228
QString button_name ;
229
CvButtonCallback callback;
230
void* userdata;
231
232
private slots:
233
void callCallBack(bool);
234
};
235
236
237
class CvTrackbar : public CvBar
238
{
239
Q_OBJECT
240
public:
241
CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback on_change);
242
CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback2 on_change, void* data);
243
244
QPointer<QSlider> slider;
245
246
private slots:
247
void createDialog();
248
void update(int myvalue);
249
250
private:
251
void setLabel(int myvalue);
252
void create(CvWindow* arg, QString name, int* value, int count);
253
QString createLabel();
254
QPointer<QPushButton > label;
255
CvTrackbarCallback callback;
256
CvTrackbarCallback2 callback2;//look like it is use by python binding
257
int* dataSlider;
258
void* userdata;
259
};
260
261
//Both are top level window, so that a way to differentiate them.
262
//if (obj->metaObject ()->className () == "CvWindow") does not give me robust result
263
264
enum typeWindow { type_CvWindow = 1, type_CvWinProperties = 2 };
265
class CvWinModel : public QWidget
266
{
267
public:
268
typeWindow type;
269
};
270
271
272
class CvWinProperties : public CvWinModel
273
{
274
Q_OBJECT
275
public:
276
CvWinProperties(QString name, QObject* parent);
277
~CvWinProperties();
278
QPointer<QBoxLayout> myLayout;
279
280
private:
281
void closeEvent ( QCloseEvent * e ) CV_OVERRIDE;
282
void showEvent ( QShowEvent * event ) CV_OVERRIDE;
283
void hideEvent ( QHideEvent * event ) CV_OVERRIDE;
284
};
285
286
287
class CvWindow : public CvWinModel
288
{
289
Q_OBJECT
290
public:
291
CvWindow(QString arg2, int flag = CV_WINDOW_NORMAL);
292
~CvWindow();
293
294
void setMouseCallBack(CvMouseCallback m, void* param);
295
296
void writeSettings();
297
void readSettings();
298
299
double getRatio();
300
void setRatio(int flags);
301
302
CvRect getWindowRect();
303
int getPropWindow();
304
void setPropWindow(int flags);
305
306
void toggleFullScreen(int flags);
307
308
void updateImage(void* arr);
309
310
void displayInfo(QString text, int delayms);
311
void displayStatusBar(QString text, int delayms);
312
313
void enablePropertiesButton();
314
315
static CvButtonbar* createButtonBar(QString bar_name);
316
317
static void addSlider(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback on_change CV_DEFAULT(NULL));
318
static void addSlider2(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback2 on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(0));
319
320
void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata);
321
void makeCurrentOpenGlContext();
322
void updateGl();
323
bool isOpenGl();
324
325
void setViewportSize(QSize size);
326
327
//parameters (will be save/load)
328
int param_flags;
329
int param_gui_mode;
330
int param_ratio_mode;
331
332
QPointer<QBoxLayout> myGlobalLayout; //All the widget (toolbar, view, LayoutBar, ...) are attached to it
333
QPointer<QBoxLayout> myBarLayout;
334
335
QVector<QAction*> vect_QActions;
336
337
QPointer<QStatusBar> myStatusBar;
338
QPointer<QToolBar> myToolBar;
339
QPointer<QLabel> myStatusBar_msg;
340
341
protected:
342
virtual void keyPressEvent(QKeyEvent* event) CV_OVERRIDE;
343
344
private:
345
346
int mode_display; //opengl or native
347
ViewPort* myView;
348
349
QVector<QShortcut*> vect_QShortcuts;
350
351
void icvLoadTrackbars(QSettings *settings);
352
void icvSaveTrackbars(QSettings *settings);
353
void icvLoadControlPanel();
354
void icvSaveControlPanel();
355
void icvLoadButtonbar(CvButtonbar* t,QSettings *settings);
356
void icvSaveButtonbar(CvButtonbar* t,QSettings *settings);
357
358
void createActions();
359
void createShortcuts();
360
void createToolBar();
361
void createView();
362
void createStatusBar();
363
void createGlobalLayout();
364
void createBarLayout();
365
CvWinProperties* createParameterWindow();
366
367
void hideTools();
368
void showTools();
369
QSize getAvailableSize();
370
371
private slots:
372
void displayPropertiesWin();
373
};
374
375
376
enum type_mouse_event { mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3, mouse_wheel = 4 };
377
static const int tableMouseButtons[][3]={
378
{CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP}, //mouse_up
379
{CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN}, //mouse_down
380
{CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK}, //mouse_dbclick
381
{CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE}, //mouse_move
382
{0, 0, 0} //mouse_wheel, to prevent exceptions in code
383
};
384
385
386
class ViewPort
387
{
388
public:
389
virtual ~ViewPort() {}
390
391
virtual QWidget* getWidget() = 0;
392
393
virtual void setMouseCallBack(CvMouseCallback callback, void* param) = 0;
394
395
virtual void writeSettings(QSettings& settings) = 0;
396
virtual void readSettings(QSettings& settings) = 0;
397
398
virtual double getRatio() = 0;
399
virtual void setRatio(int flags) = 0;
400
401
virtual void updateImage(const CvArr* arr) = 0;
402
403
virtual void startDisplayInfo(QString text, int delayms) = 0;
404
405
virtual void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) = 0;
406
virtual void makeCurrentOpenGlContext() = 0;
407
virtual void updateGl() = 0;
408
409
virtual void setSize(QSize size_) = 0;
410
};
411
412
413
class OCVViewPort : public ViewPort
414
{
415
public:
416
explicit OCVViewPort();
417
~OCVViewPort() CV_OVERRIDE {};
418
void setMouseCallBack(CvMouseCallback callback, void* param) CV_OVERRIDE;
419
420
protected:
421
void icvmouseEvent(QMouseEvent* event, type_mouse_event category);
422
void icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags);
423
virtual void icvmouseProcessing(QPointF pt, int cv_event, int flags);
424
425
CvMouseCallback mouseCallback;
426
void* mouseData;
427
};
428
429
430
#ifdef HAVE_QT_OPENGL
431
432
class OpenGlViewPort : public QGLWidget, public OCVViewPort
433
{
434
public:
435
explicit OpenGlViewPort(QWidget* parent);
436
~OpenGlViewPort() CV_OVERRIDE;
437
438
QWidget* getWidget() CV_OVERRIDE;
439
440
void writeSettings(QSettings& settings) CV_OVERRIDE;
441
void readSettings(QSettings& settings) CV_OVERRIDE;
442
443
double getRatio() CV_OVERRIDE;
444
void setRatio(int flags) CV_OVERRIDE;
445
446
void updateImage(const CvArr* arr) CV_OVERRIDE;
447
448
void startDisplayInfo(QString text, int delayms) CV_OVERRIDE;
449
450
void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) CV_OVERRIDE;
451
void makeCurrentOpenGlContext() CV_OVERRIDE;
452
void updateGl() CV_OVERRIDE;
453
454
void setSize(QSize size_) CV_OVERRIDE;
455
456
protected:
457
void initializeGL() CV_OVERRIDE;
458
void resizeGL(int w, int h) CV_OVERRIDE;
459
void paintGL() CV_OVERRIDE;
460
461
void wheelEvent(QWheelEvent* event) CV_OVERRIDE;
462
void mouseMoveEvent(QMouseEvent* event) CV_OVERRIDE;
463
void mousePressEvent(QMouseEvent* event) CV_OVERRIDE;
464
void mouseReleaseEvent(QMouseEvent* event) CV_OVERRIDE;
465
void mouseDoubleClickEvent(QMouseEvent* event) CV_OVERRIDE;
466
467
QSize sizeHint() const CV_OVERRIDE;
468
469
private:
470
QSize size;
471
472
CvOpenGlDrawCallback glDrawCallback;
473
void* glDrawData;
474
};
475
476
#endif // HAVE_QT_OPENGL
477
478
479
class DefaultViewPort : public QGraphicsView, public OCVViewPort
480
{
481
Q_OBJECT
482
483
public:
484
DefaultViewPort(CvWindow* centralWidget, int arg2);
485
~DefaultViewPort() CV_OVERRIDE;
486
487
QWidget* getWidget() CV_OVERRIDE;
488
489
void writeSettings(QSettings& settings) CV_OVERRIDE;
490
void readSettings(QSettings& settings) CV_OVERRIDE;
491
492
double getRatio() CV_OVERRIDE;
493
void setRatio(int flags) CV_OVERRIDE;
494
495
void updateImage(const CvArr* arr) CV_OVERRIDE;
496
497
void startDisplayInfo(QString text, int delayms) CV_OVERRIDE;
498
499
void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) CV_OVERRIDE;
500
void makeCurrentOpenGlContext() CV_OVERRIDE;
501
void updateGl() CV_OVERRIDE;
502
503
void setSize(QSize size_) CV_OVERRIDE;
504
505
public slots:
506
//reference:
507
//http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming
508
//http://doc.qt.nokia.com/4.6/gestures-imagegestures-imagewidget-cpp.html
509
510
void siftWindowOnLeft();
511
void siftWindowOnRight();
512
void siftWindowOnUp() ;
513
void siftWindowOnDown();
514
515
void resetZoom();
516
void imgRegion();
517
void ZoomIn();
518
void ZoomOut();
519
520
void saveView();
521
522
protected:
523
void contextMenuEvent(QContextMenuEvent* event) CV_OVERRIDE;
524
void resizeEvent(QResizeEvent* event) CV_OVERRIDE;
525
void paintEvent(QPaintEvent* paintEventInfo) CV_OVERRIDE;
526
527
void wheelEvent(QWheelEvent* event) CV_OVERRIDE;
528
void mouseMoveEvent(QMouseEvent* event) CV_OVERRIDE;
529
void mousePressEvent(QMouseEvent* event) CV_OVERRIDE;
530
void mouseReleaseEvent(QMouseEvent* event) CV_OVERRIDE;
531
void mouseDoubleClickEvent(QMouseEvent* event) CV_OVERRIDE;
532
533
private:
534
int param_keepRatio;
535
536
//parameters (will be save/load)
537
QTransform param_matrixWorld;
538
539
CvMat* image2Draw_mat;
540
QImage image2Draw_qt;
541
int nbChannelOriginImage;
542
543
544
void scaleView(qreal scaleFactor, QPointF center);
545
void moveView(QPointF delta);
546
547
QPoint mouseCoordinate;
548
QPointF positionGrabbing;
549
QRect positionCorners;
550
QTransform matrixWorld_inv;
551
float ratioX, ratioY;
552
553
bool isSameSize(IplImage* img1,IplImage* img2);
554
555
QSize sizeHint() const CV_OVERRIDE;
556
QPointer<CvWindow> centralWidget;
557
QPointer<QTimer> timerDisplay;
558
bool drawInfo;
559
QString infoText;
560
QRectF target;
561
562
void drawInstructions(QPainter *painter);
563
void drawViewOverview(QPainter *painter);
564
void drawImgRegion(QPainter *painter);
565
void draw2D(QPainter *painter);
566
void drawStatusBar();
567
void controlImagePosition();
568
569
void icvmouseProcessing(QPointF pt, int cv_event, int flags) CV_OVERRIDE;
570
571
private slots:
572
void stopDisplayInfo();
573
};
574
575
#endif
576
577