CV_IMPL int cvWaitKey( int arg )
{
+
int result = -1;
+ if (!guiMainThread)
+ return result;
+
unsigned long delayms;//in milliseconds
if (arg<=0)
delayms = ULONG_MAX;
//cannot use wait here because events will not be distributed before processEvents (the main eventLoop is broken)
//so I create a Thread for the QTimer
- QTimer timer(guiMainThread);
- QObject::connect(&timer, SIGNAL(timeout()), guiMainThread, SLOT(timeOut()));
- timer.setSingleShot(true);
-
if (arg>0)
- timer.start(arg);
+ guiMainThread->timer->start(arg);
//QMutex dummy;
{
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;
- timer.stop();
+ guiMainThread->timer->stop();
//printf("keypressed\n");
}
mutexKey.unlock();
waitCondition.wait(&dummy, 2);
*/
+ //to decrease CPU usage
+ //sleep 1 millisecond
#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
- Sleep(2);
+ Sleep(1);
#else
- usleep(2);//to decrease CPU usage
+ usleep(1000);
#endif
}
CV_IMPL void cvStopLoop()
{
+
qApp->exit();
}
CvWindow* icvFindWindowByName( const char* arg )
{
- QPointer<CvWindow> window = NULL;
+ QPointer<CvWindow> window;
if( !arg )
CV_Error( CV_StsNullPtr, "NULL name string" );
QString name(arg);
- QPointer<CvWindow> w;
+ CvWindow* w;
+
foreach (QWidget *widget, QApplication::topLevelWidgets())
{
}
}
+
return window;
}
);
}
+
+
CV_IMPL void cvDestroyAllWindows(void)
{
if (!guiMainThread)
//----------OBJECT----------------
-GuiReceiver::GuiReceiver() : _bTimeOut(false)
+GuiReceiver::GuiReceiver() : _bTimeOut(false), nb_windows(0)
{
icvInitSystem();
- //qApp->setQuitOnLastWindowClosed ( false );//maybe the user would like to access this setting
+
+ timer = new QTimer;
+ QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timeOut()));
+ timer->setSingleShot(true);
+}
+
+
+void GuiReceiver::isLastWindow()
+{
+ if (--nb_windows <= 0)
+ {
+ delete guiMainThread;
+ guiMainThread = NULL;
+ qApp->quit();
+ }
+}
+
+GuiReceiver::~GuiReceiver()
+{
+ delete timer;
}
void GuiReceiver::putText(void* arg1, QString text, QPoint org, void* arg2)
}
//QPointer<CvWindow> w1 =
+ nb_windows++;
new CvWindow(name, flags);
}
{
qApp->closeAllWindows();
}else{
- QPointer<CvWindow> w;
- foreach (QWidget *widget, QApplication::topLevelWidgets())
+
+ foreach (QObject *obj, QApplication::topLevelWidgets())
{
- w = (CvWindow*) widget;
- w->close();
- delete w;
+ if (obj->metaObject ()->className () == "CvWindow")
+ {
+ delete obj;
+ }
}
+
}
}
QPointer<QAbstractButton> button;
if (button_type == CV_PUSH_BUTTON)
- //CvPushButton*
button = (QAbstractButton*) new CvPushButton(this, button_name,call, userdata);
if (button_type == CV_CHECKBOX)
- //CvCheckButton*
button = (QAbstractButton*) new CvCheckBox(this, button_name,call, userdata, initial_button_state);
if (button_type == CV_RADIOBOX)
{
- //CvCheckButton*
button = (QAbstractButton*) new CvRadioButton(this, button_name,call, userdata, initial_button_state);
group_button->addButton(button);
}
if (button)
{
- QObject::connect( button, SIGNAL( clicked() ),button, SLOT( callCallBack() ));
+ QObject::connect( button, SIGNAL( toggled(bool) ),button, SLOT( callCallBack(bool) ));
addWidget(button,Qt::AlignCenter);
}
}
setObjectName(button_name);
setText(button_name);
+
+ if (isChecked())
+ callCallBack(true);
}
-void CvPushButton::callCallBack()
+void CvPushButton::callCallBack(bool checked)
{
if (callback)
- callback(-1,userdata);
+ callback(checked,userdata);
}
CvCheckBox::CvCheckBox(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4, int initial_button_state)
setObjectName(button_name);
setCheckState((initial_button_state == 1?Qt::Checked:Qt::Unchecked));
setText(button_name);
+
+ if (isChecked())
+ callCallBack(true);
}
-void CvCheckBox::callCallBack()
+void CvCheckBox::callCallBack(bool checked)
{
if (callback)
- callback(this->isChecked(),userdata);
+ callback(checked,userdata);
}
CvRadioButton::CvRadioButton(CvButtonbar* arg1, QString arg2, CvButtonCallback arg3, void* arg4, int initial_button_state)
setObjectName(button_name);
setChecked(initial_button_state);
setText(button_name);
+
+ if (isChecked())
+ callCallBack(true);
}
-void CvRadioButton::callCallBack()
+void CvRadioButton::callCallBack(bool checked)
{
if (callback)
- callback(this->isChecked(),userdata);
+ callback(checked,userdata);
}
for (int i=0;i<vect_QShortcuts.count();i++)
delete vect_QShortcuts[i];
+
+ if (guiMainThread)
+ guiMainThread->isLastWindow();
}
void CvWindow::createToolBar()
{
- myToolBar = new QToolBar;
+ myToolBar = new QToolBar(this);
myToolBar->setFloatable(false);//is not a window
myToolBar->setMaximumHeight(28);
void CvWindow::createStatusBar()
{
- myStatusBar = new QStatusBar;
+ myStatusBar = new QStatusBar(this);
myStatusBar->setSizeGripEnabled(false);
myStatusBar->setMaximumHeight(20);
myStatusBar_msg = new QLabel;