2012-06-08 01:21:29 +08:00
//IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
2010-08-18 22:07:02 +08:00
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
// License Agreement
// For Open Source Computer Vision Library
//Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
//Copyright (C) 2008-2010, Willow Garage Inc., all rights reserved.
//Third party copyrights are property of their respective owners.
//Redistribution and use in source and binary forms, with or without modification,
//are permitted provided that the following conditions are met:
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//This software is provided by the copyright holders and contributors "as is" and
//any express or implied warranties, including, but not limited to, the implied
//warranties of merchantability and fitness for a particular purpose are disclaimed.
//In no event shall the Intel Corporation or contributors be liable for any direct,
//indirect, incidental, special, exemplary, or consequential damages
//(including, but not limited to, procurement of substitute goods or services;
//loss of use, data, or profits; or business interruption) however caused
//and on any theory of liability, whether in contract, strict liability,
//or tort (including negligence or otherwise) arising in any way out of
//the use of this software, even if advised of the possibility of such damage.
//--------------------Google Code 2010 -- Yannick Verdie--------------------//
# if defined(HAVE_QT)
2011-12-12 21:57:37 +08:00
# include <memory>
2010-08-18 22:07:02 +08:00
# include <window_QT.h>
2011-12-12 20:52:06 +08:00
2011-06-14 06:04:28 +08:00
# include <math.h>
2011-12-12 20:52:06 +08:00
2011-06-14 06:04:28 +08:00
# ifdef _WIN32
# include <windows.h>
2012-04-30 22:33:52 +08:00
# else
# include <unistd.h>
2011-06-14 06:04:28 +08:00
# endif
2011-12-12 21:57:37 +08:00
# ifdef HAVE_QT_OPENGL
# ifdef Q_WS_X11
# include <GL/glx.h>
# endif
# endif
2010-08-18 22:07:02 +08:00
//Static and global first
static GuiReceiver * guiMainThread = NULL ;
static int parameterSystemC = 1 ;
2010-12-04 23:15:06 +08:00
static char * parameterSystemV [ ] = { ( char * ) " " } ;
2010-08-18 22:07:02 +08:00
static bool multiThreads = false ;
static int last_key = - 1 ;
QWaitCondition key_pressed ;
QMutex mutexKey ;
2010-12-04 23:15:06 +08:00
static const unsigned int threshold_zoom_img_region = 30 ;
2010-08-18 22:07:02 +08:00
//the minimum zoom value to start displaying the values in the grid
//that is also the number of pixel per grid
static CvWinProperties * global_control_panel = NULL ;
//end static and global
CV_IMPL CvFont cvFontQt ( const char * nameFont , int pointSize , CvScalar color , int weight , int style , int spacing )
{
2012-06-08 01:21:29 +08:00
/*
//nameFont <- only Qt
//CvScalar color <- only Qt (blue_component, green_component, red\_component[, alpha_component])
int font_face ; //<- style in Qt
const int * ascii ;
const int * greek ;
const int * cyrillic ;
float hscale , vscale ;
float shear ;
int thickness ; //<- weight in Qt
float dx ; //spacing letter in Qt (0 default) in pixel
int line_type ; //<- pointSize in Qt
*/
CvFont f = { nameFont , color , style , NULL , NULL , NULL , 0 , 0 , 0 , weight , spacing , pointSize } ;
return f ;
2010-08-18 22:07:02 +08:00
}
CV_IMPL void cvAddText ( const CvArr * img , const char * text , CvPoint org , CvFont * font )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" putText " ,
Qt : : AutoConnection ,
Q_ARG ( void * , ( void * ) img ) ,
Q_ARG ( QString , QString ( text ) ) ,
Q_ARG ( QPoint , QPoint ( org . x , org . y ) ) ,
Q_ARG ( void * , ( void * ) font ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
double cvGetRatioWindow_QT ( const char * name )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
double result = - 1 ;
QMetaObject : : invokeMethod ( guiMainThread ,
" getRatioWindow " ,
//Qt::DirectConnection,
Qt : : AutoConnection ,
Q_RETURN_ARG ( double , result ) ,
Q_ARG ( QString , QString ( name ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
return result ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void cvSetRatioWindow_QT ( const char * name , double prop_value )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" setRatioWindow " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( double , prop_value ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
double cvGetPropWindow_QT ( const char * name )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
double result = - 1 ;
QMetaObject : : invokeMethod ( guiMainThread ,
" getPropWindow " ,
//Qt::DirectConnection,
Qt : : AutoConnection ,
Q_RETURN_ARG ( double , result ) ,
Q_ARG ( QString , QString ( name ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
return result ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void cvSetPropWindow_QT ( const char * name , double prop_value )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" setPropWindow " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( double , prop_value ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void cvSetModeWindow_QT ( const char * name , double prop_value )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" toggleFullScreen " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( double , prop_value ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
double cvGetModeWindow_QT ( const char * name )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
double result = - 1 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" isFullScreen " ,
Qt : : AutoConnection ,
Q_RETURN_ARG ( double , result ) ,
Q_ARG ( QString , QString ( name ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
return result ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CV_IMPL void cvDisplayOverlay ( const char * name , const char * text , int delayms )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" displayInfo " ,
Qt : : AutoConnection ,
//Qt::DirectConnection,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( QString , QString ( text ) ) ,
Q_ARG ( int , delayms ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CV_IMPL void cvSaveWindowParameters ( const char * name )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" saveWindowParameters " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CV_IMPL void cvLoadWindowParameters ( const char * name )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" loadWindowParameters " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CV_IMPL void cvDisplayStatusBar ( const char * name , const char * text , int delayms )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" displayStatusBar " ,
Qt : : AutoConnection ,
//Qt::DirectConnection,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( QString , QString ( text ) ) ,
Q_ARG ( int , delayms ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL int cvWaitKey ( int delay )
2010-08-18 22:07:02 +08:00
{
2012-03-29 06:22:02 +08:00
int result = - 1 ;
2010-08-18 22:07:02 +08:00
2012-03-29 06:22:02 +08:00
if ( ! guiMainThread )
return result ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
unsigned long delayms = delay < = 0 ? ULONG_MAX : delay ; //in milliseconds
2010-08-18 22:07:02 +08:00
2012-03-29 06:22:02 +08:00
if ( multiThreads )
{
mutexKey . lock ( ) ;
if ( key_pressed . wait ( & mutexKey , delayms ) ) //false if timeout
{
result = last_key ;
}
last_key = - 1 ;
mutexKey . unlock ( ) ;
}
2011-12-12 20:52:06 +08:00
else
{
2012-03-29 06:22:02 +08:00
//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
2010-08-18 22:07:02 +08:00
2012-03-29 06:22:02 +08:00
if ( delay > 0 )
guiMainThread - > timer - > start ( delay ) ;
2010-08-18 22:07:02 +08:00
2012-03-29 06:22:02 +08:00
//QMutex dummy;
2010-08-18 22:07:02 +08:00
2012-03-29 06:22:02 +08:00
while ( ! guiMainThread - > bTimeOut )
{
qApp - > processEvents ( QEventLoop : : AllEvents ) ;
if ( ! guiMainThread ) //when all the windows are deleted
return result ;
mutexKey . lock ( ) ;
if ( last_key ! = - 1 )
{
result = last_key ;
last_key = - 1 ;
guiMainThread - > timer - > stop ( ) ;
//printf("keypressed\n");
}
mutexKey . unlock ( ) ;
if ( result ! = - 1 )
{
break ;
}
else
{
/*
* //will not work, I broke the event loop !!!!
dummy . lock ( ) ;
QWaitCondition waitCondition ;
waitCondition . wait ( & dummy , 2 ) ;
*/
//to decrease CPU usage
//sleep 1 millisecond
2010-08-18 22:07:02 +08:00
# if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
2012-03-29 06:22:02 +08:00
Sleep ( 1 ) ;
2010-08-18 22:07:02 +08:00
# else
2012-03-29 06:22:02 +08:00
usleep ( 1000 ) ;
2010-08-18 22:07:02 +08:00
# endif
2012-03-29 06:22:02 +08:00
}
}
2010-08-18 22:07:02 +08:00
2012-03-29 06:22:02 +08:00
guiMainThread - > bTimeOut = false ;
}
2010-08-18 22:07:02 +08:00
2012-03-29 06:22:02 +08:00
return result ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
//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 [ ] )
{
2012-06-08 01:21:29 +08:00
multiThreads = true ;
QFuture < int > future = QtConcurrent : : run ( pt2Func , argc , argv ) ;
return guiMainThread - > start ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CV_IMPL void cvStopLoop ( )
{
2012-06-08 01:21:29 +08:00
qApp - > exit ( ) ;
2010-08-18 22:07:02 +08:00
}
2012-06-08 01:21:29 +08:00
static CvWindow * icvFindWindowByName ( QString name )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
CvWindow * window = 0 ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
//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 ( ) )
{
2012-06-08 01:21:29 +08:00
if ( widget - > isWindow ( ) & & ! widget - > parentWidget ( ) ) //is a window without parent
{
CvWinModel * temp = ( CvWinModel * ) widget ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( temp - > type = = type_CvWindow )
{
CvWindow * w = ( CvWindow * ) temp ;
2011-12-12 20:52:06 +08:00
if ( w - > windowTitle ( ) = = name )
2012-06-08 01:21:29 +08:00
{
window = w ;
break ;
}
}
}
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
return window ;
2010-08-18 22:07:02 +08:00
}
2012-06-08 01:21:29 +08:00
static CvBar * icvFindBarByName ( QBoxLayout * layout , QString name_bar , typeBar type )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
if ( ! layout )
2012-06-08 01:21:29 +08:00
return NULL ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
int stop_index = layout - > layout ( ) - > count ( ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
for ( int i = 0 ; i < stop_index ; + + i )
{
2012-06-08 01:21:29 +08:00
CvBar * t = ( CvBar * ) layout - > layout ( ) - > itemAt ( i ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( t - > type = = type & & t - > name_bar = = name_bar )
return t ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
return NULL ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
static CvTrackbar * icvFindTrackBarByName ( const char * name_trackbar , const char * name_window , QBoxLayout * layout = NULL )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
QString nameQt ( name_trackbar ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
if ( ! name_window & & global_control_panel ) //window name is null and we have a control panel
2012-06-08 01:21:29 +08:00
layout = global_control_panel - > myLayout ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
if ( ! layout )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( QLatin1String ( name_window ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
CV_Error ( CV_StsNullPtr , " NULL window handler " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w - > param_gui_mode = = CV_GUI_NORMAL )
return ( CvTrackbar * ) icvFindBarByName ( w - > myBarLayout , nameQt , type_CvTrackbar ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w - > param_gui_mode = = CV_GUI_EXPANDED )
{
CvBar * result = icvFindBarByName ( w - > myBarLayout , nameQt , type_CvTrackbar ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( result )
return ( CvTrackbar * ) result ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return ( CvTrackbar * ) icvFindBarByName ( global_control_panel - > myLayout , nameQt , type_CvTrackbar ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return NULL ;
2011-12-12 20:52:06 +08:00
}
else
{
2012-06-08 01:21:29 +08:00
//layout was specified
return ( CvTrackbar * ) icvFindBarByName ( layout , nameQt , type_CvTrackbar ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
}
2012-06-08 01:21:29 +08:00
/*
static CvButtonbar * icvFindButtonBarByName ( const char * button_name , QBoxLayout * layout )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
QString nameQt ( button_name ) ;
return ( CvButtonbar * ) icvFindBarByName ( layout , nameQt , type_CvButtonbar ) ;
2010-08-18 22:07:02 +08:00
}
2012-06-08 01:21:29 +08:00
*/
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
static int icvInitSystem ( int * c , char * * v )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
//"For any GUI application using Qt, there is precisely one QApplication object"
if ( ! QApplication : : instance ( ) )
{
2012-06-08 01:21:29 +08:00
new QApplication ( * c , v ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
qDebug ( ) < < " init done " ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
# ifdef HAVE_QT_OPENGL
2012-06-08 01:21:29 +08:00
qDebug ( ) < < " opengl support available " ;
2010-08-18 22:07:02 +08:00
# endif
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
return 0 ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL int cvInitSystem ( int , char * * )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
icvInitSystem ( & parameterSystemC , parameterSystemV ) ;
return 0 ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL int cvNamedWindow ( const char * name , int flags )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
guiMainThread = new GuiReceiver ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( multiThreads )
QMetaObject : : invokeMethod ( guiMainThread ,
" createWindow " ,
Qt : : BlockingQueuedConnection ,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( int , flags ) ) ;
else
guiMainThread - > createWindow ( QString ( name ) , flags ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return 1 ; //Dummy value
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL void cvDestroyWindow ( const char * name )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" destroyWindow " ,
//Qt::BlockingQueuedConnection,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL void cvDestroyAllWindows ( )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" destroyAllWindow " ,
//Qt::BlockingQueuedConnection,
Qt : : AutoConnection ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL void * cvGetWindowHandle ( const char * name )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! name )
CV_Error ( CV_StsNullPtr , " NULL name string " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return ( void * ) icvFindWindowByName ( QLatin1String ( name ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL const char * cvGetWindowName ( void * window_handle )
{
2012-06-08 01:21:29 +08:00
if ( ! window_handle )
CV_Error ( CV_StsNullPtr , " NULL window handler " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return ( ( CvWindow * ) window_handle ) - > windowTitle ( ) . toLatin1 ( ) . data ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL void cvMoveWindow ( const char * name , int x , int y )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" moveWindow " ,
//Qt::BlockingQueuedConnection,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( int , x ) ,
Q_ARG ( int , y ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL void cvResizeWindow ( const char * name , int width , int height )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" resizeWindow " ,
//Qt::BlockingQueuedConnection,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( int , width ) ,
Q_ARG ( int , height ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL int cvCreateTrackbar2 ( const char * name_bar , const char * window_name , int * val , int count , CvTrackbarCallback2 on_notify , void * userdata )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 19:02:11 +08:00
2012-06-08 01:21:29 +08:00
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 ) ) ;
2010-08-18 19:02:11 +08:00
2012-06-08 01:21:29 +08:00
return 1 ; //dummy value
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CV_IMPL int cvStartWindowThread ( )
{
2012-06-08 01:21:29 +08:00
return 0 ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL int cvCreateTrackbar ( const char * name_bar , const char * window_name , int * value , int count , CvTrackbarCallback on_change )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" addSlider " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( name_bar ) ) ,
Q_ARG ( QString , QString ( window_name ) ) ,
Q_ARG ( void * , ( void * ) value ) ,
Q_ARG ( int , count ) ,
Q_ARG ( void * , ( void * ) on_change ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return 1 ; //dummy value
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL int cvCreateButton ( const char * button_name , CvButtonCallback on_change , void * userdata , int button_type , int initial_button_state )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( initial_button_state < 0 | | initial_button_state > 1 )
return 0 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" addButton " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( button_name ) ) ,
Q_ARG ( int , button_type ) ,
Q_ARG ( int , initial_button_state ) ,
Q_ARG ( void * , ( void * ) on_change ) ,
Q_ARG ( void * , userdata ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return 1 ; //dummy value
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL int cvGetTrackbarPos ( const char * name_bar , const char * window_name )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
int result = - 1 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointer < CvTrackbar > t = icvFindTrackBarByName ( name_bar , window_name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( t )
result = t - > slider - > value ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return result ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL void cvSetTrackbarPos ( const char * name_bar , const char * window_name , int pos )
{
2012-06-08 01:21:29 +08:00
QPointer < CvTrackbar > t = icvFindTrackBarByName ( name_bar , window_name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( t )
t - > slider - > setValue ( pos ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
/* assign callback for mouse events */
2011-12-12 20:52:06 +08:00
CV_IMPL void cvSetMouseCallback ( const char * window_name , CvMouseCallback on_mouse , void * param )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( QLatin1String ( window_name ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
CV_Error ( CV_StsNullPtr , " NULL window handler " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
w - > setMouseCallBack ( on_mouse , param ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CV_IMPL void cvShowImage ( const char * name , const CvArr * arr )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
guiMainThread = new GuiReceiver ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" showImage " ,
//Qt::BlockingQueuedConnection,
Qt : : DirectConnection ,
Q_ARG ( QString , QString ( name ) ) ,
Q_ARG ( void * , ( void * ) arr ) ) ;
2011-12-12 20:52:06 +08:00
}
# ifdef HAVE_QT_OPENGL
CV_IMPL void cvSetOpenGlDrawCallback ( const char * window_name , CvOpenGlDrawCallback callback , void * userdata )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" setOpenGlDrawCallback " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( window_name ) ) ,
Q_ARG ( void * , ( void * ) callback ) ,
Q_ARG ( void * , userdata ) ) ;
2011-12-12 20:52:06 +08:00
}
void icvSetOpenGlCleanCallback ( const char * window_name , CvOpenGlCleanCallback callback , void * userdata )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" setOpenGlCleanCallback " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( window_name ) ) ,
Q_ARG ( void * , ( void * ) callback ) ,
Q_ARG ( void * , userdata ) ) ;
2011-12-12 20:52:06 +08:00
}
CV_IMPL void cvSetOpenGlContext ( const char * window_name )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" setOpenGlContext " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( window_name ) ) ) ;
2011-12-12 20:52:06 +08:00
}
CV_IMPL void cvUpdateWindow ( const char * window_name )
{
2012-06-08 01:21:29 +08:00
if ( ! guiMainThread )
CV_Error ( CV_StsNullPtr , " NULL guiReceiver (please create a window) " ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" updateWindow " ,
Qt : : AutoConnection ,
Q_ARG ( QString , QString ( window_name ) ) ) ;
2011-12-12 20:52:06 +08:00
}
# endif
double cvGetOpenGlProp_QT ( const char * name )
{
2012-06-08 01:21:29 +08:00
double result = - 1 ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( guiMainThread )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
QMetaObject : : invokeMethod ( guiMainThread ,
" isOpenGl " ,
Qt : : AutoConnection ,
Q_RETURN_ARG ( double , result ) ,
Q_ARG ( QString , QString ( name ) ) ) ;
2011-12-12 20:52:06 +08:00
}
2012-06-08 01:21:29 +08:00
return result ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
//////////////////////////////////////////////////////
// GuiReceiver
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
GuiReceiver : : GuiReceiver ( ) : bTimeOut ( false ) , nb_windows ( 0 )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
doesExternalQAppExist = ( QApplication : : instance ( ) ! = 0 ) ;
icvInitSystem ( & parameterSystemC , parameterSystemV ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
timer = new QTimer ( this ) ;
QObject : : connect ( timer , SIGNAL ( timeout ( ) ) , this , SLOT ( timeOut ( ) ) ) ;
timer - > setSingleShot ( true ) ;
2010-08-18 22:07:02 +08:00
}
void GuiReceiver : : isLastWindow ( )
{
2012-06-08 01:21:29 +08:00
if ( - - nb_windows < = 0 )
{
delete guiMainThread ; //delete global_control_panel too
guiMainThread = NULL ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( ! doesExternalQAppExist )
{
qApp - > quit ( ) ;
}
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
GuiReceiver : : ~ GuiReceiver ( )
2012-06-08 01:21:29 +08:00
{
if ( global_control_panel )
{
delete global_control_panel ;
global_control_panel = NULL ;
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-09-17 01:52:10 +08:00
void GuiReceiver : : putText ( void * arr , QString text , QPoint org , void * arg2 )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
CV_Assert ( arr ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
CvMat * mat , stub ;
mat = cvGetMat ( arr , & stub ) ;
2010-09-17 01:52:10 +08:00
2012-06-08 01:21:29 +08:00
int nbChannelOriginImage = cvGetElemType ( mat ) ;
if ( nbChannelOriginImage ! = CV_8UC3 ) return ; //for now, font works only with 8UC3
2010-09-17 01:52:10 +08:00
2012-06-08 01:21:29 +08:00
QImage qimg ( mat - > data . ptr , mat - > cols , mat - > rows , mat - > step , QImage : : Format_RGB888 ) ;
2010-09-17 01:52:10 +08:00
2012-06-08 01:21:29 +08:00
CvFont * font = ( CvFont * ) arg2 ;
2010-09-17 01:52:10 +08:00
2012-06-08 01:21:29 +08:00
QPainter qp ( & qimg ) ;
if ( font )
{
QFont f ( font - > nameFont , font - > line_type /*PointSize*/ , font - > thickness /*weight*/ ) ;
f . setStyle ( ( QFont : : Style ) font - > font_face /*style*/ ) ;
f . setLetterSpacing ( QFont : : AbsoluteSpacing , font - > dx /*spacing*/ ) ;
//cvScalar(blue_component, green_component, red_component[, alpha_component])
//Qt map non-transparent to 0xFF and transparent to 0
//OpenCV scalar is the reverse, so 255-font->color.val[3]
qp . setPen ( QColor ( font - > color . val [ 2 ] , font - > color . val [ 1 ] , font - > color . val [ 0 ] , 255 - font - > color . val [ 3 ] ) ) ;
qp . setFont ( f ) ;
}
qp . drawText ( org , text ) ;
qp . end ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : saveWindowParameters ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > writeSettings ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : loadWindowParameters ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > readSettings ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
double GuiReceiver : : getRatioWindow ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
return - 1 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return w - > getRatio ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : setRatioWindow ( QString name , double arg2 )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name . toLatin1 ( ) . data ( ) ) ;
if ( ! w )
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
int flags = ( int ) arg2 ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
w - > setRatio ( flags ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
double GuiReceiver : : getPropWindow ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
return - 1 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
return ( double ) w - > getPropWindow ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : setPropWindow ( QString name , double arg2 )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
int flags = ( int ) arg2 ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
w - > setPropWindow ( flags ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
double GuiReceiver : : isFullScreen ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
return - 1 ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
return w - > isFullScreen ( ) ? CV_WINDOW_FULLSCREEN : CV_WINDOW_NORMAL ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : toggleFullScreen ( QString name , double arg2 )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
int flags = ( int ) arg2 ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
w - > toggleFullScreen ( flags ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : createWindow ( QString name , int flags )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( ! qApp )
CV_Error ( CV_StsNullPtr , " NULL session handler " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
// Check the name in the storage
if ( icvFindWindowByName ( name . toLatin1 ( ) . data ( ) ) )
{
return ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
nb_windows + + ;
new CvWindow ( name , flags ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : timeOut ( )
{
2012-06-08 01:21:29 +08:00
bTimeOut = true ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : displayInfo ( QString name , QString text , int delayms )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > displayInfo ( text , delayms ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : displayStatusBar ( QString name , QString text , int delayms )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > displayStatusBar ( text , delayms ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : showImage ( QString name , void * arr )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
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 ) ;
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
if ( ! w | | ! arr )
return ; // keep silence here.
if ( w - > isOpenGl ( ) )
{
CvMat * mat , stub ;
2012-06-08 01:21:29 +08:00
mat = cvGetMat ( arr , & stub ) ;
2011-12-12 20:52:06 +08:00
cv : : Mat im ( mat ) ;
cv : : imshow ( name . toStdString ( ) , im ) ;
}
else
{
2012-06-08 01:21:29 +08:00
w - > updateImage ( arr ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-19 02:56:22 +08:00
2012-06-08 01:21:29 +08:00
if ( w - > isHidden ( ) )
w - > show ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : destroyWindow ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
{
w - > close ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//in not-multiThreads mode, looks like the window is hidden but not deleted
//so I do it manually
//otherwise QApplication do it for me if the exec command was executed (in multiThread mode)
if ( ! multiThreads )
delete w ;
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : destroyAllWindow ( )
{
2012-06-08 01:21:29 +08:00
if ( ! qApp )
CV_Error ( CV_StsNullPtr , " NULL session handler " ) ;
if ( multiThreads )
{
// WARNING: this could even close windows from an external parent app
//#TODO check externalQAppExists and in case it does, close windows carefully,
// i.e. apply the className-check from below...
qApp - > closeAllWindows ( ) ;
}
else
{
bool isWidgetDeleted = true ;
while ( isWidgetDeleted )
{
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 " ) )
{
delete obj ;
isWidgetDeleted = true ;
break ;
}
}
}
}
2010-08-18 22:07:02 +08:00
}
void GuiReceiver : : moveWindow ( QString name , int x , int y )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > move ( x , y ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : resizeWindow ( QString name , int width , int height )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
{
w - > showNormal ( ) ;
2011-12-12 20:52:06 +08:00
w - > setViewportSize ( QSize ( width , height ) ) ;
2012-06-08 01:21:29 +08:00
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : enablePropertiesButtonEachWindow ( )
{
2012-06-08 01:21:29 +08:00
//For each window, enable window property button
foreach ( QWidget * widget , QApplication : : topLevelWidgets ( ) )
{
if ( widget - > isWindow ( ) & & ! widget - > parentWidget ( ) ) //is a window without parent
{
CvWinModel * temp = ( CvWinModel * ) widget ;
if ( temp - > type = = type_CvWindow )
{
CvWindow * w = ( CvWindow * ) widget ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//active window properties button
w - > enablePropertiesButton ( ) ;
}
}
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : addButton ( QString button_name , int button_type , int initial_button_state , void * on_change , void * userdata )
{
2012-06-08 01:21:29 +08:00
if ( ! global_control_panel )
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointer < CvButtonbar > b ;
2010-08-19 18:36:57 +08:00
2012-06-08 01:21:29 +08:00
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
enablePropertiesButtonEachWindow ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
}
2011-12-12 20:52:06 +08:00
else
{
2012-06-08 01:21:29 +08:00
CvBar * lastbar = ( CvBar * ) global_control_panel - > myLayout - > itemAt ( global_control_panel - > myLayout - > count ( ) - 1 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
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 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
b - > addButton ( button_name , ( CvButtonCallback ) on_change , userdata , button_type , initial_button_state ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : addSlider2 ( QString bar_name , QString window_name , void * value , int count , void * on_change , void * userdata )
{
2012-06-08 01:21:29 +08:00
QBoxLayout * layout = NULL ;
QPointer < CvWindow > w ;
2011-12-12 20:52:06 +08:00
if ( ! window_name . isEmpty ( ) )
2012-06-08 01:21:29 +08:00
{
w = icvFindWindowByName ( window_name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
return ;
}
2011-12-12 20:52:06 +08:00
else
{
2012-06-08 01:21:29 +08:00
if ( global_control_panel )
layout = global_control_panel - > myLayout ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointer < CvTrackbar > t = icvFindTrackBarByName ( bar_name . toLatin1 ( ) . data ( ) , window_name . toLatin1 ( ) . data ( ) , layout ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( t ) //trackbar exists
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! value )
CV_Error ( CV_StsNullPtr , " NULL value pointer " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
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 " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
CvWindow : : addSlider2 ( w , bar_name , ( int * ) value , count , ( CvTrackbarCallback2 ) on_change , userdata ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void GuiReceiver : : addSlider ( QString bar_name , QString window_name , void * value , int count , void * on_change )
{
2012-06-08 01:21:29 +08:00
QBoxLayout * layout = NULL ;
QPointer < CvWindow > w ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( ! window_name . isEmpty ( ) )
{
w = icvFindWindowByName ( window_name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! w )
return ;
}
2011-12-12 20:52:06 +08:00
else
{
2012-06-08 01:21:29 +08:00
if ( global_control_panel )
layout = global_control_panel - > myLayout ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointer < CvTrackbar > t = icvFindTrackBarByName ( bar_name . toLatin1 ( ) . data ( ) , window_name . toLatin1 ( ) . data ( ) , layout ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( t ) //trackbar exists
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ! value )
CV_Error ( CV_StsNullPtr , " NULL value pointer " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
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 " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
CvWindow : : addSlider ( w , bar_name , ( int * ) value , count , ( CvTrackbarCallback ) on_change ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
int GuiReceiver : : start ( )
{
2012-06-08 01:21:29 +08:00
return qApp - > exec ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void GuiReceiver : : setOpenGlDrawCallback ( QString name , void * callback , void * userdata )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > setOpenGlDrawCallback ( ( CvOpenGlDrawCallback ) callback , userdata ) ;
2011-12-12 20:52:06 +08:00
}
void GuiReceiver : : setOpenGlCleanCallback ( QString name , void * callback , void * userdata )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > setOpenGlCleanCallback ( ( CvOpenGlCleanCallback ) callback , userdata ) ;
2011-12-12 20:52:06 +08:00
}
void GuiReceiver : : setOpenGlContext ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > makeCurrentOpenGlContext ( ) ;
2011-12-12 20:52:06 +08:00
}
void GuiReceiver : : updateWindow ( QString name )
{
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
w - > updateGl ( ) ;
2011-12-12 20:52:06 +08:00
}
double GuiReceiver : : isOpenGl ( QString name )
{
double result = - 1 ;
2012-06-08 01:21:29 +08:00
QPointer < CvWindow > w = icvFindWindowByName ( name ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
result = ( double ) w - > isOpenGl ( ) ;
2011-12-12 20:52:06 +08:00
return result ;
}
//////////////////////////////////////////////////////
// CvTrackbar
2012-06-12 22:46:12 +08:00
CvTrackbar : : CvTrackbar ( CvWindow * arg , QString name , int * value , int _count , CvTrackbarCallback2 on_change , void * data )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
callback = NULL ;
callback2 = on_change ;
userdata = data ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
create ( arg , name , value , _count ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
CvTrackbar : : CvTrackbar ( CvWindow * arg , QString name , int * value , int _count , CvTrackbarCallback on_change )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
callback = on_change ;
callback2 = NULL ;
userdata = NULL ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
create ( arg , name , value , _count ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void CvTrackbar : : create ( CvWindow * arg , QString name , int * value , int _count )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
type = type_CvTrackbar ;
myparent = arg ;
name_bar = name ;
setObjectName ( name_bar ) ;
dataSlider = value ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
slider = new QSlider ( Qt : : Horizontal ) ;
slider - > setFocusPolicy ( Qt : : StrongFocus ) ;
slider - > setMinimum ( 0 ) ;
2012-06-12 22:46:12 +08:00
slider - > setMaximum ( _count ) ;
2012-06-08 01:21:29 +08:00
slider - > setPageStep ( 5 ) ;
slider - > setValue ( * value ) ;
slider - > setTickPosition ( QSlider : : TicksBelow ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//Change style of the Slider
//slider->setStyleSheet(str_Trackbar_css);
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QFile qss ( " :/stylesheet-trackbar " ) ;
if ( qss . open ( QFile : : ReadOnly ) )
{
slider - > setStyleSheet ( QLatin1String ( qss . readAll ( ) ) ) ;
qss . close ( ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//this next line does not work if we change the style with a stylesheet, why ? (bug in QT ?)
//slider->setTickPosition(QSlider::TicksBelow);
label = new QPushButton ;
label - > setFlat ( true ) ;
setLabel ( slider - > value ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QObject : : connect ( slider , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( update ( int ) ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QObject : : connect ( label , SIGNAL ( clicked ( ) ) , this , SLOT ( createDialog ( ) ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//label->setStyleSheet("QPushButton:disabled {color: black}");
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
addWidget ( label , Qt : : AlignLeft ) ; //name + value
addWidget ( slider , Qt : : AlignCenter ) ; //slider
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void CvTrackbar : : createDialog ( )
{
2012-06-08 01:21:29 +08:00
bool ok = false ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//crash if I access the values directly and give them to QInputDialog, so do a copy first.
int value = slider - > value ( ) ;
int step = slider - > singleStep ( ) ;
int min = slider - > minimum ( ) ;
int max = slider - > maximum ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
int i =
2010-08-18 22:07:02 +08:00
# if QT_VERSION >= 0x040500
2012-06-08 01:21:29 +08:00
QInputDialog : : getInt
2010-08-18 22:07:02 +08:00
# else
2012-06-08 01:21:29 +08:00
QInputDialog : : getInteger
2010-08-18 22:07:02 +08:00
# endif
2012-06-08 01:21:29 +08:00
( this - > parentWidget ( ) ,
tr ( " Slider %1 " ) . arg ( name_bar ) ,
tr ( " New value: " ) ,
value ,
min ,
max ,
step ,
& ok ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( ok )
slider - > setValue ( i ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void CvTrackbar : : update ( int myvalue )
{
2012-06-08 01:21:29 +08:00
setLabel ( myvalue ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
* dataSlider = myvalue ;
if ( callback )
{
callback ( myvalue ) ;
return ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( callback2 )
{
callback2 ( myvalue , userdata ) ;
return ;
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void CvTrackbar : : setLabel ( int myvalue )
{
2012-06-08 01:21:29 +08:00
QString nameNormalized = name_bar . leftJustified ( 10 , ' ' , true ) ;
QString valueMaximum = QString ( " %1 " ) . arg ( slider - > maximum ( ) ) ;
QString str = QString ( " %1 (%2/%3) " ) . arg ( nameNormalized ) . arg ( myvalue , valueMaximum . length ( ) , 10 , QChar ( ' 0 ' ) ) . arg ( valueMaximum ) ;
label - > setText ( str ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
//////////////////////////////////////////////////////
// CvButtonbar
2010-08-18 22:07:02 +08:00
//here CvButtonbar class
CvButtonbar : : CvButtonbar ( QWidget * arg , QString arg2 )
{
2012-06-08 01:21:29 +08:00
type = type_CvButtonbar ;
myparent = arg ;
name_bar = arg2 ;
setObjectName ( name_bar ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
group_button = new QButtonGroup ( this ) ;
2010-08-18 22:07:02 +08:00
}
void CvButtonbar : : setLabel ( )
{
2012-06-08 01:21:29 +08:00
QString nameNormalized = name_bar . leftJustified ( 10 , ' ' , true ) ;
label - > setText ( nameNormalized ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvButtonbar : : addButton ( QString name , CvButtonCallback call , void * userdata , int button_type , int initial_button_state )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QString button_name = name ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( button_name = = " " )
button_name = tr ( " button %1 " ) . arg ( this - > count ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointer < QAbstractButton > button ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( button_type = = CV_PUSH_BUTTON )
button = ( QAbstractButton * ) new CvPushButton ( this , button_name , call , userdata ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( button_type = = CV_CHECKBOX )
button = ( QAbstractButton * ) new CvCheckBox ( this , button_name , call , userdata , initial_button_state ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( button_type = = CV_RADIOBOX )
{
button = ( QAbstractButton * ) new CvRadioButton ( this , button_name , call , userdata , initial_button_state ) ;
group_button - > addButton ( button ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
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 ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
addWidget ( button , Qt : : AlignCenter ) ;
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
//////////////////////////////////////////////////////
// Buttons
2010-08-18 22:07:02 +08:00
//buttons here
CvPushButton : : CvPushButton ( CvButtonbar * arg1 , QString arg2 , CvButtonCallback arg3 , void * arg4 )
{
2012-06-08 01:21:29 +08:00
myparent = arg1 ;
button_name = arg2 ;
callback = arg3 ;
userdata = arg4 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
setObjectName ( button_name ) ;
setText ( button_name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( isChecked ( ) )
callCallBack ( true ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void CvPushButton : : callCallBack ( bool checked )
{
2012-06-08 01:21:29 +08:00
if ( callback )
callback ( checked , userdata ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CvCheckBox : : CvCheckBox ( CvButtonbar * arg1 , QString arg2 , CvButtonCallback arg3 , void * arg4 , int initial_button_state )
{
2012-06-08 01:21:29 +08:00
myparent = arg1 ;
button_name = arg2 ;
callback = arg3 ;
userdata = arg4 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
setObjectName ( button_name ) ;
setCheckState ( ( initial_button_state = = 1 ? Qt : : Checked : Qt : : Unchecked ) ) ;
setText ( button_name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( isChecked ( ) )
callCallBack ( true ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void CvCheckBox : : callCallBack ( bool checked )
{
2012-06-08 01:21:29 +08:00
if ( callback )
callback ( checked , userdata ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CvRadioButton : : CvRadioButton ( CvButtonbar * arg1 , QString arg2 , CvButtonCallback arg3 , void * arg4 , int initial_button_state )
{
2012-06-08 01:21:29 +08:00
myparent = arg1 ;
button_name = arg2 ;
callback = arg3 ;
userdata = arg4 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
setObjectName ( button_name ) ;
setChecked ( initial_button_state ) ;
setText ( button_name ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( isChecked ( ) )
callCallBack ( true ) ;
2010-08-18 22:07:02 +08:00
}
void CvRadioButton : : callCallBack ( bool checked )
{
2012-06-08 01:21:29 +08:00
if ( callback )
callback ( checked , userdata ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
//////////////////////////////////////////////////////
// CvWinProperties
2010-08-18 22:07:02 +08:00
//here CvWinProperties class
2012-06-12 22:46:12 +08:00
CvWinProperties : : CvWinProperties ( QString name_paraWindow , QObject * /*parent*/ )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
//setParent(parent);
type = type_CvWinProperties ;
setWindowFlags ( Qt : : Tool ) ;
setContentsMargins ( 0 , 0 , 0 , 0 ) ;
setWindowTitle ( name_paraWindow ) ;
setObjectName ( name_paraWindow ) ;
resize ( 100 , 50 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
myLayout = new QBoxLayout ( QBoxLayout : : TopToBottom ) ;
myLayout - > setObjectName ( QString : : fromUtf8 ( " boxLayout " ) ) ;
myLayout - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
myLayout - > setSpacing ( 0 ) ;
myLayout - > setMargin ( 0 ) ;
myLayout - > setSizeConstraint ( QLayout : : SetFixedSize ) ;
setLayout ( myLayout ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
hide ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWinProperties : : closeEvent ( QCloseEvent * e )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
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
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void CvWinProperties : : showEvent ( QShowEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
//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 " , windowTitle ( ) ) ;
mypos = settings . value ( " pos " , mypos ) . toPoint ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( mypos . x ( ) > = 0 )
{
move ( mypos ) ;
2012-06-12 22:46:12 +08:00
evnt - > accept ( ) ;
2012-06-08 01:21:29 +08:00
}
else
2011-12-12 20:52:06 +08:00
{
2012-06-12 22:46:12 +08:00
evnt - > ignore ( ) ;
2012-06-08 01:21:29 +08:00
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void CvWinProperties : : hideEvent ( QHideEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QSettings settings ( " OpenCV2 " , windowTitle ( ) ) ;
settings . setValue ( " pos " , pos ( ) ) ; //there is an offset of 6 pixels (so the window's position is wrong -- why ?)
2012-06-12 22:46:12 +08:00
evnt - > accept ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CvWinProperties : : ~ CvWinProperties ( )
{
2012-06-08 01:21:29 +08:00
//clear the setting pos
QSettings settings ( " OpenCV2 " , windowTitle ( ) ) ;
settings . remove ( " pos " ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
//////////////////////////////////////////////////////
// CvWindow
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
CvWindow : : CvWindow ( QString name , int arg2 )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
type = type_CvWindow ;
moveToThread ( qApp - > instance ( ) - > thread ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
param_flags = arg2 & 0x0000000F ;
param_gui_mode = arg2 & 0x000000F0 ;
param_ratio_mode = arg2 & 0x00000F00 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//setAttribute(Qt::WA_DeleteOnClose); //in other case, does not release memory
setContentsMargins ( 0 , 0 , 0 , 0 ) ;
setWindowTitle ( name ) ;
2012-03-29 06:22:02 +08:00
setObjectName ( name ) ;
setFocus ( Qt : : PopupFocusReason ) ; //#1695 arrow keys are not recieved without the explicit focus
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
resize ( 400 , 300 ) ;
setMinimumSize ( 1 , 1 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//1: create control panel
if ( ! global_control_panel )
global_control_panel = createParameterWindow ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//2: Layouts
createBarLayout ( ) ;
createGlobalLayout ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//3: my view
2011-12-12 20:52:06 +08:00
# ifndef HAVE_QT_OPENGL
if ( arg2 & CV_WINDOW_OPENGL )
CV_Error ( CV_OpenGlNotSupported , " Library was built without OpenGL support " ) ;
2012-06-08 01:21:29 +08:00
mode_display = CV_MODE_NORMAL ;
2011-12-12 20:52:06 +08:00
# 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 ;
2010-08-18 22:07:02 +08:00
# endif
2012-06-08 01:21:29 +08:00
createView ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//4: shortcuts and actions
//5: toolBar and statusbar
if ( param_gui_mode = = CV_GUI_EXPANDED )
{
2011-12-12 20:52:06 +08:00
createActions ( ) ;
createShortcuts ( ) ;
2012-06-08 01:21:29 +08:00
createToolBar ( ) ;
createStatusBar ( ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//Now attach everything
if ( myToolBar )
myGlobalLayout - > addWidget ( myToolBar , Qt : : AlignCenter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
myGlobalLayout - > addWidget ( myView - > getWidget ( ) , Qt : : AlignCenter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
myGlobalLayout - > addLayout ( myBarLayout , Qt : : AlignCenter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( myStatusBar )
myGlobalLayout - > addWidget ( myStatusBar , Qt : : AlignCenter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
setLayout ( myGlobalLayout ) ;
show ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
CvWindow : : ~ CvWindow ( )
{
2012-06-08 01:21:29 +08:00
if ( guiMainThread )
guiMainThread - > isLastWindow ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : setMouseCallBack ( CvMouseCallback callback , void * param )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
myView - > setMouseCallBack ( callback , param ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : writeSettings ( )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
//organisation and application's name
QSettings settings ( " OpenCV2 " , QFileInfo ( QApplication : : applicationFilePath ( ) ) . fileName ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
settings . setValue ( " pos " , pos ( ) ) ;
settings . setValue ( " size " , size ( ) ) ;
settings . setValue ( " mode_resize " , param_flags ) ;
settings . setValue ( " mode_gui " , param_gui_mode ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
myView - > writeSettings ( settings ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
icvSaveTrackbars ( & settings ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( global_control_panel )
{
icvSaveControlPanel ( ) ;
settings . setValue ( " posPanel " , global_control_panel - > pos ( ) ) ;
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
//TODO: load CV_GUI flag (done) and act accordingly (create win property if needed and attach trackbars)
void CvWindow : : readSettings ( )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
//organisation and application's name
QSettings settings ( " OpenCV2 " , QFileInfo ( QApplication : : applicationFilePath ( ) ) . fileName ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
QPoint _pos = settings . value ( " pos " , QPoint ( 200 , 200 ) ) . toPoint ( ) ;
QSize _size = settings . value ( " size " , QSize ( 400 , 400 ) ) . toSize ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
param_flags = settings . value ( " mode_resize " , param_flags ) . toInt ( ) ;
param_gui_mode = settings . value ( " mode_gui " , param_gui_mode ) . toInt ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
param_flags = settings . value ( " mode_resize " , param_flags ) . toInt ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
myView - > readSettings ( settings ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//trackbar here
icvLoadTrackbars ( & settings ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
resize ( _size ) ;
move ( _pos ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( global_control_panel )
{
icvLoadControlPanel ( ) ;
global_control_panel - > move ( settings . value ( " posPanel " , global_control_panel - > pos ( ) ) . toPoint ( ) ) ;
}
2010-08-18 23:35:56 +08:00
}
2010-08-18 22:07:02 +08:00
2010-08-18 23:35:56 +08:00
2011-12-12 20:52:06 +08:00
double CvWindow : : getRatio ( )
2010-08-18 23:35:56 +08:00
{
2011-12-12 20:52:06 +08:00
return myView - > getRatio ( ) ;
}
2010-08-18 23:35:56 +08:00
2011-12-12 20:52:06 +08:00
void CvWindow : : setRatio ( int flags )
{
myView - > setRatio ( flags ) ;
2010-08-18 22:07:02 +08:00
}
2012-06-08 01:21:29 +08:00
int CvWindow : : getPropWindow ( )
{
return param_flags ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : setPropWindow ( int flags )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
if ( param_flags = = flags ) //nothing to do
2012-06-08 01:21:29 +08:00
return ;
2010-08-24 20:23:37 +08:00
2011-12-12 20:52:06 +08:00
switch ( flags )
{
case CV_WINDOW_NORMAL :
2012-06-08 01:21:29 +08:00
myGlobalLayout - > setSizeConstraint ( QLayout : : SetMinAndMaxSize ) ;
param_flags = flags ;
2010-08-24 20:23:37 +08:00
2012-06-08 01:21:29 +08:00
break ;
2010-08-24 20:23:37 +08:00
2011-12-12 20:52:06 +08:00
case CV_WINDOW_AUTOSIZE :
2012-06-08 01:21:29 +08:00
myGlobalLayout - > setSizeConstraint ( QLayout : : SetFixedSize ) ;
param_flags = flags ;
2010-08-24 20:23:37 +08:00
2012-06-08 01:21:29 +08:00
break ;
2010-08-24 20:23:37 +08:00
2011-12-12 20:52:06 +08:00
default :
;
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : toggleFullScreen ( int flags )
{
if ( isFullScreen ( ) & & flags = = CV_WINDOW_NORMAL )
{
2012-06-08 01:21:29 +08:00
showTools ( ) ;
showNormal ( ) ;
return ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
if ( ! isFullScreen ( ) & & flags = = CV_WINDOW_FULLSCREEN )
{
2012-06-08 01:21:29 +08:00
hideTools ( ) ;
showFullScreen ( ) ;
return ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : updateImage ( void * arr )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
myView - > updateImage ( arr ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : displayInfo ( QString text , int delayms )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
myView - > startDisplayInfo ( text , delayms ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : displayStatusBar ( QString text , int delayms )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
if ( myStatusBar )
2012-06-08 01:21:29 +08:00
myStatusBar - > showMessage ( text , delayms ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : enablePropertiesButton ( )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
vect_QActions [ 9 ] - > setDisabled ( false ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
CvButtonbar * CvWindow : : createButtonBar ( QString name_bar )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvButtonbar > t = new CvButtonbar ( global_control_panel , name_bar ) ;
t - > setAlignment ( Qt : : AlignHCenter ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
QPointer < QBoxLayout > myLayout = global_control_panel - > myLayout ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
myLayout - > insertLayout ( myLayout - > count ( ) , t ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
return t ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : addSlider ( CvWindow * w , QString name , int * value , int count , CvTrackbarCallback on_change )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvTrackbar > t = new CvTrackbar ( w , name , value , count , on_change ) ;
t - > setAlignment ( Qt : : AlignHCenter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointer < QBoxLayout > myLayout ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
{
myLayout = w - > myBarLayout ;
}
else
{
myLayout = global_control_panel - > myLayout ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//if first one, enable control panel
if ( myLayout - > count ( ) = = 0 )
guiMainThread - > enablePropertiesButtonEachWindow ( ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
myLayout - > insertLayout ( myLayout - > count ( ) , t ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : addSlider2 ( CvWindow * w , QString name , int * value , int count , CvTrackbarCallback2 on_change , void * userdata )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QPointer < CvTrackbar > t = new CvTrackbar ( w , name , value , count , on_change , userdata ) ;
t - > setAlignment ( Qt : : AlignHCenter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointer < QBoxLayout > myLayout ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( w )
{
myLayout = w - > myBarLayout ;
}
else
{
myLayout = global_control_panel - > myLayout ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//if first one, enable control panel
if ( myLayout - > count ( ) = = 0 )
guiMainThread - > enablePropertiesButtonEachWindow ( ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
myLayout - > insertLayout ( myLayout - > count ( ) , t ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void CvWindow : : setOpenGlDrawCallback ( CvOpenGlDrawCallback callback , void * userdata )
{
2012-06-08 01:21:29 +08:00
myView - > setOpenGlDrawCallback ( callback , userdata ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : setOpenGlCleanCallback ( CvOpenGlCleanCallback callback , void * userdata )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
myView - > setOpenGlCleanCallback ( callback , userdata ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void CvWindow : : makeCurrentOpenGlContext ( )
{
myView - > makeCurrentOpenGlContext ( ) ;
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void CvWindow : : updateGl ( )
{
myView - > updateGl ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
bool CvWindow : : isOpenGl ( )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
return mode_display = = CV_MODE_OPENGL ;
}
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
void CvWindow : : setViewportSize ( QSize _size )
2011-12-12 20:52:06 +08:00
{
2012-06-12 22:46:12 +08:00
myView - > getWidget ( ) - > resize ( _size ) ;
myView - > setSize ( _size ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void CvWindow : : createBarLayout ( )
{
2012-06-08 01:21:29 +08:00
myBarLayout = new QBoxLayout ( QBoxLayout : : TopToBottom ) ;
myBarLayout - > setObjectName ( QString : : fromUtf8 ( " barLayout " ) ) ;
myBarLayout - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
myBarLayout - > setSpacing ( 0 ) ;
myBarLayout - > setMargin ( 0 ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : createGlobalLayout ( )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
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 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( param_flags = = CV_WINDOW_AUTOSIZE )
myGlobalLayout - > setSizeConstraint ( QLayout : : SetFixedSize ) ;
else if ( param_flags = = CV_WINDOW_NORMAL )
myGlobalLayout - > setSizeConstraint ( QLayout : : SetMinAndMaxSize ) ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : createView ( )
{
# ifdef HAVE_QT_OPENGL
if ( isOpenGl ( ) )
2012-06-08 01:21:29 +08:00
myView = new OpenGlViewPort ( this ) ;
2011-12-12 20:52:06 +08:00
else
# endif
2012-06-08 01:21:29 +08:00
myView = new DefaultViewPort ( this , param_ratio_mode ) ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : createActions ( )
{
2012-06-08 01:21:29 +08:00
vect_QActions . resize ( 10 ) ;
2011-12-12 20:52:06 +08:00
QWidget * view = myView - > getWidget ( ) ;
2012-06-08 01:21:29 +08:00
//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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
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 ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( global_control_panel - > myLayout - > count ( ) = = 0 )
vect_QActions [ 9 ] - > setDisabled ( true ) ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : createShortcuts ( )
{
2012-06-08 01:21:29 +08:00
vect_QShortcuts . resize ( 10 ) ;
2011-12-12 20:52:06 +08:00
QWidget * view = myView - > getWidget ( ) ;
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 0 ] = new QShortcut ( shortcut_panning_left , this ) ;
QObject : : connect ( vect_QShortcuts [ 0 ] , SIGNAL ( activated ( ) ) , view , SLOT ( siftWindowOnLeft ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 1 ] = new QShortcut ( shortcut_panning_right , this ) ;
QObject : : connect ( vect_QShortcuts [ 1 ] , SIGNAL ( activated ( ) ) , view , SLOT ( siftWindowOnRight ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 2 ] = new QShortcut ( shortcut_panning_up , this ) ;
QObject : : connect ( vect_QShortcuts [ 2 ] , SIGNAL ( activated ( ) ) , view , SLOT ( siftWindowOnUp ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 3 ] = new QShortcut ( shortcut_panning_down , this ) ;
QObject : : connect ( vect_QShortcuts [ 3 ] , SIGNAL ( activated ( ) ) , view , SLOT ( siftWindowOnDown ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 4 ] = new QShortcut ( shortcut_zoom_normal , this ) ;
QObject : : connect ( vect_QShortcuts [ 4 ] , SIGNAL ( activated ( ) ) , view , SLOT ( resetZoom ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 5 ] = new QShortcut ( shortcut_zoom_imgRegion , this ) ;
QObject : : connect ( vect_QShortcuts [ 5 ] , SIGNAL ( activated ( ) ) , view , SLOT ( imgRegion ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 6 ] = new QShortcut ( shortcut_zoom_in , this ) ;
QObject : : connect ( vect_QShortcuts [ 6 ] , SIGNAL ( activated ( ) ) , view , SLOT ( ZoomIn ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 7 ] = new QShortcut ( shortcut_zoom_out , this ) ;
QObject : : connect ( vect_QShortcuts [ 7 ] , SIGNAL ( activated ( ) ) , view , SLOT ( ZoomOut ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 8 ] = new QShortcut ( shortcut_save_img , this ) ;
QObject : : connect ( vect_QShortcuts [ 8 ] , SIGNAL ( activated ( ) ) , view , SLOT ( saveView ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
vect_QShortcuts [ 9 ] = new QShortcut ( shortcut_properties_win , this ) ;
QObject : : connect ( vect_QShortcuts [ 9 ] , SIGNAL ( activated ( ) ) , this , SLOT ( displayPropertiesWin ( ) ) ) ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : createToolBar ( )
{
2012-06-08 01:21:29 +08:00
myToolBar = new QToolBar ( this ) ;
myToolBar - > setFloatable ( false ) ; //is not a window
myToolBar - > setFixedHeight ( 28 ) ;
myToolBar - > setMinimumWidth ( 1 ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
foreach ( QAction * a , vect_QActions )
myToolBar - > addAction ( a ) ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : createStatusBar ( )
{
2012-06-08 01:21:29 +08:00
myStatusBar = new QStatusBar ( this ) ;
myStatusBar - > setSizeGripEnabled ( false ) ;
myStatusBar - > setFixedHeight ( 20 ) ;
myStatusBar - > setMinimumWidth ( 1 ) ;
myStatusBar_msg = new QLabel ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//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)
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//myStatusBar_msg->setFrameStyle(QFrame::Raised);
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
myStatusBar_msg - > setAlignment ( Qt : : AlignHCenter ) ;
myStatusBar - > addWidget ( myStatusBar_msg ) ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : hideTools ( )
{
2012-06-08 01:21:29 +08:00
if ( myToolBar )
myToolBar - > hide ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( myStatusBar )
myStatusBar - > hide ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( global_control_panel )
global_control_panel - > hide ( ) ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : showTools ( )
{
2012-06-08 01:21:29 +08:00
if ( myToolBar )
myToolBar - > show ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( myStatusBar )
myStatusBar - > show ( ) ;
2011-12-12 20:52:06 +08:00
}
CvWinProperties * CvWindow : : createParameterWindow ( )
{
2012-06-08 01:21:29 +08:00
QString name_paraWindow = QFileInfo ( QApplication : : applicationFilePath ( ) ) . fileName ( ) + " settings " ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
CvWinProperties * result = new CvWinProperties ( name_paraWindow , guiMainThread ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
return result ;
2011-12-12 20:52:06 +08:00
}
void CvWindow : : displayPropertiesWin ( )
{
2012-06-08 01:21:29 +08:00
if ( global_control_panel - > isHidden ( ) )
global_control_panel - > show ( ) ;
else
global_control_panel - > hide ( ) ;
2011-12-12 20:52:06 +08:00
}
//Need more test here !
2012-06-12 22:46:12 +08:00
void CvWindow : : keyPressEvent ( QKeyEvent * evnt )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
//see http://doc.trolltech.com/4.6/qt.html#Key-enum
2012-06-12 22:46:12 +08:00
int key = evnt - > key ( ) ;
2011-12-12 20:52:06 +08:00
2012-03-29 06:22:02 +08:00
Qt : : Key qtkey = static_cast < Qt : : Key > ( key ) ;
char asciiCode = QTest : : keyToAscii ( qtkey ) ;
if ( asciiCode ! = 0 )
key = static_cast < int > ( asciiCode ) ;
else
2012-06-12 22:46:12 +08:00
key = evnt - > nativeVirtualKey ( ) ; //same codes as returned by GTK-based backend
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//control plus (Z, +, -, up, down, left, right) are used for zoom/panning functions
2012-06-12 22:46:12 +08:00
if ( evnt - > modifiers ( ) ! = Qt : : ControlModifier )
2012-03-29 06:22:02 +08:00
{
2012-06-08 01:21:29 +08:00
mutexKey . lock ( ) ;
last_key = key ;
mutexKey . unlock ( ) ;
key_pressed . wakeAll ( ) ;
2012-06-12 22:46:12 +08:00
//evnt->accept();
2012-06-08 01:21:29 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
QWidget : : keyPressEvent ( evnt ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void CvWindow : : icvLoadControlPanel ( )
{
2012-06-08 01:21:29 +08:00
QSettings settings ( " OpenCV2 " , QFileInfo ( QApplication : : applicationFilePath ( ) ) . fileName ( ) + " control panel " ) ;
2012-06-12 22:46:12 +08:00
int bsize = settings . beginReadArray ( " bars " ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
if ( bsize = = global_control_panel - > myLayout - > layout ( ) - > count ( ) )
2011-12-12 20:52:06 +08:00
{
2012-06-12 22:46:12 +08:00
for ( int i = 0 ; i < bsize ; + + i )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
CvBar * t = ( CvBar * ) global_control_panel - > myLayout - > layout ( ) - > itemAt ( i ) ;
settings . setArrayIndex ( i ) ;
if ( t - > type = = type_CvTrackbar )
{
if ( t - > name_bar = = settings . value ( " namebar " ) . toString ( ) )
{
( ( CvTrackbar * ) t ) - > slider - > setValue ( settings . value ( " valuebar " ) . toInt ( ) ) ;
}
}
if ( t - > type = = type_CvButtonbar )
{
int subsize = settings . beginReadArray ( QString ( " buttonbar " ) + i ) ;
if ( subsize = = ( ( CvButtonbar * ) t ) - > layout ( ) - > count ( ) )
icvLoadButtonbar ( ( CvButtonbar * ) t , & settings ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
settings . endArray ( ) ;
}
}
2011-12-12 20:52:06 +08:00
}
2012-06-08 01:21:29 +08:00
settings . endArray ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2010-08-18 22:07:02 +08:00
void CvWindow : : icvSaveControlPanel ( )
{
2012-06-08 01:21:29 +08:00
QSettings settings ( " OpenCV2 " , QFileInfo ( QApplication : : applicationFilePath ( ) ) . fileName ( ) + " control panel " ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
settings . beginWriteArray ( " bars " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
for ( int i = 0 ; i < global_control_panel - > myLayout - > layout ( ) - > count ( ) ; + + i )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
CvBar * t = ( CvBar * ) global_control_panel - > myLayout - > layout ( ) - > itemAt ( i ) ;
settings . setArrayIndex ( i ) ;
if ( t - > type = = type_CvTrackbar )
{
settings . setValue ( " namebar " , QString ( t - > name_bar ) ) ;
settings . setValue ( " valuebar " , ( ( CvTrackbar * ) t ) - > slider - > value ( ) ) ;
}
if ( t - > type = = type_CvButtonbar )
{
settings . beginWriteArray ( QString ( " buttonbar " ) + i ) ;
icvSaveButtonbar ( ( CvButtonbar * ) t , & settings ) ;
settings . endArray ( ) ;
}
}
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
settings . endArray ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : icvSaveButtonbar ( CvButtonbar * b , QSettings * settings )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
for ( int i = 0 , count = b - > layout ( ) - > count ( ) ; i < count ; + + i )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
settings - > setArrayIndex ( i ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
QWidget * temp = ( QWidget * ) b - > layout ( ) - > itemAt ( i ) - > widget ( ) ;
2011-12-12 20:52:06 +08:00
QString myclass ( QLatin1String ( temp - > metaObject ( ) - > className ( ) ) ) ;
2012-06-08 01:21:29 +08:00
if ( myclass = = " CvPushButton " )
{
CvPushButton * button = ( CvPushButton * ) temp ;
settings - > setValue ( " namebutton " , button - > text ( ) ) ;
settings - > setValue ( " valuebutton " , int ( button - > isChecked ( ) ) ) ;
}
else if ( myclass = = " CvCheckBox " )
{
CvCheckBox * button = ( CvCheckBox * ) temp ;
settings - > setValue ( " namebutton " , button - > text ( ) ) ;
settings - > setValue ( " valuebutton " , int ( button - > isChecked ( ) ) ) ;
}
else if ( myclass = = " CvRadioButton " )
{
CvRadioButton * button = ( CvRadioButton * ) temp ;
settings - > setValue ( " namebutton " , button - > text ( ) ) ;
settings - > setValue ( " valuebutton " , int ( button - > isChecked ( ) ) ) ;
}
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : icvLoadButtonbar ( CvButtonbar * b , QSettings * settings )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
for ( int i = 0 , count = b - > layout ( ) - > count ( ) ; i < count ; + + i )
{
settings - > setArrayIndex ( i ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
QWidget * temp = ( QWidget * ) b - > layout ( ) - > itemAt ( i ) - > widget ( ) ;
QString myclass ( QLatin1String ( temp - > metaObject ( ) - > className ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( myclass = = " CvPushButton " )
{
CvPushButton * button = ( CvPushButton * ) temp ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( button - > text ( ) = = settings - > value ( " namebutton " ) . toString ( ) )
button - > setChecked ( settings - > value ( " valuebutton " ) . toInt ( ) ) ;
}
else if ( myclass = = " CvCheckBox " )
{
CvCheckBox * button = ( CvCheckBox * ) temp ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( button - > text ( ) = = settings - > value ( " namebutton " ) . toString ( ) )
button - > setChecked ( settings - > value ( " valuebutton " ) . toInt ( ) ) ;
}
else if ( myclass = = " CvRadioButton " )
{
CvRadioButton * button = ( CvRadioButton * ) temp ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( button - > text ( ) = = settings - > value ( " namebutton " ) . toString ( ) )
button - > setChecked ( settings - > value ( " valuebutton " ) . toInt ( ) ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : icvLoadTrackbars ( QSettings * settings )
2010-08-18 22:07:02 +08:00
{
2012-06-12 22:46:12 +08:00
int bsize = settings - > beginReadArray ( " trackbars " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//trackbar are saved in the same order, so no need to use icvFindTrackbarByName
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
if ( myBarLayout - > layout ( ) - > count ( ) = = bsize ) //if not the same number, the window saved and loaded is not the same (nb trackbar not equal)
2011-12-12 20:52:06 +08:00
{
2012-06-12 22:46:12 +08:00
for ( int i = 0 ; i < bsize ; + + i )
2012-06-08 01:21:29 +08:00
{
settings - > setArrayIndex ( i ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
CvTrackbar * t = ( CvTrackbar * ) myBarLayout - > layout ( ) - > itemAt ( i ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( t - > name_bar = = settings - > value ( " name " ) . toString ( ) )
t - > slider - > setValue ( settings - > value ( " value " ) . toInt ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
}
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
settings - > endArray ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void CvWindow : : icvSaveTrackbars ( QSettings * settings )
{
2012-06-08 01:21:29 +08:00
settings - > beginWriteArray ( " trackbars " ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
for ( int i = 0 ; i < myBarLayout - > layout ( ) - > count ( ) ; + + i )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
settings - > setArrayIndex ( i ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
CvTrackbar * t = ( CvTrackbar * ) myBarLayout - > layout ( ) - > itemAt ( i ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
settings - > setValue ( " name " , t - > name_bar ) ;
settings - > setValue ( " value " , t - > slider - > value ( ) ) ;
}
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
settings - > endArray ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
//////////////////////////////////////////////////////
// DefaultViewPort
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
DefaultViewPort : : DefaultViewPort ( CvWindow * arg , int arg2 ) : QGraphicsView ( arg ) , image2Draw_mat ( 0 )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
centralWidget = arg ;
2011-12-12 20:52:06 +08:00
param_keepRatio = arg2 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
setContentsMargins ( 0 , 0 , 0 , 0 ) ;
setMinimumSize ( 1 , 1 ) ;
2011-12-12 20:52:06 +08:00
setAlignment ( Qt : : AlignHCenter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
setObjectName ( QString : : fromUtf8 ( " graphicsView " ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
timerDisplay = new QTimer ( this ) ;
timerDisplay - > setSingleShot ( true ) ;
connect ( timerDisplay , SIGNAL ( timeout ( ) ) , this , SLOT ( stopDisplayInfo ( ) ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
drawInfo = false ;
positionGrabbing = QPointF ( 0 , 0 ) ;
positionCorners = QRect ( 0 , 0 , size ( ) . width ( ) , size ( ) . height ( ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
on_mouse = 0 ;
2011-12-12 20:52:06 +08:00
on_mouse_param = 0 ;
2012-06-08 01:21:29 +08:00
mouseCoordinate = QPoint ( - 1 , - 1 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//no border
setStyleSheet ( " QGraphicsView { border-style: none; } " ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
image2Draw_mat = cvCreateMat ( viewport ( ) - > height ( ) , viewport ( ) - > width ( ) , CV_8UC3 ) ;
cvZero ( image2Draw_mat ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
nbChannelOriginImage = 0 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
setInteractive ( false ) ;
setMouseTracking ( true ) ; //receive mouse event everytime
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2010-08-19 18:36:57 +08:00
2011-12-12 20:52:06 +08:00
DefaultViewPort : : ~ DefaultViewPort ( )
{
2012-06-08 01:21:29 +08:00
if ( image2Draw_mat )
cvReleaseMat ( & image2Draw_mat ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
QWidget * DefaultViewPort : : getWidget ( )
{
return this ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : setMouseCallBack ( CvMouseCallback m , void * param )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
on_mouse = m ;
2011-12-13 14:25:37 +08:00
2012-06-08 01:21:29 +08:00
on_mouse_param = param ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
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 ( ) ) ;
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
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 ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
double DefaultViewPort : : getRatio ( )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
return param_keepRatio ;
}
void DefaultViewPort : : setRatio ( int flags )
{
if ( getRatio ( ) = = flags ) //nothing to do
2012-06-08 01:21:29 +08:00
return ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//if valid flags
if ( flags = = CV_WINDOW_FREERATIO | | flags = = CV_WINDOW_KEEPRATIO )
2011-12-12 20:52:06 +08:00
{
centralWidget - > param_ratio_mode = flags ;
2012-06-08 01:21:29 +08:00
param_keepRatio = flags ;
updateGeometry ( ) ;
viewport ( ) - > update ( ) ;
2011-12-12 20:52:06 +08:00
}
}
void DefaultViewPort : : updateImage ( const CvArr * arr )
{
2012-06-08 01:21:29 +08:00
CV_Assert ( arr ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
CvMat * mat , stub ;
int origin = 0 ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( CV_IS_IMAGE_HDR ( arr ) )
origin = ( ( IplImage * ) arr ) - > origin ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
mat = cvGetMat ( arr , & stub ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( ! image2Draw_mat | | ! CV_ARE_SIZES_EQ ( image2Draw_mat , mat ) )
{
2011-12-12 20:52:06 +08:00
if ( image2Draw_mat )
2012-06-08 01:21:29 +08:00
cvReleaseMat ( & image2Draw_mat ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//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 ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//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 ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
updateGeometry ( ) ;
}
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
nbChannelOriginImage = cvGetElemType ( mat ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
cvConvertImage ( mat , image2Draw_mat , ( origin ! = 0 ? CV_CVTIMG_FLIP : 0 ) + CV_CVTIMG_SWAP_RB ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
viewport ( ) - > update ( ) ;
2011-12-12 20:52:06 +08:00
}
void DefaultViewPort : : startDisplayInfo ( QString text , int delayms )
{
2012-06-08 01:21:29 +08:00
if ( timerDisplay - > isActive ( ) )
stopDisplayInfo ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
infoText = text ;
if ( delayms > 0 ) timerDisplay - > start ( delayms ) ;
drawInfo = true ;
2011-12-12 20:52:06 +08:00
}
2012-06-29 15:46:53 +08:00
void DefaultViewPort : : setOpenGlDrawCallback ( CvOpenGlDrawCallback /*callback*/ , void * /*userdata*/ )
2011-12-12 20:52:06 +08:00
{
CV_Error ( CV_OpenGlNotSupported , " Window doesn't support OpenGL " ) ;
}
2012-06-29 15:46:53 +08:00
void DefaultViewPort : : setOpenGlCleanCallback ( CvOpenGlCleanCallback /*callback*/ , void * /*userdata*/ )
2011-12-12 20:52:06 +08:00
{
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 ( )
{
2012-06-08 01:21:29 +08:00
float delta = 2 * width ( ) / ( 100.0 * param_matrixWorld . m11 ( ) ) ;
moveView ( QPointF ( delta , 0 ) ) ;
2011-12-12 20:52:06 +08:00
}
//Note: move 2 percent of the window
void DefaultViewPort : : siftWindowOnRight ( )
{
2012-06-08 01:21:29 +08:00
float delta = - 2 * width ( ) / ( 100.0 * param_matrixWorld . m11 ( ) ) ;
moveView ( QPointF ( delta , 0 ) ) ;
2011-12-12 20:52:06 +08:00
}
//Note: move 2 percent of the window
void DefaultViewPort : : siftWindowOnUp ( )
{
2012-06-08 01:21:29 +08:00
float delta = 2 * height ( ) / ( 100.0 * param_matrixWorld . m11 ( ) ) ;
moveView ( QPointF ( 0 , delta ) ) ;
2011-12-12 20:52:06 +08:00
}
//Note: move 2 percent of the window
void DefaultViewPort : : siftWindowOnDown ( )
{
2012-06-08 01:21:29 +08:00
float delta = - 2 * height ( ) / ( 100.0 * param_matrixWorld . m11 ( ) ) ;
moveView ( QPointF ( 0 , delta ) ) ;
2011-12-12 20:52:06 +08:00
}
void DefaultViewPort : : imgRegion ( )
{
2012-06-08 01:21:29 +08:00
scaleView ( ( threshold_zoom_img_region / param_matrixWorld . m11 ( ) - 1 ) * 5 , QPointF ( size ( ) . width ( ) / 2 , size ( ) . height ( ) / 2 ) ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : resetZoom ( )
{
2012-06-08 01:21:29 +08:00
param_matrixWorld . reset ( ) ;
controlImagePosition ( ) ;
2011-12-12 20:52:06 +08:00
}
void DefaultViewPort : : ZoomIn ( )
{
2012-06-08 01:21:29 +08:00
scaleView ( 0.5 , QPointF ( size ( ) . width ( ) / 2 , size ( ) . height ( ) / 2 ) ) ;
2011-12-12 20:52:06 +08:00
}
void DefaultViewPort : : ZoomOut ( )
{
2012-06-08 01:21:29 +08:00
scaleView ( - 0.5 , QPointF ( size ( ) . width ( ) / 2 , size ( ) . height ( ) / 2 ) ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
//can save as JPG, JPEG, BMP, PNG
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : saveView ( )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QDate date_d = QDate : : currentDate ( ) ;
QString date_s = date_d . toString ( " dd.MM.yyyy " ) ;
2011-12-12 20:52:06 +08:00
QString name_s = centralWidget - > windowTitle ( ) + " _screenshot_ " + date_s ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
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
{
QString extension = fileName . right ( 3 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
// (no need anymore) create the image resized to receive the 'screenshot'
// image2Draw_qt_resized = QImage(viewport()->width(), viewport()->height(),QImage::Format_RGB888);
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPainter saveimage ( & image2Draw_qt_resized ) ;
this - > render ( & saveimage ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
// Save it..
if ( QString : : compare ( extension , " png " , Qt : : CaseInsensitive ) = = 0 )
{
image2Draw_qt_resized . save ( fileName , " PNG " ) ;
return ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( QString : : compare ( extension , " jpg " , Qt : : CaseInsensitive ) = = 0 )
{
image2Draw_qt_resized . save ( fileName , " JPG " ) ;
return ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( QString : : compare ( extension , " bmp " , Qt : : CaseInsensitive ) = = 0 )
{
image2Draw_qt_resized . save ( fileName , " BMP " ) ;
return ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( QString : : compare ( extension , " jpeg " , Qt : : CaseInsensitive ) = = 0 )
{
image2Draw_qt_resized . save ( fileName , " JPEG " ) ;
return ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
CV_Error ( CV_StsNullPtr , " file extension not recognized, please choose between JPG, JPEG, BMP or PNG " ) ;
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : contextMenuEvent ( QContextMenuEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( centralWidget - > vect_QActions . size ( ) > 0 )
{
QMenu menu ( this ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
foreach ( QAction * a , centralWidget - > vect_QActions )
menu . addAction ( a ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
menu . exec ( evnt - > globalPos ( ) ) ;
2012-06-08 01:21:29 +08:00
}
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : resizeEvent ( QResizeEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
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 ) ;
2012-06-08 01:21:29 +08:00
2011-12-12 20:52:06 +08:00
if ( param_keepRatio = = CV_WINDOW_KEEPRATIO ) //to keep the same aspect ratio
{
2012-06-08 01:21:29 +08:00
QSize newSize = QSize ( image2Draw_mat - > cols , image2Draw_mat - > rows ) ;
2012-06-12 22:46:12 +08:00
newSize . scale ( evnt - > size ( ) , Qt : : KeepAspectRatio ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//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 ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//move to the middle
//newSize get the delta offset to place the picture in the middle of its parent
2012-06-12 22:46:12 +08:00
newSize = ( evnt - > size ( ) - newSize ) / 2 ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//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 ( ) ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
move ( newSize . width ( ) , newSize . height ( ) ) ;
}
2011-12-12 20:52:06 +08:00
}
2012-06-12 22:46:12 +08:00
return QGraphicsView : : resizeEvent ( evnt ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : wheelEvent ( QWheelEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-12 22:46:12 +08:00
scaleView ( evnt - > delta ( ) / 240.0 , evnt - > pos ( ) ) ;
2012-06-08 01:21:29 +08:00
viewport ( ) - > update ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : mousePressEvent ( QMouseEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = - 1 , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//icvmouseHandler: pass parameters for cv_event, flags
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_down , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
if ( param_matrixWorld . m11 ( ) > 1 )
{
setCursor ( Qt : : ClosedHandCursor ) ;
2012-06-12 22:46:12 +08:00
positionGrabbing = evnt - > pos ( ) ;
2012-06-08 01:21:29 +08:00
}
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
QWidget : : mousePressEvent ( evnt ) ;
2010-08-18 22:07:02 +08:00
}
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : mouseReleaseEvent ( QMouseEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = - 1 , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//icvmouseHandler: pass parameters for cv_event, flags
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_up , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( param_matrixWorld . m11 ( ) > 1 )
setCursor ( Qt : : OpenHandCursor ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
QWidget : : mouseReleaseEvent ( evnt ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : mouseDoubleClickEvent ( QMouseEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = - 1 , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//icvmouseHandler: pass parameters for cv_event, flags
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_dbclick , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
QWidget : : mouseDoubleClickEvent ( evnt ) ;
2010-08-18 22:07:02 +08:00
}
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : mouseMoveEvent ( QMouseEvent * evnt )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = CV_EVENT_MOUSEMOVE , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//icvmouseHandler: pass parameters for cv_event, flags
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_move , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
if ( param_matrixWorld . m11 ( ) > 1 & & evnt - > buttons ( ) = = Qt : : LeftButton )
2012-06-08 01:21:29 +08:00
{
QPointF dxy = ( pt - positionGrabbing ) / param_matrixWorld . m11 ( ) ;
2012-06-12 22:46:12 +08:00
positionGrabbing = evnt - > pos ( ) ;
2012-06-08 01:21:29 +08:00
moveView ( dxy ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//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 ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
QWidget : : mouseMoveEvent ( evnt ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2010-08-19 18:36:57 +08:00
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : paintEvent ( QPaintEvent * evnt )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
QPainter myPainter ( viewport ( ) ) ;
myPainter . setWorldTransform ( param_matrixWorld ) ;
2010-08-20 21:06:14 +08:00
2012-06-08 01:21:29 +08:00
draw2D ( & myPainter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//Now disable matrixWorld for overlay display
myPainter . setWorldMatrixEnabled ( false ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//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 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
drawImgRegion ( & myPainter ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
drawViewOverview ( & myPainter ) ;
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//for information overlay
if ( drawInfo )
drawInstructions ( & myPainter ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//for statusbar
if ( centralWidget - > myStatusBar )
drawStatusBar ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
QGraphicsView : : paintEvent ( evnt ) ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : stopDisplayInfo ( )
{
2012-06-08 01:21:29 +08:00
timerDisplay - > stop ( ) ;
drawInfo = false ;
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
inline bool DefaultViewPort : : isSameSize ( IplImage * img1 , IplImage * img2 )
2010-12-04 23:15:06 +08:00
{
2012-06-08 01:21:29 +08:00
return img1 - > width = = img2 - > width & & img1 - > height = = img2 - > height ;
2010-12-04 23:15:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : controlImagePosition ( )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
qreal left , top , right , bottom ;
//after check top-left, bottom right corner to avoid getting "out" during zoom/panning
param_matrixWorld . map ( 0 , 0 , & left , & top ) ;
if ( left > 0 )
{
param_matrixWorld . translate ( - left , 0 ) ;
left = 0 ;
}
if ( top > 0 )
{
param_matrixWorld . translate ( 0 , - top ) ;
top = 0 ;
}
//-------
QSize sizeImage = size ( ) ;
param_matrixWorld . map ( sizeImage . width ( ) , sizeImage . height ( ) , & right , & bottom ) ;
if ( right < sizeImage . width ( ) )
{
param_matrixWorld . translate ( sizeImage . width ( ) - right , 0 ) ;
right = sizeImage . width ( ) ;
}
if ( bottom < sizeImage . height ( ) )
{
param_matrixWorld . translate ( 0 , sizeImage . height ( ) - bottom ) ;
bottom = sizeImage . height ( ) ;
}
//save corner position
positionCorners . setTopLeft ( QPoint ( left , top ) ) ;
positionCorners . setBottomRight ( QPoint ( right , bottom ) ) ;
//save also the inv matrix
matrixWorld_inv = param_matrixWorld . inverted ( ) ;
//viewport()->update();
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : moveView ( QPointF delta )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
param_matrixWorld . translate ( delta . x ( ) , delta . y ( ) ) ;
controlImagePosition ( ) ;
viewport ( ) - > update ( ) ;
2010-08-18 22:07:02 +08:00
}
//factor is -0.5 (zoom out) or 0.5 (zoom in)
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : scaleView ( qreal factor , QPointF center )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
factor / = 5 ; //-0.1 <-> 0.1
factor + = 1 ; //0.9 <-> 1.1
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//limit zoom out ---
if ( param_matrixWorld . m11 ( ) = = 1 & & factor < 1 )
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( param_matrixWorld . m11 ( ) * factor < 1 )
factor = 1 / param_matrixWorld . m11 ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//limit zoom int ---
if ( param_matrixWorld . m11 ( ) > 100 & & factor > 1 )
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//inverse the transform
int a , b ;
matrixWorld_inv . map ( center . x ( ) , center . y ( ) , & a , & b ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
param_matrixWorld . translate ( a - factor * a , b - factor * b ) ;
param_matrixWorld . scale ( factor , factor ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
controlImagePosition ( ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//display new zoom
if ( centralWidget - > myStatusBar )
centralWidget - > displayStatusBar ( tr ( " Zoom: %1% " ) . arg ( param_matrixWorld . m11 ( ) * 100 ) , 1000 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( param_matrixWorld . m11 ( ) > 1 )
setCursor ( Qt : : OpenHandCursor ) ;
else
unsetCursor ( ) ;
2010-08-18 22:07:02 +08:00
}
2010-12-04 23:15:06 +08:00
//up, down, dclick, move
2012-06-12 22:46:12 +08:00
void DefaultViewPort : : icvmouseHandler ( QMouseEvent * evnt , type_mouse_event category , int & cv_event , int & flags )
2010-12-04 23:15:06 +08:00
{
2012-06-12 22:46:12 +08:00
Qt : : KeyboardModifiers modifiers = evnt - > modifiers ( ) ;
Qt : : MouseButtons buttons = evnt - > buttons ( ) ;
2012-06-08 01:21:29 +08:00
2010-12-04 23:15:06 +08:00
flags = 0 ;
if ( modifiers & Qt : : ShiftModifier )
2012-03-29 06:22:02 +08:00
flags | = CV_EVENT_FLAG_SHIFTKEY ;
if ( modifiers & Qt : : ControlModifier )
flags | = CV_EVENT_FLAG_CTRLKEY ;
if ( modifiers & Qt : : AltModifier )
flags | = CV_EVENT_FLAG_ALTKEY ;
2010-12-04 23:15:06 +08:00
if ( buttons & Qt : : LeftButton )
2012-03-29 06:22:02 +08:00
flags | = CV_EVENT_FLAG_LBUTTON ;
if ( buttons & Qt : : RightButton )
flags | = CV_EVENT_FLAG_RBUTTON ;
2010-12-04 23:15:06 +08:00
if ( buttons & Qt : : MidButton )
2012-03-29 06:22:02 +08:00
flags | = CV_EVENT_FLAG_MBUTTON ;
2010-12-04 23:15:06 +08:00
cv_event = CV_EVENT_MOUSEMOVE ;
2012-06-12 22:46:12 +08:00
switch ( evnt - > button ( ) )
2012-03-29 06:22:02 +08:00
{
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 : ;
}
2011-12-12 20:52:06 +08:00
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : icvmouseProcessing ( QPointF pt , int cv_event , int flags )
{
2012-06-08 01:21:29 +08:00
//to convert mouse coordinate
qreal pfx , pfy ;
matrixWorld_inv . map ( pt . x ( ) , pt . y ( ) , & pfx , & pfy ) ;
mouseCoordinate . rx ( ) = floor ( pfx / ratioX ) ;
mouseCoordinate . ry ( ) = floor ( pfy / ratioY ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( on_mouse )
on_mouse ( cv_event , mouseCoordinate . x ( ) ,
2011-12-12 20:52:06 +08:00
mouseCoordinate . y ( ) , flags , on_mouse_param ) ;
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
QSize DefaultViewPort : : sizeHint ( ) const
{
2012-06-08 01:21:29 +08:00
if ( image2Draw_mat )
return QSize ( image2Draw_mat - > cols , image2Draw_mat - > rows ) ;
else
return QGraphicsView : : sizeHint ( ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : draw2D ( QPainter * painter )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
image2Draw_qt = QImage ( image2Draw_mat - > data . ptr , image2Draw_mat - > cols , image2Draw_mat - > rows , image2Draw_mat - > step , QImage : : Format_RGB888 ) ;
2012-09-17 20:13:53 +08:00
painter - > drawImage ( QRect ( 0 , 0 , viewport ( ) - > width ( ) , viewport ( ) - > height ( ) ) , image2Draw_qt , QRect ( 0 , 0 , image2Draw_qt . width ( ) , image2Draw_qt . height ( ) ) ) ;
2010-08-18 22:07:02 +08:00
}
2010-08-19 18:36:57 +08:00
//only if CV_8UC1 or CV_8UC3
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : drawStatusBar ( )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
if ( nbChannelOriginImage ! = CV_8UC1 & & nbChannelOriginImage ! = CV_8UC3 )
return ;
if ( mouseCoordinate . x ( ) > = 0 & &
mouseCoordinate . y ( ) > = 0 & &
mouseCoordinate . x ( ) < image2Draw_qt . width ( ) & &
mouseCoordinate . y ( ) < image2Draw_qt . height ( ) )
// if (mouseCoordinate.x()>=0 && mouseCoordinate.y()>=0)
{
QRgb rgbValue = image2Draw_qt . pixel ( mouseCoordinate ) ;
if ( nbChannelOriginImage = = CV_8UC3 )
{
centralWidget - > myStatusBar_msg - > setText ( tr ( " <font color='black'>(x=%1, y=%2) ~ </font> " )
. arg ( mouseCoordinate . x ( ) )
. arg ( mouseCoordinate . y ( ) ) +
tr ( " <font color='red'>R:%3 </font> " ) . arg ( qRed ( rgbValue ) ) + //.arg(value.val[0])+
tr ( " <font color='green'>G:%4 </font> " ) . arg ( qGreen ( rgbValue ) ) + //.arg(value.val[1])+
tr ( " <font color='blue'>B:%5</font> " ) . arg ( qBlue ( rgbValue ) ) //.arg(value.val[2])
) ;
}
if ( nbChannelOriginImage = = CV_8UC1 )
{
//all the channel have the same value (because of cvconvertimage), so only the r channel is dsplayed
centralWidget - > myStatusBar_msg - > setText ( tr ( " <font color='black'>(x=%1, y=%2) ~ </font> " )
. arg ( mouseCoordinate . x ( ) )
. arg ( mouseCoordinate . y ( ) ) +
tr ( " <font color='grey'>L:%3 </font> " ) . arg ( qRed ( rgbValue ) )
) ;
}
}
2010-08-18 22:07:02 +08:00
}
2010-08-20 21:06:14 +08:00
//accept only CV_8UC1 and CV_8UC8 image for now
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : drawImgRegion ( QPainter * painter )
2010-08-18 22:07:02 +08:00
{
2010-08-20 21:06:14 +08:00
2012-06-08 01:21:29 +08:00
if ( nbChannelOriginImage ! = CV_8UC1 & & nbChannelOriginImage ! = CV_8UC3 )
return ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
qreal offsetX = param_matrixWorld . dx ( ) / param_matrixWorld . m11 ( ) ;
offsetX = offsetX - floor ( offsetX ) ;
qreal offsetY = param_matrixWorld . dy ( ) / param_matrixWorld . m11 ( ) ;
offsetY = offsetY - floor ( offsetY ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QSize view = size ( ) ;
QVarLengthArray < QLineF , 30 > linesX ;
2012-06-12 22:46:12 +08:00
for ( qreal _x = offsetX * param_matrixWorld . m11 ( ) ; _x < view . width ( ) ; _x + = param_matrixWorld . m11 ( ) )
linesX . append ( QLineF ( _x , 0 , _x , view . height ( ) ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QVarLengthArray < QLineF , 30 > linesY ;
2012-06-12 22:46:12 +08:00
for ( qreal _y = offsetY * param_matrixWorld . m11 ( ) ; _y < view . height ( ) ; _y + = param_matrixWorld . m11 ( ) )
linesY . append ( QLineF ( 0 , _y , view . width ( ) , _y ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QFont f = painter - > font ( ) ;
int original_font_size = f . pointSize ( ) ;
//change font size
//f.setPointSize(4+(param_matrixWorld.m11()-threshold_zoom_img_region)/5);
f . setPixelSize ( 10 + ( param_matrixWorld . m11 ( ) - threshold_zoom_img_region ) / 5 ) ;
painter - > setFont ( f ) ;
QString val ;
QRgb rgbValue ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
QPointF point1 ; //sorry, I do not know how to name it
QPointF point2 ; //idem
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
for ( int j = - 1 ; j < height ( ) / param_matrixWorld . m11 ( ) ; j + + ) //-1 because display the pixels top rows left colums
for ( int i = - 1 ; i < width ( ) / param_matrixWorld . m11 ( ) ; i + + ) //-1
{
point1 . setX ( ( i + offsetX ) * param_matrixWorld . m11 ( ) ) ;
point1 . setY ( ( j + offsetY ) * param_matrixWorld . m11 ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
matrixWorld_inv . map ( point1 . x ( ) , point1 . y ( ) , & point2 . rx ( ) , & point2 . ry ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
point2 . rx ( ) = ( long ) ( point2 . x ( ) + 0.5 ) ;
point2 . ry ( ) = ( long ) ( point2 . y ( ) + 0.5 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( point2 . x ( ) > = 0 & & point2 . y ( ) > = 0 )
rgbValue = image2Draw_qt_resized . pixel ( QPoint ( point2 . x ( ) , point2 . y ( ) ) ) ;
else
rgbValue = qRgb ( 0 , 0 , 0 ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
if ( nbChannelOriginImage = = CV_8UC3 )
{
//for debug
/*
val = tr ( " %1 %2 " ) . arg ( point2 . x ( ) ) . arg ( point2 . y ( ) ) ;
painter - > setPen ( QPen ( Qt : : black , 1 ) ) ;
painter - > drawText ( QRect ( point1 . x ( ) , point1 . y ( ) , param_matrixWorld . m11 ( ) , param_matrixWorld . m11 ( ) / 2 ) ,
Qt : : AlignCenter , val ) ;
*/
val = tr ( " %1 " ) . arg ( qRed ( rgbValue ) ) ;
painter - > setPen ( QPen ( Qt : : red , 1 ) ) ;
painter - > drawText ( QRect ( point1 . x ( ) , point1 . y ( ) , param_matrixWorld . m11 ( ) , param_matrixWorld . m11 ( ) / 3 ) ,
Qt : : AlignCenter , val ) ;
val = tr ( " %1 " ) . arg ( qGreen ( rgbValue ) ) ;
painter - > setPen ( QPen ( Qt : : green , 1 ) ) ;
painter - > drawText ( QRect ( point1 . x ( ) , point1 . y ( ) + param_matrixWorld . m11 ( ) / 3 , param_matrixWorld . m11 ( ) , param_matrixWorld . m11 ( ) / 3 ) ,
Qt : : AlignCenter , val ) ;
val = tr ( " %1 " ) . arg ( qBlue ( rgbValue ) ) ;
painter - > setPen ( QPen ( Qt : : blue , 1 ) ) ;
painter - > drawText ( QRect ( point1 . x ( ) , point1 . y ( ) + 2 * param_matrixWorld . m11 ( ) / 3 , param_matrixWorld . m11 ( ) , param_matrixWorld . m11 ( ) / 3 ) ,
Qt : : AlignCenter , val ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
}
2010-08-20 21:06:14 +08:00
2012-06-08 01:21:29 +08:00
if ( nbChannelOriginImage = = CV_8UC1 )
{
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
val = tr ( " %1 " ) . arg ( qRed ( rgbValue ) ) ;
painter - > drawText ( QRect ( point1 . x ( ) , point1 . y ( ) , param_matrixWorld . m11 ( ) , param_matrixWorld . m11 ( ) ) ,
Qt : : AlignCenter , val ) ;
}
}
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
painter - > setPen ( QPen ( Qt : : black , 1 ) ) ;
painter - > drawLines ( linesX . data ( ) , linesX . size ( ) ) ;
painter - > drawLines ( linesY . data ( ) , linesY . size ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//restore font size
f . setPointSize ( original_font_size ) ;
painter - > setFont ( f ) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : drawViewOverview ( QPainter * painter )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QSize viewSize = size ( ) ;
viewSize . scale ( 100 , 100 , Qt : : KeepAspectRatio ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
const int margin = 5 ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//draw the image's location
painter - > setBrush ( QColor ( 0 , 0 , 0 , 127 ) ) ;
painter - > setPen ( Qt : : darkGreen ) ;
painter - > drawRect ( QRect ( width ( ) - viewSize . width ( ) - margin , 0 , viewSize . width ( ) , viewSize . height ( ) ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-08 01:21:29 +08:00
//daw the view's location inside the image
qreal ratioSize = 1 / param_matrixWorld . m11 ( ) ;
qreal ratioWindow = ( qreal ) ( viewSize . height ( ) ) / ( qreal ) ( size ( ) . height ( ) ) ;
painter - > setPen ( Qt : : darkBlue ) ;
painter - > drawRect ( QRectF ( width ( ) - viewSize . width ( ) - positionCorners . left ( ) * ratioSize * ratioWindow - margin ,
- positionCorners . top ( ) * ratioSize * ratioWindow ,
( viewSize . width ( ) - 1 ) * ratioSize ,
( viewSize . height ( ) - 1 ) * ratioSize )
) ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void DefaultViewPort : : drawInstructions ( QPainter * painter )
2010-08-18 22:07:02 +08:00
{
2012-06-08 01:21:29 +08:00
QFontMetrics metrics = QFontMetrics ( font ( ) ) ;
int border = qMax ( 4 , metrics . leading ( ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
QRect qrect = metrics . boundingRect ( 0 , 0 , width ( ) - 2 * border , int ( height ( ) * 0.125 ) ,
2012-06-08 01:21:29 +08:00
Qt : : AlignCenter | Qt : : TextWordWrap , infoText ) ;
painter - > setRenderHint ( QPainter : : TextAntialiasing ) ;
2012-06-12 22:46:12 +08:00
painter - > fillRect ( QRect ( 0 , 0 , width ( ) , qrect . height ( ) + 2 * border ) ,
2012-06-08 01:21:29 +08:00
QColor ( 0 , 0 , 0 , 127 ) ) ;
painter - > setPen ( Qt : : white ) ;
2012-06-12 22:46:12 +08:00
painter - > fillRect ( QRect ( 0 , 0 , width ( ) , qrect . height ( ) + 2 * border ) ,
2012-06-08 01:21:29 +08:00
QColor ( 0 , 0 , 0 , 127 ) ) ;
2010-08-18 22:07:02 +08:00
2012-06-12 22:46:12 +08:00
painter - > drawText ( ( width ( ) - qrect . width ( ) ) / 2 , border ,
qrect . width ( ) , qrect . height ( ) ,
2012-06-08 01:21:29 +08:00
Qt : : AlignCenter | Qt : : TextWordWrap , infoText ) ;
2010-08-18 22:07:02 +08:00
}
2012-06-29 15:46:53 +08:00
void DefaultViewPort : : setSize ( QSize /*size_*/ )
2011-12-12 20:52:06 +08:00
{
}
//////////////////////////////////////////////////////
// OpenGlViewPort
# ifdef HAVE_QT_OPENGL
2012-06-12 22:46:12 +08:00
OpenGlViewPort : : OpenGlViewPort ( QWidget * _parent ) : QGLWidget ( _parent ) , size ( - 1 , - 1 )
2011-12-12 20:52:06 +08:00
{
mouseCallback = 0 ;
mouseData = 0 ;
glDrawCallback = 0 ;
glDrawData = 0 ;
glCleanCallback = 0 ;
glCleanData = 0 ;
glFuncTab = 0 ;
}
OpenGlViewPort : : ~ OpenGlViewPort ( )
{
if ( glFuncTab )
delete glFuncTab ;
setOpenGlCleanCallback ( 0 , 0 ) ;
}
QWidget * OpenGlViewPort : : getWidget ( )
{
return this ;
}
void OpenGlViewPort : : setMouseCallBack ( CvMouseCallback callback , void * param )
{
mouseCallback = callback ;
mouseData = param ;
}
2012-06-29 15:46:53 +08:00
void OpenGlViewPort : : writeSettings ( QSettings & /*settings*/ )
2011-12-12 20:52:06 +08:00
{
}
2012-06-29 15:46:53 +08:00
void OpenGlViewPort : : readSettings ( QSettings & /*settings*/ )
2011-12-12 20:52:06 +08:00
{
}
double OpenGlViewPort : : getRatio ( )
{
return ( double ) width ( ) / height ( ) ;
}
2012-06-29 15:46:53 +08:00
void OpenGlViewPort : : setRatio ( int /*flags*/ )
2011-12-12 20:52:06 +08:00
{
}
2012-06-29 15:46:53 +08:00
void OpenGlViewPort : : updateImage ( const CvArr * /*arr*/ )
2011-12-12 20:52:06 +08:00
{
}
2012-06-29 15:46:53 +08:00
void OpenGlViewPort : : startDisplayInfo ( QString /*text*/ , int /*delayms*/ )
2011-12-12 20:52:06 +08:00
{
}
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
2011-12-12 21:57:37 +08:00
GlFuncTab_QT ( ) ;
2011-12-12 20:52:06 +08:00
# 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 ;
2012-06-08 01:21:29 +08:00
2011-12-12 20:52:06 +08:00
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 " ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
glDeleteBuffersExt ( n , buffers ) ;
CV_CheckGlError ( ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
__END__ ;
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void GlFuncTab_QT : : bufferData ( unsigned int target , ptrdiff_t size , const void * data , unsigned int usage ) const
{
CV_FUNCNAME ( " GlFuncTab_QT::bufferData " ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
__BEGIN__ ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
if ( ! glBufferDataExt )
CV_ERROR ( CV_OpenGlApiCallError , " Current OpenGL implementation doesn't support required extension " ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
glBufferDataExt ( target , size , data , usage ) ;
CV_CheckGlError ( ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
__END__ ;
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void GlFuncTab_QT : : bufferSubData ( unsigned int target , ptrdiff_t offset , ptrdiff_t size , const void * data ) const
{
CV_FUNCNAME ( " GlFuncTab_QT::bufferSubData " ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
__BEGIN__ ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
if ( ! glBufferSubDataExt )
CV_ERROR ( CV_OpenGlApiCallError , " Current OpenGL implementation doesn't support required extension " ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
glBufferSubDataExt ( target , offset , size , data ) ;
CV_CheckGlError ( ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
__END__ ;
}
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void GlFuncTab_QT : : bindBuffer ( unsigned int target , unsigned int buffer ) const
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
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__ ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void * GlFuncTab_QT : : mapBuffer ( unsigned int target , unsigned int access ) const
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
CV_FUNCNAME ( " GlFuncTab_QT::mapBuffer " ) ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
void * res = 0 ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
__BEGIN__ ;
2010-08-18 22:07:02 +08:00
2011-12-12 20:52:06 +08:00
if ( ! glMapBufferExt )
CV_ERROR ( CV_OpenGlApiCallError , " Current OpenGL implementation doesn't support required extension " ) ;
res = glMapBufferExt ( target , access ) ;
CV_CheckGlError ( ) ;
__END__ ;
return res ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
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__ ;
}
2010-08-18 22:07:02 +08:00
2012-06-29 15:46:53 +08:00
void GlFuncTab_QT : : generateBitmapFont ( const std : : string & family , int height , int weight , bool italic , bool /*underline*/ , int start , int count , int base ) const
2010-08-18 22:07:02 +08:00
{
2012-07-02 22:20:59 +08:00
# ifdef Q_WS_WIN
CV_FUNCNAME ( " GlFuncTab_QT::generateBitmapFont " ) ;
# endif
2011-12-12 20:52:06 +08:00
QFont font ( QString ( family . c_str ( ) ) , height , weight , italic ) ;
__BEGIN__ ;
# ifndef Q_WS_WIN
2011-12-13 14:25:37 +08:00
font . setStyleStrategy ( QFont : : OpenGLCompatible ) ;
if ( font . handle ( ) )
glXUseXFont ( font . handle ( ) , start , count , base ) ;
2011-12-12 20:52:06 +08:00
# else
SelectObject ( hDC , font . handle ( ) ) ;
if ( ! wglUseFontBitmaps ( hDC , start , count , base ) )
CV_ERROR ( CV_OpenGlApiCallError , " Can't create font " ) ;
# endif
__END__ ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
bool GlFuncTab_QT : : isGlContextInitialized ( ) const
2010-08-18 22:07:02 +08:00
{
2011-12-12 20:52:06 +08:00
return initialized ;
2010-08-18 22:07:02 +08:00
}
2011-12-12 20:52:06 +08:00
void OpenGlViewPort : : initializeGL ( )
{
glHint ( GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST ) ;
# ifdef Q_WS_WIN
2012-06-12 22:46:12 +08:00
std : : auto_ptr < GlFuncTab_QT > qglFuncTab ( new GlFuncTab_QT ( getDC ( ) ) ) ;
2011-12-12 20:52:06 +08:00
# else
2012-06-12 22:46:12 +08:00
std : : auto_ptr < GlFuncTab_QT > qglFuncTab ( new GlFuncTab_QT ) ;
2010-08-18 22:07:02 +08:00
# endif
2011-12-12 20:52:06 +08:00
// Load extensions
2012-06-12 22:46:12 +08:00
qglFuncTab - > glGenBuffersExt = ( PFNGLGENBUFFERSPROC ) context ( ) - > getProcAddress ( " glGenBuffers " ) ;
qglFuncTab - > glDeleteBuffersExt = ( PFNGLDELETEBUFFERSPROC ) context ( ) - > getProcAddress ( " glDeleteBuffers " ) ;
qglFuncTab - > glBufferDataExt = ( PFNGLBUFFERDATAPROC ) context ( ) - > getProcAddress ( " glBufferData " ) ;
qglFuncTab - > glBufferSubDataExt = ( PFNGLBUFFERSUBDATAPROC ) context ( ) - > getProcAddress ( " glBufferSubData " ) ;
qglFuncTab - > glBindBufferExt = ( PFNGLBINDBUFFERPROC ) context ( ) - > getProcAddress ( " glBindBuffer " ) ;
qglFuncTab - > glMapBufferExt = ( PFNGLMAPBUFFERPROC ) context ( ) - > getProcAddress ( " glMapBuffer " ) ;
qglFuncTab - > glUnmapBufferExt = ( PFNGLUNMAPBUFFERPROC ) context ( ) - > getProcAddress ( " glUnmapBuffer " ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
qglFuncTab - > initialized = true ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
glFuncTab = qglFuncTab . release ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
icvSetOpenGlFuncTab ( glFuncTab ) ;
2011-12-12 20:52:06 +08:00
}
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 ( ) ;
}
2012-06-12 22:46:12 +08:00
void OpenGlViewPort : : mousePressEvent ( QMouseEvent * evnt )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = - 1 , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_down , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
QGLWidget : : mousePressEvent ( evnt ) ;
2011-12-12 20:52:06 +08:00
}
2012-06-12 22:46:12 +08:00
void OpenGlViewPort : : mouseReleaseEvent ( QMouseEvent * evnt )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = - 1 , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_up , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
QGLWidget : : mouseReleaseEvent ( evnt ) ;
2011-12-12 20:52:06 +08:00
}
2012-06-12 22:46:12 +08:00
void OpenGlViewPort : : mouseDoubleClickEvent ( QMouseEvent * evnt )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = - 1 , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_dbclick , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
QGLWidget : : mouseDoubleClickEvent ( evnt ) ;
2011-12-12 20:52:06 +08:00
}
2012-06-12 22:46:12 +08:00
void OpenGlViewPort : : mouseMoveEvent ( QMouseEvent * evnt )
2011-12-12 20:52:06 +08:00
{
2012-06-08 01:21:29 +08:00
int cv_event = CV_EVENT_MOUSEMOVE , flags = 0 ;
2012-06-12 22:46:12 +08:00
QPoint pt = evnt - > pos ( ) ;
2011-12-12 20:52:06 +08:00
2012-06-08 01:21:29 +08:00
//icvmouseHandler: pass parameters for cv_event, flags
2012-06-12 22:46:12 +08:00
icvmouseHandler ( evnt , mouse_move , cv_event , flags ) ;
2012-06-08 01:21:29 +08:00
icvmouseProcessing ( QPointF ( pt ) , cv_event , flags ) ;
2011-12-12 20:52:06 +08:00
2012-06-12 22:46:12 +08:00
QGLWidget : : mouseMoveEvent ( evnt ) ;
2011-12-12 20:52:06 +08:00
}
2012-06-12 22:46:12 +08:00
void OpenGlViewPort : : icvmouseHandler ( QMouseEvent * evnt , type_mouse_event category , int & cv_event , int & flags )
2011-12-12 20:52:06 +08:00
{
2012-06-12 22:46:12 +08:00
Qt : : KeyboardModifiers modifiers = evnt - > modifiers ( ) ;
Qt : : MouseButtons buttons = evnt - > buttons ( ) ;
2012-06-08 01:21:29 +08:00
2011-12-12 20:52:06 +08:00
flags = 0 ;
if ( modifiers & Qt : : ShiftModifier )
2012-06-08 01:21:29 +08:00
flags | = CV_EVENT_FLAG_SHIFTKEY ;
if ( modifiers & Qt : : ControlModifier )
flags | = CV_EVENT_FLAG_CTRLKEY ;
if ( modifiers & Qt : : AltModifier )
flags | = CV_EVENT_FLAG_ALTKEY ;
2011-12-12 20:52:06 +08:00
if ( buttons & Qt : : LeftButton )
2012-06-08 01:21:29 +08:00
flags | = CV_EVENT_FLAG_LBUTTON ;
if ( buttons & Qt : : RightButton )
flags | = CV_EVENT_FLAG_RBUTTON ;
2011-12-12 20:52:06 +08:00
if ( buttons & Qt : : MidButton )
2012-06-08 01:21:29 +08:00
flags | = CV_EVENT_FLAG_MBUTTON ;
2011-12-12 20:52:06 +08:00
cv_event = CV_EVENT_MOUSEMOVE ;
2012-06-12 22:46:12 +08:00
switch ( evnt - > button ( ) )
2012-06-08 01:21:29 +08:00
{
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 :
2011-12-12 20:52:06 +08:00
;
2012-06-08 01:21:29 +08:00
}
2011-12-12 20:52:06 +08:00
}
void OpenGlViewPort : : icvmouseProcessing ( QPointF pt , int cv_event , int flags )
{
2012-06-08 01:21:29 +08:00
if ( mouseCallback )
mouseCallback ( cv_event , pt . x ( ) , pt . y ( ) , flags , mouseData ) ;
2011-12-12 20:52:06 +08:00
}
QSize OpenGlViewPort : : sizeHint ( ) const
{
if ( size . width ( ) > 0 & & size . height ( ) > 0 )
return size ;
return QGLWidget : : sizeHint ( ) ;
}
void OpenGlViewPort : : setSize ( QSize size_ )
{
size = size_ ;
updateGeometry ( ) ;
}
2010-08-18 22:07:02 +08:00
# endif
2011-12-12 20:52:06 +08:00
# endif // HAVE_QT