diff --git a/CMakeLists.txt b/CMakeLists.txt index 36b5305897..cc54d1000f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -894,20 +894,17 @@ if (WITH_QT) find_package(Qt4) if (QT4_FOUND) set(HAVE_QT 1) - add_definitions(-DHAVE_QT)#We need to define te macro this way, using cvconfig.h.cmake does not work - find_package (OpenGL QUIET) + add_definitions(-DHAVE_QT) #We need to define te macro this way, using cvconfig.h.cmake does not work - #if (NOT WIN32) if (WITH_OPENGL) + find_package (OpenGL QUIET) if (QT_QTOPENGL_FOUND AND OPENGL_FOUND) - #set(HAVE_OPENGL 1) + set(HAVE_OPENGL 1) set(HAVE_QT_OPENGL 1) add_definitions(-DHAVE_QT_OPENGL) - #link_directories("${OPENGL_LIBRARIES}") set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} ${OPENGL_LIBRARIES}) endif() endif() - #endif() endif() endif() diff --git a/modules/highgui/include/opencv2/highgui/highgui.hpp b/modules/highgui/include/opencv2/highgui/highgui.hpp index 9e3d4d4bc4..4057de42e5 100644 --- a/modules/highgui/include/opencv2/highgui/highgui.hpp +++ b/modules/highgui/include/opencv2/highgui/highgui.hpp @@ -125,14 +125,8 @@ CV_EXPORTS_W void setTrackbarPos(const string& trackbarname, const string& winna // OpenGL support -typedef void (CV_CDECL *OpenGLCallback)(void* userdata); -CV_EXPORTS void createOpenGLCallback(const string& winname, OpenGLCallback onOpenGlDraw, void* userdata = 0); - typedef void (*OpenGlDrawCallback)(void* userdata); -static inline void setOpenGlDrawCallback(const string& winname, OpenGlDrawCallback onOpenGlDraw, void* userdata = 0) -{ - createOpenGLCallback(winname, onOpenGlDraw, userdata); -} +CV_EXPORTS void setOpenGlDrawCallback(const string& winname, OpenGlDrawCallback onOpenGlDraw, void* userdata = 0); CV_EXPORTS void setOpenGlContext(const string& winname); diff --git a/modules/highgui/include/opencv2/highgui/highgui_c.h b/modules/highgui/include/opencv2/highgui/highgui_c.h index 4513c07303..0ffc42d71e 100644 --- a/modules/highgui/include/opencv2/highgui/highgui_c.h +++ b/modules/highgui/include/opencv2/highgui/highgui_c.h @@ -251,8 +251,8 @@ CVAPI(int) cvWaitKey(int delay CV_DEFAULT(0)); // OpenGL support -typedef void (CV_CDECL *CvOpenGLCallback)(void* userdata); -CVAPI(void) cvCreateOpenGLCallback( const char* window_name, CvOpenGLCallback callbackOpenGL, void* userdata CV_DEFAULT(NULL), double angle CV_DEFAULT(-1), double zmin CV_DEFAULT(-1), double zmax CV_DEFAULT(-1)); +typedef void (CV_CDECL *CvOpenGlDrawCallback)(void* userdata); +CVAPI(void) cvSetOpenGlDrawCallback(const char* window_name, CvOpenGlDrawCallback callback, void* userdata CV_DEFAULT(NULL)); CVAPI(void) cvSetOpenGlContext(const char* window_name); CVAPI(void) cvUpdateWindow(const char* window_name); diff --git a/modules/highgui/src/precomp.hpp b/modules/highgui/src/precomp.hpp index 83cfa43923..dce0522299 100644 --- a/modules/highgui/src/precomp.hpp +++ b/modules/highgui/src/precomp.hpp @@ -198,10 +198,14 @@ double cvGetOpenGlProp_GTK(const char* name); #if defined (HAVE_QT) double cvGetModeWindow_QT(const char* name); void cvSetModeWindow_QT(const char* name, double prop_value); + double cvGetPropWindow_QT(const char* name); void cvSetPropWindow_QT(const char* name,double prop_value); + double cvGetRatioWindow_QT(const char* name); void cvSetRatioWindow_QT(const char* name,double prop_value); + +double cvGetOpenGlProp_QT(const char* name); #endif // OpenGL diff --git a/modules/highgui/src/window.cpp b/modules/highgui/src/window.cpp index 7e49c37f1e..70235f0b5e 100644 --- a/modules/highgui/src/window.cpp +++ b/modules/highgui/src/window.cpp @@ -133,6 +133,7 @@ CV_IMPL double cvGetWindowProperty(const char* name, int prop_id) case CV_WND_PROP_OPENGL: #if defined (HAVE_QT) + return cvGetOpenGlProp_QT(name); #elif defined WIN32 || defined _WIN32 return cvGetOpenGlProp_W32(name); #elif defined (HAVE_GTK) @@ -218,9 +219,9 @@ int cv::startWindowThread() // OpenGL support -void cv::createOpenGLCallback(const string& name, OpenGLCallback callback, void* userdata) +void cv::setOpenGlDrawCallback(const string& name, OpenGlDrawCallback callback, void* userdata) { - cvCreateOpenGLCallback(name.c_str(), callback, userdata); + cvSetOpenGlDrawCallback(name.c_str(), callback, userdata); } void cv::setOpenGlContext(const string& windowName) @@ -344,8 +345,6 @@ void cv::imshow( const string& winname, InputArray _img ) } else { - namedWindow(winname, WINDOW_OPENGL | WINDOW_AUTOSIZE); - double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE); if (autoSize > 0) @@ -492,12 +491,10 @@ void cv::pointCloudShow(const std::string& winname, const cv::GlCamera& camera, #ifndef HAVE_OPENGL -#ifndef HAVE_QT -CV_IMPL void cvCreateOpenGLCallback(const char*, CvOpenGLCallback, void*, double, double, double) +CV_IMPL void cvSetOpenGlDrawCallback(const char*, CvOpenGlDrawCallback, void*) { CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support"); } -#endif CV_IMPL void cvSetOpenGlContext(const char*) { diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index 65b50b6966..6dacf087ea 100755 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -42,15 +42,13 @@ #if defined(HAVE_QT) #include + #include + #ifdef _WIN32 #include -#define usleep Sleep #endif -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif //Static and global first static GuiReceiver *guiMainThread = NULL; @@ -68,10 +66,8 @@ static CvWinProperties* global_control_panel = NULL; //end static and global - CV_IMPL CvFont cvFontQt(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]) @@ -90,10 +86,8 @@ CV_IMPL CvFont cvFontQt(const char* nameFont, int pointSize,CvScalar color,int w } - CV_IMPL void cvAddText(const CvArr* img, const char* text, CvPoint org, CvFont* font) { - if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -106,9 +100,9 @@ CV_IMPL void cvAddText(const CvArr* img, const char* text, CvPoint org, CvFont* Q_ARG(void*,(void*) font)); } + double cvGetRatioWindow_QT(const char* name) { - if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -119,9 +113,11 @@ double cvGetRatioWindow_QT(const char* name) Qt::AutoConnection, Q_RETURN_ARG(double, result), Q_ARG(QString, QString(name))); + return result; } + void cvSetRatioWindow_QT(const char* name,double prop_value) { @@ -135,9 +131,9 @@ void cvSetRatioWindow_QT(const char* name,double prop_value) Q_ARG(double, prop_value)); } + double cvGetPropWindow_QT(const char* name) { - if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -148,12 +144,13 @@ double cvGetPropWindow_QT(const char* name) Qt::AutoConnection, Q_RETURN_ARG(double, result), Q_ARG(QString, QString(name))); + return result; } + void cvSetPropWindow_QT(const char* name,double prop_value) { - if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -164,9 +161,9 @@ void cvSetPropWindow_QT(const char* name,double prop_value) Q_ARG(double, prop_value)); } + void cvSetModeWindow_QT(const char* name, double prop_value) { - if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -177,6 +174,7 @@ void cvSetModeWindow_QT(const char* name, double prop_value) Q_ARG(double, prop_value)); } + double cvGetModeWindow_QT(const char* name) { if (!guiMainThread) @@ -189,9 +187,11 @@ double cvGetModeWindow_QT(const char* name) 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) { if (!guiMainThread) @@ -204,9 +204,9 @@ CV_IMPL void cvDisplayOverlay(const char* name, const char* text, int delayms) Q_ARG(QString, QString(name)), Q_ARG(QString, QString(text)), Q_ARG(int, delayms)); - } + CV_IMPL void cvSaveWindowParameters(const char* name) { if (!guiMainThread) @@ -218,6 +218,7 @@ CV_IMPL void cvSaveWindowParameters(const char* name) Q_ARG(QString, QString(name))); } + CV_IMPL void cvLoadWindowParameters(const char* name) { if (!guiMainThread) @@ -229,6 +230,7 @@ CV_IMPL void cvLoadWindowParameters(const char* name) Q_ARG(QString, QString(name))); } + CV_IMPL void cvDisplayStatusBar(const char* name, const char* text, int delayms) { if (!guiMainThread) @@ -244,44 +246,39 @@ CV_IMPL void cvDisplayStatusBar(const char* name, const char* text, int delayms) } -CV_IMPL int cvWaitKey( int arg ) +CV_IMPL int cvWaitKey(int delay) { - int result = -1; if (!guiMainThread) return result; - unsigned long delayms;//in milliseconds - if (arg<=0) - delayms = ULONG_MAX; - else - delayms = arg; + unsigned long delayms = delay <= 0 ? ULONG_MAX : delay; //in milliseconds if (multiThreads) { mutexKey.lock(); - if(key_pressed.wait(&mutexKey,delayms))//false if timeout + if (key_pressed.wait(&mutexKey, delayms)) //false if timeout { result = last_key; } last_key = -1; mutexKey.unlock(); - - }else{ + } + 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 - if (arg>0) - guiMainThread->timer->start(arg); + if (delay > 0) + guiMainThread->timer->start(delay); //QMutex dummy; - while(!guiMainThread->_bTimeOut) + while (!guiMainThread->bTimeOut) { qApp->processEvents(QEventLoop::AllEvents); - if (!guiMainThread)//when all the windows are deleted return result; @@ -315,158 +312,151 @@ CV_IMPL int cvWaitKey( int arg ) #else usleep(1000); #endif - } } - guiMainThread->_bTimeOut = false; + guiMainThread->bTimeOut = false; } return result; } + //Yannick Verdie //This function is experimental and some functions (such as cvSet/getWindowProperty will not work) //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); + QFuture future = QtConcurrent::run(pt2Func, argc, argv); return guiMainThread->start(); } + CV_IMPL void cvStopLoop() { - qApp->exit(); } -CvWindow* icvFindWindowByName( const char* arg ) +CvWindow* icvFindWindowByName(QString name) { + CvWindow* window = 0; - QPointer window; + //This is not a very clean way to do the stuff. Indeed, QAction automatically generate toolTil (QLabel) + //that can be grabbed here and crash the code at 'w->param_name==name'. + foreach (QWidget* widget, QApplication::topLevelWidgets()) + { + if (widget->isWindow() && !widget->parentWidget())//is a window without parent + { + CvWinModel* temp = (CvWinModel*) widget; - if( !arg ) - CV_Error( CV_StsNullPtr, "NULL name string" ); + if (temp->type == type_CvWindow) + { + CvWindow* w = (CvWindow*) temp; + if (w->windowTitle() == name) + { + window = w; + break; + } + } + } + } - QString name(arg); - CvWindow* w; - CvWinModel* temp; - - //This is not a very clean way to do the stuff. Indeed, QAction automatically generate toolTil (QLabel) - //that can be grabbed here and crash the code at 'w->param_name==name'. - foreach (QWidget *widget, QApplication::topLevelWidgets()) - { - - if (widget->isWindow() && !widget->parentWidget ())//is a window without parent - { - temp = (CvWinModel*) widget; - if (temp->type == type_CvWindow) - { - w = (CvWindow*) temp; - if (name.compare(w->param_name)==0) - { - window = w; - break; - } - } - } - } - - - return window; + return window; } -CvBar* icvFindBarbyName(QBoxLayout* layout, QString name_bar, typeBar type) +CvBar* icvFindBarByName(QBoxLayout* layout, QString name_bar, typeBar type) { - if (!layout) - return NULL; + if (!layout) + return NULL; - CvBar* t; + int stop_index = layout->layout()->count(); - int stop_index = layout->layout()->count(); + for (int i = 0; i < stop_index; ++i) + { + CvBar* t = (CvBar*) layout->layout()->itemAt(i); - 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; - } + if (t->type == type && t->name_bar == name_bar) + return t; + } - return NULL; + return NULL; } -CvTrackbar* icvFindTrackbarByName( const char* name_trackbar, const char* name_window, QBoxLayout* layout = NULL ) + +CvTrackbar* icvFindTrackBarByName(const char* name_trackbar, const char* name_window, QBoxLayout* layout = NULL) { - QString nameQt(name_trackbar); - CvBar* result = NULL; + QString nameQt(name_trackbar); - if (!name_window && global_control_panel)//window name is null and we have a control panel - layout = global_control_panel->myLayout; + if (!name_window && global_control_panel) //window name is null and we have a control panel + layout = global_control_panel->myLayout; - if (!layout) - { - QPointer w = icvFindWindowByName( name_window ); + if (!layout) + { + QPointer w = icvFindWindowByName(QLatin1String(name_window)); - if( !w ) - CV_Error( CV_StsNullPtr, "NULL window handler" ); + if (!w) + CV_Error(CV_StsNullPtr, "NULL window handler"); - if ( w->param_gui_mode == CV_GUI_NORMAL) - return (CvTrackbar*) icvFindBarbyName( w->myBarLayout, nameQt, type_CvTrackbar); + if (w->param_gui_mode == CV_GUI_NORMAL) + return (CvTrackbar*) icvFindBarByName(w->myBarLayout, nameQt, type_CvTrackbar); - if ( w->param_gui_mode == CV_GUI_EXPANDED) - { - result = icvFindBarbyName( w->myBarLayout, nameQt, type_CvTrackbar); + if (w->param_gui_mode == CV_GUI_EXPANDED) + { + CvBar* result = icvFindBarByName(w->myBarLayout, nameQt, type_CvTrackbar); - if (result) - return (CvTrackbar*) result; + if (result) + return (CvTrackbar*) result; - return (CvTrackbar*) icvFindBarbyName(global_control_panel->myLayout, nameQt, type_CvTrackbar); - } + return (CvTrackbar*) icvFindBarByName(global_control_panel->myLayout, nameQt, type_CvTrackbar); + } - return NULL; - }else - //layout was specified - { - return (CvTrackbar*) icvFindBarbyName( layout, nameQt, type_CvTrackbar); - } + return NULL; + } + else + { + //layout was specified + return (CvTrackbar*) icvFindBarByName(layout, nameQt, type_CvTrackbar); + } } -CvButtonbar* icvFindButtonbarByName( const char* button_name,QBoxLayout* layout) + +CvButtonbar* icvFindButtonBarByName(const char* button_name, QBoxLayout* layout) { - QString nameQt(button_name); - return (CvButtonbar*) icvFindBarbyName( layout, nameQt, type_CvButtonbar); + QString nameQt(button_name); + return (CvButtonbar*) icvFindBarByName(layout, nameQt, type_CvButtonbar); } -int icvInitSystem(int *c, char** v) + +int icvInitSystem(int* c, char** v) { - //"For any GUI application using Qt, there is precisely one QApplication object" - if(!QApplication::instance()) - { - new QApplication(*c,v); + //"For any GUI application using Qt, there is precisely one QApplication object" + if (!QApplication::instance()) + { + new QApplication(*c, v); - qDebug()<<"init done"; + qDebug() << "init done"; -#if defined( HAVE_QT_OPENGL ) - qDebug()<<"opengl support available"; +#ifdef HAVE_QT_OPENGL + qDebug() << "opengl support available"; #endif - } + } - return 0; + return 0; } -CV_IMPL int cvInitSystem( int, char** ) + +CV_IMPL int cvInitSystem(int, char**) { icvInitSystem(¶meterSystemC, parameterSystemV); return 0; } -CV_IMPL int cvNamedWindow( const char* name, int flags ) +CV_IMPL int cvNamedWindow(const char* name, int flags) { - if (!guiMainThread) guiMainThread = new GuiReceiver; @@ -477,12 +467,13 @@ CV_IMPL int cvNamedWindow( const char* name, int flags ) Q_ARG(QString, QString(name)), Q_ARG(int, flags)); else - guiMainThread->createWindow(QString(name),flags); + guiMainThread->createWindow(QString(name), flags); - return 1;//Dummy value + return 1; //Dummy value } -CV_IMPL void cvDestroyWindow( const char* name ) + +CV_IMPL void cvDestroyWindow(const char* name) { if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -491,13 +482,11 @@ CV_IMPL void cvDestroyWindow( const char* name ) "destroyWindow", //Qt::BlockingQueuedConnection, Qt::AutoConnection, - Q_ARG(QString, QString(name)) - ); + Q_ARG(QString, QString(name))); } - -CV_IMPL void cvDestroyAllWindows(void) +CV_IMPL void cvDestroyAllWindows() { if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -505,30 +494,30 @@ CV_IMPL void cvDestroyAllWindows(void) QMetaObject::invokeMethod(guiMainThread, "destroyAllWindow", //Qt::BlockingQueuedConnection, - Qt::AutoConnection - ); + Qt::AutoConnection); } -CV_IMPL void* cvGetWindowHandle( const char* name ) + +CV_IMPL void* cvGetWindowHandle(const char* name) { - if( !name ) + if (!name) CV_Error( CV_StsNullPtr, "NULL name string" ); - return (void*) icvFindWindowByName( name ); + return (void*) icvFindWindowByName(QLatin1String(name)); } -CV_IMPL const char* cvGetWindowName( void* window_handle ) -{ +CV_IMPL const char* cvGetWindowName(void* window_handle) +{ if( !window_handle ) CV_Error( CV_StsNullPtr, "NULL window handler" ); return ((CvWindow*)window_handle)->windowTitle().toLatin1().data(); } -CV_IMPL void cvMoveWindow( const char* name, int x, int y ) -{ +CV_IMPL void cvMoveWindow(const char* name, int x, int y) +{ if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -538,14 +527,12 @@ CV_IMPL void cvMoveWindow( const char* name, int x, int y ) Qt::AutoConnection, Q_ARG(QString, QString(name)), Q_ARG(int, x), - Q_ARG(int, y) - ); - + Q_ARG(int, y)); } -CV_IMPL void cvResizeWindow(const char* name, int width, int height ) -{ +CV_IMPL void cvResizeWindow(const char* name, int width, int height) +{ if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -555,36 +542,36 @@ CV_IMPL void cvResizeWindow(const char* name, int width, int height ) Qt::AutoConnection, Q_ARG(QString, QString(name)), Q_ARG(int, width), - Q_ARG(int, height) - ); - + Q_ARG(int, height)); } -CV_IMPL int cvCreateTrackbar2( const char* name_bar, const char* window_name, int* val, int count, CvTrackbarCallback2 on_notify, void* userdata ) + +CV_IMPL int cvCreateTrackbar2(const char* name_bar, const char* window_name, int* val, int count, CvTrackbarCallback2 on_notify, void* userdata) { if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); QMetaObject::invokeMethod(guiMainThread, - "addSlider2", - Qt::AutoConnection, - Q_ARG(QString, QString(name_bar)), - Q_ARG(QString, QString(window_name)), - Q_ARG(void*, (void*)val), - Q_ARG(int, count), - Q_ARG(void*, (void*)on_notify), - Q_ARG(void*, (void*)userdata) - ); + "addSlider2", + Qt::AutoConnection, + Q_ARG(QString, QString(name_bar)), + Q_ARG(QString, QString(window_name)), + Q_ARG(void*, (void*)val), + Q_ARG(int, count), + Q_ARG(void*, (void*)on_notify), + Q_ARG(void*, (void*)userdata)); - return 1;//dummy value + return 1; //dummy value } + CV_IMPL int cvStartWindowThread() { return 0; } -CV_IMPL int cvCreateTrackbar( const char* name_bar, const char* window_name, int* value, int count, CvTrackbarCallback on_change) + +CV_IMPL int cvCreateTrackbar(const char* name_bar, const char* window_name, int* value, int count, CvTrackbarCallback on_change) { if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -596,17 +583,14 @@ CV_IMPL int cvCreateTrackbar( const char* name_bar, const char* window_name, int Q_ARG(QString, QString(window_name)), Q_ARG(void*, (void*)value), Q_ARG(int, count), - Q_ARG(void*, (void*)on_change) - ); + Q_ARG(void*, (void*)on_change)); - return 1;//dummy value + return 1; //dummy value } - -CV_IMPL int cvCreateButton(const char* button_name,CvButtonCallback on_change, void* userdata , int button_type, int initial_button_state ) +CV_IMPL int cvCreateButton(const char* button_name, CvButtonCallback on_change, void* userdata, int button_type, int initial_button_state) { - if (!guiMainThread) CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); @@ -620,34 +604,17 @@ CV_IMPL int cvCreateButton(const char* button_name,CvButtonCallback on_change, v Q_ARG(int, button_type), Q_ARG(int, initial_button_state), Q_ARG(void*, (void*)on_change), - Q_ARG(void*, userdata) - ); + Q_ARG(void*, userdata)); return 1;//dummy value } -CV_IMPL void cvCreateOpenGLCallback( const char* window_name, CvOpenGLCallback callbackOpenGL, void* userdata, double angle, double zmin, double zmax) -{ - if (!guiMainThread) - CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); - QMetaObject::invokeMethod(guiMainThread, - "setOpenGLCallback", - Qt::AutoConnection, - Q_ARG(QString, QString(window_name)), - Q_ARG(void*, (void*)callbackOpenGL), - Q_ARG(void*, userdata), - Q_ARG(double, angle), - Q_ARG(double, zmin), - Q_ARG(double, zmax) - ); -} - -CV_IMPL int cvGetTrackbarPos( const char* name_bar, const char* window_name ) +CV_IMPL int cvGetTrackbarPos(const char* name_bar, const char* window_name) { int result = -1; - QPointer t = icvFindTrackbarByName( name_bar, window_name ); + QPointer t = icvFindTrackBarByName(name_bar, window_name); if (t) result = t->slider->value(); @@ -655,31 +622,31 @@ CV_IMPL int cvGetTrackbarPos( const char* name_bar, const char* window_name ) return result; } -CV_IMPL void cvSetTrackbarPos( const char* name_bar, const char* window_name, int pos ) -{ - QPointer t = icvFindTrackbarByName( name_bar, window_name ); +CV_IMPL void cvSetTrackbarPos(const char* name_bar, const char* window_name, int pos) +{ + QPointer t = icvFindTrackBarByName(name_bar, window_name); if (t) t->slider->setValue(pos); - } + /* assign callback for mouse events */ -CV_IMPL void cvSetMouseCallback( const char* window_name, CvMouseCallback on_mouse,void* param ) +CV_IMPL void cvSetMouseCallback(const char* window_name, CvMouseCallback on_mouse, void* param) { - QPointer w = icvFindWindowByName( window_name ); + QPointer w = icvFindWindowByName(QLatin1String(window_name)); if (!w) - CV_Error(CV_StsNullPtr, "NULL window handler" ); + CV_Error(CV_StsNullPtr, "NULL window handler"); w->setMouseCallBack(on_mouse, param); } -CV_IMPL void cvShowImage( const char* name, const CvArr* arr ) -{ +CV_IMPL void cvShowImage(const char* name, const CvArr* arr) +{ if (!guiMainThread) guiMainThread = new GuiReceiver; @@ -688,19 +655,93 @@ CV_IMPL void cvShowImage( const char* name, const CvArr* arr ) //Qt::BlockingQueuedConnection, Qt::DirectConnection, Q_ARG(QString, QString(name)), - Q_ARG(void*, (void*)arr) - ); + Q_ARG(void*, (void*)arr)); } -//----------OBJECT---------------- +#ifdef HAVE_QT_OPENGL -GuiReceiver::GuiReceiver() : _bTimeOut(false), nb_windows(0) +CV_IMPL void cvSetOpenGlDrawCallback(const char* window_name, CvOpenGlDrawCallback callback, void* userdata) +{ + if (!guiMainThread) + CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); + + QMetaObject::invokeMethod(guiMainThread, + "setOpenGlDrawCallback", + Qt::AutoConnection, + Q_ARG(QString, QString(window_name)), + Q_ARG(void*, (void*)callback), + Q_ARG(void*, userdata)); +} + + +void icvSetOpenGlCleanCallback(const char* window_name, CvOpenGlCleanCallback callback, void* userdata) +{ + if (!guiMainThread) + CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); + + QMetaObject::invokeMethod(guiMainThread, + "setOpenGlCleanCallback", + Qt::AutoConnection, + Q_ARG(QString, QString(window_name)), + Q_ARG(void*, (void*)callback), + Q_ARG(void*, userdata)); +} + + +CV_IMPL void cvSetOpenGlContext(const char* window_name) +{ + if (!guiMainThread) + CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); + + QMetaObject::invokeMethod(guiMainThread, + "setOpenGlContext", + Qt::AutoConnection, + Q_ARG(QString, QString(window_name))); +} + + +CV_IMPL void cvUpdateWindow(const char* window_name) +{ + if (!guiMainThread) + CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" ); + + QMetaObject::invokeMethod(guiMainThread, + "updateWindow", + Qt::AutoConnection, + Q_ARG(QString, QString(window_name))); +} + +#endif + + +double cvGetOpenGlProp_QT(const char* name) +{ + double result = -1; + + if (guiMainThread) + { + QMetaObject::invokeMethod(guiMainThread, + "isOpenGl", + Qt::AutoConnection, + Q_RETURN_ARG(double, result), + Q_ARG(QString, QString(name))); + } + + return result; +} + + +////////////////////////////////////////////////////// +// GuiReceiver + + +GuiReceiver::GuiReceiver() : bTimeOut(false), nb_windows(0) { doesExternalQAppExist = (QApplication::instance() != 0); icvInitSystem(¶meterSystemC, parameterSystemV); - timer = new QTimer; + timer = new QTimer(this); QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timeOut())); timer->setSingleShot(true); } @@ -712,263 +753,226 @@ void GuiReceiver::isLastWindow() { delete guiMainThread;//delete global_control_panel too guiMainThread = NULL; - if(!doesExternalQAppExist) + + if (!doesExternalQAppExist) { qApp->quit(); } } } + GuiReceiver::~GuiReceiver() -{ - +{ if (global_control_panel) { delete global_control_panel; global_control_panel = NULL; } - - delete timer; } + void GuiReceiver::putText(void* arr, QString text, QPoint org, void* arg2) { CV_Assert(arr); - CvMat * mat, stub; - int origin=0; - - if( CV_IS_IMAGE_HDR( arr )) - origin = ((IplImage*)arr)->origin; - + CvMat* mat, stub; mat = cvGetMat(arr, &stub); int nbChannelOriginImage = cvGetElemType(mat); + if (nbChannelOriginImage != CV_8UC3) return; //for now, font works only with 8UC3 - - if (nbChannelOriginImage!=CV_8UC3) return;//for now, font works only with 8UC3 - - //CvMat* image2Draw_mat = cvCreateMat( mat->rows, mat->cols, CV_8UC3 ); - QImage qimg = QImage(mat->data.ptr, mat->cols,mat->rows, mat->step,QImage::Format_RGB888); - - - //IplImage* img = (IplImage*)arg1; - - //for now, only support QImage::Format_RGB888 - //if (img->depth !=IPL_DEPTH_8U || img->nChannels != 3) - // return; + QImage qimg(mat->data.ptr, mat->cols, mat->rows, mat->step, QImage::Format_RGB888); 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]) + 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.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.drawText(org, text); qp.end(); - - //cvReleaseMat(&image2Draw_mat); } + void GuiReceiver::saveWindowParameters(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (w) w->writeSettings(); } + void GuiReceiver::loadWindowParameters(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (w) w->readSettings(); } + double GuiReceiver::getRatioWindow(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); - + QPointer w = icvFindWindowByName(name); if (!w) return -1; - return (double)w->getView()->getRatio(); + return w->getRatio(); } -void GuiReceiver::setRatioWindow(QString name, double arg2 ) + +void GuiReceiver::setRatioWindow(QString name, double arg2) { QPointer w = icvFindWindowByName( name.toLatin1().data() ); if (!w) return; - + int flags = (int) arg2; - 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); - + w->setRatio(flags); } + double GuiReceiver::getPropWindow(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); - + QPointer w = icvFindWindowByName(name); if (!w) return -1; - return (double)w->param_flags; + return (double) w->getPropWindow(); } -void GuiReceiver::setPropWindow(QString name, double arg2 ) + +void GuiReceiver::setPropWindow(QString name, double arg2) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (!w) return; int flags = (int) arg2; - if (w->param_flags == flags)//nothing to do - return; - - - 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:; - } + w->setPropWindow(flags); } + double GuiReceiver::isFullScreen(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (!w) return -1; - if (w->isFullScreen()) - return CV_WINDOW_FULLSCREEN; - else - return CV_WINDOW_NORMAL; + return w->isFullScreen() ? CV_WINDOW_FULLSCREEN : CV_WINDOW_NORMAL; } -//accept CV_WINDOW_NORMAL or CV_WINDOW_FULLSCREEN -void GuiReceiver::toggleFullScreen(QString name, double flags ) + +void GuiReceiver::toggleFullScreen(QString name, double arg2) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (!w) 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; - } + int flags = (int) arg2; + w->toggleFullScreen(flags); } -void GuiReceiver::createWindow( QString name, int flags ) + +void GuiReceiver::createWindow(QString name, int flags) { if (!qApp) CV_Error(CV_StsNullPtr, "NULL session handler" ); // Check the name in the storage - if( icvFindWindowByName( name.toLatin1().data() )) + if (icvFindWindowByName(name.toLatin1().data())) { return; } - //QPointer w1 = nb_windows++; new CvWindow(name, flags); } + void GuiReceiver::timeOut() { - _bTimeOut = true; + bTimeOut = true; } -void GuiReceiver::displayInfo( QString name, QString text, int delayms ) + +void GuiReceiver::displayInfo(QString name, QString text, int delayms) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (w && delayms > 0) - w->displayInfo(text,delayms); + w->displayInfo(text, delayms); } -void GuiReceiver::displayStatusBar( QString name, QString text, int delayms ) + +void GuiReceiver::displayStatusBar(QString name, QString text, int delayms) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (w && delayms > 0) - if (w->myStatusBar)//if statusbar was created - w->displayStatusBar(text,delayms); + w->displayStatusBar(text, delayms); } + void GuiReceiver::showImage(QString name, void* arr) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); - if (!w)//as observed in the previous implementation (W32, GTK or Carbon), create a new window is the pointer returned is null + 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() ); + cvNamedWindow(name.toLatin1().data()); + w = icvFindWindowByName(name); } - if( w && arr ) - { + if (!w || !arr) + return; // keep silence here. + + if (w->isOpenGl()) + { + CvMat* mat, stub; + + mat = cvGetMat(arr, &stub); + + cv::Mat im(mat); + cv::imshow(name.toStdString(), im); + } + else + { w->updateImage(arr); + } - if (w->isHidden()) - w->show(); - } - else - { - CV_Error(CV_StsNullPtr, "Do nothing (Window or Image NULL)"); - } + if (w->isHidden()) + w->show(); } + void GuiReceiver::destroyWindow(QString name) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); 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) @@ -977,6 +981,7 @@ void GuiReceiver::destroyWindow(QString name) } } + void GuiReceiver::destroyAllWindow() { if (!qApp) @@ -994,12 +999,12 @@ void GuiReceiver::destroyAllWindow() bool isWidgetDeleted = true; while(isWidgetDeleted) { - isWidgetDeleted = false; + isWidgetDeleted = false; QWidgetList list = QApplication::topLevelWidgets(); for (int i = 0; i < list.count(); i++) { QObject *obj = list.at(i); - if (obj->metaObject ()->className () == QString("CvWindow")) + if (obj->metaObject()->className() == QString("CvWindow")) { delete obj; isWidgetDeleted = true; @@ -1008,173 +1013,222 @@ void GuiReceiver::destroyAllWindow() } } } - } -void GuiReceiver::setOpenGLCallback(QString window_name, void* callbackOpenGL, void* userdata, double angle, double zmin, double zmax) -{ - QPointer w = icvFindWindowByName( window_name.toLatin1().data() ); - - if (w && callbackOpenGL) - w->setOpenGLCallback((CvOpenGLCallback) callbackOpenGL, userdata,angle,zmin,zmax); -} void GuiReceiver::moveWindow(QString name, int x, int y) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (w) - w->move(x,y); - + w->move(x, y); } + void GuiReceiver::resizeWindow(QString name, int width, int height) { - QPointer w = icvFindWindowByName( name.toLatin1().data() ); + QPointer w = icvFindWindowByName(name); if (w) - { + { w->showNormal(); - w->resize(width, height); + w->setViewportSize(QSize(width, height)); } } + void GuiReceiver::enablePropertiesButtonEachWindow() { - CvWindow* w; - CvWinModel* temp; - //For each window, enable window property button - foreach (QWidget *widget, QApplication::topLevelWidgets()) + foreach (QWidget* widget, QApplication::topLevelWidgets()) { - - if (widget->isWindow() && !widget->parentWidget ())//is a window without parent + if (widget->isWindow() && !widget->parentWidget()) //is a window without parent { - temp = (CvWinModel*) widget; + CvWinModel* temp = (CvWinModel*) widget; if (temp->type == type_CvWindow) { - w = (CvWindow*) widget; + CvWindow* w = (CvWindow*) widget; + //active window properties button - w->vect_QActions[9]->setDisabled(false); + w->enablePropertiesButton(); } } } } -void GuiReceiver::addButton(QString button_name, int button_type, int initial_button_state , void* on_change, void* userdata) -{ +void GuiReceiver::addButton(QString button_name, int button_type, int initial_button_state, void* on_change, void* userdata) +{ if (!global_control_panel) return; - QPointer b;// = icvFindButtonbarByName( button_name.toLatin1().data(), global_control_panel->myLayout ); - //if (b)//button with this name already exist - // return; + QPointer b; - - if (global_control_panel->myLayout->count() == 0)//if that is the first button attach to the control panel, create a new button bar + if (global_control_panel->myLayout->count() == 0) //if that is the first button attach to the control panel, create a new button bar { - - b = CvWindow::createButtonbar(button_name);//the bar has the name of the first button attached to it + b = CvWindow::createButtonBar(button_name); //the bar has the name of the first button attached to it enablePropertiesButtonEachWindow(); - }else{ - CvBar* lastbar = (CvBar*) global_control_panel->myLayout->itemAt(global_control_panel->myLayout->count()-1); + } + else + { + CvBar* lastbar = (CvBar*) global_control_panel->myLayout->itemAt(global_control_panel->myLayout->count() - 1); - 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 + 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); + b->addButton(button_name, (CvButtonCallback) on_change, userdata, button_type, initial_button_state); } + void GuiReceiver::addSlider2(QString bar_name, QString window_name, void* value, int count, void* on_change, void *userdata) { QBoxLayout *layout = NULL; QPointer w; - if (window_name != "") + + if (!window_name.isEmpty()) { - w = icvFindWindowByName( window_name.toLatin1().data() ); + w = icvFindWindowByName(window_name); if (!w) return; - }else{ + } + else + { if (global_control_panel) layout = global_control_panel->myLayout; } - QPointer t = icvFindTrackbarByName( bar_name.toLatin1().data() , window_name.toLatin1().data(), layout ); + QPointer t = icvFindTrackBarByName(bar_name.toLatin1().data(), window_name.toLatin1().data(), layout); - if (t)//trackbar exists + if (t) //trackbar exists return; 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 + 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" ); - CvWindow::addSlider2(w,bar_name,(int*)value,count,(CvTrackbarCallback2) on_change, userdata); + CvWindow::addSlider2(w, bar_name, (int*)value, count, (CvTrackbarCallback2) on_change, userdata); } + void GuiReceiver::addSlider(QString bar_name, QString window_name, void* value, int count, void* on_change) { QBoxLayout *layout = NULL; QPointer w; - if (window_name != "") + + if (!window_name.isEmpty()) { - w = icvFindWindowByName( window_name.toLatin1().data() ); + w = icvFindWindowByName(window_name); if (!w) return; - }else{ + } + else + { if (global_control_panel) layout = global_control_panel->myLayout; } - QPointer t = icvFindTrackbarByName( bar_name.toLatin1().data() , window_name.toLatin1().data(), layout ); + QPointer t = icvFindTrackBarByName(bar_name.toLatin1().data(), window_name.toLatin1().data(), layout); - if (t)//trackbar exists + if (t) //trackbar exists return; 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 + 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" ); - CvWindow::addSlider(w,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(); } -CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTrackbarCallback2 on_change, void* data ) + +void GuiReceiver::setOpenGlDrawCallback(QString name, void* callback, void* userdata) +{ + QPointer w = icvFindWindowByName(name); + + if (w) + w->setOpenGlDrawCallback((CvOpenGlDrawCallback) callback, userdata); +} + +void GuiReceiver::setOpenGlCleanCallback(QString name, void* callback, void* userdata) +{ + QPointer w = icvFindWindowByName(name); + + if (w) + w->setOpenGlCleanCallback((CvOpenGlCleanCallback) callback, userdata); +} + +void GuiReceiver::setOpenGlContext(QString name) +{ + QPointer w = icvFindWindowByName(name); + + if (w) + w->makeCurrentOpenGlContext(); +} + +void GuiReceiver::updateWindow(QString name) +{ + QPointer w = icvFindWindowByName(name); + + if (w) + w->updateGl(); +} + +double GuiReceiver::isOpenGl(QString name) +{ + double result = -1; + + QPointer w = icvFindWindowByName(name); + + if (w) + result = (double) w->isOpenGl(); + + return result; +} + + +////////////////////////////////////////////////////// +// CvTrackbar + + +CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTrackbarCallback2 on_change, void* data) { callback = NULL; callback2 = on_change; userdata = data; - construc_trackbar(arg,name, value, count); + create(arg, name, value, count); } -CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTrackbarCallback on_change ) + +CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTrackbarCallback on_change) { callback = on_change; callback2 = NULL; userdata = NULL; - construc_trackbar(arg,name, value, count); + create(arg, name, value, count); } -void CvTrackbar::construc_trackbar(CvWindow* arg, QString name, int* value, int count) + +void CvTrackbar::create(CvWindow* arg, QString name, int* value, int count) { - type=type_CvTrackbar; + type = type_CvTrackbar; myparent = arg; name_bar = name; setObjectName(name_bar); @@ -1207,20 +1261,20 @@ void CvTrackbar::construc_trackbar(CvWindow* arg, QString name, int* value, int 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}"); - 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(); @@ -1245,9 +1299,9 @@ void CvTrackbar::createDialog() if (ok) slider->setValue(i); - } + void CvTrackbar::update(int myvalue) { setLabel(myvalue); @@ -1261,11 +1315,12 @@ void CvTrackbar::update(int myvalue) if (callback2) { - callback2(myvalue,userdata); + callback2(myvalue, userdata); return; } } + void CvTrackbar::setLabel(int myvalue) { QString nameNormalized = name_bar.leftJustified( 10, ' ', true ); @@ -1274,49 +1329,31 @@ void CvTrackbar::setLabel(int myvalue) label->setText(str); } -CvTrackbar::~CvTrackbar() -{ - delete slider; - delete label; -} - +////////////////////////////////////////////////////// +// CvButtonbar //here CvButtonbar class CvButtonbar::CvButtonbar(QWidget* arg, QString arg2) { - type=type_CvButtonbar; + type = type_CvButtonbar; myparent = arg; name_bar = arg2; setObjectName(name_bar); - group_button = new QButtonGroup; - - /* - label = new QLabel; - setLabel(); - addWidget(label,Qt::AlignLeft ); - */ + group_button = new QButtonGroup(this); } -CvButtonbar::~CvButtonbar() -{ - QLayoutItem *child; - - while ((child = takeAt(0)) != 0) - delete child; - - delete group_button; -} void CvButtonbar::setLabel() { - QString nameNormalized = name_bar.leftJustified( 10, ' ', true ); + QString nameNormalized = name_bar.leftJustified(10, ' ', true); label->setText(nameNormalized); } -void CvButtonbar::addButton( QString name, CvButtonCallback call, void* userdata, int button_type, int initial_button_state) + +void CvButtonbar::addButton(QString name, CvButtonCallback call, void* userdata, int button_type, int initial_button_state) { QString button_name = name; @@ -1338,23 +1375,28 @@ void CvButtonbar::addButton( QString name, CvButtonCallback call, void* userdata } if (button) - { - if (button_type == CV_PUSH_BUTTON) - QObject::connect( button, SIGNAL( clicked(bool) ),button, SLOT( callCallBack(bool) )); - else - QObject::connect( button, SIGNAL( toggled(bool) ),button, SLOT( callCallBack(bool) )); - addWidget(button,Qt::AlignCenter); + { + if (button_type == CV_PUSH_BUTTON) + QObject::connect(button, SIGNAL(clicked(bool)), button, SLOT(callCallBack(bool))); + else + QObject::connect(button, SIGNAL(toggled(bool)), button, SLOT(callCallBack(bool))); + + addWidget(button, Qt::AlignCenter); } } +////////////////////////////////////////////////////// +// Buttons + + //buttons here CvPushButton::CvPushButton(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4) { myparent = arg1; button_name = arg2; callback = arg3; - userdata=arg4; + userdata = arg4; setObjectName(button_name); setText(button_name); @@ -1363,39 +1405,43 @@ CvPushButton::CvPushButton(CvButtonbar* arg1, QString arg2, CvButtonCallback arg callCallBack(true); } + void CvPushButton::callCallBack(bool checked) { if (callback) - callback(checked,userdata); + callback(checked, userdata); } + CvCheckBox::CvCheckBox(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4, int initial_button_state) { myparent = arg1; button_name = arg2; callback = arg3; - userdata=arg4; + userdata = arg4; setObjectName(button_name); - setCheckState((initial_button_state == 1?Qt::Checked:Qt::Unchecked)); + setCheckState((initial_button_state == 1 ? Qt::Checked : Qt::Unchecked)); setText(button_name); if (isChecked()) callCallBack(true); } + void CvCheckBox::callCallBack(bool checked) { if (callback) - callback(checked,userdata); + callback(checked, userdata); } + CvRadioButton::CvRadioButton(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4, int initial_button_state) { myparent = arg1; button_name = arg2; callback = arg3; - userdata=arg4; + userdata = arg4; setObjectName(button_name); setChecked(initial_button_state); @@ -1408,10 +1454,12 @@ CvRadioButton::CvRadioButton(CvButtonbar* arg1, QString arg2, CvButtonCallback a void CvRadioButton::callCallBack(bool checked) { if (callback) - callback(checked,userdata); + callback(checked, userdata); } +////////////////////////////////////////////////////// +// CvWinProperties //here CvWinProperties class @@ -1420,10 +1468,10 @@ CvWinProperties::CvWinProperties(QString name_paraWindow, QObject* parent) //setParent(parent); type = type_CvWinProperties; setWindowFlags(Qt::Tool); - setContentsMargins(0,0,0,0); + setContentsMargins(0, 0, 0, 0); setWindowTitle(name_paraWindow); setObjectName(name_paraWindow); - resize(100,50); + resize(100, 50); myLayout = new QBoxLayout(QBoxLayout::TopToBottom); myLayout->setObjectName(QString::fromUtf8("boxLayout")); @@ -1436,73 +1484,71 @@ CvWinProperties::CvWinProperties(QString name_paraWindow, QObject* parent) hide(); } -void CvWinProperties::closeEvent ( QCloseEvent * e ) + +void CvWinProperties::closeEvent(QCloseEvent* e) { - e->accept();//intersept the close event (not sure I really need it) + 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 ) + +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()); + QPoint mypos(-1, -1); + QSettings settings("OpenCV2", windowTitle()); mypos = settings.value("pos", mypos).toPoint(); - if (mypos.x()>=0) + if (mypos.x() >= 0) { move(mypos); event->accept(); } - else{ + else + { event->ignore(); } } -void CvWinProperties::hideEvent ( QHideEvent * event ) + +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 ?) + QSettings settings("OpenCV2", 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()); + QSettings settings("OpenCV2", windowTitle()); settings.remove("pos"); - - QLayoutItem *child; - if (myLayout) - { - while ((child = myLayout->takeAt(0)) != 0) - delete child; - - delete myLayout; - } } -//Here CvWindow class -CvWindow::CvWindow(QString arg, int arg2) +////////////////////////////////////////////////////// +// CvWindow + + +CvWindow::CvWindow(QString name, int arg2) { type = type_CvWindow; moveToThread(qApp->instance()->thread()); - param_name = arg; param_flags = arg2 & 0x0000000F; param_gui_mode = arg2 & 0x000000F0; param_ratio_mode = arg2 & 0x00000F00; - //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(name); + setObjectName(name); - resize(400,300); - setMinimumSize(1,1); + resize(400, 300); + setMinimumSize(1, 1); //1: create control panel if (!global_control_panel) @@ -1513,97 +1559,451 @@ CvWindow::CvWindow(QString arg, int arg2) createGlobalLayout(); //3: my view - int mode_display = CV_MODE_NORMAL; -#if defined( HAVE_QT_OPENGL ) - mode_display = CV_MODE_OPENGL; +#ifndef HAVE_QT_OPENGL + if (arg2 & CV_WINDOW_OPENGL) + CV_Error( CV_OpenGlNotSupported, "Library was built without OpenGL support" ); + mode_display = CV_MODE_NORMAL; +#else + mode_display = arg2 & CV_WINDOW_OPENGL ? CV_MODE_OPENGL : CV_MODE_NORMAL; + if (mode_display == CV_MODE_OPENGL) + param_gui_mode = CV_GUI_NORMAL; #endif - createView(mode_display, param_ratio_mode); + createView(); //4: shortcuts and actions - createActions(); - createShortcuts(); - //5: toolBar and statusbar if (param_gui_mode == CV_GUI_EXPANDED) { + createActions(); + createShortcuts(); + createToolBar(); createStatusBar(); } - //Now attach everything if (myToolBar) - myGlobalLayout->addWidget(myToolBar,Qt::AlignCenter); + myGlobalLayout->addWidget(myToolBar, Qt::AlignCenter); - myGlobalLayout->addWidget(myview,Qt::AlignCenter); + myGlobalLayout->addWidget(myView->getWidget(), Qt::AlignCenter); - myGlobalLayout->addLayout(myBarLayout,Qt::AlignCenter); + myGlobalLayout->addLayout(myBarLayout, Qt::AlignCenter); if (myStatusBar) - myGlobalLayout->addWidget(myStatusBar,Qt::AlignCenter); + myGlobalLayout->addWidget(myStatusBar, Qt::AlignCenter); setLayout(myGlobalLayout); show(); - } + CvWindow::~CvWindow() { - - QLayoutItem *child; - - if (myGlobalLayout) - { - while ((child = myGlobalLayout->takeAt(0)) != 0) - delete child; - - delete myGlobalLayout; - } - - if (myBarLayout) - { - while ((child = myBarLayout->takeAt(0)) != 0) - delete child; - - delete myBarLayout; - } - - if (myStatusBar) - { - delete myStatusBar; - delete myStatusBar_msg; - } - - if (myToolBar) - { - for (int i=0;iisLastWindow(); } - -CvButtonbar* CvWindow::createButtonbar(QString name_bar) +void CvWindow::setMouseCallBack(CvMouseCallback callback, void* param) { - QPointer t = new CvButtonbar(global_control_panel,name_bar); + myView->setMouseCallBack(callback, param); +} + + +void CvWindow::writeSettings() +{ + //organisation and application's name + QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName()); + + settings.setValue("pos", pos()); + settings.setValue("size", size()); + settings.setValue("mode_resize" ,param_flags); + settings.setValue("mode_gui", param_gui_mode); + + myView->writeSettings(settings); + + icvSaveTrackbars(&settings); + + if (global_control_panel) + { + icvSaveControlPanel(); + settings.setValue("posPanel", global_control_panel->pos()); + } +} + + + +//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_flags = settings.value("mode_resize", param_flags).toInt(); + param_gui_mode = settings.value("mode_gui", param_gui_mode).toInt(); + + param_flags = settings.value("mode_resize", param_flags).toInt(); + + myView->readSettings(settings); + + //trackbar here + icvLoadTrackbars(&settings); + + resize(size); + move(pos); + + if (global_control_panel) + { + icvLoadControlPanel(); + global_control_panel->move(settings.value("posPanel", global_control_panel->pos()).toPoint()); + } +} + + +double CvWindow::getRatio() +{ + return myView->getRatio(); +} + + +void CvWindow::setRatio(int flags) +{ + myView->setRatio(flags); +} + + +int CvWindow::getPropWindow() +{ + return param_flags; +} + + +void CvWindow::setPropWindow(int flags) +{ + if (param_flags == flags) //nothing to do + return; + + switch(flags) + { + case CV_WINDOW_NORMAL: + myGlobalLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); + param_flags = flags; + + break; + + case CV_WINDOW_AUTOSIZE: + myGlobalLayout->setSizeConstraint(QLayout::SetFixedSize); + param_flags = flags; + + break; + + default: + ; + } +} + + +void CvWindow::toggleFullScreen(int flags) +{ + if (isFullScreen() && flags == CV_WINDOW_NORMAL) + { + showTools(); + showNormal(); + return; + } + + if (!isFullScreen() && flags == CV_WINDOW_FULLSCREEN) + { + hideTools(); + showFullScreen(); + return; + } +} + + +void CvWindow::updateImage(void* arr) +{ + myView->updateImage(arr); +} + + +void CvWindow::displayInfo(QString text, int delayms) +{ + myView->startDisplayInfo(text, delayms); +} + + +void CvWindow::displayStatusBar(QString text, int delayms) +{ + if (myStatusBar) + myStatusBar->showMessage(text, delayms); +} + + +void CvWindow::enablePropertiesButton() +{ + vect_QActions[9]->setDisabled(false); +} + + +CvButtonbar* CvWindow::createButtonBar(QString name_bar) +{ + QPointer t = new CvButtonbar(global_control_panel, name_bar); t->setAlignment(Qt::AlignHCenter); QPointer myLayout = global_control_panel->myLayout; - myLayout->insertLayout(myLayout->count(),t); + myLayout->insertLayout(myLayout->count(), t); return t; } +void CvWindow::addSlider(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback on_change) +{ + QPointer t = new CvTrackbar(w, name, value, count, on_change); + t->setAlignment(Qt::AlignHCenter); + + QPointer myLayout; + + if (w) + { + myLayout = w->myBarLayout; + } + else + { + myLayout = global_control_panel->myLayout; + + //if first one, enable control panel + if (myLayout->count() == 0) + guiMainThread->enablePropertiesButtonEachWindow(); + } + + myLayout->insertLayout(myLayout->count(), t); +} + + +void CvWindow::addSlider2(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback2 on_change, void* userdata) +{ + QPointer t = new CvTrackbar(w, name, value, count, on_change, userdata); + t->setAlignment(Qt::AlignHCenter); + + QPointer myLayout; + + if (w) + { + myLayout = w->myBarLayout; + } + else + { + myLayout = global_control_panel->myLayout; + + //if first one, enable control panel + if (myLayout->count() == 0) + guiMainThread->enablePropertiesButtonEachWindow(); + } + + myLayout->insertLayout(myLayout->count(), t); +} + + +void CvWindow::setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) +{ + myView->setOpenGlDrawCallback(callback, userdata); +} + + +void CvWindow::setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata) +{ + myView->setOpenGlCleanCallback(callback, userdata); +} + + +void CvWindow::makeCurrentOpenGlContext() +{ + myView->makeCurrentOpenGlContext(); +} + + +void CvWindow::updateGl() +{ + myView->updateGl(); +} + + +bool CvWindow::isOpenGl() +{ + return mode_display == CV_MODE_OPENGL; +} + + +void CvWindow::setViewportSize(QSize size) +{ + myView->getWidget()->resize(size); + myView->setSize(size); +} + + +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::createGlobalLayout() +{ + myGlobalLayout = new QBoxLayout(QBoxLayout::TopToBottom); + myGlobalLayout->setObjectName(QString::fromUtf8("boxLayout")); + myGlobalLayout->setContentsMargins(0, 0, 0, 0); + myGlobalLayout->setSpacing(0); + myGlobalLayout->setMargin(0); + setMinimumSize(1, 1); + + if (param_flags == CV_WINDOW_AUTOSIZE) + myGlobalLayout->setSizeConstraint(QLayout::SetFixedSize); + else if (param_flags == CV_WINDOW_NORMAL) + myGlobalLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); +} + + +void CvWindow::createView() +{ +#ifdef HAVE_QT_OPENGL + if (isOpenGl()) + myView = new OpenGlViewPort(this); + else +#endif + myView = new DefaultViewPort(this, param_ratio_mode); +} + + +void CvWindow::createActions() +{ + vect_QActions.resize(10); + + QWidget* view = myView->getWidget(); + + //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()), view, SLOT(siftWindowOnLeft())); + + 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()), view, SLOT(siftWindowOnRight())); + + 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()), view, SLOT(siftWindowOnUp())); + + 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()), view, SLOT(siftWindowOnDown()) ); + + 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()), view, SLOT(resetZoom())); + + 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()), view, SLOT(imgRegion())); + + vect_QActions[6] = new QAction(QIcon(":/zoom_in-icon"), "Zoom in (CTRL++)", this); + vect_QActions[6]->setIconVisibleInMenu(true); + QObject::connect(vect_QActions[6], SIGNAL(triggered()), view, SLOT(ZoomIn())); + + vect_QActions[7] = new QAction(QIcon(":/zoom_out-icon"), "Zoom out (CTRL+-)", this); + vect_QActions[7]->setIconVisibleInMenu(true); + QObject::connect(vect_QActions[7], SIGNAL(triggered()), view, SLOT(ZoomOut())); + + vect_QActions[8] = new QAction(QIcon(":/save-icon"), "Save current image (CTRL+S)", this); + vect_QActions[8]->setIconVisibleInMenu(true); + QObject::connect(vect_QActions[8], SIGNAL(triggered()), view, SLOT(saveView())); + + vect_QActions[9] = new QAction(QIcon(":/properties-icon"), "Display properties window (CTRL+P)", this); + vect_QActions[9]->setIconVisibleInMenu(true); + QObject::connect(vect_QActions[9], SIGNAL(triggered()), this, SLOT(displayPropertiesWin())); + + if (global_control_panel->myLayout->count() == 0) + vect_QActions[9]->setDisabled(true); +} + + +void CvWindow::createShortcuts() +{ + vect_QShortcuts.resize(10); + + QWidget* view = myView->getWidget(); + + vect_QShortcuts[0] = new QShortcut(shortcut_panning_left, this); + QObject::connect(vect_QShortcuts[0], SIGNAL(activated()), view, SLOT(siftWindowOnLeft())); + + vect_QShortcuts[1] = new QShortcut(shortcut_panning_right, this); + QObject::connect(vect_QShortcuts[1], SIGNAL(activated()), view, SLOT(siftWindowOnRight())); + + vect_QShortcuts[2] = new QShortcut(shortcut_panning_up, this); + QObject::connect(vect_QShortcuts[2], SIGNAL(activated()), view, SLOT(siftWindowOnUp())); + + vect_QShortcuts[3] = new QShortcut(shortcut_panning_down, this); + QObject::connect(vect_QShortcuts[3], SIGNAL(activated()), view, SLOT(siftWindowOnDown())); + + vect_QShortcuts[4] = new QShortcut(shortcut_zoom_normal, this); + QObject::connect(vect_QShortcuts[4], SIGNAL(activated()), view, SLOT(resetZoom())); + + vect_QShortcuts[5] = new QShortcut(shortcut_zoom_imgRegion, this); + QObject::connect(vect_QShortcuts[5], SIGNAL(activated()), view, SLOT(imgRegion())); + + vect_QShortcuts[6] = new QShortcut(shortcut_zoom_in, this); + QObject::connect(vect_QShortcuts[6], SIGNAL(activated()), view, SLOT(ZoomIn())); + + vect_QShortcuts[7] = new QShortcut(shortcut_zoom_out, this); + QObject::connect(vect_QShortcuts[7], SIGNAL(activated()), view, SLOT(ZoomOut())); + + vect_QShortcuts[8] = new QShortcut(shortcut_save_img, this); + QObject::connect(vect_QShortcuts[8], SIGNAL(activated()), view, SLOT(saveView())); + + vect_QShortcuts[9] = new QShortcut(shortcut_properties_win, this); + QObject::connect(vect_QShortcuts[9], SIGNAL(activated()), this, SLOT(displayPropertiesWin())); +} + + +void CvWindow::createToolBar() +{ + myToolBar = new QToolBar(this); + myToolBar->setFloatable(false); //is not a window + myToolBar->setFixedHeight(28); + myToolBar->setMinimumWidth(1); + + foreach (QAction *a, vect_QActions) + myToolBar->addAction(a); +} + + +void CvWindow::createStatusBar() +{ + myStatusBar = new QStatusBar(this); + myStatusBar->setSizeGripEnabled(false); + myStatusBar->setFixedHeight(20); + myStatusBar->setMinimumWidth(1); + myStatusBar_msg = new QLabel; + + //I comment this because if we change the style, myview (the picture) + //will not be the correct size anymore (will lost 2 pixel because of the borders) + + //myStatusBar_msg->setFrameStyle(QFrame::Raised); + + myStatusBar_msg->setAlignment(Qt::AlignHCenter); + myStatusBar->addWidget(myStatusBar_msg); +} + + void CvWindow::hideTools() { if (myToolBar) @@ -1616,6 +2016,7 @@ void CvWindow::hideTools() global_control_panel->hide(); } + void CvWindow::showTools() { if (myToolBar) @@ -1623,17 +2024,19 @@ void CvWindow::showTools() if (myStatusBar) myStatusBar->show(); - } + CvWinProperties* CvWindow::createParameterWindow() { - QString name_paraWindow =QFileInfo(QApplication::applicationFilePath()).fileName()+" settings"; + QString name_paraWindow = QFileInfo(QApplication::applicationFilePath()).fileName() + " settings"; + + CvWinProperties* result = new CvWinProperties(name_paraWindow, guiMainThread); - CvWinProperties *result = new CvWinProperties(name_paraWindow,guiMainThread); return result; } + void CvWindow::displayPropertiesWin() { if (global_control_panel->isHidden()) @@ -1642,242 +2045,19 @@ void CvWindow::displayPropertiesWin() global_control_panel->hide(); } -void CvWindow::createActions() -{ - 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() )); - - 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[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[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[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[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[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[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[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[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() )); - - if (global_control_panel->myLayout->count() == 0) - vect_QActions[9]->setDisabled(true); -} - - -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() )); -} - -void CvWindow::createToolBar() -{ - myToolBar = new QToolBar(this); - myToolBar->setFloatable(false);//is not a window - myToolBar->setFixedHeight(28); - myToolBar->setMinimumWidth(1); - - foreach (QAction *a, vect_QActions) - myToolBar->addAction(a); -} - -void CvWindow::createStatusBar() -{ - myStatusBar = new QStatusBar(this); - myStatusBar->setSizeGripEnabled(false); - myStatusBar->setFixedHeight(20); - myStatusBar->setMinimumWidth(1); - myStatusBar_msg = new QLabel; - - - //I comment this because if we change the style, myview (the picture) - //will not be the correct size anymore (will lost 2 pixel because of the borders) - - //myStatusBar_msg->setFrameStyle(QFrame::Raised); - - - myStatusBar_msg->setAlignment(Qt::AlignHCenter); - myStatusBar->addWidget(myStatusBar_msg); -} - -void CvWindow::createGlobalLayout() -{ - myGlobalLayout = new QBoxLayout(QBoxLayout::TopToBottom); - myGlobalLayout->setObjectName(QString::fromUtf8("boxLayout")); - myGlobalLayout->setContentsMargins(0, 0, 0, 0); - myGlobalLayout->setSpacing(0); - myGlobalLayout->setMargin(0); - setMinimumSize(1,1); - - if (param_flags == CV_WINDOW_AUTOSIZE) - myGlobalLayout->setSizeConstraint(QLayout::SetFixedSize); - - if (param_flags == CV_WINDOW_NORMAL) - myGlobalLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); -} - -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::createView(int mode, int ratio) -{ - //mode = CV_MODE_NORMAL or CV_MODE_OPENGL - //ratio = CV_WINDOW_KEEPRATIO or CV_WINDOW_FREERATIO - myview = new ViewPort(this, mode,ratio);//parent, mode_display, keep_aspect_ratio - myview->setAlignment(Qt::AlignHCenter); -} - -void CvWindow::setOpenGLCallback(CvOpenGLCallback func,void* userdata, double angle, double zmin, double zmax) -{ - myview->setOpenGLCallback(func,userdata, angle, zmin, zmax ); -} - -ViewPort* CvWindow::getView() -{ - return myview; -} - -void CvWindow::displayInfo(QString text,int delayms) -{ - myview->startDisplayInfo(text, delayms); -} - -void CvWindow::displayStatusBar(QString text,int delayms) -{ - myStatusBar->showMessage(text,delayms); -} - -void CvWindow::updateImage(void* arr) -{ - myview->updateImage(arr); -} - -void CvWindow::setMouseCallBack(CvMouseCallback m, void* param) -{ - myview->setMouseCallBack(m,param); -} - -//addSlider2 is static -void CvWindow::addSlider2(CvWindow* w,QString name, int* value, int count,CvTrackbarCallback2 on_change, void* userdata) -{ - QPointer t = new CvTrackbar(w,name,value, count, on_change, userdata); - t->setAlignment(Qt::AlignHCenter); - - QPointer myLayout; - - if (w) - { - myLayout = w->myBarLayout; - } - else - { - myLayout = global_control_panel->myLayout; - - //if first one, enable control panel - if (myLayout->count() == 0) - guiMainThread->enablePropertiesButtonEachWindow(); - } - - myLayout->insertLayout( myLayout->count(),t); - - -} - -//addSlider is static -void CvWindow::addSlider(CvWindow* w,QString name, int* value, int count,CvTrackbarCallback on_change) -{ - QPointer t = new CvTrackbar(w,name,value, count, on_change); - t->setAlignment(Qt::AlignHCenter); - - QPointer myLayout; - - if (w) - { - myLayout = w->myBarLayout; - } - else - { - myLayout = global_control_panel->myLayout; - - //if first one, enable control panel - if (myLayout->count() == 0) - guiMainThread->enablePropertiesButtonEachWindow(); - } - - myLayout->insertLayout( myLayout->count(),t); - - -} //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; bool goodKey = true; Qt::Key qtkey = static_cast(key); - char asciiCode = QTest::keyToAscii(qtkey); - if(asciiCode != 0) + char asciiCode = QTest::keyToAscii(qtkey); + if (asciiCode != 0) { key = static_cast(asciiCode); } @@ -1896,87 +2076,18 @@ void CvWindow::keyPressEvent(QKeyEvent *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(); - - param_ratio_mode = settings.value("mode_ratio",param_ratio_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); - - if (global_control_panel) - { - icvLoadControlPanel(); - global_control_panel->move(settings.value("posPanel", global_control_panel->pos()).toPoint()); - } -} - -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("param_ratio_mode",param_ratio_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()); - - icvSaveTrackbars(&settings); - - - if (global_control_panel) - { - icvSaveControlPanel(); - settings.setValue("posPanel", global_control_panel->pos()); - } -} void CvWindow::icvLoadControlPanel() { - QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName()+" control panel"); - int size = settings.beginReadArray("bars"); - int subsize; - CvBar* t; + QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName() + " control panel"); + + int size = settings.beginReadArray("bars"); + if (size == global_control_panel->myLayout->layout()->count()) - for (int i = 0; i < size; ++i) { - t = (CvBar*) global_control_panel->myLayout->layout()->itemAt(i); + { + for (int i = 0; i < size; ++i) + { + CvBar* t = (CvBar*) global_control_panel->myLayout->layout()->itemAt(i); settings.setArrayIndex(i); if (t->type == type_CvTrackbar) { @@ -1987,7 +2098,7 @@ void CvWindow::icvLoadControlPanel() } if (t->type == type_CvButtonbar) { - subsize = settings.beginReadArray(QString("buttonbar")+i); + int subsize = settings.beginReadArray(QString("buttonbar")+i); if ( subsize == ((CvButtonbar*)t)->layout()->count() ) icvLoadButtonbar((CvButtonbar*)t,&settings); @@ -1995,17 +2106,21 @@ void CvWindow::icvLoadControlPanel() settings.endArray(); } } - settings.endArray(); + } + + settings.endArray(); } + void CvWindow::icvSaveControlPanel() { QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName()+" control panel"); + settings.beginWriteArray("bars"); - CvBar* t; - for (int i = 0; i < global_control_panel->myLayout->layout()->count(); ++i) { - t = (CvBar*) global_control_panel->myLayout->layout()->itemAt(i); + for (int i = 0; i < global_control_panel->myLayout->layout()->count(); ++i) + { + CvBar* t = (CvBar*) global_control_panel->myLayout->layout()->itemAt(i); settings.setArrayIndex(i); if (t->type == type_CvTrackbar) { @@ -2019,66 +2134,69 @@ void CvWindow::icvSaveControlPanel() settings.endArray(); } } + settings.endArray(); } -void CvWindow::icvSaveButtonbar(CvButtonbar* b,QSettings *settings) + +void CvWindow::icvSaveButtonbar(CvButtonbar* b, QSettings* settings) { - QWidget* temp; - QString myclass; - for (int i = 0; i < b->layout()->count(); ++i) { + for (int i = 0, count = b->layout()->count(); i < count; ++i) + { settings->setArrayIndex(i); - temp = (QWidget*) b->layout()->itemAt(i)->widget(); - myclass = QString(temp->metaObject ()->className ()); + + QWidget* temp = (QWidget*) b->layout()->itemAt(i)->widget(); + QString myclass(QLatin1String(temp->metaObject()->className())); + if (myclass == "CvPushButton") { CvPushButton* button = (CvPushButton*) temp; - settings->setValue("namebutton", QString(button->text()) ); + settings->setValue("namebutton", button->text()); settings->setValue("valuebutton", int(button->isChecked())); } - - if (myclass == "CvCheckBox") + else if (myclass == "CvCheckBox") { CvCheckBox* button = (CvCheckBox*) temp; - settings->setValue("namebutton", QString(button->text()) ); + settings->setValue("namebutton", button->text()); settings->setValue("valuebutton", int(button->isChecked())); } - - if (myclass == "CvRadioButton") + else if (myclass == "CvRadioButton") { CvRadioButton* button = (CvRadioButton*) temp; - settings->setValue("namebutton", QString(button->text()) ); + settings->setValue("namebutton", button->text()); settings->setValue("valuebutton", int(button->isChecked())); } } } -void CvWindow::icvLoadButtonbar(CvButtonbar* b,QSettings *settings) + +void CvWindow::icvLoadButtonbar(CvButtonbar* b, QSettings* settings) { - QWidget* temp; - QString myclass; - for (int i = 0; i < b->layout()->count(); ++i) + for (int i = 0, count = b->layout()->count(); i < count; ++i) { settings->setArrayIndex(i); - temp = (QWidget*) b->layout()->itemAt(i)->widget(); - myclass = QString(temp->metaObject ()->className ()); + + QWidget* temp = (QWidget*) b->layout()->itemAt(i)->widget(); + QString myclass(QLatin1String(temp->metaObject()->className())); + if (myclass == "CvPushButton") { CvPushButton* button = (CvPushButton*) temp; + if (button->text() == settings->value("namebutton").toString()) button->setChecked(settings->value("valuebutton").toInt()); } - - if (myclass == "CvCheckBox") + else if (myclass == "CvCheckBox") { CvCheckBox* button = (CvCheckBox*) temp; + if (button->text() == settings->value("namebutton").toString()) button->setChecked(settings->value("valuebutton").toInt()); } - - if (myclass == "CvRadioButton") + else if (myclass == "CvRadioButton") { CvRadioButton* button = (CvRadioButton*) temp; + if (button->text() == settings->value("namebutton").toString()) button->setChecked(settings->value("valuebutton").toInt()); } @@ -2087,168 +2205,305 @@ void CvWindow::icvLoadButtonbar(CvButtonbar* b,QSettings *settings) } -void CvWindow::icvLoadTrackbars(QSettings *settings) +void CvWindow::icvLoadTrackbars(QSettings* settings) { int size = settings->beginReadArray("trackbars"); - QPointer t; //trackbar are saved in the same order, so no need to use icvFindTrackbarByName - if (myBarLayout->layout()->count() == size)//if not the same number, the window saved and loaded is not the same (nb trackbar not equal) + if (myBarLayout->layout()->count() == size) //if not the same number, the window saved and loaded is not the same (nb trackbar not equal) + { for (int i = 0; i < size; ++i) { settings->setArrayIndex(i); - t = (CvTrackbar*) myBarLayout->layout()->itemAt(i); + + CvTrackbar* t = (CvTrackbar*) myBarLayout->layout()->itemAt(i); if (t->name_bar == settings->value("name").toString()) t->slider->setValue(settings->value("value").toInt()); } - settings->endArray(); + } -} - -void CvWindow::icvSaveTrackbars(QSettings *settings) -{ - QPointer t; - - settings->beginWriteArray("trackbars"); - - for (int i = 0; i < myBarLayout->layout()->count(); ++i) { - t = (CvTrackbar*) myBarLayout->layout()->itemAt(i); - settings->setArrayIndex(i); - settings->setValue("name", t->name_bar); - settings->setValue("value", t->slider->value()); - } settings->endArray(); } - - - - - -//Here is ViewPort class -ViewPort::ViewPort(CvWindow* arg, int arg2, int arg3) +void CvWindow::icvSaveTrackbars(QSettings* settings) { - centralWidget = arg, - setParent(centralWidget); - mode_display = arg2; - param_keepRatio = arg3; + settings->beginWriteArray("trackbars"); + + for (int i = 0; i < myBarLayout->layout()->count(); ++i) + { + settings->setArrayIndex(i); + + CvTrackbar* t = (CvTrackbar*) myBarLayout->layout()->itemAt(i); + + settings->setValue("name", t->name_bar); + settings->setValue("value", t->slider->value()); + } + + settings->endArray(); +} - //setAlignment(Qt::AlignLeft | Qt::AlignTop); - setContentsMargins(0,0,0,0); - setMinimumSize(1,1); +////////////////////////////////////////////////////// +// DefaultViewPort + + +DefaultViewPort::DefaultViewPort(CvWindow* arg, int arg2) : QGraphicsView(arg), image2Draw_mat(0) +{ + centralWidget = arg; + param_keepRatio = arg2; + + setContentsMargins(0, 0, 0, 0); + setMinimumSize(1, 1); + setAlignment(Qt::AlignHCenter); 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; + positionGrabbing = QPointF(0, 0); + positionCorners = QRect(0, 0, size().width(), size().height()); + + on_mouse = 0; + on_mouse_param = 0; + mouseCoordinate = QPoint(-1, -1); //no border setStyleSheet( "QGraphicsView { border-style: none; }" ); -#if defined( HAVE_QT_OPENGL ) - - if ( mode_display == CV_MODE_OPENGL) - { - myGL = new QGLWidget(QGLFormat(QGL::SampleBuffers)); - setViewport(myGL); - if (param_keepRatio == CV_WINDOW_KEEPRATIO) - { - //TODO: fix this bug: - //::::blinking in OpenGL with CV_WINDOW_KEEPRATIO:::: - //The raison is that to move the widget in the middle and resize it manually to keep the aspect ratio, - //we resize in resizeEvent() and use a trick not to be blocked in an infinity loop. - //This is working fine if the viewport is not OpenGL, however we have two rendering with OpenGL. - //The first rendering with the widget not moved in the midle (so stuck in top left), then the final and correct rendering. - //This two rendering are visible with OpenGL and CV_WINDOW_KEEPRATIO but not with native rendering (why ???) + image2Draw_mat = cvCreateMat(viewport()->height(), viewport()->width(), CV_8UC3); + cvZero(image2Draw_mat); - //I tried to use Qt::AlignCenter of the layout manager but the widget does not expand anymore - //I tried to center with painter.drawImage (in draw2D), but the imgRegion and all other function using the size of the widget will not work anymore. - startDisplayInfo("WARNING: For now, you cannot use OpenGL rendering with CV_WINDOW_KEEPRATIO, so we changed to CV_WINDOW_FREERATIO", 5000); - setRatio(CV_WINDOW_FREERATIO); - } - - //setViewport(new QGLWidget()); - angle = DEFAULT_ANGLE; - zmin = DEFAULT_ZMIN; - zmax = DEFAULT_ZMAX; - initGL(); - } - -#endif - - image2Draw_mat = cvCreateMat( viewport()->width(),viewport()->height(), CV_8UC3 ); - - nbChannelOriginImage = 0; - cvZero(image2Draw_mat); + nbChannelOriginImage = 0; setInteractive(false); - setMouseTracking (true);//receive mouse event everytime - + setMouseTracking(true); //receive mouse event everytime } -ViewPort::~ViewPort() + +DefaultViewPort::~DefaultViewPort() { - if (image2Draw_mat) - + if (image2Draw_mat) cvReleaseMat(&image2Draw_mat); - - -//#if defined( HAVE_QT_OPENGL ) -// if (myGL) -// delete myGL; -//#endif; - - delete timerDisplay; } -void ViewPort::contextMenuEvent(QContextMenuEvent *event) + +QWidget* DefaultViewPort::getWidget() { - 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()); - } + return this; } + +void DefaultViewPort::setMouseCallBack(CvMouseCallback m, void* param) +{ + on_mouse = m; + on_mouse_param = param; +} + +void DefaultViewPort::writeSettings(QSettings& settings) +{ + settings.setValue("matrix_view.m11", param_matrixWorld.m11()); + settings.setValue("matrix_view.m12", param_matrixWorld.m12()); + settings.setValue("matrix_view.m13", param_matrixWorld.m13()); + settings.setValue("matrix_view.m21", param_matrixWorld.m21()); + settings.setValue("matrix_view.m22", param_matrixWorld.m22()); + settings.setValue("matrix_view.m23", param_matrixWorld.m23()); + settings.setValue("matrix_view.m31", param_matrixWorld.m31()); + settings.setValue("matrix_view.m32", param_matrixWorld.m32()); + settings.setValue("matrix_view.m33", param_matrixWorld.m33()); +} + + +void DefaultViewPort::readSettings(QSettings& settings) +{ + qreal m11 = settings.value("matrix_view.m11", param_matrixWorld.m11()).toDouble(); + qreal m12 = settings.value("matrix_view.m12", param_matrixWorld.m12()).toDouble(); + qreal m13 = settings.value("matrix_view.m13", param_matrixWorld.m13()).toDouble(); + qreal m21 = settings.value("matrix_view.m21", param_matrixWorld.m21()).toDouble(); + qreal m22 = settings.value("matrix_view.m22", param_matrixWorld.m22()).toDouble(); + qreal m23 = settings.value("matrix_view.m23", param_matrixWorld.m23()).toDouble(); + qreal m31 = settings.value("matrix_view.m31", param_matrixWorld.m31()).toDouble(); + qreal m32 = settings.value("matrix_view.m32", param_matrixWorld.m32()).toDouble(); + qreal m33 = settings.value("matrix_view.m33", param_matrixWorld.m33()).toDouble(); + + param_matrixWorld = QTransform(m11, m12, m13, m21, m22, m23, m31, m32, m33); +} + + +double DefaultViewPort::getRatio() +{ + return param_keepRatio; +} + + +void DefaultViewPort::setRatio(int flags) +{ + if (getRatio() == flags) //nothing to do + return; + + //if valid flags + if (flags == CV_WINDOW_FREERATIO || flags == CV_WINDOW_KEEPRATIO) + { + centralWidget->param_ratio_mode = flags; + param_keepRatio = flags; + updateGeometry(); + viewport()->update(); + } +} + + +void DefaultViewPort::updateImage(const CvArr* arr) +{ + CV_Assert(arr); + + CvMat* mat, stub; + int origin = 0; + + if (CV_IS_IMAGE_HDR(arr)) + origin = ((IplImage*)arr)->origin; + + mat = cvGetMat(arr, &stub); + + if (!image2Draw_mat || !CV_ARE_SIZES_EQ(image2Draw_mat, mat)) + { + if (image2Draw_mat) + cvReleaseMat(&image2Draw_mat); + + //the image in ipl (to do a deep copy with cvCvtColor) + image2Draw_mat = cvCreateMat(mat->rows, mat->cols, CV_8UC3); + image2Draw_qt = QImage(image2Draw_mat->data.ptr, image2Draw_mat->cols, image2Draw_mat->rows, image2Draw_mat->step, QImage::Format_RGB888); + + //use to compute mouse coordinate, I need to update the ratio here and in resizeEvent + ratioX = width() / float(image2Draw_mat->cols); + ratioY = height() / float(image2Draw_mat->rows); + + updateGeometry(); + } + + nbChannelOriginImage = cvGetElemType(mat); + + cvConvertImage(mat, image2Draw_mat, (origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB); + + viewport()->update(); +} + + +void DefaultViewPort::startDisplayInfo(QString text, int delayms) +{ + if (timerDisplay->isActive()) + stopDisplayInfo(); + + infoText = text; + timerDisplay->start(delayms); + drawInfo = true; +} + + +void DefaultViewPort::setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) +{ + CV_Error(CV_OpenGlNotSupported, "Window doesn't support OpenGL"); +} + + +void DefaultViewPort::setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata) +{ + CV_Error(CV_OpenGlNotSupported, "Window doesn't support OpenGL"); +} + + +void DefaultViewPort::makeCurrentOpenGlContext() +{ + CV_Error(CV_OpenGlNotSupported, "Window doesn't support OpenGL"); +} + + +void DefaultViewPort::updateGl() +{ + CV_Error(CV_OpenGlNotSupported, "Window doesn't support OpenGL"); +} + + +//Note: move 2 percent of the window +void DefaultViewPort::siftWindowOnLeft() +{ + float delta = 2 * width() / (100.0 * param_matrixWorld.m11()); + moveView(QPointF(delta, 0)); +} + + +//Note: move 2 percent of the window +void DefaultViewPort::siftWindowOnRight() +{ + float delta = -2 * width() / (100.0 * param_matrixWorld.m11()); + moveView(QPointF(delta, 0)); +} + + +//Note: move 2 percent of the window +void DefaultViewPort::siftWindowOnUp() +{ + float delta = 2 * height() / (100.0 * param_matrixWorld.m11()); + moveView(QPointF(0, delta)); +} + + +//Note: move 2 percent of the window +void DefaultViewPort::siftWindowOnDown() +{ + float delta = -2 * height() / (100.0 * param_matrixWorld.m11()); + moveView(QPointF(0, delta)); +} + + +void DefaultViewPort::imgRegion() +{ + scaleView((threshold_zoom_img_region / param_matrixWorld.m11() - 1) * 5, QPointF(size().width() / 2, size().height() / 2)); +} + + +void DefaultViewPort::resetZoom() +{ + param_matrixWorld.reset(); + controlImagePosition(); +} + + +void DefaultViewPort::ZoomIn() +{ + scaleView(0.5, QPointF(size().width() / 2, size().height() / 2)); +} + + +void DefaultViewPort::ZoomOut() +{ + scaleView(-0.5, QPointF(size().width() / 2, size().height() / 2)); +} + + //can save as JPG, JPEG, BMP, PNG -void ViewPort::saveView() +void DefaultViewPort::saveView() { - QDate date_d = QDate::currentDate (); + QDate date_d = QDate::currentDate(); QString date_s = date_d.toString("dd.MM.yyyy"); - QString name_s = centralWidget->param_name+"_screenshot_"+date_s; + QString name_s = centralWidget->windowTitle() + "_screenshot_" + date_s; - QString fileName = QFileDialog::getSaveFileName(this, tr("Save File %1").arg(name_s), - name_s+".png", - tr("Images (*.png *.jpg *.bmp *.jpeg)")); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save File %1").arg(name_s), name_s + ".png", tr("Images (*.png *.jpg *.bmp *.jpeg)")); - if (!fileName.isEmpty ())//save the picture + if (!fileName.isEmpty()) //save the picture { QString extension = fileName.right(3); - -#if defined( HAVE_QT_OPENGL ) - image2Draw_qt_resized = ((QGLWidget*)viewport())->grabFrameBuffer(); -#else - // (no need anymore) create the image resized to receive the 'screenshot' - // image2Draw_qt_resized = QImage(viewport()->width(), viewport()->height(),QImage::Format_RGB888); - - QPainter saveimage(&image2Draw_qt_resized); - this->render(&saveimage); -#endif + // (no need anymore) create the image resized to receive the 'screenshot' + // image2Draw_qt_resized = QImage(viewport()->width(), viewport()->height(),QImage::Format_RGB888); + + QPainter saveimage(&image2Draw_qt_resized); + this->render(&saveimage); // Save it.. if (QString::compare(extension, "png", Qt::CaseInsensitive) == 0) @@ -2279,167 +2534,188 @@ void ViewPort::saveView() } } -void ViewPort::setRatio(int flags) + +void DefaultViewPort::contextMenuEvent(QContextMenuEvent* event) { - centralWidget->param_ratio_mode = flags; - param_keepRatio = flags; - updateGeometry(); + if (centralWidget->vect_QActions.size() > 0) + { + QMenu menu(this); + + foreach (QAction *a, centralWidget->vect_QActions) + menu.addAction(a); + + menu.exec(event->globalPos()); + } +} + + +void DefaultViewPort::resizeEvent(QResizeEvent* event) +{ + controlImagePosition(); + + //use to compute mouse coordinate, I need to update the ratio here and in resizeEvent + ratioX = width() / float(image2Draw_mat->cols); + ratioY = height() / float(image2Draw_mat->rows); + + if (param_keepRatio == CV_WINDOW_KEEPRATIO)//to keep the same aspect ratio + { + QSize newSize = QSize(image2Draw_mat->cols, image2Draw_mat->rows); + newSize.scale(event->size(), Qt::KeepAspectRatio); + + //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()); + + move(newSize.width(), newSize.height()); + } + } + + return QGraphicsView::resizeEvent(event); +} + + +void DefaultViewPort::wheelEvent(QWheelEvent* event) +{ + scaleView(event->delta() / 240.0, event->pos()); viewport()->update(); } -void ViewPort::imgRegion() + +void DefaultViewPort::mousePressEvent(QMouseEvent* event) { - scaleView( (threshold_zoom_img_region/param_matrixWorld.m11()-1)*5,QPointF(size().width()/2,size().height()/2)); + 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); + + if (param_matrixWorld.m11()>1) + { + setCursor(Qt::ClosedHandCursor); + positionGrabbing = event->pos(); + } + + QWidget::mousePressEvent(event); } -int ViewPort::getRatio() + +void DefaultViewPort::mouseReleaseEvent(QMouseEvent* event) { - return param_keepRatio; + 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); + + if (param_matrixWorld.m11()>1) + setCursor(Qt::OpenHandCursor); + + QWidget::mouseReleaseEvent(event); } -void ViewPort::resetZoom() + +void DefaultViewPort::mouseDoubleClickEvent(QMouseEvent* event) { - param_matrixWorld.reset(); - controlImagePosition(); + 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); + + QWidget::mouseDoubleClickEvent(event); } -void ViewPort::ZoomIn() + +void DefaultViewPort::mouseMoveEvent(QMouseEvent* event) { - scaleView( 0.5,QPointF(size().width()/2,size().height()/2)); + int cv_event = CV_EVENT_MOUSEMOVE, 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); + + 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(); + + QWidget::mouseMoveEvent(event); } -void ViewPort::ZoomOut() + +void DefaultViewPort::paintEvent(QPaintEvent* event) { - scaleView( -0.5,QPointF(size().width()/2,size().height()/2)); + QPainter myPainter(viewport()); + myPainter.setWorldTransform(param_matrixWorld); + + draw2D(&myPainter); + + //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) + { + if (centralWidget->param_flags == CV_WINDOW_NORMAL) + startDisplayInfo("WARNING: The values displayed are the resized image's values. If you want the original image's values, use CV_WINDOW_AUTOSIZE", 1000); + + drawImgRegion(&myPainter); + } + + drawViewOverview(&myPainter); + } + + //for information overlay + if (drawInfo) + drawInstructions(&myPainter); + + //for statusbar + if (centralWidget->myStatusBar) + drawStatusBar(); + + QGraphicsView::paintEvent(event); } -//Note: move 2 percent of the window -void ViewPort::siftWindowOnLeft() -{ - 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)); -} - -//Note: move 2 percent of the window -void ViewPort::siftWindowOnUp() -{ - 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)); -} - -void ViewPort::startDisplayInfo(QString text, int delayms) -{ - if (timerDisplay->isActive()) - stopDisplayInfo(); - - infoText = text; - timerDisplay->start(delayms); - drawInfo = true; -} - -void ViewPort::stopDisplayInfo() +void DefaultViewPort::stopDisplayInfo() { timerDisplay->stop(); drawInfo = false; } -inline bool ViewPort::isSameSize(IplImage* img1,IplImage* img2) + +inline bool DefaultViewPort::isSameSize(IplImage* img1, IplImage* img2) { return img1->width == img2->width && img1->height == img2->height; } -void ViewPort::updateImage(const CvArr *arr) -{ - //if (!arr) - //CV_Error(CV_StsNullPtr, "NULL arr pointer (in showImage)" ); - CV_Assert(arr); - CvMat * mat, stub; - int origin=0; - - if( CV_IS_IMAGE_HDR( arr )) - origin = ((IplImage*)arr)->origin; - - mat = cvGetMat(arr, &stub); - - //IplImage* tempImage = (IplImage*)arr; - - if( CV_IS_IMAGE_HDR( arr )) - origin = ((IplImage*)arr)->origin; - - if (!CV_ARE_SIZES_EQ(image2Draw_mat,mat)) - { - cvReleaseMat(&image2Draw_mat); - //the image in ipl (to do a deep copy with cvCvtColor) - image2Draw_mat = cvCreateMat( mat->rows, mat->cols, CV_8UC3 ); - image2Draw_qt = QImage(image2Draw_mat->data.ptr, image2Draw_mat->cols,image2Draw_mat->rows, image2Draw_mat->step,QImage::Format_RGB888); - - //use to compute mouse coordinate, I need to update the ratio here and in resizeEvent - ratioX=width()/float(image2Draw_mat->cols); - ratioY=height()/float(image2Draw_mat->rows); - - updateGeometry(); - } - - nbChannelOriginImage = cvGetElemType(mat); - - cvConvertImage(mat,image2Draw_mat,(origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB ); - - viewport()->update(); -} - -void ViewPort::setMouseCallBack(CvMouseCallback m, void* param) -{ - on_mouse = m; - on_mouse_param = param; -} - -#if defined( HAVE_QT_OPENGL ) -void ViewPort::setOpenGLCallback(CvOpenGLCallback func, void* userdata, double angle_arg, double zmin_arg, double zmax_arg) -{ - //avoid unreferenced formal parameter warning with vs 2008 - //http://msdn.microsoft.com/en-en/library/26kb9fy0%28VS.80%29.aspx - on_openGL_draw3D = func; - on_openGL_param = userdata; - - if (angle_arg > 0) - angle = angle_arg; - else - angle = DEFAULT_ANGLE; - - - if (zmin_arg >= 0) - zmin = zmin_arg; - else - zmin = DEFAULT_ZMIN; - - - if (zmax_arg > 0) - zmax = zmax_arg; - else - zmax = DEFAULT_ZMAX; -} -#else -void ViewPort::setOpenGLCallback(CvOpenGLCallback, void*, double, double, double) -{ -} -#endif - -void ViewPort::controlImagePosition() +void DefaultViewPort::controlImagePosition() { qreal left, top, right, bottom; @@ -2480,7 +2756,7 @@ void ViewPort::controlImagePosition() //viewport()->update(); } -void ViewPort::moveView(QPointF delta) +void DefaultViewPort::moveView(QPointF delta) { param_matrixWorld.translate(delta.x(),delta.y()); controlImagePosition(); @@ -2488,7 +2764,7 @@ void ViewPort::moveView(QPointF delta) } //factor is -0.5 (zoom out) or 0.5 (zoom in) -void ViewPort::scaleView(qreal factor,QPointF center) +void DefaultViewPort::scaleView(qreal factor,QPointF center) { factor/=5;//-0.1 <-> 0.1 factor+=1;//0.9 <-> 1.1 @@ -2524,107 +2800,9 @@ void ViewPort::scaleView(qreal factor,QPointF center) unsetCursor(); } -void ViewPort::wheelEvent(QWheelEvent *event) -{ - scaleView( event->delta() / 240.0,event->pos()); - viewport()->update(); -} - -void ViewPort::mousePressEvent(QMouseEvent *event) -{ - 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); - - if (param_matrixWorld.m11()>1) - { - setCursor(Qt::ClosedHandCursor); - positionGrabbing = event->pos(); - } - - QWidget::mousePressEvent(event); -} - -void ViewPort::mouseReleaseEvent(QMouseEvent *event) -{ - 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); - - if (param_matrixWorld.m11()>1) - setCursor(Qt::OpenHandCursor); - - QWidget::mouseReleaseEvent(event); -} - -void ViewPort::mouseDoubleClickEvent(QMouseEvent *event) -{ - 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); - - QWidget::mouseDoubleClickEvent(event); -} - -void ViewPort::mouseMoveEvent(QMouseEvent *event) -{ - int cv_event = CV_EVENT_MOUSEMOVE, 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); - - 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(); - - QWidget::mouseMoveEvent(event); -} - - -/*void ViewPort::dragMoveEvent(QDragMoveEvent *event) -{ - QPoint pt = event->pos(); - - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseProcessing(QPointF(pt), CV_EVENT_MOUSEMOVE, CV_EVENT_FLAG_LBUTTON); - - if (param_matrixWorld.m11()>1) - { - 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(); - - QWidget::dragMoveEvent(event); -}*/ - //up, down, dclick, move -void ViewPort::icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags) +void DefaultViewPort::icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags) { Qt::KeyboardModifiers modifiers = event->modifiers(); Qt::MouseButtons buttons = event->buttons(); @@ -2663,7 +2841,8 @@ void ViewPort::icvmouseHandler(QMouseEvent *event, type_mouse_event category, in } } -void ViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) + +void DefaultViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) { //to convert mouse coordinate qreal pfx, pfy; @@ -2677,105 +2856,17 @@ void ViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) mouseCoordinate.y(), flags, on_mouse_param ); } -QSize ViewPort::sizeHint() const + +QSize DefaultViewPort::sizeHint() const { if(image2Draw_mat) - return QSize(image2Draw_mat->cols,image2Draw_mat->rows); + return QSize(image2Draw_mat->cols, image2Draw_mat->rows); else return QGraphicsView::sizeHint(); } -void ViewPort::resizeEvent ( QResizeEvent *event) -{ - controlImagePosition(); - - //use to compute mouse coordinate, I need to update the ratio here and in resizeEvent - ratioX=width()/float(image2Draw_mat->cols); - ratioY=height()/float(image2Draw_mat->rows); - - - if(param_keepRatio == CV_WINDOW_KEEPRATIO)//to keep the same aspect ratio - { - QSize newSize = QSize(image2Draw_mat->cols,image2Draw_mat->rows); - newSize.scale(event->size(),Qt::KeepAspectRatio); - - //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()); - - move(newSize.width(),newSize.height()); - } - } - - return QGraphicsView::resizeEvent(event); -} - -void ViewPort::paintEvent(QPaintEvent* event) -{ - - QPainter myPainter(viewport()); - myPainter.setWorldTransform(param_matrixWorld); - - draw2D(&myPainter); - -#if defined( HAVE_QT_OPENGL ) - if ( mode_display == CV_MODE_OPENGL && on_openGL_draw3D) - { - myPainter.save(); // Needed when using the GL1 engine - myPainter.beginNativePainting(); // Needed when using the GL2 engine - - setGL(width(),height()); - on_openGL_draw3D(on_openGL_param); - unsetGL(); - - myPainter.endNativePainting(); // Needed when using the GL2 engine - myPainter.restore(); // Needed when using the GL1 engine - } -#endif - - //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) - { - if (centralWidget->param_flags == CV_WINDOW_NORMAL) - startDisplayInfo("WARNING: The values displayed are the resized image's values. If you want the original image's values, use CV_WINDOW_AUTOSIZE", 1000); - - drawImgRegion(&myPainter); - } - - drawViewOverview(&myPainter); - } - - //for information overlay - if (drawInfo) - drawInstructions(&myPainter); - - //for statusbar - if (centralWidget->myStatusBar) - drawStatusBar(); - - QGraphicsView::paintEvent(event); -} - -void ViewPort::draw2D(QPainter *painter) +void DefaultViewPort::draw2D(QPainter *painter) { image2Draw_qt = QImage(image2Draw_mat->data.ptr, image2Draw_mat->cols, image2Draw_mat->rows,image2Draw_mat->step,QImage::Format_RGB888); image2Draw_qt_resized = image2Draw_qt.scaled(viewport()->width(),viewport()->height(),Qt::IgnoreAspectRatio,Qt::FastTransformation);//Qt::SmoothTransformation); @@ -2783,7 +2874,7 @@ void ViewPort::draw2D(QPainter *painter) } //only if CV_8UC1 or CV_8UC3 -void ViewPort::drawStatusBar() +void DefaultViewPort::drawStatusBar() { if (nbChannelOriginImage!=CV_8UC1 && nbChannelOriginImage!=CV_8UC3) return; @@ -2820,7 +2911,7 @@ void ViewPort::drawStatusBar() } //accept only CV_8UC1 and CV_8UC8 image for now -void ViewPort::drawImgRegion(QPainter *painter) +void DefaultViewPort::drawImgRegion(QPainter *painter) { if (nbChannelOriginImage!=CV_8UC1 && nbChannelOriginImage!=CV_8UC3) @@ -2915,7 +3006,7 @@ void ViewPort::drawImgRegion(QPainter *painter) } -void ViewPort::drawViewOverview(QPainter *painter) +void DefaultViewPort::drawViewOverview(QPainter *painter) { QSize viewSize = size(); viewSize.scale ( 100, 100,Qt::KeepAspectRatio ); @@ -2938,7 +3029,7 @@ void ViewPort::drawViewOverview(QPainter *painter) ); } -void ViewPort::drawInstructions(QPainter *painter) +void DefaultViewPort::drawInstructions(QPainter *painter) { QFontMetrics metrics = QFontMetrics(font()); int border = qMax(4, metrics.leading()); @@ -2958,62 +3049,480 @@ void ViewPort::drawInstructions(QPainter *painter) } - - - - - - - - - - - - -#if defined( HAVE_QT_OPENGL )//all this section -> not tested - -void ViewPort::initGL() +void DefaultViewPort::setSize(QSize size_) { - 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; - - ymax = zNear * tan(fovy * M_PI / 360.0); - ymin = -ymax; - xmin = ymin * aspect; - xmax = ymax * aspect; - - - glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); } -void ViewPort::setGL(int width, int height) +////////////////////////////////////////////////////// +// OpenGlViewPort + +#ifdef HAVE_QT_OPENGL + +OpenGlViewPort::OpenGlViewPort(QWidget* parent) : QGLWidget(parent), size(-1, -1) { - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - icvgluPerspective(angle, float(width) / float(height), zmin, zmax); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); + mouseCallback = 0; + mouseData = 0; + + glDrawCallback = 0; + glDrawData = 0; + + glCleanCallback = 0; + glCleanData = 0; + + glFuncTab = 0; } -void ViewPort::unsetGL() +OpenGlViewPort::~OpenGlViewPort() { - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); + if (glFuncTab) + delete glFuncTab; + + setOpenGlCleanCallback(0, 0); } + +QWidget* OpenGlViewPort::getWidget() +{ + return this; +} + +void OpenGlViewPort::setMouseCallBack(CvMouseCallback callback, void* param) +{ + mouseCallback = callback; + mouseData = param; +} + +void OpenGlViewPort::writeSettings(QSettings& settings) +{ +} + +void OpenGlViewPort::readSettings(QSettings& settings) +{ +} + +double OpenGlViewPort::getRatio() +{ + return (double)width() / height(); +} + +void OpenGlViewPort::setRatio(int flags) +{ +} + +void OpenGlViewPort::updateImage(const CvArr* arr) +{ +} + +void OpenGlViewPort::startDisplayInfo(QString text, int delayms) +{ +} + +void OpenGlViewPort::setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) +{ + glDrawCallback = callback; + glDrawData = userdata; +} + +void OpenGlViewPort::setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata) +{ + makeCurrentOpenGlContext(); + + if (glCleanCallback) + glCleanCallback(glCleanData); + + glCleanCallback = callback; + glCleanData = userdata; +} + +void OpenGlViewPort::makeCurrentOpenGlContext() +{ + makeCurrent(); + icvSetOpenGlFuncTab(glFuncTab); +} + +void OpenGlViewPort::updateGl() +{ + QGLWidget::updateGL(); +} + +#ifndef APIENTRY + #define APIENTRY #endif +#ifndef APIENTRYP + #define APIENTRYP APIENTRY * #endif + +#ifndef GL_VERSION_1_5 + /* GL types for handling large vertex buffer objects */ + typedef ptrdiff_t GLintptr; + typedef ptrdiff_t GLsizeiptr; +#endif + +typedef void (APIENTRYP PFNGLGENBUFFERSPROC ) (GLsizei n, GLuint *buffers); +typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers); + +typedef void (APIENTRYP PFNGLBUFFERDATAPROC ) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage); +typedef void (APIENTRYP PFNGLBUFFERSUBDATAPROC) (GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data); + +typedef void (APIENTRYP PFNGLBINDBUFFERPROC ) (GLenum target, GLuint buffer); + +typedef GLvoid* (APIENTRYP PFNGLMAPBUFFERPROC) (GLenum target, GLenum access); +typedef GLboolean (APIENTRYP PFNGLUNMAPBUFFERPROC) (GLenum target); + +class GlFuncTab_QT : public CvOpenGlFuncTab +{ +public: +#ifdef Q_WS_WIN + GlFuncTab_QT(HDC hDC); +#else + GlFuncTab_QT(HDC hDC); +#endif + + void genBuffers(int n, unsigned int* buffers) const; + void deleteBuffers(int n, const unsigned int* buffers) const; + + void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const; + void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const; + + void bindBuffer(unsigned int target, unsigned int buffer) const; + + void* mapBuffer(unsigned int target, unsigned int access) const; + void unmapBuffer(unsigned int target) const; + + void generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const; + + bool isGlContextInitialized() const; + + PFNGLGENBUFFERSPROC glGenBuffersExt; + PFNGLDELETEBUFFERSPROC glDeleteBuffersExt; + + PFNGLBUFFERDATAPROC glBufferDataExt; + PFNGLBUFFERSUBDATAPROC glBufferSubDataExt; + + PFNGLBINDBUFFERPROC glBindBufferExt; + + PFNGLMAPBUFFERPROC glMapBufferExt; + PFNGLUNMAPBUFFERPROC glUnmapBufferExt; + + bool initialized; + +#ifdef Q_WS_WIN + HDC hDC; +#endif +}; + +#ifdef Q_WS_WIN + GlFuncTab_QT::GlFuncTab_QT(HDC hDC_) : hDC(hDC_) +#else + GlFuncTab_QT::GlFuncTab_QT() +#endif +{ + glGenBuffersExt = 0; + glDeleteBuffersExt = 0; + + glBufferDataExt = 0; + glBufferSubDataExt = 0; + + glBindBufferExt = 0; + + glMapBufferExt = 0; + glUnmapBufferExt = 0; + + initialized = false; +} + +void GlFuncTab_QT::genBuffers(int n, unsigned int* buffers) const +{ + CV_FUNCNAME( "GlFuncTab_QT::genBuffers" ); + + __BEGIN__; + + if (!glGenBuffersExt) + CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension"); + + glGenBuffersExt(n, buffers); + CV_CheckGlError(); + + __END__; +} + +void GlFuncTab_QT::deleteBuffers(int n, const unsigned int* buffers) const +{ + CV_FUNCNAME( "GlFuncTab_QT::deleteBuffers" ); + + __BEGIN__; + + if (!glDeleteBuffersExt) + CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension"); + + glDeleteBuffersExt(n, buffers); + CV_CheckGlError(); + + __END__; +} + +void GlFuncTab_QT::bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const +{ + CV_FUNCNAME( "GlFuncTab_QT::bufferData" ); + + __BEGIN__; + + if (!glBufferDataExt) + CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension"); + + glBufferDataExt(target, size, data, usage); + CV_CheckGlError(); + + __END__; +} + +void GlFuncTab_QT::bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const +{ + CV_FUNCNAME( "GlFuncTab_QT::bufferSubData" ); + + __BEGIN__; + + if (!glBufferSubDataExt) + CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension"); + + glBufferSubDataExt(target, offset, size, data); + CV_CheckGlError(); + + __END__; +} + +void GlFuncTab_QT::bindBuffer(unsigned int target, unsigned int buffer) const +{ + CV_FUNCNAME( "GlFuncTab_QT::bindBuffer" ); + + __BEGIN__; + + if (!glBindBufferExt) + CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension"); + + glBindBufferExt(target, buffer); + CV_CheckGlError(); + + __END__; +} + +void* GlFuncTab_QT::mapBuffer(unsigned int target, unsigned int access) const +{ + CV_FUNCNAME( "GlFuncTab_QT::mapBuffer" ); + + void* res = 0; + + __BEGIN__; + + if (!glMapBufferExt) + CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension"); + + res = glMapBufferExt(target, access); + CV_CheckGlError(); + + __END__; + + return res; +} + +void GlFuncTab_QT::unmapBuffer(unsigned int target) const +{ + CV_FUNCNAME( "GlFuncTab_QT::unmapBuffer" ); + + __BEGIN__; + + if (!glUnmapBufferExt) + CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension"); + + glUnmapBufferExt(target); + CV_CheckGlError(); + + __END__; +} + +void GlFuncTab_QT::generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const +{ + CV_FUNCNAME( "GlFuncTab_QT::generateBitmapFont" ); + + QFont font(QString(family.c_str()), height, weight, italic); + + __BEGIN__; + +#ifndef Q_WS_WIN + if (!glXUseXFont(font.handle(), start, count, base)) + CV_ERROR(CV_OpenGlApiCallError, "Can't create font"); +#else + SelectObject(hDC, font.handle()); + if (!wglUseFontBitmaps(hDC, start, count, base)) + CV_ERROR(CV_OpenGlApiCallError, "Can't create font"); +#endif + + __END__; +} + +bool GlFuncTab_QT::isGlContextInitialized() const +{ + return initialized; +} + +void OpenGlViewPort::initializeGL() +{ + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + +#ifdef Q_WS_WIN + std::auto_ptr glFuncTab(new GlFuncTab_QT(getDC())); +#else + std::auto_ptr glFuncTab(new GlFuncTab_QT); +#endif + + // Load extensions + + glFuncTab->glGenBuffersExt = (PFNGLGENBUFFERSPROC)context()->getProcAddress("glGenBuffers"); + glFuncTab->glDeleteBuffersExt = (PFNGLDELETEBUFFERSPROC)context()->getProcAddress("glDeleteBuffers"); + glFuncTab->glBufferDataExt = (PFNGLBUFFERDATAPROC)context()->getProcAddress("glBufferData"); + glFuncTab->glBufferSubDataExt = (PFNGLBUFFERSUBDATAPROC)context()->getProcAddress("glBufferSubData"); + glFuncTab->glBindBufferExt = (PFNGLBINDBUFFERPROC)context()->getProcAddress("glBindBuffer"); + glFuncTab->glMapBufferExt = (PFNGLMAPBUFFERPROC)context()->getProcAddress("glMapBuffer"); + glFuncTab->glUnmapBufferExt = (PFNGLUNMAPBUFFERPROC)context()->getProcAddress("glUnmapBuffer"); + + glFuncTab->initialized = true; + + this->glFuncTab = glFuncTab.release(); + + icvSetOpenGlFuncTab(this->glFuncTab); +} + +void OpenGlViewPort::resizeGL(int w, int h) +{ + glViewport(0, 0, w, h); +} + +void OpenGlViewPort::paintGL() +{ + icvSetOpenGlFuncTab(glFuncTab); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + if (glDrawCallback) + glDrawCallback(glDrawData); + + CV_CheckGlError(); +} + +void OpenGlViewPort::mousePressEvent(QMouseEvent* event) +{ + int cv_event = -1, flags = 0; + QPoint pt = event->pos(); + + icvmouseHandler(event, mouse_down, cv_event, flags); + icvmouseProcessing(QPointF(pt), cv_event, flags); + + QGLWidget::mousePressEvent(event); +} + + +void OpenGlViewPort::mouseReleaseEvent(QMouseEvent* event) +{ + int cv_event = -1, flags = 0; + QPoint pt = event->pos(); + + icvmouseHandler(event, mouse_up, cv_event, flags); + icvmouseProcessing(QPointF(pt), cv_event, flags); + + QGLWidget::mouseReleaseEvent(event); +} + + +void OpenGlViewPort::mouseDoubleClickEvent(QMouseEvent* event) +{ + int cv_event = -1, flags = 0; + QPoint pt = event->pos(); + + icvmouseHandler(event, mouse_dbclick, cv_event, flags); + icvmouseProcessing(QPointF(pt), cv_event, flags); + + QGLWidget::mouseDoubleClickEvent(event); +} + + +void OpenGlViewPort::mouseMoveEvent(QMouseEvent* event) +{ + int cv_event = CV_EVENT_MOUSEMOVE, 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); + + QGLWidget::mouseMoveEvent(event); +} + +void OpenGlViewPort::icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags) +{ + Qt::KeyboardModifiers modifiers = event->modifiers(); + Qt::MouseButtons buttons = event->buttons(); + + flags = 0; + if (modifiers & Qt::ShiftModifier) + flags |= CV_EVENT_FLAG_SHIFTKEY; + if (modifiers & Qt::ControlModifier) + flags |= CV_EVENT_FLAG_CTRLKEY; + if (modifiers & Qt::AltModifier) + flags |= CV_EVENT_FLAG_ALTKEY; + + if (buttons & Qt::LeftButton) + flags |= CV_EVENT_FLAG_LBUTTON; + if (buttons & Qt::RightButton) + flags |= CV_EVENT_FLAG_RBUTTON; + if (buttons & Qt::MidButton) + flags |= CV_EVENT_FLAG_MBUTTON; + + cv_event = CV_EVENT_MOUSEMOVE; + 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 OpenGlViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) +{ + if (mouseCallback) + mouseCallback(cv_event, pt.x(), pt.y(), flags, mouseData); +} + + +QSize OpenGlViewPort::sizeHint() const +{ + if (size.width() > 0 && size.height() > 0) + return size; + + return QGLWidget::sizeHint(); +} + +void OpenGlViewPort::setSize(QSize size_) +{ + size = size_; + updateGeometry(); +} + +#endif + +#endif // HAVE_QT diff --git a/modules/highgui/src/window_QT.h b/modules/highgui/src/window_QT.h index 282bb24d3d..1553b91f97 100644 --- a/modules/highgui/src/window_QT.h +++ b/modules/highgui/src/window_QT.h @@ -40,7 +40,6 @@ #ifndef __OPENCV_HIGHGUI_QT_H__ #define __OPENCV_HIGHGUI_QT_H__ - #include "precomp.hpp" #if defined( HAVE_QT_OPENGL ) @@ -82,7 +81,7 @@ #include //start private enum -enum {CV_MODE_NORMAL= 0, CV_MODE_OPENGL = 1}; +enum { CV_MODE_NORMAL = 0, CV_MODE_OPENGL = 1 }; //we can change the keyboard shortcuts from here ! enum { shortcut_zoom_normal = Qt::CTRL + Qt::Key_Z, @@ -97,14 +96,10 @@ enum { shortcut_zoom_normal = Qt::CTRL + Qt::Key_Z, shortcut_panning_down = Qt::CTRL + Qt::Key_Down }; - //end enum class CvWindow; class ViewPort; -#if defined( HAVE_QT_OPENGL ) -class OpenGLWidget; -#endif class GuiReceiver : public QObject @@ -114,15 +109,12 @@ class GuiReceiver : public QObject public: GuiReceiver(); ~GuiReceiver(); + int start(); void isLastWindow(); - bool _bTimeOut; - QTimer *timer; - -private: - int nb_windows; - bool doesExternalQAppExist; + bool bTimeOut; + QTimer* timer; public slots: void createWindow( QString name, int flags = 0 ); @@ -144,14 +136,24 @@ public slots: void setRatioWindow(QString name, double arg2 ); void saveWindowParameters(QString name); void loadWindowParameters(QString name); - void setOpenGLCallback(QString window_name, void* callbackOpenGL, void* userdata, double angle, double zmin, double zmax); void putText(void* arg1, QString text, QPoint org, void* font); void addButton(QString button_name, int button_type, int initial_button_state , void* on_change, void* userdata); void enablePropertiesButtonEachWindow(); + + void setOpenGlDrawCallback(QString name, void* callback, void* userdata); + void setOpenGlCleanCallback(QString name, void* callback, void* userdata); + void setOpenGlContext(QString name); + void updateWindow(QString name); + double isOpenGl(QString name); + +private: + int nb_windows; + bool doesExternalQAppExist; }; -enum typeBar{type_CvTrackbar = 0, type_CvButtonbar = 1}; -class CvBar : public QHBoxLayout + +enum typeBar { type_CvTrackbar = 0, type_CvButtonbar = 1 }; +class CvBar : public QHBoxLayout { public: typeBar type; @@ -165,8 +167,8 @@ class CvButtonbar : public CvBar Q_OBJECT public: CvButtonbar(QWidget* arg, QString bar_name); - ~CvButtonbar(); - void addButton( QString button_name, CvButtonCallback call, void* userdata, int button_type, int initial_button_state); + + void addButton(QString button_name, CvButtonCallback call, void* userdata, int button_type, int initial_button_state); private: void setLabel(); @@ -193,7 +195,6 @@ private slots: }; - class CvCheckBox : public QCheckBox { Q_OBJECT @@ -210,6 +211,7 @@ private slots: void callCallBack(bool); }; + class CvRadioButton : public QRadioButton { Q_OBJECT @@ -227,16 +229,13 @@ private slots: }; - class CvTrackbar : public CvBar { Q_OBJECT public: CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback on_change); CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback2 on_change, void* data); - ~CvTrackbar(); - //QString trackbar_name; QPointer slider; private slots: @@ -245,21 +244,19 @@ private slots: private: void setLabel(int myvalue); - void construc_trackbar(CvWindow* arg, QString name, int* value, int count); + void create(CvWindow* arg, QString name, int* value, int count); QString createLabel(); QPointer label; CvTrackbarCallback callback; CvTrackbarCallback2 callback2;//look like it is use by python binding int* dataSlider; void* userdata; - }; //Both are top level window, so that a way to differenciate them. //if (obj->metaObject ()->className () == "CvWindow") does not give me robust result -enum typeWindow{type_CvWindow = 1, type_CvWinProperties = 2}; - +enum typeWindow { type_CvWindow = 1, type_CvWinProperties = 2 }; class CvWinModel : public QWidget { public: @@ -271,7 +268,7 @@ class CvWinProperties : public CvWinModel { Q_OBJECT public: - CvWinProperties(QString name,QObject* parent); + CvWinProperties(QString name, QObject* parent); ~CvWinProperties(); QPointer myLayout; @@ -288,42 +285,62 @@ class CvWindow : public CvWinModel public: CvWindow(QString arg2, int flag = CV_WINDOW_NORMAL); ~CvWindow(); - static void addSlider(CvWindow* w,QString name, int* value, int count, CvTrackbarCallback on_change CV_DEFAULT(NULL)); - static void addSlider2(CvWindow* w,QString name, int* value, int count, CvTrackbarCallback2 on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(0)); + void setMouseCallBack(CvMouseCallback m, void* param); - void updateImage(void* arr); - void displayInfo(QString text, int delayms ); - void displayStatusBar(QString text, int delayms ); - void readSettings(); + void writeSettings(); - void setOpenGLCallback(CvOpenGLCallback arg1,void* userdata, double angle, double zmin, double zmax); - void hideTools(); - void showTools(); - static CvButtonbar* createButtonbar(QString bar_name); - QSize getAvailableSize(); + void readSettings(); + double getRatio(); + void setRatio(int flags); - ViewPort* getView(); + int getPropWindow(); + void setPropWindow(int flags); - QPointer myGlobalLayout;//All the widget (toolbar, view, LayoutBar, ...) are attached to it + void toggleFullScreen(int flags); + + void updateImage(void* arr); + + void displayInfo(QString text, int delayms); + void displayStatusBar(QString text, int delayms); + + void enablePropertiesButton(); + + static CvButtonbar* createButtonBar(QString bar_name); + + static void addSlider(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback on_change CV_DEFAULT(NULL)); + static void addSlider2(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback2 on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(0)); + + void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata); + void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata); + void makeCurrentOpenGlContext(); + void updateGl(); + bool isOpenGl(); + + void setViewportSize(QSize size); + + //parameters (will be save/load) + int param_flags; + int param_gui_mode; + int param_ratio_mode; + + QPointer myGlobalLayout; //All the widget (toolbar, view, LayoutBar, ...) are attached to it QPointer myBarLayout; + + QVector vect_QActions; + QPointer myStatusBar; QPointer myToolBar; QPointer myStatusBar_msg; - //parameters (will be save/load) - QString param_name; - int param_flags; - int param_gui_mode; - int param_ratio_mode; - QVector vect_QActions; - - protected: - virtual void keyPressEvent(QKeyEvent *event); + virtual void keyPressEvent(QKeyEvent* event); private: - QPointer myview; + + int mode_display; //opengl or native + ViewPort* myView; + QVector vect_QShortcuts; void icvLoadTrackbars(QSettings *settings); @@ -336,106 +353,211 @@ private: void createActions(); void createShortcuts(); void createToolBar(); - void createView(int display_mode, int ratio_mode); + void createView(); void createStatusBar(); void createGlobalLayout(); void createBarLayout(); CvWinProperties* createParameterWindow(); + void hideTools(); + void showTools(); + QSize getAvailableSize(); + private slots: void displayPropertiesWin(); }; - - -enum type_mouse_event {mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3}; - +enum type_mouse_event { mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3 }; static const int tableMouseButtons[][3]={ - {CV_EVENT_LBUTTONUP,CV_EVENT_RBUTTONUP,CV_EVENT_MBUTTONUP}, //mouse_up - {CV_EVENT_LBUTTONDOWN,CV_EVENT_RBUTTONDOWN,CV_EVENT_MBUTTONDOWN}, //mouse_down - {CV_EVENT_LBUTTONDBLCLK,CV_EVENT_RBUTTONDBLCLK,CV_EVENT_MBUTTONDBLCLK}, //mouse_dbclick - {CV_EVENT_MOUSEMOVE,CV_EVENT_MOUSEMOVE,CV_EVENT_MOUSEMOVE} //mouse_move + {CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP}, //mouse_up + {CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN}, //mouse_down + {CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK}, //mouse_dbclick + {CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE, CV_EVENT_MOUSEMOVE} //mouse_move }; -static const double DEFAULT_ANGLE = 45.0; -static const double DEFAULT_ZMIN = 0.01; -static const double DEFAULT_ZMAX = 1000.0; -class ViewPort : public QGraphicsView +class ViewPort +{ +public: + virtual ~ViewPort() {} + + virtual QWidget* getWidget() = 0; + + virtual void setMouseCallBack(CvMouseCallback callback, void* param) = 0; + + virtual void writeSettings(QSettings& settings) = 0; + virtual void readSettings(QSettings& settings) = 0; + + virtual double getRatio() = 0; + virtual void setRatio(int flags) = 0; + + virtual void updateImage(const CvArr* arr) = 0; + + virtual void startDisplayInfo(QString text, int delayms) = 0; + + virtual void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) = 0; + virtual void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata) = 0; + virtual void makeCurrentOpenGlContext() = 0; + virtual void updateGl() = 0; + + virtual void setSize(QSize size_) = 0; +}; + + + +#ifdef HAVE_QT_OPENGL + +class OpenGlViewPort : public QGLWidget, public ViewPort +{ +public: + explicit OpenGlViewPort(QWidget* parent); + ~OpenGlViewPort(); + + QWidget* getWidget(); + + void setMouseCallBack(CvMouseCallback callback, void* param); + + void writeSettings(QSettings& settings); + void readSettings(QSettings& settings); + + double getRatio(); + void setRatio(int flags); + + void updateImage(const CvArr* arr); + + void startDisplayInfo(QString text, int delayms); + + void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata); + void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata); + void makeCurrentOpenGlContext(); + void updateGl(); + + void setSize(QSize size_); + +protected: + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + + void mouseMoveEvent(QMouseEvent* event); + void mousePressEvent(QMouseEvent* event); + void mouseReleaseEvent(QMouseEvent* event); + void mouseDoubleClickEvent(QMouseEvent* event); + + QSize sizeHint() const; + +private: + QSize size; + + CvMouseCallback mouseCallback; + void* mouseData; + + CvOpenGlDrawCallback glDrawCallback; + void* glDrawData; + + CvOpenGlCleanCallback glCleanCallback; + void* glCleanData; + + CvOpenGlFuncTab* glFuncTab; + + void icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags); + void icvmouseProcessing(QPointF pt, int cv_event, int flags); +}; + +#endif // HAVE_QT_OPENGL + + +class DefaultViewPort : public QGraphicsView, public ViewPort { Q_OBJECT + public: - ViewPort(CvWindow* centralWidget, int mode = CV_MODE_NORMAL, int keepRatio = CV_WINDOW_KEEPRATIO); - ~ViewPort(); - void updateImage(const CvArr *arr); + DefaultViewPort(CvWindow* centralWidget, int arg2); + ~DefaultViewPort(); + + QWidget* getWidget(); + + void setMouseCallBack(CvMouseCallback callback, void* param); + + void writeSettings(QSettings& settings); + void readSettings(QSettings& settings); + + double getRatio(); + void setRatio(int flags); + + void updateImage(const CvArr* arr); + void startDisplayInfo(QString text, int delayms); - void setMouseCallBack(CvMouseCallback m, void* param); - void setOpenGLCallback(CvOpenGLCallback func,void* userdata, double arg3, double arg4, double arg5); - int getRatio(); - void setRatio(int arg); - //parameters (will be save/load) - QTransform param_matrixWorld; + void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata); + void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata); + void makeCurrentOpenGlContext(); + void updateGl(); - int param_keepRatio; - - //IplImage* image2Draw_ipl; - CvMat* image2Draw_mat; - QImage image2Draw_qt; - QImage image2Draw_qt_resized; - int mode_display;//opengl or native - int nbChannelOriginImage; + void setSize(QSize size_); public slots: //reference: //http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming //http://doc.qt.nokia.com/4.6/gestures-imagegestures-imagewidget-cpp.html - void scaleView(qreal scaleFactor, QPointF center); - void imgRegion(); - void moveView(QPointF delta); - void resetZoom(); - void ZoomIn(); - void ZoomOut(); + void siftWindowOnLeft(); void siftWindowOnRight(); void siftWindowOnUp() ; void siftWindowOnDown(); - void resizeEvent ( QResizeEvent * ); - void saveView(); - void contextMenuEvent(QContextMenuEvent *event); + void resetZoom(); + void imgRegion(); + void ZoomIn(); + void ZoomOut(); + + void saveView(); + +protected: + void contextMenuEvent(QContextMenuEvent* event); + void resizeEvent(QResizeEvent* event); + void paintEvent(QPaintEvent* paintEventInfo); + void wheelEvent(QWheelEvent* event); + void mouseMoveEvent(QMouseEvent* event); + void mousePressEvent(QMouseEvent* event); + void mouseReleaseEvent(QMouseEvent* event); + void mouseDoubleClickEvent(QMouseEvent* event); private: + int param_keepRatio; + + //parameters (will be save/load) + QTransform param_matrixWorld; + + CvMat* image2Draw_mat; + QImage image2Draw_qt; + QImage image2Draw_qt_resized; + int nbChannelOriginImage; + + //for mouse callback + CvMouseCallback on_mouse; + void* on_mouse_param; + + + void scaleView(qreal scaleFactor, QPointF center); + void moveView(QPointF delta); + QPoint mouseCoordinate; QPointF positionGrabbing; QRect positionCorners; QTransform matrixWorld_inv; float ratioX, ratioY; - //for mouse callback - CvMouseCallback on_mouse; - void* on_mouse_param; - - //for opengl callback - CvOpenGLCallback on_openGL_draw3D; - void* on_openGL_param; - bool isSameSize(IplImage* img1,IplImage* img2); + QSize sizeHint() const; QPointer centralWidget; QPointer timerDisplay; bool drawInfo; QString infoText; QRectF target; - //QImage* image; - void paintEvent(QPaintEvent* paintEventInfo); - void wheelEvent(QWheelEvent *event); - void mouseMoveEvent(QMouseEvent *event); - void mousePressEvent(QMouseEvent *event); - void mouseReleaseEvent(QMouseEvent *event); - void mouseDoubleClickEvent(QMouseEvent *event); void drawInstructions(QPainter *painter); void drawViewOverview(QPainter *painter); void drawImgRegion(QPainter *painter); @@ -445,17 +567,6 @@ private: void icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags); void icvmouseProcessing(QPointF pt, int cv_event, int flags); -#if defined( HAVE_QT_OPENGL ) - QPointer myGL; - double angle; - double zmin; - double zmax; - void unsetGL(); - void initGL(); - void setGL(int width, int height); - void icvgluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar); -#endif - private slots: void stopDisplayInfo(); }; diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index 4680bd6000..b5b6c0dc7c 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -427,7 +427,7 @@ typedef struct CvWindow #ifdef HAVE_OPENGL bool useGl; - CvOpenGLCallback glDrawCallback; + CvOpenGlDrawCallback glDrawCallback; void* glDrawData; CvOpenGlCleanCallback glCleanCallback; @@ -1216,7 +1216,7 @@ CV_IMPL void cvUpdateWindow(const char* name) __END__; } -CV_IMPL void cvCreateOpenGLCallback(const char* name, CvOpenGLCallback callback, void* userdata, double, double, double) +CV_IMPL void cvSetOpenGlDrawCallback(const char* name, CvOpenGlDrawCallback callback, void* userdata) { CvWindow* window; diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index 3bbed9e340..4b230b76ad 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -174,7 +174,7 @@ typedef struct CvWindow bool useGl; HGLRC hGLRC; - CvOpenGLCallback glDrawCallback; + CvOpenGlDrawCallback glDrawCallback; void* glDrawData; CvOpenGlCleanCallback glCleanCallback; @@ -1121,7 +1121,7 @@ CV_IMPL void cvUpdateWindow(const char* name) __END__; } -CV_IMPL void cvCreateOpenGLCallback(const char* name, CvOpenGLCallback callback, void* userdata, double, double, double) +CV_IMPL void cvSetOpenGlDrawCallback(const char* name, CvOpenGlDrawCallback callback, void* userdata) { CV_FUNCNAME( "cvCreateOpenGLCallback" );