QT new functions:

- cvAddButton done
 - All the "actions" of the toolbar are working
 - Window properties done
pull/13383/head
Yannick Verdie 15 years ago
parent 3a3745daac
commit 14bf624af4
  1. 3
      modules/highgui/include/opencv2/highgui/highgui.hpp
  2. 3
      modules/highgui/include/opencv2/highgui/highgui_c.h
  3. 74
      modules/highgui/src/window.cpp
  4. 214
      modules/highgui/src/window_QT.cpp
  5. 52
      modules/highgui/src/window_QT.h

@ -78,6 +78,9 @@ CV_EXPORTS void saveWindowParameters(const string& windowName);
CV_EXPORTS void loadWindowParameters(const string& windowName); CV_EXPORTS void loadWindowParameters(const string& windowName);
CV_EXPORTS int startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]); CV_EXPORTS int startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]);
CV_EXPORTS void stopLoop(); CV_EXPORTS void stopLoop();
typedef void (CV_CDECL *ButtonCallback)(void* userdata);
CV_EXPORTS int createButton( const char* bar_name, const char* window_name, ButtonCallback on_change, const char* button_name CV_DEFAULT(NULL), void* userdata CV_DEFAULT(NULL));
//------------------------- //-------------------------
CV_EXPORTS void imshow( const string& winname, const Mat& mat ); CV_EXPORTS void imshow( const string& winname, const Mat& mat );

@ -93,6 +93,9 @@ CVAPI(void) cvSaveWindowParameters(const char* name);
CVAPI(void) cvLoadWindowParameters(const char* name); CVAPI(void) cvLoadWindowParameters(const char* name);
CVAPI(int) cvStartLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]); CVAPI(int) cvStartLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]);
CVAPI(void) cvStopLoop(); CVAPI(void) cvStopLoop();
typedef void (CV_CDECL *CvButtonCallback)(void* userdata);
CVAPI(int) cvCreateButton( const char* bar_name, const char* window_name, CvButtonCallback on_change, const char* button_name CV_DEFAULT(NULL), void* userdata CV_DEFAULT(NULL));
//---------------------- //----------------------

@ -231,6 +231,11 @@ void loadWindowParameters(const string& windowName)
cvLoadWindowParameters(windowName.c_str()); cvLoadWindowParameters(windowName.c_str());
} }
int createButton( const string& bar_name, const string& window_name, ButtonCallback on_change, const string& button_name, void* userdata)
{
return cvCreateButton( bar_name.c_str(), window_name.c_str(), on_change, button_name.c_str(), userdata);
}
#endif #endif
} }
@ -265,12 +270,6 @@ CV_IMPL int cvNamedWindow( const char*, int )
return -1; return -1;
} }
CV_IMPL void cvDisplayOverlay(const char* name, const char* text, int delayms)
{
CV_NO_GUI_ERROR("cvNamedWindow");
return -1;
}
CV_IMPL void cvDestroyWindow( const char* ) CV_IMPL void cvDestroyWindow( const char* )
{ {
CV_NO_GUI_ERROR( "cvDestroyWindow" ); CV_NO_GUI_ERROR( "cvDestroyWindow" );
@ -364,6 +363,69 @@ CV_IMPL int cvStartWindowThread()
return -1; return -1;
} }
//-------- Qt ---------
CV_IMPL void cvAddText( const CvArr*, const char*, CvPoint org, CvFont font)
{
CV_NO_GUI_ERROR("cvAddText");
return -1;
}
CV_IMPL void cvDisplayStatusBar(const char* name, const char* arg2, int arg3)
{
CV_NO_GUI_ERROR("cvDisplayStatusBar");
return -1;
}
CV_IMPL void cvDisplayStatusBar(const char* name, const char* arg2, int arg3)
{
CV_NO_GUI_ERROR("cvDisplayStatusBar");
return -1;
}
CV_IMPL void cvCreateOpenGLCallback(const char* name, cvOpenGLCallback callback, void* param)
{
CV_NO_GUI_ERROR("createOpenGLCallback");
return -1;
}
CV_IMPL void cvDisplayOverlay(const char* name, const char* text, int delayms)
{
CV_NO_GUI_ERROR("cvNamedWindow");
return -1;
}
CV_IMPL void cvStartLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[])
{
CV_NO_GUI_ERROR("cvStartLoop");
return -1;
}
CV_IMPL void cvStopLoop()
{
CV_NO_GUI_ERROR("cvStopLoop");
return -1;
}
CV_IMPL void cvSaveWindowParameters(const char* name)
{
CV_NO_GUI_ERROR("cvSaveWindowParameters");
return -1;
}
CV_IMPL void cvLoadWindowParameterss(const char* name)
{
CV_NO_GUI_ERROR("cvLoadWindowParameters");
return -1;
}
CV_IMPL void cvCreateButton(const char* arg1, const char* arg2, cvButtonCallback on_change, const char* name, void* userdata)
{
CV_NO_GUI_ERROR("cvCreateButton");
return -1;
}
#endif #endif
/* End of file. */ /* End of file. */

@ -197,6 +197,8 @@ CV_IMPL void cvDisplayStatusBar(const char* name, const char* text, int delayms)
} }
CV_IMPL int cvInitSystem( int, char** ) CV_IMPL int cvInitSystem( int, char** )
{ {
return 0; return 0;
@ -292,7 +294,7 @@ CV_IMPL void cvStopLoop()
} }
CV_IMPL CvWindow* icvFindWindowByName( const char* arg ) CvWindow* icvFindWindowByName( const char* arg )
{ {
QPointer<CvWindow> window = NULL; QPointer<CvWindow> window = NULL;
@ -329,11 +331,11 @@ CvTrackbar* icvFindTrackbarByName( const char* name_trackbar, const char* name_w
if( !w ) if( !w )
CV_Error( CV_StsNullPtr, "NULL window handler" ); CV_Error( CV_StsNullPtr, "NULL window handler" );
QString nameQt = QString(name_trackbar); QString nameQt(name_trackbar);
QPointer<CvTrackbar> t; CvBar* t;
int start_index; int start_index = 0;
int stop_index; int stop_index = 0;
QPointer<QLayout> myLayout; QPointer<QLayout> myLayout;
if (w->param_gui_mode == CV_GUI_NORMAL) if (w->param_gui_mode == CV_GUI_NORMAL)
@ -352,26 +354,30 @@ CvTrackbar* icvFindTrackbarByName( const char* name_trackbar, const char* name_w
if (w->param_gui_mode == CV_GUI_EXPANDED) if (w->param_gui_mode == CV_GUI_EXPANDED)
{ {
myLayout = w->getWinProp()->myLayout; myLayout = w->getWinProp()->myLayout;
if (!myLayout)
CV_Error( CV_StsNullPtr, "NULL window prop handler" );
start_index = 0; start_index = 0;
stop_index = myLayout->layout()->count(); stop_index = myLayout->layout()->count();
} }
for (int i = start_index; i < stop_index; ++i) for (int i = start_index; i < stop_index; ++i)
{ {
t = (CvTrackbar*) myLayout->layout()->itemAt(i); t = (CvBar*) myLayout->layout()->itemAt(i);
if (t->trackbar_name==nameQt) if (t->type == type_CvTrackbar && t->name_bar == nameQt)
{ {
result = t; result = (CvTrackbar*) t;
break; break;
} }
} }
return result; return result;
} }
/* CvButtonbar* icvFindButtonbarByName( const char* name_Buttonbar, const char* name_window )
CvTrackbar* icvFindButtonbarByName( const char* name_Buttonbar, const char* name_window )
{ {
QPointer<CvButtonbar> result = NULL; QPointer<CvButtonbar> result = NULL;
@ -382,25 +388,35 @@ CvTrackbar* icvFindButtonbarByName( const char* name_Buttonbar, const char* name
CV_Error( CV_StsNullPtr, "NULL window handler" ); CV_Error( CV_StsNullPtr, "NULL window handler" );
QString nameQt = QString(name_Buttonbar); QString nameQt = QString(name_Buttonbar);
QPointer<CvButtonbar> t; CvBar* t;
int start_index;
int stop_index;
QPointer<QLayout> myLayout;
int start_index = 1; if (w->param_gui_mode == CV_GUI_EXPANDED)
if (w->myToolBar)
start_index = 2;
//Warning ---- , asume the location 0 is toolbar, 1 is myview and max-1 the status bar
//done three times in the code, in loadtrackbars, savetrackbar and in findtrackbar
for (int i = start_index; i < w->myLayout->layout()->count()-1; ++i)
{ {
t = (CvTrackbar*) w->myLayout->layout()->itemAt(i); myLayout = w->getWinProp()->myLayout;
if (t->trackbar_name==nameQt) if (!myLayout)
CV_Error( CV_StsNullPtr, "NULL window prop handler" );
start_index = 0;
stop_index = myLayout->layout()->count();
for (int i = start_index; i < stop_index; ++i)
{
t = (CvBar*) myLayout->layout()->itemAt(i);
if (t->type == type_CvButtonbar && t->name_bar == nameQt)
{ {
result = t; result = (CvButtonbar*) t;
break; break;
} }
} }
}
return result; return result;
}*/ }
int icvInitSystem() int icvInitSystem()
{ {
@ -508,9 +524,9 @@ CV_IMPL void cvResizeWindow(const char* name, int width, int height )
} }
//TODO: implement the real one, not a wrapper //TODO: implement the real one, not a wrapper
CV_IMPL int cvCreateTrackbar2( const char* trackbar_name, const char* window_name, int* val, int count, CvTrackbarCallback2 on_notify, void* userdata ) CV_IMPL int cvCreateTrackbar2( const char* name_bar, const char* window_name, int* val, int count, CvTrackbarCallback2 on_notify, void* userdata )
{ {
return cvCreateTrackbar( trackbar_name, window_name, val, count, (CvTrackbarCallback)on_notify ); return cvCreateTrackbar( name_bar, window_name, val, count, (CvTrackbarCallback)on_notify );
} }
CV_IMPL int cvStartWindowThread() CV_IMPL int cvStartWindowThread()
@ -518,21 +534,33 @@ CV_IMPL int cvStartWindowThread()
return 0; return 0;
} }
CV_IMPL int cvCreateTrackbar( const char* trackbar_name, const char* window_name, int* value, int count, CvTrackbarCallback on_change) CV_IMPL int cvCreateTrackbar( const char* name_bar, const char* window_name, int* value, int count, CvTrackbarCallback on_change)
{ {
if (multiThreads)
QMetaObject::invokeMethod(&guiMainThread, QMetaObject::invokeMethod(&guiMainThread,
"addSlider", "addSlider",
Qt::AutoConnection, Qt::AutoConnection,
Q_ARG(QString, QString(trackbar_name)), Q_ARG(QString, QString(name_bar)),
Q_ARG(QString, QString(window_name)), Q_ARG(QString, QString(window_name)),
Q_ARG(void*, (void*)value), Q_ARG(void*, (void*)value),
Q_ARG(int, count), Q_ARG(int, count),
Q_ARG(void*, (void*)on_change) Q_ARG(void*, (void*)on_change)
); );
else
guiMainThread.addSlider(QString(trackbar_name),QString(window_name),(void*)value,count,(void*)on_change); return 1;//dummy value
}
CV_IMPL int cvCreateButton( const char* bar_name, const char* window_name, CvButtonCallback on_change, const char* button_name , void* userdata )
{
QMetaObject::invokeMethod(&guiMainThread,
"addButton",
Qt::AutoConnection,
Q_ARG(QString, QString(window_name)),
Q_ARG(QString, QString(bar_name)),
Q_ARG(QString, QString(button_name)),
Q_ARG(void*, (void*)on_change),
Q_ARG(void*, userdata)
);
return 1;//dummy value return 1;//dummy value
} }
@ -548,11 +576,11 @@ CV_IMPL void cvCreateOpenGLCallback( const char* window_name, CvOpenGLCallback c
); );
} }
CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name ) CV_IMPL int cvGetTrackbarPos( const char* name_bar, const char* window_name )
{ {
int result = -1; int result = -1;
QPointer<CvTrackbar> t = icvFindTrackbarByName( trackbar_name, window_name ); QPointer<CvTrackbar> t = icvFindTrackbarByName( name_bar, window_name );
if (t) if (t)
result = t->slider->value(); result = t->slider->value();
@ -560,10 +588,10 @@ CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name
return result; return result;
} }
CV_IMPL void cvSetTrackbarPos( const char* trackbar_name, const char* window_name, int pos ) CV_IMPL void cvSetTrackbarPos( const char* name_bar, const char* window_name, int pos )
{ {
QPointer<CvTrackbar> t = icvFindTrackbarByName( trackbar_name, window_name ); QPointer<CvTrackbar> t = icvFindTrackbarByName( name_bar, window_name );
if (t) if (t)
t->slider->setValue(pos); t->slider->setValue(pos);
@ -872,20 +900,46 @@ void GuiReceiver::resizeWindow(QString name, int width, int height)
w->resize(width, height); w->resize(width, height);
} }
void GuiReceiver::addSlider(QString trackbar_name, QString window_name, void* value, int count, void* on_change) void GuiReceiver::addButton(QString window_name, QString bar_name, QString button_name, void* on_change, void* userdata)
{ {
QPointer<CvWindow> w = icvFindWindowByName( window_name.toLatin1().data() ); QPointer<CvWindow> w = icvFindWindowByName( window_name.toLatin1().data() );
if (!w) if (!w)
return; return;
if (!on_change)
CV_Error(CV_StsNullPtr, "Callback is NULL");
QPointer<CvButtonbar> b = icvFindButtonbarByName( bar_name.toLatin1().data() ,window_name.toLatin1().data() );
if (!b)//if the buttonbar does not exist, create it THEN, attached a new button
b = w->createButtonbar(bar_name);
if (b)
b->addButton( button_name,(CvButtonCallback) on_change, userdata);
}
void GuiReceiver::addSlider(QString bar_name, QString window_name, void* value, int count, void* on_change)
{
QPointer<CvWindow> w = icvFindWindowByName( window_name.toLatin1().data() );
if (!w)
return;
QPointer<CvTrackbar> t = icvFindTrackbarByName( bar_name.toLatin1().data() , window_name.toLatin1().data() );
if (t)//trackbar exists
return;
if (!value) if (!value)
CV_Error(CV_StsNullPtr, "NULL value pointer" ); CV_Error(CV_StsNullPtr, "NULL value pointer" );
if (count<= 0)//count is the max value of the slider, so must be bigger than 0 if (count<= 0)//count is the max value of the slider, so must be bigger than 0
CV_Error(CV_StsNullPtr, "Max value of the slider must be bigger than 0" ); CV_Error(CV_StsNullPtr, "Max value of the slider must be bigger than 0" );
w->addSlider(trackbar_name,(int*)value,count,(CvTrackbarCallback) on_change); w->addSlider(bar_name,(int*)value,count,(CvTrackbarCallback) on_change);
} }
int GuiReceiver::start() int GuiReceiver::start()
@ -893,11 +947,13 @@ int GuiReceiver::start()
return qApp->exec(); return qApp->exec();
} }
CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTrackbarCallback on_change ) CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTrackbarCallback on_change )
{ {
setObjectName(trackbar_name); type=type_CvTrackbar;
parent = arg; myparent = arg;
trackbar_name = name; name_bar = name;
setObjectName(name_bar);
dataSlider = value; dataSlider = value;
callback = on_change; callback = on_change;
@ -934,8 +990,8 @@ CvTrackbar::CvTrackbar(CvWindow* arg, QString name, int* value, int count, CvTra
//label->setStyleSheet("QPushButton:disabled {color: black}"); //label->setStyleSheet("QPushButton:disabled {color: black}");
addWidget(label);//name + value addWidget(label,Qt::AlignLeft);//name + value
addWidget(slider);//slider addWidget(slider,Qt::AlignCenter);//slider
} }
void CvTrackbar::createDialog() void CvTrackbar::createDialog()
@ -956,7 +1012,7 @@ void CvTrackbar::createDialog()
QInputDialog::getInteger QInputDialog::getInteger
#endif #endif
(this->parentWidget(), (this->parentWidget(),
tr("Slider %1").arg(trackbar_name), tr("Slider %1").arg(name_bar),
tr("New value:"), tr("New value:"),
value, value,
min, min,
@ -980,7 +1036,7 @@ void CvTrackbar::update(int myvalue)
void CvTrackbar::setLabel(int myvalue) void CvTrackbar::setLabel(int myvalue)
{ {
QString nameNormalized = trackbar_name.leftJustified( 10, ' ', true ); QString nameNormalized = name_bar.leftJustified( 10, ' ', true );
QString valueMaximum = QString("%1").arg(slider->maximum()); QString valueMaximum = QString("%1").arg(slider->maximum());
QString str = QString("%1 (%2/%3)").arg(nameNormalized).arg(myvalue,valueMaximum.length(),10,QChar('0')).arg(valueMaximum); QString str = QString("%1 (%2/%3)").arg(nameNormalized).arg(myvalue,valueMaximum.length(),10,QChar('0')).arg(valueMaximum);
label->setText(str); label->setText(str);
@ -994,16 +1050,63 @@ CvTrackbar::~CvTrackbar()
//here CvButtonbar class //here CvButtonbar class
/* CvButtonbar::CvButtonbar(CvWindow* arg, QString arg2)
CvButtonbar::CvButtonbar()
{ {
type=type_CvButtonbar;
myparent = arg;
name_bar = arg2;
setObjectName(name_bar);
/*
label = new QLabel;
setLabel();
addWidget(label,Qt::AlignLeft );
*/
}
CvButtonbar::~CvButtonbar()
{
QLayoutItem *child;
while ((child = takeAt(0)) != 0)
delete child;
}
void CvButtonbar::setLabel()
{
QString nameNormalized = name_bar.leftJustified( 10, ' ', true );
label->setText(nameNormalized);
} }
*/
void CvButtonbar::addButton( QString name, CvButtonCallback call, void* userdata)
{
CvButton* button = new CvButton(this, name,call, userdata);
QString button_name = name;
if (button_name == "")
button_name = tr("button %1").arg(this->count());
button->setText(button_name);
QObject::connect( button, SIGNAL( clicked() ),button, SLOT( callCallBack() ));
this->addWidget(button,Qt::AlignCenter);
}
CvButton::CvButton(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4)
{
myparent = arg1;
button_name = arg2;
callback = arg3;
userdata=arg4;
}
void CvButton::callCallBack()
{
callback(userdata);
}
//here CvWinProperties class //here CvWinProperties class
CvWinProperties::CvWinProperties(QString name_paraWindow, QWidget* parent) CvWinProperties::CvWinProperties(QString name_paraWindow, QWidget* parent)
@ -1164,6 +1267,23 @@ CvWindow::~CvWindow()
delete vect_QShortcuts[i]; delete vect_QShortcuts[i];
} }
CvButtonbar* CvWindow::createButtonbar(QString name_bar)
{
QPointer<CvButtonbar> t;
if (param_gui_mode == CV_GUI_EXPANDED)
{
t = new CvButtonbar(this,name_bar);
t->setAlignment(Qt::AlignHCenter);
parameters_window->myLayout->insertLayout(parameters_window->myLayout->count(),t);
}
return t;
}
CvWinProperties* CvWindow::getWinProp() CvWinProperties* CvWindow::getWinProp()
{ {
return parameters_window; return parameters_window;
@ -1486,7 +1606,8 @@ void CvWindow::icvLoadTrackbars(QSettings *settings)
settings->setArrayIndex(i-start_index); settings->setArrayIndex(i-start_index);
t = (CvTrackbar*) myLayout->layout()->itemAt(i); t = (CvTrackbar*) myLayout->layout()->itemAt(i);
if (t->trackbar_name == settings->value("name").toString()) if (t->name_bar == settings->value("name").toString())
//if (t->getName() == settings->value("name").toString())
{ {
t->slider->setValue(settings->value("value").toInt()); t->slider->setValue(settings->value("value").toInt());
} }
@ -1510,7 +1631,8 @@ void CvWindow::icvSaveTrackbars(QSettings *settings)
for (int i = start_index; i < myLayout->layout()->count()-1; ++i) { for (int i = start_index; i < myLayout->layout()->count()-1; ++i) {
t = (CvTrackbar*) myLayout->layout()->itemAt(i); t = (CvTrackbar*) myLayout->layout()->itemAt(i);
settings->setArrayIndex(i-start_index); settings->setArrayIndex(i-start_index);
settings->setValue("name", t->trackbar_name); settings->setValue("name", t->name_bar);
//settings->setValue("name", t->getName());
settings->setValue("value", t->slider->value()); settings->setValue("value", t->slider->value());
} }
settings->endArray(); settings->endArray();

@ -96,6 +96,7 @@ enum { shortcut_zoom_normal = Qt::CTRL + Qt::Key_Z,
class CvWindow; class CvWindow;
class ViewPort; class ViewPort;
class GuiReceiver : public QObject class GuiReceiver : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -129,28 +130,60 @@ public slots:
void loadWindowParameters(QString name); void loadWindowParameters(QString name);
void setOpenGLCallback(QString window_name, void* callbackOpenGL, void* userdata); void setOpenGLCallback(QString window_name, void* callbackOpenGL, void* userdata);
void putText(void* arg1, QString text, QPoint org, void* font); void putText(void* arg1, QString text, QPoint org, void* font);
void addButton(QString window_name, QString bar_name, QString button_name, void* on_change, void* userdata);
};
enum typeBar{type_CvTrackbar = 0, type_CvButtonbar = 1};
class CvBar : public QHBoxLayout
{
public:
typeBar type;
QString name_bar;
QPointer<CvWindow> myparent;
}; };
/*
protected:
int width, height;
class CvButtonbar : public QButtonGroup class CvButtonbar : public CvBar
{ {
Q_OBJECT Q_OBJECT
public: public:
CvButtonbar(); CvButtonbar(CvWindow* arg, QString bar_name);
};*/ ~CvButtonbar();
void addButton( QString button_name, CvButtonCallback call, void* userdata);
class CvTrackbar : public QHBoxLayout private:
void setLabel();
QPointer<QLabel> label;
};
class CvButton : public QPushButton
{
Q_OBJECT
public:
CvButton(CvButtonbar* par, QString button_name, CvButtonCallback call, void* userdata);
private:
CvButtonbar* myparent;
QString button_name ;
CvButtonCallback callback;
void* userdata;
private slots:
void callCallBack();
};
class CvTrackbar : public CvBar
{ {
Q_OBJECT Q_OBJECT
public: public:
CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback on_change = NULL); CvTrackbar(CvWindow* parent, QString name, int* value, int count, CvTrackbarCallback on_change = NULL);
~CvTrackbar(); ~CvTrackbar();
QString trackbar_name; //QString trackbar_name;
QPointer<QSlider> slider; QPointer<QSlider> slider;
private slots: private slots:
@ -159,11 +192,9 @@ private slots:
private: private:
void setLabel(int myvalue); void setLabel(int myvalue);
QString createLabel(); QString createLabel();
QPointer<QPushButton > label; QPointer<QPushButton > label;
CvTrackbarCallback callback; CvTrackbarCallback callback;
QPointer<CvWindow> parent;
int* dataSlider; int* dataSlider;
}; };
@ -198,6 +229,7 @@ public:
void setOpenGLCallback(CvOpenGLCallback arg1,void* userdata); void setOpenGLCallback(CvOpenGLCallback arg1,void* userdata);
void hideTools(); void hideTools();
void showTools(); void showTools();
CvButtonbar* createButtonbar(QString bar_name);

Loading…
Cancel
Save