From 2c923c7eba1dde7cb1049ef8e1803af025ceb291 Mon Sep 17 00:00:00 2001 From: Yannick Verdie Date: Thu, 22 Jul 2010 20:39:44 +0000 Subject: [PATCH] QT new functions: - cvAddButton changed to match requirement - CV_GUI_NORMAL and CV_GUI_EXPANDED done - context menu with right click --- .../include/opencv2/highgui/highgui.hpp | 4 +- .../include/opencv2/highgui/highgui_c.h | 5 +- modules/highgui/src/window.cpp | 4 +- modules/highgui/src/window_QT.cpp | 2896 +++++++++-------- modules/highgui/src/window_QT.h | 52 +- 5 files changed, 1507 insertions(+), 1454 deletions(-) diff --git a/modules/highgui/include/opencv2/highgui/highgui.hpp b/modules/highgui/include/opencv2/highgui/highgui.hpp index 88fa6ab023..003712704e 100644 --- a/modules/highgui/include/opencv2/highgui/highgui.hpp +++ b/modules/highgui/include/opencv2/highgui/highgui.hpp @@ -79,8 +79,8 @@ CV_EXPORTS void loadWindowParameters(const string& windowName); CV_EXPORTS int startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]); CV_EXPORTS void stopLoop(); -typedef void (CV_CDECL *ButtonCallback)(void* userdata); -CV_EXPORTS int createButton( const char* bar_name, const char* window_name, ButtonCallback on_change, const char* button_name CV_DEFAULT(NULL), void* userdata CV_DEFAULT(NULL)); +typedef void (CV_CDECL *ButtonCallback)(int state, void* userdata); +CV_EXPORTS int createButton( const string& bar_name, ButtonCallback on_change , void* userdata CV_DEFAULT(NULL), int type CV_DEFAULT(CV_PUSH_BUTTON), bool initial_button_state CV_DEFAULT(0)); //------------------------- CV_EXPORTS void imshow( const string& winname, const Mat& mat ); diff --git a/modules/highgui/include/opencv2/highgui/highgui_c.h b/modules/highgui/include/opencv2/highgui/highgui_c.h index 554ebcd1b3..9aa06e0559 100644 --- a/modules/highgui/include/opencv2/highgui/highgui_c.h +++ b/modules/highgui/include/opencv2/highgui/highgui_c.h @@ -94,8 +94,9 @@ CVAPI(void) cvLoadWindowParameters(const char* name); CVAPI(int) cvStartLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]); CVAPI(void) cvStopLoop(); -typedef void (CV_CDECL *CvButtonCallback)(void* userdata); -CVAPI(int) cvCreateButton( const char* bar_name, const char* window_name, CvButtonCallback on_change, const char* button_name CV_DEFAULT(NULL), void* userdata CV_DEFAULT(NULL)); +typedef void (CV_CDECL *CvButtonCallback)(int state, void* userdata); +enum {CV_PUSH_BUTTON = 0, CV_CHECKBOX = 1}; +CVAPI(int) cvCreateButton( const char* button_name CV_DEFAULT(NULL),CvButtonCallback on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(NULL) , int button_type CV_DEFAULT(CV_PUSH_BUTTON), bool initial_button_state CV_DEFAULT(0)); //---------------------- diff --git a/modules/highgui/src/window.cpp b/modules/highgui/src/window.cpp index ab65d54ac2..5c19c17387 100644 --- a/modules/highgui/src/window.cpp +++ b/modules/highgui/src/window.cpp @@ -231,9 +231,9 @@ void loadWindowParameters(const string& windowName) cvLoadWindowParameters(windowName.c_str()); } -int createButton( const string& bar_name, const string& window_name, ButtonCallback on_change, const string& button_name, void* userdata) +int createButton(const string& button_name, ButtonCallback on_change, void* userdata, int button_type , bool initial_button_state ) { - return cvCreateButton( bar_name.c_str(), window_name.c_str(), on_change, button_name.c_str(), userdata); + return cvCreateButton(button_name.c_str(), on_change, userdata, button_type , initial_button_state ); } #endif diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index 4a4f1a843d..b1200d5f27 100755 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -55,6 +55,7 @@ static const unsigned int threshold_zoom_img_region = 15; //the minimum zoom value to start displaying the values in the grid //that is also the number of pixel per grid +static CvWinProperties* global_control_panel = NULL; //end static and global @@ -62,138 +63,138 @@ static const unsigned int threshold_zoom_img_region = 15; CV_IMPL CvFont cvFont_Qt(const char* nameFont, int pointSize,CvScalar color,int weight,int style, int spacing) { - /* + /* //nameFont <- only Qt //CvScalar color <- only Qt (blue_component, green_component, red\_component[, alpha_component]) - int font_face;//<- style in Qt - const int* ascii; - const int* greek; - const int* cyrillic; - float hscale, vscale; - float shear; - int thickness;//<- weight in Qt - float dx;//spacing letter in Qt (0 default) in pixel - int line_type;//<- pointSize in Qt - */ - CvFont f = {nameFont,color,style,NULL,NULL,NULL,0,0,0,weight,spacing,pointSize}; - return f; + int font_face;//<- style in Qt + const int* ascii; + const int* greek; + const int* cyrillic; + float hscale, vscale; + float shear; + int thickness;//<- weight in Qt + float dx;//spacing letter in Qt (0 default) in pixel + int line_type;//<- pointSize in Qt + */ + CvFont f = {nameFont,color,style,NULL,NULL,NULL,0,0,0,weight,spacing,pointSize}; + return f; } CV_IMPL void cvAddText( CvArr* img, const char* text, CvPoint org, CvFont* font) { - QMetaObject::invokeMethod(&guiMainThread, - "putText", - Qt::AutoConnection, - Q_ARG(void*, (void*) img), - Q_ARG(QString,QString(text)), - Q_ARG(QPoint, QPoint(org.x,org.y)), - Q_ARG(void*,(void*) font)); + QMetaObject::invokeMethod(&guiMainThread, + "putText", + Qt::AutoConnection, + Q_ARG(void*, (void*) img), + Q_ARG(QString,QString(text)), + Q_ARG(QPoint, QPoint(org.x,org.y)), + Q_ARG(void*,(void*) font)); } double cvGetRatioWindow_QT(const char* name) { - double result = -1; - QMetaObject::invokeMethod(&guiMainThread, - "getRatioWindow", - //Qt::DirectConnection, - Qt::AutoConnection, - Q_RETURN_ARG(double, result), - Q_ARG(QString, QString(name))); - return result; + double result = -1; + QMetaObject::invokeMethod(&guiMainThread, + "getRatioWindow", + //Qt::DirectConnection, + Qt::AutoConnection, + Q_RETURN_ARG(double, result), + Q_ARG(QString, QString(name))); + return result; } void cvSetRatioWindow_QT(const char* name,double prop_value) { - QMetaObject::invokeMethod(&guiMainThread, - "setRatioWindow", - Qt::AutoConnection, - Q_ARG(QString, QString(name)), - Q_ARG(double, prop_value)); + QMetaObject::invokeMethod(&guiMainThread, + "setRatioWindow", + Qt::AutoConnection, + Q_ARG(QString, QString(name)), + Q_ARG(double, prop_value)); } double cvGetPropWindow_QT(const char* name) { - double result = -1; - QMetaObject::invokeMethod(&guiMainThread, - "getPropWindow", - //Qt::DirectConnection, - Qt::AutoConnection, - Q_RETURN_ARG(double, result), - Q_ARG(QString, QString(name))); - return result; + double result = -1; + QMetaObject::invokeMethod(&guiMainThread, + "getPropWindow", + //Qt::DirectConnection, + Qt::AutoConnection, + Q_RETURN_ARG(double, result), + Q_ARG(QString, QString(name))); + return result; } void cvSetPropWindow_QT(const char* name,double prop_value) { - QMetaObject::invokeMethod(&guiMainThread, - "setPropWindow", - Qt::AutoConnection, - Q_ARG(QString, QString(name)), - Q_ARG(double, prop_value)); + QMetaObject::invokeMethod(&guiMainThread, + "setPropWindow", + Qt::AutoConnection, + Q_ARG(QString, QString(name)), + Q_ARG(double, prop_value)); } void cvSetModeWindow_QT(const char* name, double prop_value) { - QMetaObject::invokeMethod(&guiMainThread, - "toggleFullScreen", - Qt::AutoConnection, - Q_ARG(QString, QString(name)), - Q_ARG(double, prop_value)); + QMetaObject::invokeMethod(&guiMainThread, + "toggleFullScreen", + Qt::AutoConnection, + Q_ARG(QString, QString(name)), + Q_ARG(double, prop_value)); } double cvGetModeWindow_QT(const char* name) { - double result = -1; + double result = -1; - QMetaObject::invokeMethod(&guiMainThread, - "isFullScreen", - Qt::AutoConnection, - Q_RETURN_ARG(double, result), - Q_ARG(QString, QString(name))); - return result; + QMetaObject::invokeMethod(&guiMainThread, + "isFullScreen", + Qt::AutoConnection, + Q_RETURN_ARG(double, result), + Q_ARG(QString, QString(name))); + return result; } CV_IMPL void cvDisplayOverlay(const char* name, const char* text, int delayms) { - QMetaObject::invokeMethod(&guiMainThread, - "displayInfo", - Qt::AutoConnection, - //Qt::DirectConnection, - Q_ARG(QString, QString(name)), - Q_ARG(QString, QString(text)), - Q_ARG(int, delayms)); + QMetaObject::invokeMethod(&guiMainThread, + "displayInfo", + Qt::AutoConnection, + //Qt::DirectConnection, + Q_ARG(QString, QString(name)), + Q_ARG(QString, QString(text)), + Q_ARG(int, delayms)); } CV_IMPL void cvSaveWindowParameters(const char* name) { - QMetaObject::invokeMethod(&guiMainThread, - "saveWindowParameters", - Qt::AutoConnection, - Q_ARG(QString, QString(name))); + QMetaObject::invokeMethod(&guiMainThread, + "saveWindowParameters", + Qt::AutoConnection, + Q_ARG(QString, QString(name))); } CV_IMPL void cvLoadWindowParameters(const char* name) { - QMetaObject::invokeMethod(&guiMainThread, - "loadWindowParameters", - Qt::AutoConnection, - Q_ARG(QString, QString(name))); + QMetaObject::invokeMethod(&guiMainThread, + "loadWindowParameters", + Qt::AutoConnection, + Q_ARG(QString, QString(name))); } CV_IMPL void cvDisplayStatusBar(const char* name, const char* text, int delayms) { - QMetaObject::invokeMethod(&guiMainThread, - "displayStatusBar", - Qt::AutoConnection, - //Qt::DirectConnection, - Q_ARG(QString, QString(name)), - Q_ARG(QString, QString(text)), - Q_ARG(int, delayms)); + QMetaObject::invokeMethod(&guiMainThread, + "displayStatusBar", + Qt::AutoConnection, + //Qt::DirectConnection, + Q_ARG(QString, QString(name)), + Q_ARG(QString, QString(text)), + Q_ARG(int, delayms)); } @@ -201,81 +202,81 @@ CV_IMPL void cvDisplayStatusBar(const char* name, const char* text, int delayms) CV_IMPL int cvInitSystem( int, char** ) { - return 0; + return 0; } CV_IMPL int cvWaitKey( int arg ) { - int result = -1; + int result = -1; - unsigned long delayms;//in milliseconds - if (arg<=0) - delayms = ULONG_MAX; - else - delayms = arg; + unsigned long delayms;//in milliseconds + if (arg<=0) + delayms = ULONG_MAX; + else + delayms = arg; - if (multiThreads) - { - mutexKey.lock(); - if(key_pressed.wait(&mutexKey,delayms))//false if timeout + if (multiThreads) { - result = last_key; - } - last_key = -1; - mutexKey.unlock(); + mutexKey.lock(); + if(key_pressed.wait(&mutexKey,delayms))//false if timeout + { + result = last_key; + } + last_key = -1; + mutexKey.unlock(); - }else{ - //cannot use wait here because events will not be distributed before processEvents (the main eventLoop is broken) - //so I create a Thread for the QTimer + }else{ + //cannot use wait here because events will not be distributed before processEvents (the main eventLoop is broken) + //so I create a Thread for the QTimer - QTimer timer(&guiMainThread); - QObject::connect(&timer, SIGNAL(timeout()), &guiMainThread, SLOT(timeOut())); - timer.setSingleShot(true); + QTimer timer(&guiMainThread); + QObject::connect(&timer, SIGNAL(timeout()), &guiMainThread, SLOT(timeOut())); + timer.setSingleShot(true); - if (arg>0) - timer.start(arg); + if (arg>0) + timer.start(arg); - //QMutex dummy; + //QMutex dummy; - while(!guiMainThread._bTimeOut) - { - qApp->processEvents(QEventLoop::AllEvents); - - mutexKey.lock(); - if (last_key != -1) - { - result = last_key; - last_key = -1; - timer.stop(); - //printf("keypressed\n"); - } - mutexKey.unlock(); - - if (result!=-1) - { - break; - } - else - { - /* + while(!guiMainThread._bTimeOut) + { + qApp->processEvents(QEventLoop::AllEvents); + + mutexKey.lock(); + if (last_key != -1) + { + result = last_key; + last_key = -1; + timer.stop(); + //printf("keypressed\n"); + } + mutexKey.unlock(); + + if (result!=-1) + { + break; + } + else + { + /* * //will not work, I broke the event loop !!!! - dummy.lock(); - QWaitCondition waitCondition; - waitCondition.wait(&dummy, 2); - */ + dummy.lock(); + QWaitCondition waitCondition; + waitCondition.wait(&dummy, 2); + */ -#if defined WIN32 || defined _WIN32 - sleep(2); +#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 + sleep(2); #else - usleep(2);//to decrease CPU usage + usleep(2);//to decrease CPU usage #endif - } + } + } + guiMainThread._bTimeOut = false; } - guiMainThread._bTimeOut = false; - } - return result; + return result; } //Yannick Verdie @@ -283,343 +284,307 @@ CV_IMPL int cvWaitKey( int arg ) //We recommend not using this function for now CV_IMPL int cvStartLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]) { - multiThreads = true; - QFuture future = QtConcurrent::run(pt2Func,argc,argv); - return guiMainThread.start(); + multiThreads = true; + QFuture future = QtConcurrent::run(pt2Func,argc,argv); + return guiMainThread.start(); } CV_IMPL void cvStopLoop() { - qApp->exit(); + qApp->exit(); } CvWindow* icvFindWindowByName( const char* arg ) { - QPointer window = NULL; - - if( !arg ) - CV_Error( CV_StsNullPtr, "NULL name string" ); + QPointer window = NULL; - QString name(arg); - QPointer w; - foreach (QWidget *widget, QApplication::topLevelWidgets()) - { + if( !arg ) + CV_Error( CV_StsNullPtr, "NULL name string" ); - if (widget->isWindow() && !widget->parentWidget ())//is a window without parent + QString name(arg); + QPointer w; + foreach (QWidget *widget, QApplication::topLevelWidgets()) { - w = (CvWindow*) widget; - if (w->param_name==name) - { - window = w; - break; - } + + if (widget->isWindow() && !widget->parentWidget ())//is a window without parent + { + w = (CvWindow*) widget; + if (w->param_name==name) + { + window = w; + break; + } + } } - } - return window; + return window; } -CvTrackbar* icvFindTrackbarByName( const char* name_trackbar, const char* name_window ) -{ - - QPointer result = NULL; - - QPointer w = icvFindWindowByName( name_window ); - - if( !w ) - CV_Error( CV_StsNullPtr, "NULL window handler" ); - - QString nameQt(name_trackbar); - CvBar* t; - - int start_index = 0; - int stop_index = 0; - QPointer myLayout; - - if (w->param_gui_mode == CV_GUI_NORMAL) - { - myLayout = w->myLayout; - - start_index = 1; - if (w->myToolBar) - start_index = 2; - //Warning ---- , asume the location 0 is toolbar, 1 is myview and max-1 the status bar - //done three times in the code, in loadtrackbars, savetrackbar and in findtrackbar - - stop_index = myLayout->layout()->count()-1; - } - if (w->param_gui_mode == CV_GUI_EXPANDED) - { - myLayout = w->getWinProp()->myLayout; - if (!myLayout) - CV_Error( CV_StsNullPtr, "NULL window prop handler" ); - - start_index = 0; - stop_index = myLayout->layout()->count(); - } +CvBar* icvFindBarbyName(QBoxLayout* layout, QString name_bar, typeBar type) +{ + if (!layout) + return NULL; + CvBar* t; - for (int i = start_index; i < stop_index; ++i) - { - t = (CvBar*) myLayout->layout()->itemAt(i); - if (t->type == type_CvTrackbar && t->name_bar == nameQt) - { - result = (CvTrackbar*) t; - break; - } - } - - return result; + int stop_index = layout->layout()->count(); + for (int i = 0; i < stop_index; ++i) + { + t = (CvBar*) layout->layout()->itemAt(i); + if (t->type == type && t->name_bar == name_bar) + return t; + } + return NULL; } -CvButtonbar* icvFindButtonbarByName( const char* name_Buttonbar, const char* name_window ) +CvTrackbar* icvFindTrackbarByName( const char* name_trackbar, const char* name_window, QBoxLayout* layout = NULL ) { + QString nameQt(name_trackbar); + CvBar* result = NULL; - QPointer result = NULL; - - QPointer w = icvFindWindowByName( name_window ); - - if( !w ) - CV_Error( CV_StsNullPtr, "NULL window handler" ); + if (!layout) + { + QPointer w = icvFindWindowByName( name_window ); - QString nameQt = QString(name_Buttonbar); - CvBar* t; - int start_index; - int stop_index; - QPointer myLayout; + if( !w ) + CV_Error( CV_StsNullPtr, "NULL window handler" ); - if (w->param_gui_mode == CV_GUI_EXPANDED) - { - myLayout = w->getWinProp()->myLayout; - if (!myLayout) - CV_Error( CV_StsNullPtr, "NULL window prop handler" ); + if ( w->param_gui_mode == CV_GUI_NORMAL) + return (CvTrackbar*) icvFindBarbyName( w->myBarLayout, nameQt, type_CvTrackbar); - start_index = 0; - stop_index = myLayout->layout()->count(); + if ( w->param_gui_mode == CV_GUI_EXPANDED) + { + result = icvFindBarbyName( w->myBarLayout, nameQt, type_CvTrackbar); + if (result) + return (CvTrackbar*) result; + return (CvTrackbar*) icvFindBarbyName(w->parameters_window->myLayout, nameQt, type_CvTrackbar); + } - for (int i = start_index; i < stop_index; ++i) + return NULL; + }else + //layout was specified { - t = (CvBar*) myLayout->layout()->itemAt(i); - if (t->type == type_CvButtonbar && t->name_bar == nameQt) - { - result = (CvButtonbar*) t; - break; - } + return (CvTrackbar*) icvFindBarbyName( layout, nameQt, type_CvTrackbar); } - } +} - return result; +CvButtonbar* icvFindButtonbarByName( const char* button_name,QBoxLayout* layout) +{ + QString nameQt(button_name); + return (CvButtonbar*) icvFindBarbyName( layout, nameQt, type_CvButtonbar); } int icvInitSystem() { - static int wasInitialized = 0; + static int wasInitialized = 0; - // check initialization status - if( !wasInitialized) - { - new QApplication(parameterSystemC,parameterSystemV); + // check initialization status + if( !wasInitialized) + { + new QApplication(parameterSystemC,parameterSystemV); - wasInitialized = 1; - qDebug()<<"init done"; + wasInitialized = 1; + qDebug()<<"init done"; #if defined(OPENCV_GL)//OK tested ! - qDebug()<<"opengl support available"; + qDebug()<<"opengl support available"; #endif - } + } - return 0; + return 0; } CV_IMPL int cvNamedWindow( const char* name, int flags ) { - if (multiThreads) - QMetaObject::invokeMethod(&guiMainThread, - "createWindow", - //Qt::AutoConnection, - Qt::BlockingQueuedConnection, - //TypeConnection, - //Qt::AutoConnection, - Q_ARG(QString, QString(name)), - Q_ARG(int, flags)); - else - guiMainThread.createWindow(QString(name),flags); + if (multiThreads) + QMetaObject::invokeMethod(&guiMainThread, + "createWindow", + //Qt::AutoConnection, + Qt::BlockingQueuedConnection, + //TypeConnection, + //Qt::AutoConnection, + Q_ARG(QString, QString(name)), + Q_ARG(int, flags)); + else + guiMainThread.createWindow(QString(name),flags); - return 1;//Dummy value + return 1;//Dummy value } CV_IMPL void cvDestroyWindow( const char* name ) { - QMetaObject::invokeMethod(&guiMainThread, - "destroyWindow", - //Qt::BlockingQueuedConnection, - Qt::AutoConnection, - Q_ARG(QString, QString(name)) - ); + QMetaObject::invokeMethod(&guiMainThread, + "destroyWindow", + //Qt::BlockingQueuedConnection, + Qt::AutoConnection, + Q_ARG(QString, QString(name)) + ); } CV_IMPL void cvDestroyAllWindows(void) { - QMetaObject::invokeMethod(&guiMainThread, - "destroyAllWindow", - //Qt::BlockingQueuedConnection, - Qt::AutoConnection - ); + QMetaObject::invokeMethod(&guiMainThread, + "destroyAllWindow", + //Qt::BlockingQueuedConnection, + Qt::AutoConnection + ); } CV_IMPL void* cvGetWindowHandle( const char* name ) { - if( !name ) - CV_Error( CV_StsNullPtr, "NULL name string" ); + if( !name ) + CV_Error( CV_StsNullPtr, "NULL name string" ); - return (void*) icvFindWindowByName( name ); + return (void*) icvFindWindowByName( name ); } CV_IMPL const char* cvGetWindowName( void* window_handle ) { - if( !window_handle ) - CV_Error( CV_StsNullPtr, "NULL window handler" ); + if( !window_handle ) + CV_Error( CV_StsNullPtr, "NULL window handler" ); - return ((CvWindow*)window_handle)->windowTitle().toLatin1().data(); + return ((CvWindow*)window_handle)->windowTitle().toLatin1().data(); } CV_IMPL void cvMoveWindow( const char* name, int x, int y ) { - QMetaObject::invokeMethod(&guiMainThread, - "moveWindow", - //Qt::BlockingQueuedConnection, - Qt::AutoConnection, - Q_ARG(QString, QString(name)), - Q_ARG(int, x), - Q_ARG(int, y) - ); + QMetaObject::invokeMethod(&guiMainThread, + "moveWindow", + //Qt::BlockingQueuedConnection, + Qt::AutoConnection, + Q_ARG(QString, QString(name)), + Q_ARG(int, x), + Q_ARG(int, y) + ); } CV_IMPL void cvResizeWindow(const char* name, int width, int height ) { - QMetaObject::invokeMethod(&guiMainThread, - "resizeWindow", - //Qt::BlockingQueuedConnection, - Qt::AutoConnection, - Q_ARG(QString, QString(name)), - Q_ARG(int, width), - Q_ARG(int, height) - ); + QMetaObject::invokeMethod(&guiMainThread, + "resizeWindow", + //Qt::BlockingQueuedConnection, + Qt::AutoConnection, + Q_ARG(QString, QString(name)), + Q_ARG(int, width), + Q_ARG(int, height) + ); } //TODO: implement the real one, not a wrapper CV_IMPL int cvCreateTrackbar2( const char* name_bar, const char* window_name, int* val, int count, CvTrackbarCallback2 on_notify, void* userdata ) { - return cvCreateTrackbar( name_bar, window_name, val, count, (CvTrackbarCallback)on_notify ); + return cvCreateTrackbar( name_bar, window_name, val, count, (CvTrackbarCallback)on_notify ); } CV_IMPL int cvStartWindowThread() { - return 0; + return 0; } CV_IMPL int cvCreateTrackbar( const char* name_bar, const char* window_name, int* value, int count, CvTrackbarCallback on_change) { - QMetaObject::invokeMethod(&guiMainThread, - "addSlider", - Qt::AutoConnection, - Q_ARG(QString, QString(name_bar)), - Q_ARG(QString, QString(window_name)), - Q_ARG(void*, (void*)value), - Q_ARG(int, count), - Q_ARG(void*, (void*)on_change) - ); + QMetaObject::invokeMethod(&guiMainThread, + "addSlider", + Qt::AutoConnection, + Q_ARG(QString, QString(name_bar)), + Q_ARG(QString, QString(window_name)), + Q_ARG(void*, (void*)value), + Q_ARG(int, count), + Q_ARG(void*, (void*)on_change) + ); - return 1;//dummy value + return 1;//dummy value } -CV_IMPL int cvCreateButton( const char* bar_name, const char* window_name, CvButtonCallback on_change, const char* button_name , void* userdata ) + + +CV_IMPL int cvCreateButton(const char* button_name,CvButtonCallback on_change, void* userdata , int button_type, bool initial_button_state ) { - QMetaObject::invokeMethod(&guiMainThread, - "addButton", - Qt::AutoConnection, - Q_ARG(QString, QString(window_name)), - Q_ARG(QString, QString(bar_name)), - Q_ARG(QString, QString(button_name)), - Q_ARG(void*, (void*)on_change), - Q_ARG(void*, userdata) - ); + QMetaObject::invokeMethod(&guiMainThread, + "addButton", + Qt::AutoConnection, + Q_ARG(QString, QString(button_name)), + Q_ARG(int, button_type), + Q_ARG(bool, initial_button_state), + Q_ARG(void*, (void*)on_change), + Q_ARG(void*, userdata) + ); - return 1;//dummy value + return 1;//dummy value } CV_IMPL void cvCreateOpenGLCallback( const char* window_name, CvOpenGLCallback callbackOpenGL, void* userdata) { - QMetaObject::invokeMethod(&guiMainThread, - "setOpenGLCallback", - Qt::AutoConnection, - Q_ARG(QString, QString(window_name)), - Q_ARG(void*, (void*)callbackOpenGL), - Q_ARG(void*, userdata) - ); + QMetaObject::invokeMethod(&guiMainThread, + "setOpenGLCallback", + Qt::AutoConnection, + Q_ARG(QString, QString(window_name)), + Q_ARG(void*, (void*)callbackOpenGL), + Q_ARG(void*, userdata) + ); } CV_IMPL int cvGetTrackbarPos( const char* name_bar, const char* window_name ) { - int result = -1; + int result = -1; - QPointer t = icvFindTrackbarByName( name_bar, window_name ); + QPointer t = icvFindTrackbarByName( name_bar, window_name ); - if (t) - result = t->slider->value(); + if (t) + result = t->slider->value(); - return result; + return result; } CV_IMPL void cvSetTrackbarPos( const char* name_bar, const char* window_name, int pos ) { - QPointer t = icvFindTrackbarByName( name_bar, window_name ); + QPointer t = icvFindTrackbarByName( name_bar, window_name ); - if (t) - t->slider->setValue(pos); + if (t) + t->slider->setValue(pos); } /* assign callback for mouse events */ CV_IMPL void cvSetMouseCallback( const char* window_name, CvMouseCallback on_mouse,void* param ) { - QPointer w = icvFindWindowByName( window_name ); + QPointer w = icvFindWindowByName( window_name ); - if (!w) - CV_Error(CV_StsNullPtr, "NULL window handler" ); + if (!w) + CV_Error(CV_StsNullPtr, "NULL window handler" ); - w->setMouseCallBack(on_mouse, param); + w->setMouseCallBack(on_mouse, param); } CV_IMPL void cvShowImage( const char* name, const CvArr* arr ) { - QMetaObject::invokeMethod(&guiMainThread, - "showImage", - //Qt::BlockingQueuedConnection, - Qt::DirectConnection, - Q_ARG(QString, QString(name)), - Q_ARG(void*, (void*)arr) - ); + QMetaObject::invokeMethod(&guiMainThread, + "showImage", + //Qt::BlockingQueuedConnection, + Qt::DirectConnection, + Q_ARG(QString, QString(name)), + Q_ARG(void*, (void*)arr) + ); } @@ -627,556 +592,605 @@ CV_IMPL void cvShowImage( const char* name, const CvArr* arr ) GuiReceiver::GuiReceiver() : _bTimeOut(false) { - icvInitSystem(); - qApp->setQuitOnLastWindowClosed ( false );//maybe the user would like to access this setting + icvInitSystem(); + qApp->setQuitOnLastWindowClosed ( false );//maybe the user would like to access this setting } void GuiReceiver::putText(void* arg1, QString text, QPoint org, void* arg2) { - CV_Assert(arg1) + CV_Assert(arg1) - IplImage* img = (IplImage*)arg1; + IplImage* img = (IplImage*)arg1; - //for now, only support QImage::Format_RGB888 - if (img->depth !=IPL_DEPTH_8U || img->nChannels != 3) - return; + //for now, only support QImage::Format_RGB888 + if (img->depth !=IPL_DEPTH_8U || img->nChannels != 3) + return; - CvFont* font = (CvFont*)arg2; + CvFont* font = (CvFont*)arg2; - QImage qimg((uchar*) img->imageData, img->width, img->height,QImage::Format_RGB888); - QPainter qp(&qimg); - if (font) - { - QFont f(font->nameFont, font->line_type/*PointSize*/, font->thickness/*weight*/); - f.setStyle((QFont::Style)font->font_face/*style*/); - f.setLetterSpacing ( QFont::AbsoluteSpacing, font->dx/*spacing*/ ); - //cvScalar(blue_component, green_component, red\_component[, alpha_component]) - //Qt map non-transparent to 0xFF and transparent to 0 - //OpenCV scalar is the reverse, so 255-font->color.val[3] - qp.setPen(QColor(font->color.val[2],font->color.val[1],font->color.val[0],255-font->color.val[3])); - qp.setFont ( f ); - } - qp.drawText (org, text ); - qp.end(); + QImage qimg((uchar*) img->imageData, img->width, img->height,QImage::Format_RGB888); + QPainter qp(&qimg); + if (font) + { + QFont f(font->nameFont, font->line_type/*PointSize*/, font->thickness/*weight*/); + f.setStyle((QFont::Style)font->font_face/*style*/); + f.setLetterSpacing ( QFont::AbsoluteSpacing, font->dx/*spacing*/ ); + //cvScalar(blue_component, green_component, red\_component[, alpha_component]) + //Qt map non-transparent to 0xFF and transparent to 0 + //OpenCV scalar is the reverse, so 255-font->color.val[3] + qp.setPen(QColor(font->color.val[2],font->color.val[1],font->color.val[0],255-font->color.val[3])); + qp.setFont ( f ); + } + qp.drawText (org, text ); + qp.end(); } void GuiReceiver::saveWindowParameters(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (w) - w->writeSettings(); + if (w) + w->writeSettings(); } void GuiReceiver::loadWindowParameters(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (w) - w->readSettings(); + if (w) + w->readSettings(); } double GuiReceiver::getRatioWindow(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (!w) - return -1; + if (!w) + return -1; - return (double)w->getView()->getRatio(); + return (double)w->getView()->getRatio(); } void GuiReceiver::setRatioWindow(QString name, double arg2 ) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (!w) - return; + if (!w) + return; - int flags = (int) arg2; + int flags = (int) arg2; - if (w->getView()->getRatio() == flags)//nothing to do - return; + if (w->getView()->getRatio() == flags)//nothing to do + return; - //if valid flags - if (flags == CV_WINDOW_FREERATIO || flags == CV_WINDOW_KEEPRATIO) - w->getView()->setRatio(flags); + //if valid flags + if (flags == CV_WINDOW_FREERATIO || flags == CV_WINDOW_KEEPRATIO) + w->getView()->setRatio(flags); } double GuiReceiver::getPropWindow(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (!w) - return -1; + if (!w) + return -1; - return (double)w->param_flags; + return (double)w->param_flags; } void GuiReceiver::setPropWindow(QString name, double arg2 ) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (!w) - return; + if (!w) + return; - int flags = (int) arg2; + int flags = (int) arg2; - if (w->param_flags == flags)//nothing to do - return; + if (w->param_flags == flags)//nothing to do + return; - switch(flags) - { - case CV_WINDOW_NORMAL: - w->myLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); - w->param_flags = flags; - break; - case CV_WINDOW_AUTOSIZE: - w->myLayout->setSizeConstraint(QLayout::SetFixedSize); - w->param_flags = flags; - break; - default:; - } + switch(flags) + { + case CV_WINDOW_NORMAL: + w->myGlobalLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); + w->param_flags = flags; + break; + case CV_WINDOW_AUTOSIZE: + w->myGlobalLayout->setSizeConstraint(QLayout::SetFixedSize); + w->param_flags = flags; + break; + default:; + } } double GuiReceiver::isFullScreen(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (!w) - return -1; + if (!w) + return -1; - if (w->isFullScreen()) - return CV_WINDOW_FULLSCREEN; - else - return CV_WINDOW_NORMAL; + if (w->isFullScreen()) + return CV_WINDOW_FULLSCREEN; + else + return CV_WINDOW_NORMAL; } //accept CV_WINDOW_NORMAL or CV_WINDOW_FULLSCREEN void GuiReceiver::toggleFullScreen(QString name, double flags ) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (!w) - return; + if (!w) + return; - if (w->isFullScreen() && flags == CV_WINDOW_NORMAL) - { - w->showTools(); - w->showNormal(); - return; - } + if (w->isFullScreen() && flags == CV_WINDOW_NORMAL) + { + w->showTools(); + w->showNormal(); + return; + } - if (!w->isFullScreen() && flags == CV_WINDOW_FULLSCREEN) - { - w->hideTools(); - w->showFullScreen(); - return; - } + if (!w->isFullScreen() && flags == CV_WINDOW_FULLSCREEN) + { + w->hideTools(); + w->showFullScreen(); + return; + } } void GuiReceiver::createWindow( QString name, int flags ) { - if (!qApp) - CV_Error(CV_StsNullPtr, "NULL session handler" ); + if (!qApp) + CV_Error(CV_StsNullPtr, "NULL session handler" ); - // Check the name in the storage - if( icvFindWindowByName( name.toLatin1().data() )) - { - return; - } + // Check the name in the storage + if( icvFindWindowByName( name.toLatin1().data() )) + { + return; + } - //QPointer w1 = - new CvWindow(name, flags); + //QPointer w1 = + new CvWindow(name, flags); } void GuiReceiver::timeOut() { - _bTimeOut = true; + _bTimeOut = true; } void GuiReceiver::displayInfo( QString name, QString text, int delayms ) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (w && delayms > 0) - w->displayInfo(text,delayms); + if (w && delayms > 0) + w->displayInfo(text,delayms); } void GuiReceiver::displayStatusBar( QString name, QString text, int delayms ) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (w && delayms > 0) - if (w->myStatusBar)//if statusbar was created - w->displayStatusBar(text,delayms); + if (w && delayms > 0) + if (w->myStatusBar)//if statusbar was created + w->displayStatusBar(text,delayms); } void GuiReceiver::showImage(QString name, void* arr) { - //qDebug()<<"inshowimage"< w = icvFindWindowByName( name.toLatin1().data() ); + //qDebug()<<"inshowimage"< w = icvFindWindowByName( name.toLatin1().data() ); - if (!w)//as observed in the previous implementation (W32, GTK or Carbon), create a new window is the pointer returned is null - { - cvNamedWindow( name.toLatin1().data() ); - w = icvFindWindowByName( name.toLatin1().data() ); - } + if (!w)//as observed in the previous implementation (W32, GTK or Carbon), create a new window is the pointer returned is null + { + cvNamedWindow( name.toLatin1().data() ); + w = icvFindWindowByName( name.toLatin1().data() ); + } - if( w && arr ) - { - w->updateImage(arr); - } - else - { - qDebug()<<"Do nothing (Window or Image NULL)"<updateImage(arr); + } + else + { + qDebug()<<"Do nothing (Window or Image NULL)"< w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (w) - { - w->close(); - //in not-multiThreads mode, looks like the window is hidden but not deleted - //so I do it manually - //otherwise QApplication do it for me if the exec command was executed (in multiThread mode) - if (!multiThreads) - delete w; - } + if (w) + { + w->close(); + //in not-multiThreads mode, looks like the window is hidden but not deleted + //so I do it manually + //otherwise QApplication do it for me if the exec command was executed (in multiThread mode) + if (!multiThreads) + delete w; + } } void GuiReceiver::destroyAllWindow() { - if (!qApp) - CV_Error(CV_StsNullPtr, "NULL session handler" ); + if (!qApp) + CV_Error(CV_StsNullPtr, "NULL session handler" ); - if (multiThreads) - { - qApp->closeAllWindows(); - }else{ - QPointer w; - foreach (QWidget *widget, QApplication::topLevelWidgets()) + if (multiThreads) { - w = (CvWindow*) widget; - w->close(); - delete w; + qApp->closeAllWindows(); + }else{ + QPointer w; + foreach (QWidget *widget, QApplication::topLevelWidgets()) + { + w = (CvWindow*) widget; + w->close(); + delete w; + } } - } } void GuiReceiver::setOpenGLCallback(QString window_name, void* callbackOpenGL, void* userdata) { - QPointer w = icvFindWindowByName( window_name.toLatin1().data() ); + QPointer w = icvFindWindowByName( window_name.toLatin1().data() ); - if (w && callbackOpenGL) - w->setOpenGLCallback((CvOpenGLCallback) callbackOpenGL, userdata); + if (w && callbackOpenGL) + w->setOpenGLCallback((CvOpenGLCallback) callbackOpenGL, userdata); } void GuiReceiver::moveWindow(QString name, int x, int y) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (w) - w->move(x,y); + if (w) + w->move(x,y); } void GuiReceiver::resizeWindow(QString name, int width, int height) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName( name.toLatin1().data() ); - if (w) - w->resize(width, height); + if (w) + w->resize(width, height); } -void GuiReceiver::addButton(QString window_name, QString bar_name, QString button_name, void* on_change, void* userdata) +void GuiReceiver::addButton(QString button_name, int button_type, bool initial_button_state , void* on_change, void* userdata) { - QPointer w = icvFindWindowByName( window_name.toLatin1().data() ); - if (!w) - return; + if (!global_control_panel) + return; - if (!on_change) - CV_Error(CV_StsNullPtr, "Callback is NULL"); + QPointer b;// = icvFindButtonbarByName( button_name.toLatin1().data(), global_control_panel->myLayout ); - QPointer b = icvFindButtonbarByName( bar_name.toLatin1().data() ,window_name.toLatin1().data() ); + //if (b)//button with this name already exist + // return; - if (!b)//if the buttonbar does not exist, create it THEN, attached a new button - b = w->createButtonbar(bar_name); + CvBar* lastbar = (CvBar*) global_control_panel->myLayout->itemAt(global_control_panel->myLayout->count()-1); - if (b) - b->addButton( button_name,(CvButtonCallback) on_change, userdata); + if (lastbar->type == type_CvTrackbar)//if last bar is a trackbar, create a new buttonbar, else, attach to the current bar + b = CvWindow::createButtonbar(button_name);//the bar has the name of the first button attached to it + else + b = (CvButtonbar*) lastbar; + + b->addButton( button_name,(CvButtonCallback) on_change, userdata, button_type, initial_button_state); } void GuiReceiver::addSlider(QString bar_name, QString window_name, void* value, int count, void* on_change) { - QPointer w = icvFindWindowByName( window_name.toLatin1().data() ); - - if (!w) - return; + QBoxLayout *layout = NULL; + QPointer w; + if (window_name != "") + { + w = icvFindWindowByName( window_name.toLatin1().data() ); - QPointer t = icvFindTrackbarByName( bar_name.toLatin1().data() , window_name.toLatin1().data() ); + if (!w) + return; + }else{ + if (global_control_panel) + layout = global_control_panel->myLayout; + } - if (t)//trackbar exists - return; + QPointer t = icvFindTrackbarByName( bar_name.toLatin1().data() , window_name.toLatin1().data(), layout ); + if (t)//trackbar exists + return; - if (!value) - CV_Error(CV_StsNullPtr, "NULL value pointer" ); + if (!value) + CV_Error(CV_StsNullPtr, "NULL value pointer" ); - if (count<= 0)//count is the max value of the slider, so must be bigger than 0 - CV_Error(CV_StsNullPtr, "Max value of the slider must be bigger than 0" ); + if (count<= 0)//count is the max value of the slider, so must be bigger than 0 + CV_Error(CV_StsNullPtr, "Max value of the slider must be bigger than 0" ); - w->addSlider(bar_name,(int*)value,count,(CvTrackbarCallback) on_change); + CvWindow::addSlider(w,bar_name,(int*)value,count,(CvTrackbarCallback) on_change); } int GuiReceiver::start() { - return qApp->exec(); + return qApp->exec(); } CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTrackbarCallback on_change ) { - type=type_CvTrackbar; - myparent = arg; - name_bar = name; - setObjectName(name_bar); - dataSlider = value; + type=type_CvTrackbar; + myparent = arg; + name_bar = name; + setObjectName(name_bar); + dataSlider = value; - callback = on_change; - slider = new QSlider(Qt::Horizontal); - slider->setFocusPolicy(Qt::StrongFocus); - slider->setMinimum(0); - slider->setMaximum(count); - slider->setPageStep(5); - slider->setValue(*value); - slider->setTickPosition(QSlider::TicksBelow); + callback = on_change; + slider = new QSlider(Qt::Horizontal); + slider->setFocusPolicy(Qt::StrongFocus); + slider->setMinimum(0); + slider->setMaximum(count); + slider->setPageStep(5); + slider->setValue(*value); + slider->setTickPosition(QSlider::TicksBelow); - //Change style of the Slider - //slider->setStyleSheet(str_Trackbar_css); + //Change style of the Slider + //slider->setStyleSheet(str_Trackbar_css); - QFile qss(":/stylesheet-trackbar"); - if (qss.open(QFile::ReadOnly)) - { - slider->setStyleSheet(QLatin1String(qss.readAll())); - qss.close(); - } + QFile qss(":/stylesheet-trackbar"); + if (qss.open(QFile::ReadOnly)) + { + slider->setStyleSheet(QLatin1String(qss.readAll())); + qss.close(); + } - //this next line does not work if we change the style with a stylesheet, why ? (bug in QT ?) - //slider->setTickPosition(QSlider::TicksBelow); - label = new QPushButton; - label->setFlat(true); - setLabel(slider->value()); + //this next line does not work if we change the style with a stylesheet, why ? (bug in QT ?) + //slider->setTickPosition(QSlider::TicksBelow); + label = new QPushButton; + label->setFlat(true); + setLabel(slider->value()); - QObject::connect( slider, SIGNAL( valueChanged( int ) ),this, SLOT( update( int ) ) ); + QObject::connect( slider, SIGNAL( valueChanged( int ) ),this, SLOT( update( int ) ) ); - QObject::connect( label, SIGNAL( clicked() ),this, SLOT( createDialog() )); + QObject::connect( label, SIGNAL( clicked() ),this, SLOT( createDialog() )); - //label->setStyleSheet("QPushButton:disabled {color: black}"); + //label->setStyleSheet("QPushButton:disabled {color: black}"); - addWidget(label,Qt::AlignLeft);//name + value - addWidget(slider,Qt::AlignCenter);//slider + addWidget(label,Qt::AlignLeft);//name + value + addWidget(slider,Qt::AlignCenter);//slider } void CvTrackbar::createDialog() { - bool ok= false; + bool ok= false; - //crash if I access the values directly and give them to QInputDialog, so do a copy first. - int value = slider->value(); - int step = slider->singleStep(); - int min = slider->minimum(); - int max = slider->maximum(); + //crash if I access the values directly and give them to QInputDialog, so do a copy first. + int value = slider->value(); + int step = slider->singleStep(); + int min = slider->minimum(); + int max = slider->maximum(); - int i = + int i = #if QT_VERSION >= 0x040500 - QInputDialog::getInt + QInputDialog::getInt #else - QInputDialog::getInteger + QInputDialog::getInteger #endif - (this->parentWidget(), - tr("Slider %1").arg(name_bar), - tr("New value:"), - value, - min, - max, - step, - &ok); + (this->parentWidget(), + tr("Slider %1").arg(name_bar), + tr("New value:"), + value, + min, + max, + step, + &ok); - if (ok) - slider->setValue(i); + if (ok) + slider->setValue(i); } void CvTrackbar::update(int myvalue) { - setLabel(myvalue); + setLabel(myvalue); - *dataSlider = myvalue; - if (callback) - callback(myvalue); + *dataSlider = myvalue; + if (callback) + callback(myvalue); } void CvTrackbar::setLabel(int myvalue) { - QString nameNormalized = name_bar.leftJustified( 10, ' ', true ); - QString valueMaximum = QString("%1").arg(slider->maximum()); - QString str = QString("%1 (%2/%3)").arg(nameNormalized).arg(myvalue,valueMaximum.length(),10,QChar('0')).arg(valueMaximum); - label->setText(str); + QString nameNormalized = name_bar.leftJustified( 10, ' ', true ); + QString valueMaximum = QString("%1").arg(slider->maximum()); + QString str = QString("%1 (%2/%3)").arg(nameNormalized).arg(myvalue,valueMaximum.length(),10,QChar('0')).arg(valueMaximum); + label->setText(str); } CvTrackbar::~CvTrackbar() { - delete slider; - delete label; + delete slider; + delete label; } //here CvButtonbar class -CvButtonbar::CvButtonbar(CvWindow* arg, QString arg2) +CvButtonbar::CvButtonbar(QWidget* arg, QString arg2) { - type=type_CvButtonbar; - myparent = arg; - name_bar = arg2; - setObjectName(name_bar); + type=type_CvButtonbar; + myparent = arg; + name_bar = arg2; + setObjectName(name_bar); - /* - label = new QLabel; - setLabel(); - addWidget(label,Qt::AlignLeft ); - */ + /* + label = new QLabel; + setLabel(); + addWidget(label,Qt::AlignLeft ); + */ } CvButtonbar::~CvButtonbar() { - QLayoutItem *child; + QLayoutItem *child; - while ((child = takeAt(0)) != 0) - delete child; + while ((child = takeAt(0)) != 0) + delete child; } void CvButtonbar::setLabel() { - QString nameNormalized = name_bar.leftJustified( 10, ' ', true ); - label->setText(nameNormalized); + QString nameNormalized = name_bar.leftJustified( 10, ' ', true ); + label->setText(nameNormalized); } -void CvButtonbar::addButton( QString name, CvButtonCallback call, void* userdata) +void CvButtonbar::addButton( QString name, CvButtonCallback call, void* userdata, int button_type, bool initial_button_state) { - CvButton* button = new CvButton(this, name,call, userdata); - QString button_name = name; + QString button_name = name; - if (button_name == "") - button_name = tr("button %1").arg(this->count()); + if (button_name == "") + button_name = tr("button %1").arg(this->count()); - button->setText(button_name); - QObject::connect( button, SIGNAL( clicked() ),button, SLOT( callCallBack() )); + QPointer button; - this->addWidget(button,Qt::AlignCenter); + if (button_type == CV_PUSH_BUTTON) + //CvPushButton* + button = (QAbstractButton*) new CvPushButton(this, button_name,call, userdata); + + if (button_type == CV_CHECKBOX) + //CvCheckButton* + button = (QAbstractButton*) new CvCheckBox(this, button_name,call, userdata, initial_button_state); + + if (button) + { + QObject::connect( button, SIGNAL( clicked() ),button, SLOT( callCallBack() )); + addWidget(button,Qt::AlignCenter); + } } -CvButton::CvButton(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4) + + + + +//buttons here +CvPushButton::CvPushButton(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4) { - myparent = arg1; - button_name = arg2; - callback = arg3; - userdata=arg4; + myparent = arg1; + button_name = arg2; + callback = arg3; + userdata=arg4; + + setObjectName(button_name); + setText(button_name); } -void CvButton::callCallBack() +void CvPushButton::callCallBack() { - callback(userdata); + callback(-1,userdata); +} + +CvCheckBox::CvCheckBox(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4, bool initial_button_state) +{ + myparent = arg1; + button_name = arg2; + callback = arg3; + userdata=arg4; + + setObjectName(button_name); + setCheckState((initial_button_state == 1?Qt::Checked:Qt::Unchecked)); + setText(button_name); } +void CvCheckBox::callCallBack() +{ + callback(this->isChecked(),userdata); +} + + + + + + //here CvWinProperties class CvWinProperties::CvWinProperties(QString name_paraWindow, QWidget* parent) { - setParent(parent); - setWindowFlags(Qt::Tool); - setContentsMargins(0,0,0,0); - setWindowTitle(name_paraWindow); - setObjectName(name_paraWindow); - //parameters_window->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); - resize(100,50); - - myLayout = new QBoxLayout(QBoxLayout::TopToBottom); - myLayout->setObjectName(QString::fromUtf8("boxLayout")); - myLayout->setContentsMargins(0, 0, 0, 0); - myLayout->setSpacing(0); - myLayout->setMargin(0); - myLayout->setSizeConstraint(QLayout::SetFixedSize); - setLayout(myLayout); + setParent(parent); + setWindowFlags(Qt::Tool); + setContentsMargins(0,0,0,0); + setWindowTitle(name_paraWindow); + setObjectName(name_paraWindow); + //parameters_window->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); + resize(100,50); + + myLayout = new QBoxLayout(QBoxLayout::TopToBottom); + myLayout->setObjectName(QString::fromUtf8("boxLayout")); + myLayout->setContentsMargins(0, 0, 0, 0); + myLayout->setSpacing(0); + myLayout->setMargin(0); + myLayout->setSizeConstraint(QLayout::SetFixedSize); + setLayout(myLayout); - hide(); + hide(); } void CvWinProperties::closeEvent ( QCloseEvent * e ) { - e->accept();//intersept the close event (not sure I really need it) - //an hide event is also sent. I will intercept it and do some processing + e->accept();//intersept the close event (not sure I really need it) + //an hide event is also sent. I will intercept it and do some processing } void CvWinProperties::showEvent ( QShowEvent * event ) { - //why -1,-1 ?: do this trick because the first time the code is run, - //no value pos was saved so we let Qt move the window in the middle of its parent (event ignored). - //then hide will save the last position and thus, we want to retreive it (event accepted). - QPoint mypos(-1,-1); - QSettings settings("OpenCV2", this->windowTitle()); - mypos = settings.value("pos", mypos).toPoint(); + //why -1,-1 ?: do this trick because the first time the code is run, + //no value pos was saved so we let Qt move the window in the middle of its parent (event ignored). + //then hide will save the last position and thus, we want to retreive it (event accepted). + QPoint mypos(-1,-1); + QSettings settings("OpenCV2", this->windowTitle()); + mypos = settings.value("pos", mypos).toPoint(); - if (mypos.x()>=0) - { - move(mypos); - event->accept(); - } - else{ - event->ignore(); - } + if (mypos.x()>=0) + { + move(mypos); + event->accept(); + } + else{ + event->ignore(); + } } void CvWinProperties::hideEvent ( QHideEvent * event ) { - QSettings settings("OpenCV2", this->windowTitle()); - settings.setValue("pos", pos());//there is an offset of 6 pixels (so the window's position is wrong -- why ?) - event->accept(); + QSettings settings("OpenCV2", this->windowTitle()); + settings.setValue("pos", pos());//there is an offset of 6 pixels (so the window's position is wrong -- why ?) + event->accept(); } CvWinProperties::~CvWinProperties() { - //clear the setting pos - QSettings settings("OpenCV2", this->windowTitle()); - settings.remove("pos"); + //clear the setting pos + QSettings settings("OpenCV2", this->windowTitle()); + settings.remove("pos"); - QLayoutItem *child; - if (myLayout) - { - while ((child = myLayout->takeAt(0)) != 0) - delete child; + QLayoutItem *child; + if (myLayout) + { + while ((child = myLayout->takeAt(0)) != 0) + delete child; - delete myLayout; - } + delete myLayout; + } } @@ -1187,455 +1201,457 @@ CvWinProperties::~CvWinProperties() //Here CvWindow class CvWindow::CvWindow(QString arg, int arg2) { - moveToThread(qApp->instance()->thread()); - param_name = arg; + moveToThread(qApp->instance()->thread()); + param_name = arg; - //the first bit is for normal or autoresize - //CV_WINDOW_NORMAL = 0x00000000 and CV_WINDOW_AUTOSIZE = 0x00000001 - //the secont bit is for the gui mode (normal or expanded) - //CV_GUI_EXPANDED = 0x00000000 and CV_GUI_NORMAL = 0x00000010 - param_flags = arg2 & 0x0000000F; - param_gui_mode = arg2 & 0x000000F0; + param_flags = arg2 & 0x0000000F; + param_gui_mode = arg2 & 0x000000F0; - setAttribute(Qt::WA_DeleteOnClose);//in other case, does not release memory - setContentsMargins(0,0,0,0); - setWindowTitle(param_name); - setObjectName(param_name); + setAttribute(Qt::WA_DeleteOnClose);//in other case, does not release memory + setContentsMargins(0,0,0,0); + setWindowTitle(param_name); + setObjectName(param_name); - resize(400,300); + resize(400,300); - createLayout(); + //1: Layouts + createBarLayout(); + createGlobalLayout(); - //1: my view - int mode_display = CV_MODE_NORMAL; + //2: my view + int mode_display = CV_MODE_NORMAL; #if defined(OPENCV_GL) - mode_display = CV_MODE_OPENGL; + mode_display = CV_MODE_OPENGL; #endif - createView(mode_display); + createView(mode_display); + + //3: shortcuts and actions + createActions(); + createShortcuts(); + + //4: toolBar and statusbar + if (param_gui_mode == CV_GUI_EXPANDED) + { + createToolBar(); + createStatusBar(); + } - //2: shortcuts - createShortcuts(); + //5: create control panel + if (!global_control_panel) + global_control_panel = createParameterWindow(); - //toolBar and statusbar - if (param_gui_mode == CV_GUI_EXPANDED) - { - createToolBar(); - createStatusBar(); - createParameterWindow(); - } + parameters_window = global_control_panel; - //Now attach everything - if (myToolBar) - myLayout->addWidget(myToolBar,Qt::AlignCenter); - myLayout->addWidget(myview,Qt::AlignCenter); + //Now attach everything + if (myToolBar) + myGlobalLayout->addWidget(myToolBar,Qt::AlignCenter); - if (myStatusBar) - myLayout->addWidget(myStatusBar,Qt::AlignCenter); + myGlobalLayout->addWidget(myview,Qt::AlignCenter); - setLayout(myLayout); - show(); + myGlobalLayout->addLayout(myBarLayout,Qt::AlignCenter); + + if (myStatusBar) + myGlobalLayout->addWidget(myStatusBar,Qt::AlignCenter); + + setLayout(myGlobalLayout); + show(); } CvWindow::~CvWindow() { - QLayoutItem *child; - - if (myLayout) - { - while ((child = myLayout->takeAt(0)) != 0) - delete child; + QLayoutItem *child; - delete myLayout; - } + if (myGlobalLayout) + { + while ((child = myGlobalLayout->takeAt(0)) != 0) + delete child; - if (myStatusBar) - { - delete myStatusBar; - delete myStatusBar_msg; - } + delete myGlobalLayout; + } - if (myToolBar) - { - for (int i=0;itakeAt(0)) != 0) + delete child; - delete myToolBar; - } + delete myBarLayout; + } - for (int i=0;i t; + if (myStatusBar) + { + delete myStatusBar; + delete myStatusBar_msg; + } - if (param_gui_mode == CV_GUI_EXPANDED) - { - t = new CvButtonbar(this,name_bar); - t->setAlignment(Qt::AlignHCenter); + if (myToolBar) + { + for (int i=0;imyLayout->insertLayout(parameters_window->myLayout->count(),t); - } + delete myToolBar; + } - return t; + for (int i=0;i t = new CvButtonbar(global_control_panel,name_bar); + t->setAlignment(Qt::AlignHCenter); + + QPointer myLayout = global_control_panel->myLayout; + + myLayout->insertLayout(myLayout->count(),t); + + return t; } + void CvWindow::hideTools() { - if (myToolBar) - myToolBar->hide(); + if (myToolBar) + myToolBar->hide(); - if (myStatusBar) - myStatusBar->hide(); + if (myStatusBar) + myStatusBar->hide(); - if (parameters_window) - parameters_window->hide(); + if (parameters_window) + parameters_window->hide(); } void CvWindow::showTools() { - if (myToolBar) - myToolBar->show(); + if (myToolBar) + myToolBar->show(); - if (myStatusBar) - myStatusBar->show(); + if (myStatusBar) + myStatusBar->show(); - //if (parameters_window) - // parameters_window->show(); + //if (parameters_window) + // parameters_window->show(); } -void CvWindow::createParameterWindow() +CvWinProperties* CvWindow::createParameterWindow() { - QString name_paraWindow=param_name+" window parameters"; - parameters_window = new CvWinProperties(name_paraWindow,this); + QString name_paraWindow ="Global control panel"; + + CvWinProperties *result = new CvWinProperties(name_paraWindow,this); + return result; } void CvWindow::displayPropertiesWin() { - if (parameters_window->isHidden()) - parameters_window->show(); - else - parameters_window->hide(); + if (parameters_window->isHidden()) + parameters_window->show(); + else + parameters_window->hide(); } -void CvWindow::createToolBar() +void CvWindow::createActions() { - myToolBar = new QToolBar; - myToolBar->setFloatable(false);//is not a window - myToolBar->setMaximumHeight(28); + vect_QActions.resize(10); - vect_QActions.resize(10); + //if the shortcuts are changed in window_QT.h, we need to update the tooltip manually + vect_QActions[0] = new QAction(QIcon(":/left-icon"),"Panning left (CTRL+arrowLEFT)",this); + vect_QActions[0]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[0],SIGNAL(triggered()),myview, SLOT( siftWindowOnLeft() )); - //if the shortcuts are changed in window_QT.h, we need to update the tooltip manually - vect_QActions[0] = new QAction(QIcon(":/left-icon"),"Panning left (CTRL+arrowLEFT)",this); - QObject::connect( vect_QActions[0],SIGNAL(triggered()),myview, SLOT( siftWindowOnLeft() )); - myToolBar->addAction(vect_QActions[0]); + vect_QActions[1] = new QAction(QIcon(":/right-icon"),"Panning right (CTRL+arrowRIGHT)",this); + vect_QActions[1]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[1],SIGNAL(triggered()),myview, SLOT( siftWindowOnRight() )); - vect_QActions[1] = new QAction(QIcon(":/right-icon"),"Panning right (CTRL+arrowRIGHT)",this); - QObject::connect( vect_QActions[1],SIGNAL(triggered()),myview, SLOT( siftWindowOnRight() )); - myToolBar->addAction(vect_QActions[1]); + vect_QActions[2] = new QAction(QIcon(":/up-icon"),"Panning up (CTRL+arrowUP)",this); + vect_QActions[2]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[2],SIGNAL(triggered()),myview, SLOT( siftWindowOnUp() )); - vect_QActions[2] = new QAction(QIcon(":/up-icon"),"Panning up (CTRL+arrowUP)",this); - QObject::connect( vect_QActions[2],SIGNAL(triggered()),myview, SLOT( siftWindowOnUp() )); - myToolBar->addAction(vect_QActions[2]); + vect_QActions[3] = new QAction(QIcon(":/down-icon"),"Panning down (CTRL+arrowDOWN)",this); + vect_QActions[3]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[3],SIGNAL(triggered()),myview, SLOT( siftWindowOnDown() )); - vect_QActions[3] = new QAction(QIcon(":/down-icon"),"Panning down (CTRL+arrowDOWN)",this); - QObject::connect( vect_QActions[3],SIGNAL(triggered()),myview, SLOT( siftWindowOnDown() )); - myToolBar->addAction(vect_QActions[3]); + vect_QActions[4] = new QAction(QIcon(":/zoom_x1-icon"),"Zoom x1 (CTRL+P)",this); + vect_QActions[4]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[4],SIGNAL(triggered()),myview, SLOT( resetZoom() )); - vect_QActions[4] = new QAction(QIcon(":/zoom_x1-icon"),"Zoom x1 (CTRL+P)",this); - QObject::connect( vect_QActions[4],SIGNAL(triggered()),myview, SLOT( resetZoom() )); - myToolBar->addAction(vect_QActions[4]); + vect_QActions[5] = new QAction(QIcon(":/imgRegion-icon"),tr("Zoom x%1 (see label) (CTRL+X)") + .arg(threshold_zoom_img_region) + ,this); + vect_QActions[5]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[5],SIGNAL(triggered()),myview, SLOT( imgRegion() )); - vect_QActions[5] = new QAction(QIcon(":/imgRegion-icon"),tr("Zoom x%1 (see label) (CTRL+X)") - .arg(threshold_zoom_img_region) - ,this); - QObject::connect( vect_QActions[5],SIGNAL(triggered()),myview, SLOT( imgRegion() )); - myToolBar->addAction(vect_QActions[5]); + vect_QActions[6] = new QAction(QIcon(":/zoom_in-icon"),tr("Zoom in (CTRL++)"),this); + vect_QActions[6]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[6],SIGNAL(triggered()),myview, SLOT( ZoomIn() )); - vect_QActions[6] = new QAction(QIcon(":/zoom_in-icon"),tr("Zoom in (CTRL++)"),this); - QObject::connect( vect_QActions[6],SIGNAL(triggered()),myview, SLOT( ZoomIn() )); - myToolBar->addAction(vect_QActions[6]); + vect_QActions[7] = new QAction(QIcon(":/zoom_out-icon"),tr("Zoom out (CTRL+-)"),this); + vect_QActions[7]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[7],SIGNAL(triggered()),myview, SLOT( ZoomOut() )); - vect_QActions[7] = new QAction(QIcon(":/zoom_out-icon"),tr("Zoom out (CTRL+-)"),this); - QObject::connect( vect_QActions[7],SIGNAL(triggered()),myview, SLOT( ZoomOut() )); - myToolBar->addAction(vect_QActions[7]); + vect_QActions[8] = new QAction(QIcon(":/save-icon"),tr("Save current image (CTRL+S)"),this); + vect_QActions[8]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[8],SIGNAL(triggered()),myview, SLOT( saveView() )); - vect_QActions[8] = new QAction(QIcon(":/save-icon"),tr("Save current image (CTRL+S)"),this); - QObject::connect( vect_QActions[8],SIGNAL(triggered()),myview, SLOT( saveView() )); - myToolBar->addAction(vect_QActions[8]); + vect_QActions[9] = new QAction(QIcon(":/properties-icon"),tr("Display properties window (CTRL+P)"),this); + vect_QActions[9]->setIconVisibleInMenu(true); + QObject::connect( vect_QActions[9],SIGNAL(triggered()),this, SLOT( displayPropertiesWin() )); +} - vect_QActions[9] = new QAction(QIcon(":/properties-icon"),tr("Display properties window (CTRL+P)"),this); - QObject::connect( vect_QActions[9],SIGNAL(triggered()),this, SLOT( displayPropertiesWin() )); - myToolBar->addAction(vect_QActions[9]); +void CvWindow::createToolBar() +{ + myToolBar = new QToolBar; + myToolBar->setFloatable(false);//is not a window + myToolBar->setMaximumHeight(28); + + foreach (QAction *a, vect_QActions) + myToolBar->addAction(a); } void CvWindow::createStatusBar() { - myStatusBar = new QStatusBar; - myStatusBar->setSizeGripEnabled(false); - myStatusBar->setMaximumHeight(20); - myStatusBar_msg = new QLabel; - myStatusBar_msg->setFrameStyle(QFrame::Raised); - myStatusBar_msg->setAlignment(Qt::AlignHCenter); - myStatusBar->addWidget(myStatusBar_msg); + myStatusBar = new QStatusBar; + myStatusBar->setSizeGripEnabled(false); + myStatusBar->setMaximumHeight(20); + myStatusBar_msg = new QLabel; + myStatusBar_msg->setFrameStyle(QFrame::Raised); + myStatusBar_msg->setAlignment(Qt::AlignHCenter); + myStatusBar->addWidget(myStatusBar_msg); } -void CvWindow::createLayout() +void CvWindow::createGlobalLayout() { - myLayout = new QBoxLayout(QBoxLayout::TopToBottom); - myLayout->setObjectName(QString::fromUtf8("boxLayout")); - myLayout->setContentsMargins(0, 0, 0, 0); - myLayout->setSpacing(0); - myLayout->setMargin(0); + myGlobalLayout = new QBoxLayout(QBoxLayout::TopToBottom); + myGlobalLayout->setObjectName(QString::fromUtf8("boxLayout")); + myGlobalLayout->setContentsMargins(0, 0, 0, 0); + myGlobalLayout->setSpacing(0); + myGlobalLayout->setMargin(0); - if (param_flags == CV_WINDOW_AUTOSIZE) - myLayout->setSizeConstraint(QLayout::SetFixedSize); + if (param_flags == CV_WINDOW_AUTOSIZE) + myGlobalLayout->setSizeConstraint(QLayout::SetFixedSize); +} + +void CvWindow::createBarLayout() +{ + myBarLayout = new QBoxLayout(QBoxLayout::TopToBottom); + myBarLayout->setObjectName(QString::fromUtf8("barLayout")); + myBarLayout->setContentsMargins(0, 0, 0, 0); + myBarLayout->setSpacing(0); + myBarLayout->setMargin(0); } void CvWindow::createShortcuts() { - vect_QShortcuts.resize(10); - - vect_QShortcuts[0] = new QShortcut(shortcut_panning_left, this); - QObject::connect( vect_QShortcuts[0], SIGNAL( activated ()),myview, SLOT( siftWindowOnLeft() )); - vect_QShortcuts[1] = new QShortcut(shortcut_panning_right, this); - QObject::connect( vect_QShortcuts[1], SIGNAL( activated ()),myview, SLOT( siftWindowOnRight() )); - vect_QShortcuts[2] = new QShortcut(shortcut_panning_up, this); - QObject::connect(vect_QShortcuts[2], SIGNAL( activated ()),myview, SLOT( siftWindowOnUp() )); - vect_QShortcuts[3] = new QShortcut(shortcut_panning_down, this); - QObject::connect(vect_QShortcuts[3], SIGNAL( activated ()),myview, SLOT( siftWindowOnDown() )); - - vect_QShortcuts[4] = new QShortcut(shortcut_zoom_normal, this); - QObject::connect( vect_QShortcuts[4], SIGNAL( activated ()),myview, SLOT( resetZoom( ) )); - vect_QShortcuts[5] = new QShortcut(shortcut_zoom_imgRegion, this); - QObject::connect( vect_QShortcuts[5], SIGNAL( activated ()),myview, SLOT( imgRegion( ) )); - vect_QShortcuts[6] = new QShortcut(shortcut_zoom_in, this); - QObject::connect( vect_QShortcuts[6], SIGNAL( activated ()),myview, SLOT( ZoomIn() )); - vect_QShortcuts[7] = new QShortcut(shortcut_zoom_out, this); - QObject::connect(vect_QShortcuts[7], SIGNAL( activated ()),myview, SLOT( ZoomOut() )); - vect_QShortcuts[8] = new QShortcut(shortcut_save_img, this); - QObject::connect( vect_QShortcuts[8], SIGNAL( activated ()),myview, SLOT( saveView( ) )); - vect_QShortcuts[9] = new QShortcut(shortcut_properties_win, this); - QObject::connect( vect_QShortcuts[9], SIGNAL( activated ()),this, SLOT( displayPropertiesWin() )); + vect_QShortcuts.resize(10); + + vect_QShortcuts[0] = new QShortcut(shortcut_panning_left, this); + QObject::connect( vect_QShortcuts[0], SIGNAL( activated ()),myview, SLOT( siftWindowOnLeft() )); + vect_QShortcuts[1] = new QShortcut(shortcut_panning_right, this); + QObject::connect( vect_QShortcuts[1], SIGNAL( activated ()),myview, SLOT( siftWindowOnRight() )); + vect_QShortcuts[2] = new QShortcut(shortcut_panning_up, this); + QObject::connect(vect_QShortcuts[2], SIGNAL( activated ()),myview, SLOT( siftWindowOnUp() )); + vect_QShortcuts[3] = new QShortcut(shortcut_panning_down, this); + QObject::connect(vect_QShortcuts[3], SIGNAL( activated ()),myview, SLOT( siftWindowOnDown() )); + + vect_QShortcuts[4] = new QShortcut(shortcut_zoom_normal, this); + QObject::connect( vect_QShortcuts[4], SIGNAL( activated ()),myview, SLOT( resetZoom( ) )); + vect_QShortcuts[5] = new QShortcut(shortcut_zoom_imgRegion, this); + QObject::connect( vect_QShortcuts[5], SIGNAL( activated ()),myview, SLOT( imgRegion( ) )); + vect_QShortcuts[6] = new QShortcut(shortcut_zoom_in, this); + QObject::connect( vect_QShortcuts[6], SIGNAL( activated ()),myview, SLOT( ZoomIn() )); + vect_QShortcuts[7] = new QShortcut(shortcut_zoom_out, this); + QObject::connect(vect_QShortcuts[7], SIGNAL( activated ()),myview, SLOT( ZoomOut() )); + vect_QShortcuts[8] = new QShortcut(shortcut_save_img, this); + QObject::connect( vect_QShortcuts[8], SIGNAL( activated ()),myview, SLOT( saveView( ) )); + vect_QShortcuts[9] = new QShortcut(shortcut_properties_win, this); + QObject::connect( vect_QShortcuts[9], SIGNAL( activated ()),this, SLOT( displayPropertiesWin() )); } void CvWindow::createView(int mode) { - //mode = CV_MODE_NORMAL or CV_MODE_OPENGL - myview = new ViewPort(this, mode,CV_WINDOW_KEEPRATIO);//parent, mode_display, keep_aspect_ratio - myview->setAlignment(Qt::AlignHCenter); + //mode = CV_MODE_NORMAL or CV_MODE_OPENGL + myview = new ViewPort(this, mode,CV_WINDOW_KEEPRATIO);//parent, mode_display, keep_aspect_ratio + myview->setAlignment(Qt::AlignHCenter); } void CvWindow::setOpenGLCallback(CvOpenGLCallback func,void* userdata) { - myview->setOpenGLCallback(func,userdata); + myview->setOpenGLCallback(func,userdata); } ViewPort* CvWindow::getView() { - return myview; + return myview; } void CvWindow::displayInfo(QString text,int delayms) { - myview->startDisplayInfo(text, delayms); + myview->startDisplayInfo(text, delayms); } void CvWindow::displayStatusBar(QString text,int delayms) { - myStatusBar->showMessage(text,delayms); + myStatusBar->showMessage(text,delayms); } void CvWindow::updateImage(void* arr) { - myview->updateImage(arr); + myview->updateImage(arr); } void CvWindow::setMouseCallBack(CvMouseCallback m, void* param) { - myview->setMouseCallBack(m,param); + myview->setMouseCallBack(m,param); } -void CvWindow::addSlider(QString name, int* value, int count,CvTrackbarCallback on_change) +//addSlider is static +void CvWindow::addSlider(CvWindow* w,QString name, int* value, int count,CvTrackbarCallback on_change) { - QPointer t = new CvTrackbar(this,name,value, count, on_change); - t->setAlignment(Qt::AlignHCenter); + QPointer t = new CvTrackbar(w,name,value, count, on_change); + t->setAlignment(Qt::AlignHCenter); - int position_insert; - if (param_gui_mode == CV_GUI_NORMAL) - { - position_insert = myLayout->count(); + QPointer myLayout; - if (myStatusBar) - position_insert--;//max-1 means add trackbar between myview and statusbar + if (w) + myLayout = w->myBarLayout; + else + myLayout = global_control_panel->myLayout; - myLayout->insertLayout(position_insert,t); - return; - } + myLayout->insertLayout( myLayout->count(),t); - if (param_gui_mode == CV_GUI_EXPANDED) - { - position_insert = parameters_window->myLayout->count(); - parameters_window->myLayout->insertLayout(position_insert,t); - return; - } } //Need more test here ! void CvWindow::keyPressEvent(QKeyEvent *event) { - //see http://doc.trolltech.com/4.6/qt.html#Key-enum - int key = event->key(); - bool goodKey = false; + //see http://doc.trolltech.com/4.6/qt.html#Key-enum + int key = event->key(); + bool goodKey = false; - if (key>=20 && key<=255 ) - { - key = (int)event->text().toLocal8Bit().at(0); - goodKey = true; - } + if (key>=20 && key<=255 ) + { + key = (int)event->text().toLocal8Bit().at(0); + goodKey = true; + } - if (key == Qt::Key_Escape) - { - key = 27; - goodKey = true; - } + if (key == Qt::Key_Escape) + { + key = 27; + goodKey = true; + } - //control plus (Z, +, -, up, down, left, right) are used for zoom/panning functions - if (event->modifiers() != Qt::ControlModifier && goodKey) - { - mutexKey.lock(); - last_key = key; - //last_key = event->nativeVirtualKey (); - mutexKey.unlock(); - key_pressed.wakeAll(); - //event->accept(); - } + //control plus (Z, +, -, up, down, left, right) are used for zoom/panning functions + if (event->modifiers() != Qt::ControlModifier && goodKey) + { + mutexKey.lock(); + last_key = key; + //last_key = event->nativeVirtualKey (); + mutexKey.unlock(); + key_pressed.wakeAll(); + //event->accept(); + } - QWidget::keyPressEvent(event); + QWidget::keyPressEvent(event); } //TODO: load CV_GUI flag (done) and act accordingly (create win property if needed and attach trackbars) void CvWindow::readSettings() { - //organisation and application's name - QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName()); - QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); - QSize size = settings.value("size", QSize(400, 400)).toSize(); - //param_name = settings.value("name_window",param_name).toString(); - param_flags = settings.value("mode_resize",param_flags).toInt(); - param_gui_mode = settings.value("mode_gui",param_gui_mode).toInt(); - myview->param_keepRatio = settings.value("view_aspectRatio",myview->param_keepRatio).toInt(); - - param_flags = settings.value("mode_resize",param_flags).toInt(); - qreal m11 = settings.value("matrix_view.m11",myview->param_matrixWorld.m11()).toDouble(); - qreal m12 = settings.value("matrix_view.m12",myview->param_matrixWorld.m12()).toDouble(); - qreal m13 = settings.value("matrix_view.m13",myview->param_matrixWorld.m13()).toDouble(); - qreal m21 = settings.value("matrix_view.m21",myview->param_matrixWorld.m21()).toDouble(); - qreal m22 = settings.value("matrix_view.m22",myview->param_matrixWorld.m22()).toDouble(); - qreal m23 = settings.value("matrix_view.m23",myview->param_matrixWorld.m23()).toDouble(); - qreal m31 = settings.value("matrix_view.m31",myview->param_matrixWorld.m31()).toDouble(); - qreal m32 = settings.value("matrix_view.m32",myview->param_matrixWorld.m32()).toDouble(); - qreal m33 = settings.value("matrix_view.m33",myview->param_matrixWorld.m33()).toDouble(); - myview->param_matrixWorld = QTransform(m11,m12,m13,m21,m22,m23,m31,m32,m33); - - //trackbar here - icvLoadTrackbars(&settings); - - resize(size); - move(pos); + //organisation and application's name + QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName()); + QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); + QSize size = settings.value("size", QSize(400, 400)).toSize(); + //param_name = settings.value("name_window",param_name).toString(); + param_flags = settings.value("mode_resize",param_flags).toInt(); + param_gui_mode = settings.value("mode_gui",param_gui_mode).toInt(); + myview->param_keepRatio = settings.value("view_aspectRatio",myview->param_keepRatio).toInt(); + + param_flags = settings.value("mode_resize",param_flags).toInt(); + qreal m11 = settings.value("matrix_view.m11",myview->param_matrixWorld.m11()).toDouble(); + qreal m12 = settings.value("matrix_view.m12",myview->param_matrixWorld.m12()).toDouble(); + qreal m13 = settings.value("matrix_view.m13",myview->param_matrixWorld.m13()).toDouble(); + qreal m21 = settings.value("matrix_view.m21",myview->param_matrixWorld.m21()).toDouble(); + qreal m22 = settings.value("matrix_view.m22",myview->param_matrixWorld.m22()).toDouble(); + qreal m23 = settings.value("matrix_view.m23",myview->param_matrixWorld.m23()).toDouble(); + qreal m31 = settings.value("matrix_view.m31",myview->param_matrixWorld.m31()).toDouble(); + qreal m32 = settings.value("matrix_view.m32",myview->param_matrixWorld.m32()).toDouble(); + qreal m33 = settings.value("matrix_view.m33",myview->param_matrixWorld.m33()).toDouble(); + myview->param_matrixWorld = QTransform(m11,m12,m13,m21,m22,m23,m31,m32,m33); + + //trackbar here + icvLoadTrackbars(&settings); + + resize(size); + move(pos); } void CvWindow::writeSettings() { - //organisation and application's name - QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName()); - //settings.setValue("name_window",param_name); - settings.setValue("pos", pos()); - settings.setValue("size", size()); - settings.setValue("mode_resize",param_flags); - settings.setValue("mode_gui",param_gui_mode); - settings.setValue("view_aspectRatio",myview->param_keepRatio); + //organisation and application's name + QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName()); + //settings.setValue("name_window",param_name); + settings.setValue("pos", pos()); + settings.setValue("size", size()); + settings.setValue("mode_resize",param_flags); + settings.setValue("mode_gui",param_gui_mode); + settings.setValue("view_aspectRatio",myview->param_keepRatio); - settings.setValue("matrix_view.m11",myview->param_matrixWorld.m11()); - settings.setValue("matrix_view.m12",myview->param_matrixWorld.m12()); - settings.setValue("matrix_view.m13",myview->param_matrixWorld.m13()); - settings.setValue("matrix_view.m21",myview->param_matrixWorld.m21()); - settings.setValue("matrix_view.m22",myview->param_matrixWorld.m22()); - settings.setValue("matrix_view.m23",myview->param_matrixWorld.m23()); - settings.setValue("matrix_view.m31",myview->param_matrixWorld.m31()); - settings.setValue("matrix_view.m32",myview->param_matrixWorld.m32()); - settings.setValue("matrix_view.m33",myview->param_matrixWorld.m33()); + settings.setValue("matrix_view.m11",myview->param_matrixWorld.m11()); + settings.setValue("matrix_view.m12",myview->param_matrixWorld.m12()); + settings.setValue("matrix_view.m13",myview->param_matrixWorld.m13()); + settings.setValue("matrix_view.m21",myview->param_matrixWorld.m21()); + settings.setValue("matrix_view.m22",myview->param_matrixWorld.m22()); + settings.setValue("matrix_view.m23",myview->param_matrixWorld.m23()); + settings.setValue("matrix_view.m31",myview->param_matrixWorld.m31()); + settings.setValue("matrix_view.m32",myview->param_matrixWorld.m32()); + settings.setValue("matrix_view.m33",myview->param_matrixWorld.m33()); - icvSaveTrackbars(&settings); + icvSaveTrackbars(&settings); } void CvWindow::icvLoadTrackbars(QSettings *settings) { - int size = settings->beginReadArray("trackbars"); - QPointer t; - //Warning ---- , asume the location 0 is toolbar, 1 is myview and max-1 the status bar - //done three times in the code, in loadtrackbars, savetrackbar and in findtrackbar - + int size = settings->beginReadArray("trackbars"); + QPointer t; - //trackbar are saved in the same order, so no need to use icvFindTrackbarByName + //trackbar are saved in the same order, so no need to use icvFindTrackbarByName - int start_index = 1;//index 0 is myview - if (myToolBar) - start_index ++;//index 0 is statusbar, 1 is myview + int start_index = 0; + int stop_index = myBarLayout->layout()->count() ; - int stop_index = myLayout->layout()->count() - start_index ; - if (myStatusBar) - stop_index --;// index max-1 is the statusbar + if (stop_index-start_index == size)//if not the same number, the window saved and loaded is not the same (nb trackbar not equal) + for (int i = start_index; i < size+start_index; ++i) + { + settings->setArrayIndex(i-start_index); + t = (CvTrackbar*) myBarLayout->layout()->itemAt(i); - //(in expended mode) nbTrackbar = count() - (toolbar + myview + statusbar) (3) = stop_index - start_index + if (t->name_bar == settings->value("name").toString()) + t->slider->setValue(settings->value("value").toInt()); - if (stop_index-start_index == size)//if not the same number, the window saved and loaded is not the same (nb trackbar not equal) - for (int i = start_index; i < size+start_index; ++i) - { - settings->setArrayIndex(i-start_index); - t = (CvTrackbar*) myLayout->layout()->itemAt(i); - - if (t->name_bar == settings->value("name").toString()) - //if (t->getName() == settings->value("name").toString()) - { - t->slider->setValue(settings->value("value").toInt()); - } - } - settings->endArray(); + } + settings->endArray(); } void CvWindow::icvSaveTrackbars(QSettings *settings) { - QPointer t; + QPointer t; - //Warning ---- , asume the location 0 is toolbar, 1 is myview and max-1 the status bar - //done three times in the code, in loadtrackbars, savetrackbar and in findtrackbar - settings->beginWriteArray("trackbars"); + settings->beginWriteArray("trackbars"); - int start_index = 2; - if (myToolBar) - start_index=3; + int start_index = 0; - for (int i = start_index; i < myLayout->layout()->count()-1; ++i) { - t = (CvTrackbar*) myLayout->layout()->itemAt(i); - settings->setArrayIndex(i-start_index); - settings->setValue("name", t->name_bar); - //settings->setValue("name", t->getName()); - settings->setValue("value", t->slider->value()); - } - settings->endArray(); + for (int i = start_index; i < myBarLayout->layout()->count()-1; ++i) { + t = (CvTrackbar*) myBarLayout->layout()->itemAt(i); + settings->setArrayIndex(i-start_index); + settings->setValue("name", t->name_bar); + //settings->setValue("name", t->getName()); + settings->setValue("value", t->slider->value()); + } + settings->endArray(); } @@ -1647,688 +1663,700 @@ void CvWindow::icvSaveTrackbars(QSettings *settings) //Here is ViewPort class ViewPort::ViewPort(CvWindow* arg, int arg2, int arg3) { - centralWidget = arg, - setParent(centralWidget); - mode_display = arg2; - param_keepRatio = arg3; + centralWidget = arg, + setParent(centralWidget); + mode_display = arg2; + param_keepRatio = arg3; - setupViewport(centralWidget); - setContentsMargins(0,0,0,0); + setupViewport(centralWidget); + setContentsMargins(0,0,0,0); - setObjectName(QString::fromUtf8("graphicsView")); - timerDisplay = new QTimer(this); - timerDisplay->setSingleShot(true); - connect(timerDisplay, SIGNAL(timeout()), this, SLOT(stopDisplayInfo())); - drawInfo = false; - positionGrabbing = QPointF(0,0); - positionCorners = QRect(0,0,size().width(),size().height()); - on_mouse = NULL; - mouseCoordinate = QPoint(-1,-1); - on_openGL_draw3D = NULL; + setObjectName(QString::fromUtf8("graphicsView")); + timerDisplay = new QTimer(this); + timerDisplay->setSingleShot(true); + connect(timerDisplay, SIGNAL(timeout()), this, SLOT(stopDisplayInfo())); + drawInfo = false; + positionGrabbing = QPointF(0,0); + positionCorners = QRect(0,0,size().width(),size().height()); + on_mouse = NULL; + mouseCoordinate = QPoint(-1,-1); + on_openGL_draw3D = NULL; #if defined(OPENCV_GL) - if ( mode_display == CV_MODE_OPENGL) - { - //QGLWidget* wGL = new QGLWidget(QGLFormat(QGL::SampleBuffers)); - setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); - initGL(); - } + if ( mode_display == CV_MODE_OPENGL) + { + //QGLWidget* wGL = new QGLWidget(QGLFormat(QGL::SampleBuffers)); + setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); + initGL(); + } #endif - image2Draw_ipl=cvCreateImage(cvSize(centralWidget->width(),centralWidget->height()),IPL_DEPTH_8U,3); - image2Draw_qt = QImage((uchar*) image2Draw_ipl->imageData, image2Draw_ipl->width, image2Draw_ipl->height,QImage::Format_RGB888); - image2Draw_qt_resized = image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); + image2Draw_ipl=cvCreateImage(cvSize(centralWidget->width(),centralWidget->height()),IPL_DEPTH_8U,3); + image2Draw_qt = QImage((uchar*) image2Draw_ipl->imageData, image2Draw_ipl->width, image2Draw_ipl->height,QImage::Format_RGB888); + image2Draw_qt_resized = image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); - nbChannelOriginImage = 0; - cvZero(image2Draw_ipl); + nbChannelOriginImage = 0; + cvZero(image2Draw_ipl); - setInteractive(false); - setMouseTracking (true);//receive mouse event everytime + setInteractive(false); + setMouseTracking (true);//receive mouse event everytime } ViewPort::~ViewPort() { - if (image2Draw_ipl) - cvReleaseImage(&image2Draw_ipl); + if (image2Draw_ipl) + cvReleaseImage(&image2Draw_ipl); + + delete timerDisplay; +} - delete timerDisplay; +void ViewPort::contextMenuEvent(QContextMenuEvent *event) +{ + if (centralWidget->vect_QActions.size() > 0) + { + QMenu menu(this); + foreach (QAction *a, centralWidget->vect_QActions) + menu.addAction(a); + // menu.popup(event->globalPos()); + menu.exec(event->globalPos()); + } } //can save as JPG, JPEG, BMP, PNG void ViewPort::saveView() { - QDate date_d = QDate::currentDate (); - QString date_s = date_d.toString("dd.MM.yyyy"); - QString name_s = centralWidget->param_name+"_screenshot_"+date_s; - - QString fileName = QFileDialog::getSaveFileName(this, tr("Save File %1").arg(name_s), - name_s+".png", - tr("Images (*.png *.jpg *.bmp *.jpeg)")); + QDate date_d = QDate::currentDate (); + QString date_s = date_d.toString("dd.MM.yyyy"); + QString name_s = centralWidget->param_name+"_screenshot_"+date_s; - if (!fileName.isEmpty ())//save the picture - { - QString extension = fileName.right(3); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save File %1").arg(name_s), + name_s+".png", + tr("Images (*.png *.jpg *.bmp *.jpeg)")); - // Save it.. - if (QString::compare(extension, "png", Qt::CaseInsensitive) == 0) + if (!fileName.isEmpty ())//save the picture { - image2Draw_qt_resized.save(fileName, "PNG"); - return; - } + QString extension = fileName.right(3); - if (QString::compare(extension, "jpg", Qt::CaseInsensitive) == 0) - { - image2Draw_qt_resized.save(fileName, "JPG"); - return; - } + // Save it.. + if (QString::compare(extension, "png", Qt::CaseInsensitive) == 0) + { + image2Draw_qt_resized.save(fileName, "PNG"); + return; + } - if (QString::compare(extension, "bmp", Qt::CaseInsensitive) == 0) - { - image2Draw_qt_resized.save(fileName, "BMP"); - return; - } + if (QString::compare(extension, "jpg", Qt::CaseInsensitive) == 0) + { + image2Draw_qt_resized.save(fileName, "JPG"); + return; + } - if (QString::compare(extension, "jpeg", Qt::CaseInsensitive) == 0) - { - image2Draw_qt_resized.save(fileName, "JPEG"); - return; - } + if (QString::compare(extension, "bmp", Qt::CaseInsensitive) == 0) + { + image2Draw_qt_resized.save(fileName, "BMP"); + return; + } + + if (QString::compare(extension, "jpeg", Qt::CaseInsensitive) == 0) + { + image2Draw_qt_resized.save(fileName, "JPEG"); + return; + } - qDebug()<<"file extension not recognized, please choose between JPG, JPEG, BMP or PNG"; - } + qDebug()<<"file extension not recognized, please choose between JPG, JPEG, BMP or PNG"; + } } void ViewPort::setRatio(int flags) { - param_keepRatio = flags; - updateGeometry(); - viewport()->update(); + param_keepRatio = flags; + updateGeometry(); + viewport()->update(); } void ViewPort::imgRegion() { - scaleView( (threshold_zoom_img_region/param_matrixWorld.m11()-1)*5,QPointF(size().width()/2,size().height()/2)); + scaleView( (threshold_zoom_img_region/param_matrixWorld.m11()-1)*5,QPointF(size().width()/2,size().height()/2)); } int ViewPort::getRatio() { - return param_keepRatio; + return param_keepRatio; } void ViewPort::resetZoom() { - param_matrixWorld.reset(); - controlImagePosition(); + param_matrixWorld.reset(); + controlImagePosition(); } void ViewPort::ZoomIn() { - scaleView( 0.5,QPointF(size().width()/2,size().height()/2)); + scaleView( 0.5,QPointF(size().width()/2,size().height()/2)); } void ViewPort::ZoomOut() { - scaleView( -0.5,QPointF(size().width()/2,size().height()/2)); + scaleView( -0.5,QPointF(size().width()/2,size().height()/2)); } //Note: move 2 percent of the window void ViewPort::siftWindowOnLeft() { - float delta = 2*width()/(100.0*param_matrixWorld.m11()); - moveView(QPointF(delta,0)); + float delta = 2*width()/(100.0*param_matrixWorld.m11()); + moveView(QPointF(delta,0)); } //Note: move 2 percent of the window void ViewPort::siftWindowOnRight() { - float delta = -2*width()/(100.0*param_matrixWorld.m11()); - moveView(QPointF(delta,0)); + float delta = -2*width()/(100.0*param_matrixWorld.m11()); + moveView(QPointF(delta,0)); } //Note: move 2 percent of the window void ViewPort::siftWindowOnUp() { - float delta = 2*height()/(100.0*param_matrixWorld.m11()); - moveView(QPointF(0,delta)); + float delta = 2*height()/(100.0*param_matrixWorld.m11()); + moveView(QPointF(0,delta)); } //Note: move 2 percent of the window void ViewPort::siftWindowOnDown() { - float delta = -2*height()/(100.0*param_matrixWorld.m11()); - moveView(QPointF(0,delta)); + float delta = -2*height()/(100.0*param_matrixWorld.m11()); + moveView(QPointF(0,delta)); } void ViewPort::startDisplayInfo(QString text, int delayms) { - if (timerDisplay->isActive()) - stopDisplayInfo(); + if (timerDisplay->isActive()) + stopDisplayInfo(); - infoText = text; - timerDisplay->start(delayms); - drawInfo = true; + infoText = text; + timerDisplay->start(delayms); + drawInfo = true; } void ViewPort::stopDisplayInfo() { - timerDisplay->stop(); - drawInfo = false; + timerDisplay->stop(); + drawInfo = false; } inline bool ViewPort::isSameSize(IplImage* img1,IplImage* img2) { - return img1->width == img2->width && img1->height == img2->height; + return img1->width == img2->width && img1->height == img2->height; } void ViewPort::updateImage(void* arr) { - //if (!arr) - //CV_Error(CV_StsNullPtr, "NULL arr pointer (in showImage)" ); - CV_Assert(arr) + //if (!arr) + //CV_Error(CV_StsNullPtr, "NULL arr pointer (in showImage)" ); + CV_Assert(arr) - IplImage* tempImage = (IplImage*)arr; + IplImage* tempImage = (IplImage*)arr; - if (!isSameSize(image2Draw_ipl,tempImage)) - { - cvReleaseImage(&image2Draw_ipl); - image2Draw_ipl=cvCreateImage(cvGetSize(tempImage),IPL_DEPTH_8U,3); - image2Draw_qt = QImage((uchar*) image2Draw_ipl->imageData, image2Draw_ipl->width, image2Draw_ipl->height,QImage::Format_RGB888); - image2Draw_qt_resized = image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); + if (!isSameSize(image2Draw_ipl,tempImage)) + { + cvReleaseImage(&image2Draw_ipl); + image2Draw_ipl=cvCreateImage(cvGetSize(tempImage),IPL_DEPTH_8U,3); + image2Draw_qt = QImage((uchar*) image2Draw_ipl->imageData, image2Draw_ipl->width, image2Draw_ipl->height,QImage::Format_RGB888); + image2Draw_qt_resized = image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); - nbChannelOriginImage = tempImage->nChannels; - updateGeometry(); - } + nbChannelOriginImage = tempImage->nChannels; + updateGeometry(); + } - cvConvertImage(tempImage,image2Draw_ipl,CV_CVTIMG_SWAP_RB ); + cvConvertImage(tempImage,image2Draw_ipl,CV_CVTIMG_SWAP_RB ); - viewport()->update(); + viewport()->update(); } void ViewPort::setMouseCallBack(CvMouseCallback m, void* param) { - on_mouse = m; - on_mouse_param = param; + on_mouse = m; + on_mouse_param = param; } void ViewPort::setOpenGLCallback(CvOpenGLCallback func,void* userdata) { - on_openGL_draw3D = func; - on_openGL_param = userdata; + on_openGL_draw3D = func; + on_openGL_param = userdata; } void ViewPort::controlImagePosition() { - qreal left, top, right, bottom; - - //after check top-left, bottom right corner to avoid getting "out" during zoom/panning - param_matrixWorld.map(0,0,&left,&top); - - if (left > 0) - { - param_matrixWorld.translate(-left,0); - left = 0; - } - if (top > 0) - { - param_matrixWorld.translate(0,-top); - top = 0; - } - //------- - - QSize sizeImage = size(); - param_matrixWorld.map(sizeImage.width(),sizeImage.height(),&right,&bottom); - if (right < sizeImage.width()) - { - param_matrixWorld.translate(sizeImage.width()-right,0); - right = sizeImage.width(); - } - if (bottom < sizeImage.height()) - { - param_matrixWorld.translate(0,sizeImage.height()-bottom); - bottom = sizeImage.height(); - } - - //save corner position - positionCorners.setTopLeft(QPoint(left,top)); - positionCorners.setBottomRight(QPoint(right,bottom)); - //save also the inv matrix - matrixWorld_inv = param_matrixWorld.inverted(); - - viewport()->update(); + qreal left, top, right, bottom; + + //after check top-left, bottom right corner to avoid getting "out" during zoom/panning + param_matrixWorld.map(0,0,&left,&top); + + if (left > 0) + { + param_matrixWorld.translate(-left,0); + left = 0; + } + if (top > 0) + { + param_matrixWorld.translate(0,-top); + top = 0; + } + //------- + + QSize sizeImage = size(); + param_matrixWorld.map(sizeImage.width(),sizeImage.height(),&right,&bottom); + if (right < sizeImage.width()) + { + param_matrixWorld.translate(sizeImage.width()-right,0); + right = sizeImage.width(); + } + if (bottom < sizeImage.height()) + { + param_matrixWorld.translate(0,sizeImage.height()-bottom); + bottom = sizeImage.height(); + } + + //save corner position + positionCorners.setTopLeft(QPoint(left,top)); + positionCorners.setBottomRight(QPoint(right,bottom)); + //save also the inv matrix + matrixWorld_inv = param_matrixWorld.inverted(); + + viewport()->update(); } void ViewPort::moveView(QPointF delta) { - param_matrixWorld.translate(delta.x(),delta.y()); - controlImagePosition(); + param_matrixWorld.translate(delta.x(),delta.y()); + controlImagePosition(); } //factor is -0.5 (zoom out) or 0.5 (zoom in) void ViewPort::scaleView(qreal factor,QPointF center) { - factor/=5;//-0.1 <-> 0.1 - factor+=1;//0.9 <-> 1.1 + factor/=5;//-0.1 <-> 0.1 + factor+=1;//0.9 <-> 1.1 - //limit zoom out --- - if (param_matrixWorld.m11()==1 && factor < 1) - return; + //limit zoom out --- + if (param_matrixWorld.m11()==1 && factor < 1) + return; - if (param_matrixWorld.m11()*factor<1) - factor = 1/param_matrixWorld.m11(); + if (param_matrixWorld.m11()*factor<1) + factor = 1/param_matrixWorld.m11(); - //limit zoom int --- - if (param_matrixWorld.m11()>100 && factor > 1) - return; + //limit zoom int --- + if (param_matrixWorld.m11()>100 && factor > 1) + return; - //inverse the transform - int a, b; - matrixWorld_inv.map(center.x(),center.y(),&a,&b); + //inverse the transform + int a, b; + matrixWorld_inv.map(center.x(),center.y(),&a,&b); - param_matrixWorld.translate(a-factor*a,b-factor*b); - param_matrixWorld.scale(factor,factor); + param_matrixWorld.translate(a-factor*a,b-factor*b); + param_matrixWorld.scale(factor,factor); - controlImagePosition(); + controlImagePosition(); - //display new zoom - if (centralWidget->myStatusBar) - centralWidget->displayStatusBar(tr("Zoom: %1%").arg(param_matrixWorld.m11()*100),1000); + //display new zoom + if (centralWidget->myStatusBar) + centralWidget->displayStatusBar(tr("Zoom: %1%").arg(param_matrixWorld.m11()*100),1000); - if (param_matrixWorld.m11()>1) - setCursor(Qt::OpenHandCursor); - else - unsetCursor(); + if (param_matrixWorld.m11()>1) + setCursor(Qt::OpenHandCursor); + else + unsetCursor(); } void ViewPort::wheelEvent(QWheelEvent *event) { - scaleView( -event->delta() / 240.0,event->pos()); + scaleView( -event->delta() / 240.0,event->pos()); } void ViewPort::mousePressEvent(QMouseEvent *event) { - int cv_event = -1, flags = 0; - QPoint pt = event->pos(); + int cv_event = -1, flags = 0; + QPoint pt = event->pos(); - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(event, mouse_down, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + //icvmouseHandler: pass parameters for cv_event, flags + icvmouseHandler(event, mouse_down, cv_event, flags); + icvmouseProcessing(QPointF(pt), cv_event, flags); - if (param_matrixWorld.m11()>1) - { - setCursor(Qt::ClosedHandCursor); - positionGrabbing = event->pos(); - } + if (param_matrixWorld.m11()>1) + { + setCursor(Qt::ClosedHandCursor); + positionGrabbing = event->pos(); + } - QWidget::mousePressEvent(event); + QWidget::mousePressEvent(event); } void ViewPort::mouseReleaseEvent(QMouseEvent *event) { - int cv_event = -1, flags = 0; - QPoint pt = event->pos(); + int cv_event = -1, flags = 0; + QPoint pt = event->pos(); - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(event, mouse_up, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + //icvmouseHandler: pass parameters for cv_event, flags + icvmouseHandler(event, mouse_up, cv_event, flags); + icvmouseProcessing(QPointF(pt), cv_event, flags); - if (param_matrixWorld.m11()>1) - setCursor(Qt::OpenHandCursor); + if (param_matrixWorld.m11()>1) + setCursor(Qt::OpenHandCursor); - QWidget::mouseReleaseEvent(event); + QWidget::mouseReleaseEvent(event); } void ViewPort::mouseDoubleClickEvent(QMouseEvent *event) { - int cv_event = -1, flags = 0; - QPoint pt = event->pos(); + int cv_event = -1, flags = 0; + QPoint pt = event->pos(); - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(event, mouse_dbclick, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + //icvmouseHandler: pass parameters for cv_event, flags + icvmouseHandler(event, mouse_dbclick, cv_event, flags); + icvmouseProcessing(QPointF(pt), cv_event, flags); - QWidget::mouseDoubleClickEvent(event); + QWidget::mouseDoubleClickEvent(event); } void ViewPort::mouseMoveEvent(QMouseEvent *event) { - int cv_event = -1, flags = 0; - QPoint pt = event->pos(); + int cv_event = -1, flags = 0; + QPoint pt = event->pos(); - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(event, mouse_move, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + //icvmouseHandler: pass parameters for cv_event, flags + icvmouseHandler(event, mouse_move, cv_event, flags); + icvmouseProcessing(QPointF(pt), cv_event, flags); - if (param_matrixWorld.m11()>1 && event->buttons() == Qt::LeftButton) - { - QPointF dxy = (pt - positionGrabbing)/param_matrixWorld.m11(); - positionGrabbing = event->pos(); - moveView(dxy); - } + if (param_matrixWorld.m11()>1 && event->buttons() == Qt::LeftButton) + { + QPointF dxy = (pt - positionGrabbing)/param_matrixWorld.m11(); + positionGrabbing = event->pos(); + moveView(dxy); + } - //I update the statusbar here because if the user does a cvWaitkey(0) (like with inpaint.cpp) - //the status bar will only be repaint when a click occurs. - if (centralWidget->myStatusBar) - viewport()->update(); + //I update the statusbar here because if the user does a cvWaitkey(0) (like with inpaint.cpp) + //the status bar will only be repaint when a click occurs. + if (centralWidget->myStatusBar) + viewport()->update(); - QWidget::mouseMoveEvent(event); + QWidget::mouseMoveEvent(event); } //up, down, dclick, move void ViewPort::icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags) { - switch(event->modifiers()) - { - case Qt::ShiftModifier: - flags = CV_EVENT_FLAG_SHIFTKEY; - break; - case Qt::ControlModifier: - flags = CV_EVENT_FLAG_CTRLKEY; - break; - case Qt::AltModifier: - flags = CV_EVENT_FLAG_ALTKEY; - break; - case Qt::NoModifier : - break; - case Qt::MetaModifier: - break; - case Qt::KeypadModifier: - break; - default:; - } - - switch(event->button()) - { - case Qt::LeftButton: - cv_event = tableMouseButtons[category][0]; - flags |= CV_EVENT_FLAG_LBUTTON; - break; - case Qt::RightButton: - cv_event = tableMouseButtons[category][1]; - flags |= CV_EVENT_FLAG_RBUTTON; - break; - case Qt::MidButton: - cv_event = tableMouseButtons[category][2]; - flags |= CV_EVENT_FLAG_MBUTTON; - break; - default:; - } + switch(event->modifiers()) + { + case Qt::ShiftModifier: + flags = CV_EVENT_FLAG_SHIFTKEY; + break; + case Qt::ControlModifier: + flags = CV_EVENT_FLAG_CTRLKEY; + break; + case Qt::AltModifier: + flags = CV_EVENT_FLAG_ALTKEY; + break; + case Qt::NoModifier : + break; + case Qt::MetaModifier: + break; + case Qt::KeypadModifier: + break; + default:; + } + + switch(event->button()) + { + case Qt::LeftButton: + cv_event = tableMouseButtons[category][0]; + flags |= CV_EVENT_FLAG_LBUTTON; + break; + case Qt::RightButton: + cv_event = tableMouseButtons[category][1]; + flags |= CV_EVENT_FLAG_RBUTTON; + break; + case Qt::MidButton: + cv_event = tableMouseButtons[category][2]; + flags |= CV_EVENT_FLAG_MBUTTON; + break; + default:; + } } void ViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) { - //to convert mouse coordinate - qreal pfx, pfy; - matrixWorld_inv.map(pt.x(),pt.y(),&pfx,&pfy); - mouseCoordinate.rx()=floor(pfx); - mouseCoordinate.ry()=floor(pfy); + //to convert mouse coordinate + qreal pfx, pfy; + matrixWorld_inv.map(pt.x(),pt.y(),&pfx,&pfy); + mouseCoordinate.rx()=floor(pfx); + mouseCoordinate.ry()=floor(pfy); - if (on_mouse) - on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param ); + if (on_mouse) + on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param ); } QSize ViewPort::sizeHint() const { - if(image2Draw_ipl) - { - return QSize(image2Draw_ipl->width,image2Draw_ipl->height); - } else { - return QGraphicsView::sizeHint(); - } + if(image2Draw_ipl) + { + return QSize(image2Draw_ipl->width,image2Draw_ipl->height); + } else { + return QGraphicsView::sizeHint(); + } } void ViewPort::resizeEvent ( QResizeEvent *event) { - image2Draw_qt_resized = image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); - - controlImagePosition(); - ratioX=width()/float(image2Draw_ipl->width); - ratioY=height()/float(image2Draw_ipl->height); + image2Draw_qt_resized = image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); - if(param_keepRatio == CV_WINDOW_KEEPRATIO)//to keep the same aspect ratio - { - QSize newSize = QSize(image2Draw_ipl->width,image2Draw_ipl->height); - newSize.scale(event->size(),Qt::KeepAspectRatio); + controlImagePosition(); + ratioX=width()/float(image2Draw_ipl->width); + ratioY=height()/float(image2Draw_ipl->height); - //imageWidth/imageHeight = newWidth/newHeight +/- epsilon - //ratioX = ratioY +/- epsilon - //||ratioX - ratioY|| = epsilon - if (fabs(ratioX - ratioY)*100> ratioX)//avoid infinity loop / epsilon = 1% of ratioX + if(param_keepRatio == CV_WINDOW_KEEPRATIO)//to keep the same aspect ratio { - resize(newSize); + QSize newSize = QSize(image2Draw_ipl->width,image2Draw_ipl->height); + newSize.scale(event->size(),Qt::KeepAspectRatio); - //move to the middle - //newSize get the delta offset to place the picture in the middle of its parent - newSize= (event->size()-newSize)/2; + //imageWidth/imageHeight = newWidth/newHeight +/- epsilon + //ratioX = ratioY +/- epsilon + //||ratioX - ratioY|| = epsilon + if (fabs(ratioX - ratioY)*100> ratioX)//avoid infinity loop / epsilon = 1% of ratioX + { + resize(newSize); + + //move to the middle + //newSize get the delta offset to place the picture in the middle of its parent + newSize= (event->size()-newSize)/2; - //if the toolbar is displayed, avoid drawing myview on top of it - if (centralWidget->myToolBar) - if(!centralWidget->myToolBar->isHidden()) - newSize +=QSize(0,centralWidget->myToolBar->height()); + //if the toolbar is displayed, avoid drawing myview on top of it + if (centralWidget->myToolBar) + if(!centralWidget->myToolBar->isHidden()) + newSize +=QSize(0,centralWidget->myToolBar->height()); - move(newSize.width(),newSize.height()); + move(newSize.width(),newSize.height()); + } } - } - return QGraphicsView::resizeEvent(event); + return QGraphicsView::resizeEvent(event); } void ViewPort::paintEvent(QPaintEvent* event) { - //first paint on a file (to be able to save it if needed) - // --------- START PAINTING FILE -------------- // - QPainter myPainter(&image2Draw_qt_resized); - myPainter.setWorldTransform(param_matrixWorld); + //first paint on a file (to be able to save it if needed) + // --------- START PAINTING FILE -------------- // + QPainter myPainter(&image2Draw_qt_resized); + myPainter.setWorldTransform(param_matrixWorld); - draw2D(&myPainter); + draw2D(&myPainter); #if defined(OPENCV_GL) - if ( mode_display == CV_MODE_OPENGL && on_openGL_draw3D) - { - //myPainter.beginNativePainting(); + if ( mode_display == CV_MODE_OPENGL && on_openGL_draw3D) + { + //myPainter.beginNativePainting(); - setGL(width(),height()); - on_openGL_draw3D(on_openGL_param); - //draw3D(); - unsetGL(); + setGL(width(),height()); + on_openGL_draw3D(on_openGL_param); + //draw3D(); + unsetGL(); - //myPainter.endNativePainting(); - } + //myPainter.endNativePainting(); + } #endif - //Now disable matrixWorld for overlay display - myPainter.setWorldMatrixEnabled (false ); + //Now disable matrixWorld for overlay display + myPainter.setWorldMatrixEnabled (false ); - //in mode zoom/panning - if (param_matrixWorld.m11()>1) - { - if (param_matrixWorld.m11()>=threshold_zoom_img_region) - drawImgRegion(&myPainter); + //in mode zoom/panning + if (param_matrixWorld.m11()>1) + { + if (param_matrixWorld.m11()>=threshold_zoom_img_region) + drawImgRegion(&myPainter); - drawViewOverview(&myPainter); - } + drawViewOverview(&myPainter); + } - //for information overlay - if (drawInfo) - drawInstructions(&myPainter); + //for information overlay + if (drawInfo) + drawInstructions(&myPainter); - // --------- END PAINTING FILE -------------- // - myPainter.end(); + // --------- END PAINTING FILE -------------- // + myPainter.end(); - //and now display the file - myPainter.begin(viewport()); - myPainter.drawImage(0, 0, image2Draw_qt_resized); - //end display + //and now display the file + myPainter.begin(viewport()); + myPainter.drawImage(0, 0, image2Draw_qt_resized); + //end display - //for statusbar - if (centralWidget->myStatusBar) - drawStatusBar(); + //for statusbar + if (centralWidget->myStatusBar) + drawStatusBar(); - QGraphicsView::paintEvent(event); + QGraphicsView::paintEvent(event); } void ViewPort::draw2D(QPainter *painter) { - painter->drawImage(0,0,image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)); + painter->drawImage(0,0,image2Draw_qt.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)); } void ViewPort::drawStatusBar() { - if (mouseCoordinate.x()>=0 && - mouseCoordinate.y()>=0 && - mouseCoordinate.x()width && - mouseCoordinate.y()height) - { - QRgb rgbValue = image2Draw_qt.pixel(mouseCoordinate); - - if (nbChannelOriginImage==3) + if (mouseCoordinate.x()>=0 && + mouseCoordinate.y()>=0 && + mouseCoordinate.x()width && + mouseCoordinate.y()height) { - centralWidget->myStatusBar_msg->setText(tr("Coordinate: %1x%2 ~ ") - .arg(mouseCoordinate.x()) - .arg(mouseCoordinate.y())+ - tr("R:%3 ").arg(qRed(rgbValue))+//.arg(value.val[0])+ - tr("G:%4 ").arg(qGreen(rgbValue))+//.arg(value.val[1])+ - tr("B:%5").arg(qBlue(rgbValue))//.arg(value.val[2]) - ); - }else{ - //all the channel have the same value (because of cvconvertimage), so only the r channel is dsplayed - centralWidget->myStatusBar_msg->setText(tr("Coordinate: %1x%2 ~ ") - .arg(mouseCoordinate.x()) - .arg(mouseCoordinate.y())+ - tr("grey:%3 ").arg(qRed(rgbValue)) - ); + QRgb rgbValue = image2Draw_qt.pixel(mouseCoordinate); + + if (nbChannelOriginImage==3) + { + centralWidget->myStatusBar_msg->setText(tr("Coordinate: %1x%2 ~ ") + .arg(mouseCoordinate.x()) + .arg(mouseCoordinate.y())+ + tr("R:%3 ").arg(qRed(rgbValue))+//.arg(value.val[0])+ + tr("G:%4 ").arg(qGreen(rgbValue))+//.arg(value.val[1])+ + tr("B:%5").arg(qBlue(rgbValue))//.arg(value.val[2]) + ); + }else{ + //all the channel have the same value (because of cvconvertimage), so only the r channel is dsplayed + centralWidget->myStatusBar_msg->setText(tr("Coordinate: %1x%2 ~ ") + .arg(mouseCoordinate.x()) + .arg(mouseCoordinate.y())+ + tr("grey:%3 ").arg(qRed(rgbValue)) + ); + } } - } } void ViewPort::drawImgRegion(QPainter *painter) { - qreal offsetX = param_matrixWorld.dx()/param_matrixWorld.m11(); - offsetX = offsetX - floor(offsetX); - qreal offsetY = param_matrixWorld.dy()/param_matrixWorld.m11(); - offsetY = offsetY - floor(offsetY); + qreal offsetX = param_matrixWorld.dx()/param_matrixWorld.m11(); + offsetX = offsetX - floor(offsetX); + qreal offsetY = param_matrixWorld.dy()/param_matrixWorld.m11(); + offsetY = offsetY - floor(offsetY); - QSize view = size(); - QVarLengthArray linesX; - for (qreal x = offsetX*param_matrixWorld.m11(); x < view.width(); x += param_matrixWorld.m11() ) - linesX.append(QLineF(x, 0, x, view.height())); + QSize view = size(); + QVarLengthArray linesX; + for (qreal x = offsetX*param_matrixWorld.m11(); x < view.width(); x += param_matrixWorld.m11() ) + linesX.append(QLineF(x, 0, x, view.height())); - QVarLengthArray linesY; - for (qreal y = offsetY*param_matrixWorld.m11(); y < view.height(); y += param_matrixWorld.m11() ) - linesY.append(QLineF(0, y, view.width(), y)); + QVarLengthArray linesY; + for (qreal y = offsetY*param_matrixWorld.m11(); y < view.height(); y += param_matrixWorld.m11() ) + linesY.append(QLineF(0, y, view.width(), y)); - QFont f = painter->font(); - int original_font_size = f.pointSize(); - //change font size - //f.setPointSize(4+(param_matrixWorld.m11()-threshold_zoom_img_region)/5); - f.setPixelSize(6+(param_matrixWorld.m11()-threshold_zoom_img_region)/5); - painter->setFont(f); - QString val; - QRgb rgbValue; + QFont f = painter->font(); + int original_font_size = f.pointSize(); + //change font size + //f.setPointSize(4+(param_matrixWorld.m11()-threshold_zoom_img_region)/5); + f.setPixelSize(6+(param_matrixWorld.m11()-threshold_zoom_img_region)/5); + painter->setFont(f); + QString val; + QRgb rgbValue; - QPointF point1;//sorry, I do not know how to name it - QPointF point2;//idem + QPointF point1;//sorry, I do not know how to name it + QPointF point2;//idem - for (int j=-1;j= 0 && point2.y() >= 0) - rgbValue = image2Draw_qt.pixel(QPoint(point2.x(),point2.y())); - else - rgbValue = qRgb(0,0,0); - - if (nbChannelOriginImage==3) - { - val = tr("%1").arg(qRed(rgbValue)); - painter->setPen(QPen(Qt::red, 1)); - painter->drawText(QRect(point1.x(),point1.y(),param_matrixWorld.m11(),param_matrixWorld.m11()/3), - Qt::AlignCenter, val); - - val = tr("%1").arg(qGreen(rgbValue)); - painter->setPen(QPen(Qt::green, 1)); - painter->drawText(QRect(point1.x(),point1.y()+param_matrixWorld.m11()/3,param_matrixWorld.m11(),param_matrixWorld.m11()/3), - Qt::AlignCenter, val); - - val = tr("%1").arg(qBlue(rgbValue)); - painter->setPen(QPen(Qt::blue, 1)); - painter->drawText(QRect(point1.x(),point1.y()+2*param_matrixWorld.m11()/3,param_matrixWorld.m11(),param_matrixWorld.m11()/3), - Qt::AlignCenter, val); - - } - else - { - - val = tr("%1").arg(qRed(rgbValue)); - painter->drawText(QRect(point1.x(),point1.y(),param_matrixWorld.m11(),param_matrixWorld.m11()), - Qt::AlignCenter, val); - } - } + for (int j=-1;j= 0 && point2.y() >= 0) + rgbValue = image2Draw_qt.pixel(QPoint(point2.x(),point2.y())); + else + rgbValue = qRgb(0,0,0); + + if (nbChannelOriginImage==3) + { + val = tr("%1").arg(qRed(rgbValue)); + painter->setPen(QPen(Qt::red, 1)); + painter->drawText(QRect(point1.x(),point1.y(),param_matrixWorld.m11(),param_matrixWorld.m11()/3), + Qt::AlignCenter, val); + + val = tr("%1").arg(qGreen(rgbValue)); + painter->setPen(QPen(Qt::green, 1)); + painter->drawText(QRect(point1.x(),point1.y()+param_matrixWorld.m11()/3,param_matrixWorld.m11(),param_matrixWorld.m11()/3), + Qt::AlignCenter, val); + + val = tr("%1").arg(qBlue(rgbValue)); + painter->setPen(QPen(Qt::blue, 1)); + painter->drawText(QRect(point1.x(),point1.y()+2*param_matrixWorld.m11()/3,param_matrixWorld.m11(),param_matrixWorld.m11()/3), + Qt::AlignCenter, val); + + } + else + { + + val = tr("%1").arg(qRed(rgbValue)); + painter->drawText(QRect(point1.x(),point1.y(),param_matrixWorld.m11(),param_matrixWorld.m11()), + Qt::AlignCenter, val); + } + } - painter->setPen(QPen(Qt::black, 1)); - painter->drawLines(linesX.data(), linesX.size()); - painter->drawLines(linesY.data(), linesY.size()); + painter->setPen(QPen(Qt::black, 1)); + painter->drawLines(linesX.data(), linesX.size()); + painter->drawLines(linesY.data(), linesY.size()); - //restore font size - f.setPointSize(original_font_size); - painter->setFont(f); + //restore font size + f.setPointSize(original_font_size); + painter->setFont(f); } void ViewPort::drawViewOverview(QPainter *painter) { - QSize viewSize = size(); - viewSize.scale ( 100, 100,Qt::KeepAspectRatio ); + QSize viewSize = size(); + viewSize.scale ( 100, 100,Qt::KeepAspectRatio ); - const int margin = 5; + const int margin = 5; - //draw the image's location - painter->setBrush(QColor(0, 0, 0, 127)); - painter->setPen(Qt::darkGreen); - painter->drawRect(QRect(width()-viewSize.width()-margin, 0,viewSize.width(),viewSize.height())); + //draw the image's location + painter->setBrush(QColor(0, 0, 0, 127)); + painter->setPen(Qt::darkGreen); + painter->drawRect(QRect(width()-viewSize.width()-margin, 0,viewSize.width(),viewSize.height())); - //daw the view's location inside the image - qreal ratioSize = 1/param_matrixWorld.m11(); - qreal ratioWindow = (qreal)(viewSize.height())/(qreal)(size().height()); - painter->setPen(Qt::darkBlue); - painter->drawRect(QRectF(width()-viewSize.width()-positionCorners.left()*ratioSize*ratioWindow-margin, - -positionCorners.top()*ratioSize*ratioWindow, - (viewSize.width()-1)*ratioSize, - (viewSize.height()-1)*ratioSize) - ); + //daw the view's location inside the image + qreal ratioSize = 1/param_matrixWorld.m11(); + qreal ratioWindow = (qreal)(viewSize.height())/(qreal)(size().height()); + painter->setPen(Qt::darkBlue); + painter->drawRect(QRectF(width()-viewSize.width()-positionCorners.left()*ratioSize*ratioWindow-margin, + -positionCorners.top()*ratioSize*ratioWindow, + (viewSize.width()-1)*ratioSize, + (viewSize.height()-1)*ratioSize) + ); } void ViewPort::drawInstructions(QPainter *painter) { - QFontMetrics metrics = QFontMetrics(font()); - int border = qMax(4, metrics.leading()); + QFontMetrics metrics = QFontMetrics(font()); + int border = qMax(4, metrics.leading()); - QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125), - Qt::AlignCenter | Qt::TextWordWrap, infoText); - painter->setRenderHint(QPainter::TextAntialiasing); - painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border), - QColor(0, 0, 0, 127)); - painter->setPen(Qt::white); - painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border), - QColor(0, 0, 0, 127)); + QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125), + Qt::AlignCenter | Qt::TextWordWrap, infoText); + painter->setRenderHint(QPainter::TextAntialiasing); + painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border), + QColor(0, 0, 0, 127)); + painter->setPen(Qt::white); + painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border), + QColor(0, 0, 0, 127)); - painter->drawText((width() - rect.width())/2, border, - rect.width(), rect.height(), - Qt::AlignCenter | Qt::TextWordWrap, infoText); + painter->drawText((width() - rect.width())/2, border, + rect.width(), rect.height(), + Qt::AlignCenter | Qt::TextWordWrap, infoText); } @@ -2348,77 +2376,77 @@ void ViewPort::drawInstructions(QPainter *painter) void ViewPort::initGL() { - glShadeModel( GL_SMOOTH ); - glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); - glEnable( GL_TEXTURE_2D ); - glEnable( GL_CULL_FACE ); - glEnable( GL_DEPTH_TEST ); + glShadeModel( GL_SMOOTH ); + glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); + glEnable( GL_TEXTURE_2D ); + glEnable( GL_CULL_FACE ); + glEnable( GL_DEPTH_TEST ); } //from http://steinsoft.net/index.php?site=Programming/Code%20Snippets/OpenGL/gluperspective //do not want to link glu void ViewPort::icvgluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) { - GLdouble xmin, xmax, ymin, ymax; + GLdouble xmin, xmax, ymin, ymax; - ymax = zNear * tan(fovy * M_PI / 360.0); - ymin = -ymax; - xmin = ymin * aspect; - xmax = ymax * aspect; + ymax = zNear * tan(fovy * M_PI / 360.0); + ymin = -ymax; + xmin = ymin * aspect; + xmax = ymax * aspect; - glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); + glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); } void ViewPort::setGL(int width, int height) { - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - icvgluPerspective(45, float(width) / float(height), 0.01, 1000); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + icvgluPerspective(45, float(width) / float(height), 0.01, 1000); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); } void ViewPort::unsetGL() { - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); } void ViewPort::draw3D() { - //draw scene here - glLoadIdentity(); - - glTranslated(10.0, 10.0, -1.0); - // QVector3D p = convert(mouseCoordinate); - //glTranslated(p.x(),p.y(),p.z()); - - glRotatef( 55, 1, 0, 0 ); - glRotatef( 45, 0, 1, 0 ); - glRotatef( 0, 0, 0, 1 ); - - static const int coords[6][4][3] = { - { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } }, - { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } }, - { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } }, - { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } }, - { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } }, - { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } - }; - - for (int i = 0; i < 6; ++i) { - glColor3ub( i*20, 100+i*10, i*42 ); - glBegin(GL_QUADS); - for (int j = 0; j < 4; ++j) { - glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], 0.2 * coords[i][j][2]); + //draw scene here + glLoadIdentity(); + + glTranslated(10.0, 10.0, -1.0); + // QVector3D p = convert(mouseCoordinate); + //glTranslated(p.x(),p.y(),p.z()); + + glRotatef( 55, 1, 0, 0 ); + glRotatef( 45, 0, 1, 0 ); + glRotatef( 0, 0, 0, 1 ); + + static const int coords[6][4][3] = { + { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } }, + { { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } }, + { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } }, + { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } }, + { { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } }, + { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } + }; + + for (int i = 0; i < 6; ++i) { + glColor3ub( i*20, 100+i*10, i*42 ); + glBegin(GL_QUADS); + for (int j = 0; j < 4; ++j) { + glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], 0.2 * coords[i][j][2]); + } + glEnd(); } - glEnd(); - } } #endif diff --git a/modules/highgui/src/window_QT.h b/modules/highgui/src/window_QT.h index 17027053e1..13fb539fb8 100644 --- a/modules/highgui/src/window_QT.h +++ b/modules/highgui/src/window_QT.h @@ -74,6 +74,9 @@ #include #include #include +#include +#include +#include //start private enum enum {CV_MODE_NORMAL= 0, CV_MODE_OPENGL = 1}; @@ -130,7 +133,7 @@ public slots: void loadWindowParameters(QString name); void setOpenGLCallback(QString window_name, void* callbackOpenGL, void* userdata); void putText(void* arg1, QString text, QPoint org, void* font); - void addButton(QString window_name, QString bar_name, QString button_name, void* on_change, void* userdata); + void addButton(QString button_name, int button_type, bool initial_button_state , void* on_change, void* userdata); }; @@ -140,7 +143,7 @@ class CvBar : public QHBoxLayout public: typeBar type; QString name_bar; - QPointer myparent; + QPointer myparent; }; @@ -148,9 +151,9 @@ class CvButtonbar : public CvBar { Q_OBJECT public: - CvButtonbar(CvWindow* arg, QString bar_name); + CvButtonbar(QWidget* arg, QString bar_name); ~CvButtonbar(); - void addButton( QString button_name, CvButtonCallback call, void* userdata); + void addButton( QString button_name, CvButtonCallback call, void* userdata, int button_type, bool initial_button_state); private: void setLabel(); @@ -158,11 +161,12 @@ private: QPointer label; }; -class CvButton : public QPushButton + +class CvPushButton : public QPushButton { Q_OBJECT public: - CvButton(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata); + CvPushButton(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata); private: CvButtonbar* myparent; @@ -176,6 +180,23 @@ private slots: +class CvCheckBox : public QCheckBox +{ + Q_OBJECT +public: + CvCheckBox(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata, bool initial_button_state); + +private: + CvButtonbar* myparent; + QString button_name ; + CvButtonCallback callback; + void* userdata; + +private slots: + void callCallBack(); +}; + + class CvTrackbar : public CvBar { Q_OBJECT @@ -219,7 +240,7 @@ class CvWindow : public QWidget public: CvWindow(QString arg2, int flag = CV_WINDOW_NORMAL); ~CvWindow(); - void addSlider(QString name, int* value, int count, CvTrackbarCallback on_change = NULL); + static void addSlider(CvWindow* w,QString name, int* value, int count, CvTrackbarCallback on_change CV_DEFAULT(NULL)); void setMouseCallBack(CvMouseCallback m, void* param); void updateImage(void* arr); void displayInfo(QString text, int delayms ); @@ -229,42 +250,44 @@ public: void setOpenGLCallback(CvOpenGLCallback arg1,void* userdata); void hideTools(); void showTools(); - CvButtonbar* createButtonbar(QString bar_name); + static CvButtonbar* createButtonbar(QString bar_name); ViewPort* getView(); - CvWinProperties* getWinProp(); - QPointer myLayout; + QPointer myGlobalLayout;//All the widget (toolbar, view, LayoutBar, ...) are attached to it + QPointer myBarLayout; QPointer myStatusBar; QPointer myToolBar; QPointer myStatusBar_msg; //parameters (will be save/load) QString param_name; + QPointer parameters_window ; int param_flags; int param_gui_mode; + QVector vect_QActions; protected: virtual void keyPressEvent(QKeyEvent *event); private: - QPointer parameters_window ; QPointer myview; - QVector vect_QActions; QVector vect_QShortcuts; void icvLoadTrackbars(QSettings *settings); void icvSaveTrackbars(QSettings *settings); void createShortcuts(); + void createActions(); void createToolBar(); void createView(int mode); void createStatusBar(); - void createLayout(); - void createParameterWindow(); + void createGlobalLayout(); + void createBarLayout(); + CvWinProperties* createParameterWindow(); private slots: void displayPropertiesWin(); @@ -323,6 +346,7 @@ public slots: void siftWindowOnDown(); void resizeEvent ( QResizeEvent * ); void saveView(); + void contextMenuEvent(QContextMenuEvent *event); private: