window_QT.h 15.5 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
//IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.

// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.


//                          License Agreement
//               For Open Source Computer Vision Library

//Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
//Copyright (C) 2008-2010, Willow Garage Inc., all rights reserved.
//Third party copyrights are property of their respective owners.

//Redistribution and use in source and binary forms, with or without modification,
//are permitted provided that the following conditions are met:

//  * Redistribution's of source code must retain the above copyright notice,
//  this list of conditions and the following disclaimer.

//  * Redistribution's in binary form must reproduce the above copyright notice,
//  this list of conditions and the following disclaimer in the documentation
//  and/or other materials provided with the distribution.

//  * The name of the copyright holders may not be used to endorse or promote products
//  derived from this software without specific prior written permission.

//This software is provided by the copyright holders and contributors "as is" and
//any express or implied warranties, including, but not limited to, the implied
//warranties of merchantability and fitness for a particular purpose are disclaimed.
//In no event shall the Intel Corporation or contributors be liable for any direct,
//indirect, incidental, special, exemplary, or consequential damages
//(including, but not limited to, procurement of substitute goods or services;
//loss of use, data, or profits; or business interruption) however caused
//and on any theory of liability, whether in contract, strict liability,
//or tort (including negligence or otherwise) arising in any way out of
//the use of this software, even if advised of the possibility of such damage.

//--------------------Google Code 2010 -- Yannick Verdie--------------------//
#ifndef __OPENCV_HIGHGUI_QT_H__
#define __OPENCV_HIGHGUI_QT_H__

#include "precomp.hpp"

#if defined( HAVE_QT_OPENGL )
#include <QtOpenGL>
#include <QGLWidget>
#endif

#include <QAbstractEventDispatcher>
#include <QApplication>
#include <QFile>
#include <QPushButton>
#include <QGraphicsView>
#include <QSizePolicy>
#include <QInputDialog>
#include <QBoxLayout>
#include <QSettings>
#include <qtimer.h>
#include <QtConcurrentRun>
#include <QWaitCondition>
#include <QKeyEvent>
#include <QMetaObject>
#include <QPointer>
#include <QSlider>
#include <QLabel>
#include <QIODevice>
#include <QShortcut>
#include <QStatusBar>
#include <QVarLengthArray>
#include <QFileInfo>
#include <QDate>
#include <QFileDialog>
#include <QToolBar>
#include <QAction>
wester committed
76
#include <QPushButton>
wester committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
#include <QCheckBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QMenu>
#include <QTest>

//start private enum
enum { CV_MODE_NORMAL = 0, CV_MODE_OPENGL = 1 };

//we can change the keyboard shortcuts from here !
enum {	shortcut_zoom_normal 	= Qt::CTRL + Qt::Key_Z,
        shortcut_zoom_imgRegion = Qt::CTRL + Qt::Key_X,
        shortcut_save_img		= Qt::CTRL + Qt::Key_S,
        shortcut_properties_win	= Qt::CTRL + Qt::Key_P,
        shortcut_zoom_in 		= Qt::CTRL + Qt::Key_Plus,//QKeySequence(QKeySequence::ZoomIn),
        shortcut_zoom_out		= Qt::CTRL + Qt::Key_Minus,//QKeySequence(QKeySequence::ZoomOut),
        shortcut_panning_left 	= Qt::CTRL + Qt::Key_Left,
        shortcut_panning_right 	= Qt::CTRL + Qt::Key_Right,
        shortcut_panning_up 	= Qt::CTRL + Qt::Key_Up,
        shortcut_panning_down 	= Qt::CTRL + Qt::Key_Down
    };

//end enum

class CvWindow;
class ViewPort;


class GuiReceiver : public QObject
{
    Q_OBJECT

public:
    GuiReceiver();
    ~GuiReceiver();

    int start();
    void isLastWindow();

    bool bTimeOut;
    QTimer* timer;

public slots:
    void createWindow( QString name, int flags = 0 );
    void destroyWindow(QString name);
    void destroyAllWindow();
    void addSlider(QString trackbar_name, QString window_name, void* value, int count, void* on_change);
    void addSlider2(QString trackbar_name, QString window_name, void* value, int count, void* on_change, void *userdata);
    void moveWindow(QString name, int x, int y);
    void resizeWindow(QString name, int width, int height);
    void showImage(QString name, void* arr);
    void displayInfo( QString name, QString text, int delayms );
    void displayStatusBar( QString name, QString text, int delayms );
    void timeOut();
    void toggleFullScreen(QString name, double flags );
    double isFullScreen(QString name);
    double getPropWindow(QString name);
    void setPropWindow(QString name, double flags );
    double getRatioWindow(QString name);
    void setRatioWindow(QString name, double arg2 );
    void saveWindowParameters(QString name);
    void loadWindowParameters(QString name);
    void putText(void* arg1, QString text, QPoint org, void* font);
    void addButton(QString button_name, int button_type, int initial_button_state , void* on_change, void* userdata);
    void enablePropertiesButtonEachWindow();

    void setOpenGlDrawCallback(QString name, void* callback, void* userdata);
    void setOpenGlContext(QString name);
    void updateWindow(QString name);
    double isOpenGl(QString name);

private:
    int nb_windows;
    bool doesExternalQAppExist;
};


enum typeBar { type_CvTrackbar = 0, type_CvButtonbar = 1 };
class CvBar : public QHBoxLayout
{
public:
    typeBar type;
    QString name_bar;
    QPointer<QWidget> myparent;
};


class CvButtonbar : public CvBar
{
    Q_OBJECT
public:
    CvButtonbar(QWidget* arg, QString bar_name);

    void addButton(QString button_name, CvButtonCallback call, void* userdata,  int button_type, int initial_button_state);

private:
    void setLabel();

    QPointer<QLabel> label;
    QPointer<QButtonGroup> group_button;
};


class CvPushButton : public QPushButton
{
    Q_OBJECT
public:
    CvPushButton(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata);

private:
    CvButtonbar* myparent;
    QString button_name ;
    CvButtonCallback callback;
    void* userdata;

private slots:
    void callCallBack(bool);
};


class CvCheckBox : public QCheckBox
{
    Q_OBJECT
public:
    CvCheckBox(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata, int initial_button_state);

private:
    CvButtonbar* myparent;
    QString button_name ;
    CvButtonCallback callback;
    void* userdata;

private slots:
    void callCallBack(bool);
};


class CvRadioButton : public QRadioButton
{
    Q_OBJECT
public:
    CvRadioButton(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata, int initial_button_state);

private:
    CvButtonbar* myparent;
    QString button_name ;
    CvButtonCallback callback;
    void* userdata;

private slots:
    void callCallBack(bool);
};


class CvTrackbar :  public CvBar
{
    Q_OBJECT
public:
    CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback on_change);
    CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback2 on_change, void* data);

    QPointer<QSlider> slider;

private slots:
    void createDialog();
    void update(int myvalue);

private:
    void setLabel(int myvalue);
    void create(CvWindow* arg, QString name, int* value, int count);
    QString createLabel();
    QPointer<QPushButton > label;
    CvTrackbarCallback callback;
    CvTrackbarCallback2 callback2;//look like it is use by python binding
    int* dataSlider;
    void* userdata;
};

//Both are top level window, so that a way to differenciate them.
//if (obj->metaObject ()->className () == "CvWindow") does not give me robust result

enum typeWindow { type_CvWindow = 1, type_CvWinProperties = 2 };
class CvWinModel : public QWidget
{
public:
typeWindow type;
};


class CvWinProperties : public CvWinModel
{
    Q_OBJECT
public:
    CvWinProperties(QString name, QObject* parent);
    ~CvWinProperties();
    QPointer<QBoxLayout> myLayout;

private:
    void closeEvent ( QCloseEvent * e );
    void showEvent ( QShowEvent * event ) ;
    void hideEvent ( QHideEvent * event ) ;
};


class CvWindow : public CvWinModel
{
    Q_OBJECT
public:
    CvWindow(QString arg2, int flag = CV_WINDOW_NORMAL);
    ~CvWindow();

    void setMouseCallBack(CvMouseCallback m, void* param);

    void writeSettings();
    void readSettings();

    double getRatio();
    void setRatio(int flags);

    int getPropWindow();
    void setPropWindow(int flags);

    void toggleFullScreen(int flags);

    void updateImage(void* arr);

    void displayInfo(QString text, int delayms);
    void displayStatusBar(QString text, int delayms);

    void enablePropertiesButton();

    static CvButtonbar* createButtonBar(QString bar_name);

    static void addSlider(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback on_change CV_DEFAULT(NULL));
    static void addSlider2(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback2 on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(0));

    void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata);
    void makeCurrentOpenGlContext();
    void updateGl();
    bool isOpenGl();

    void setViewportSize(QSize size);

    //parameters (will be save/load)
    int param_flags;
    int param_gui_mode;
    int param_ratio_mode;

    QPointer<QBoxLayout> myGlobalLayout; //All the widget (toolbar, view, LayoutBar, ...) are attached to it
    QPointer<QBoxLayout> myBarLayout;

    QVector<QAction*> vect_QActions;

    QPointer<QStatusBar> myStatusBar;
    QPointer<QToolBar> myToolBar;
    QPointer<QLabel> myStatusBar_msg;

protected:
    virtual void keyPressEvent(QKeyEvent* event);

private:

    int mode_display; //opengl or native
    ViewPort* myView;

    QVector<QShortcut*> vect_QShortcuts;

    void icvLoadTrackbars(QSettings *settings);
    void icvSaveTrackbars(QSettings *settings);
    void icvLoadControlPanel();
    void icvSaveControlPanel();
    void icvLoadButtonbar(CvButtonbar* t,QSettings *settings);
    void icvSaveButtonbar(CvButtonbar* t,QSettings *settings);

    void createActions();
    void createShortcuts();
    void createToolBar();
    void createView();
    void createStatusBar();
    void createGlobalLayout();
    void createBarLayout();
    CvWinProperties* createParameterWindow();

    void hideTools();
    void showTools();
    QSize getAvailableSize();

private slots:
    void displayPropertiesWin();
};


a  
Kai Westerkamp committed
369
enum type_mouse_event { mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3 };
wester committed
370 371 372 373
static const int tableMouseButtons[][3]={
    {CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP},               //mouse_up
    {CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN},         //mouse_down
    {CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK},   //mouse_dbclick
a  
Kai Westerkamp committed
374
    {CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE}                //mouse_move
wester committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
};


class ViewPort
{
public:
    virtual ~ViewPort() {}

    virtual QWidget* getWidget() = 0;

    virtual void setMouseCallBack(CvMouseCallback callback, void* param) = 0;

    virtual void writeSettings(QSettings& settings) = 0;
    virtual void readSettings(QSettings& settings) = 0;

    virtual double getRatio() = 0;
    virtual void setRatio(int flags) = 0;

    virtual void updateImage(const CvArr* arr) = 0;

    virtual void startDisplayInfo(QString text, int delayms) = 0;

    virtual void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) = 0;
    virtual void makeCurrentOpenGlContext() = 0;
    virtual void updateGl() = 0;

    virtual void setSize(QSize size_) = 0;
};



#ifdef HAVE_QT_OPENGL

a  
Kai Westerkamp committed
408
class OpenGlViewPort : public QGLWidget, public ViewPort
wester committed
409 410 411 412 413 414 415
{
public:
    explicit OpenGlViewPort(QWidget* parent);
    ~OpenGlViewPort();

    QWidget* getWidget();

a  
Kai Westerkamp committed
416 417
    void setMouseCallBack(CvMouseCallback callback, void* param);

wester committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    void writeSettings(QSettings& settings);
    void readSettings(QSettings& settings);

    double getRatio();
    void setRatio(int flags);

    void updateImage(const CvArr* arr);

    void startDisplayInfo(QString text, int delayms);

    void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata);
    void makeCurrentOpenGlContext();
    void updateGl();

    void setSize(QSize size_);

protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();

    void mouseMoveEvent(QMouseEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
    void mouseDoubleClickEvent(QMouseEvent* event);

    QSize sizeHint() const;

private:
    QSize size;

a  
Kai Westerkamp committed
449 450 451
    CvMouseCallback mouseCallback;
    void* mouseData;

wester committed
452 453
    CvOpenGlDrawCallback glDrawCallback;
    void* glDrawData;
a  
Kai Westerkamp committed
454 455 456

    void icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags);
    void icvmouseProcessing(QPointF pt, int cv_event, int flags);
wester committed
457 458 459 460 461
};

#endif // HAVE_QT_OPENGL


a  
Kai Westerkamp committed
462
class DefaultViewPort : public QGraphicsView, public ViewPort
wester committed
463 464 465 466 467 468 469 470 471
{
    Q_OBJECT

public:
    DefaultViewPort(CvWindow* centralWidget, int arg2);
    ~DefaultViewPort();

    QWidget* getWidget();

a  
Kai Westerkamp committed
472 473
    void setMouseCallBack(CvMouseCallback callback, void* param);

wester committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
    void writeSettings(QSettings& settings);
    void readSettings(QSettings& settings);

    double getRatio();
    void setRatio(int flags);

    void updateImage(const CvArr* arr);

    void startDisplayInfo(QString text, int delayms);

    void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata);
    void makeCurrentOpenGlContext();
    void updateGl();

    void setSize(QSize size_);

public slots:
    //reference:
    //http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming
    //http://doc.qt.nokia.com/4.6/gestures-imagegestures-imagewidget-cpp.html

    void siftWindowOnLeft();
    void siftWindowOnRight();
    void siftWindowOnUp() ;
    void siftWindowOnDown();

    void resetZoom();
    void imgRegion();
    void ZoomIn();
    void ZoomOut();

    void saveView();

protected:
    void contextMenuEvent(QContextMenuEvent* event);
    void resizeEvent(QResizeEvent* event);
    void paintEvent(QPaintEvent* paintEventInfo);
    void wheelEvent(QWheelEvent* event);
    void mouseMoveEvent(QMouseEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
    void mouseDoubleClickEvent(QMouseEvent* event);

private:
    int param_keepRatio;

    //parameters (will be save/load)
    QTransform param_matrixWorld;

    CvMat* image2Draw_mat;
    QImage image2Draw_qt;
    int nbChannelOriginImage;

a  
Kai Westerkamp committed
527 528 529 530
    //for mouse callback
    CvMouseCallback on_mouse;
    void* on_mouse_param;

wester committed
531 532 533 534

    void scaleView(qreal scaleFactor, QPointF center);
    void moveView(QPointF delta);

a  
Kai Westerkamp committed
535
    QPoint mouseCoordinate;
wester committed
536
    QPointF positionGrabbing;
a  
Kai Westerkamp committed
537
    QRect  positionCorners;
wester committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    QTransform matrixWorld_inv;
    float ratioX, ratioY;

    bool isSameSize(IplImage* img1,IplImage* img2);

    QSize sizeHint() const;
    QPointer<CvWindow> centralWidget;
    QPointer<QTimer> timerDisplay;
    bool drawInfo;
    QString infoText;
    QRectF target;

    void drawInstructions(QPainter *painter);
    void drawViewOverview(QPainter *painter);
    void drawImgRegion(QPainter *painter);
    void draw2D(QPainter *painter);
    void drawStatusBar();
    void controlImagePosition();
a  
Kai Westerkamp committed
556
    void icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags);
wester committed
557 558 559 560 561 562 563
    void icvmouseProcessing(QPointF pt, int cv_event, int flags);

private slots:
    void stopDisplayInfo();
};

#endif