#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>
#include <QPushButton>
#include <QCheckBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QMenu>
#include <QTest>
enum { CV_MODE_NORMAL = 0, CV_MODE_OPENGL = 1 };
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,
shortcut_zoom_out = Qt::CTRL + Qt::Key_Minus,
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
};
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 );
void setWindowTitle(QString name, QString title);
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;
int* dataSlider;
void* userdata;
};
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);
int param_flags;
int param_gui_mode;
int param_ratio_mode;
QPointer<QBoxLayout> myGlobalLayout;
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;
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();
};
enum type_mouse_event { mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3 };
static const int tableMouseButtons[][3]={
{CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP},
{CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN},
{CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK},
{CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE}
};
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
class OpenGlViewPort : public QGLWidget, public ViewPort
{
public:
explicit OpenGlViewPort(QWidget* parent);
~OpenGlViewPort();
QWidget* getWidget();
void setMouseCallBack(CvMouseCallback callback, void* param);
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;
CvMouseCallback mouseCallback;
void* mouseData;
CvOpenGlDrawCallback glDrawCallback;
void* glDrawData;
void icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags);
void icvmouseProcessing(QPointF pt, int cv_event, int flags);
};
#endif
class DefaultViewPort : public QGraphicsView, public ViewPort
{
Q_OBJECT
public:
DefaultViewPort(CvWindow* centralWidget, int arg2);
~DefaultViewPort();
QWidget* getWidget();
void setMouseCallBack(CvMouseCallback callback, void* param);
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:
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;
QTransform param_matrixWorld;
CvMat* image2Draw_mat;
QImage image2Draw_qt;
int nbChannelOriginImage;
CvMouseCallback on_mouse;
void* on_mouse_param;
void scaleView(qreal scaleFactor, QPointF center);
void moveView(QPointF delta);
QPoint mouseCoordinate;
QPointF positionGrabbing;
QRect positionCorners;
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();
void icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags);
void icvmouseProcessing(QPointF pt, int cv_event, int flags);
private slots:
void stopDisplayInfo();
};
#endif