00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <QDebug>
00026 #include <QMetaType>
00027 #include <QSet>
00028 #include <QGLWidget>
00029
00030 #include <QVApplication>
00031 #include <QVGUI>
00032 #include <QVImageCanvas>
00033 #include <QVProcessingBlock>
00034 #include <QMutexLocker>
00035
00036 #ifdef QVQWT
00037 #include "qvguiblocks/qvplot.h"
00038 #endif
00039
00040 QVApplication::QVApplication (int &argc,char **argv, QString infoString,bool GUIenabled) : QApplication(argc,argv,GUIenabled), info(infoString), unusedArguments(), qvps(), visionInterface(NULL), isRunningFlag(FALSE), blockCount(0), terminateOnLastBlock(TRUE), forHelpFlag(FALSE)
00041 {
00042
00043 if (GUIenabled and not QGLFormat::hasOpenGL() )
00044 qFatal("This system has no OpenGL support, and QVision GUI needs it. Exiting.");
00045
00046 qRegisterMetaType< QVariant >("QVariant");
00047
00048 qRegisterMetaType< QVProcessingBlock::TBlockStatus >("QVProcessingBlock::TBlockStatus");
00049 qRegisterMetaType< QVImage<uChar,1> >("QVImage<uChar,1>");
00050 qRegisterMetaType< QVImage<sShort,1> >("QVImage<sShort,1>");
00051 qRegisterMetaType< QVImage<sFloat,1> >("QVImage<sFloat,1>");
00052 qRegisterMetaType< QVImage<uChar,3> >("QVImage<uChar,3>");
00053 qRegisterMetaType< QVImage<sShort,3> >("QVImage<sShort,3>");
00054 qRegisterMetaType< QVImage<sFloat,3> >("QVImage<sFloat,3>");
00055 qRegisterMetaType< QVPropertyContainerChange >("QVPropertyContainerChange");
00056
00057 unusedArguments = arguments();
00058 setApplicationName(unusedArguments[0].split("/").last());
00059 unusedArguments.removeAt(0);
00060
00061 if(unusedArguments.contains("--help")) forHelpFlag = TRUE;
00062
00063 }
00064
00065 int QVApplication::processArguments()
00066 {
00067
00068 if(unusedArguments.contains("--help"))
00069 {
00070 printHelp();
00071 return 0;
00072 }
00073
00074
00075 foreach(QVPropertyContainer* qvp, qvps)
00076 {
00077 QString lastError;
00078 if((lastError = qvp->getLastError()) != QString())
00079 {
00080 std::cerr << "Error initializing QVApplication: "
00081 << qPrintable(lastError) << std::endl;
00082 return -1;
00083 }
00084 }
00085
00086
00087 if(not unusedArguments.isEmpty())
00088 {
00089 foreach(QString argument, unusedArguments)
00090 std::cerr << "Error initializing QVApplication: "
00091 << "unknown command line parameter: "
00092 << qPrintable(argument) << std::endl;
00093 return -1;
00094 }
00095 return 1;
00096 }
00097
00098
00099 int QVApplication::exec()
00100 {
00101 qDebug() << "QVApplication::exec()";
00102
00103
00104 int ret_value = processArguments();
00105 if(ret_value != 1)
00106 return ret_value;
00107
00108
00109 if (visionInterface)
00110 {
00111 visionInterface->init();
00112 visionInterface->show();
00113 }
00114 qDebug() << "QVApplication::exec(): GUI initied";
00115
00116
00117 if (!startItems()) return -1;
00118
00119 qDebug() << "Entering in QApplication::exec()";
00120 isRunningFlag = TRUE;
00121 int returnvalue = QApplication::exec();
00122 qDebug() << "Back from QApplication::exec()";
00123
00124 qDebug() << "QVApplication::exec() <- return";
00125 return returnvalue;
00126 }
00127
00128 bool QVApplication::startItems()
00129 {
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 #ifdef QVQWT
00157 foreach(QVPropertyContainer* qvp, qvps)
00158 {
00159 QVPlot* plot;
00160 if((plot = dynamic_cast<QVPlot*>(qvp)) != NULL)
00161 {
00162 if (!plot->isInitied()) plot->init();
00163 if (plot->isAutoShow()) plot->show();
00164 }
00165 }
00166 qDebug() << "QVApplication::initItems(): plots initied";
00167 #endif
00168
00169
00170 foreach(QVPropertyContainer* qvp, qvps)
00171 {
00172 QVImageCanvas* canvas;
00173 if((canvas = dynamic_cast<QVImageCanvas*>(qvp)) != NULL)
00174 canvas->show();
00175 }
00176 qDebug() << "QVApplication::initItems(): canvas shown";
00177
00178
00179 foreach(QVPropertyContainer* qvp, qvps)
00180 {
00181 QVProcessingBlock* block;
00182 if((block = dynamic_cast<QVProcessingBlock*>(qvp)) != NULL)
00183 {
00184 QVProcessingBlock* masterBlock;
00185
00186 if((masterBlock = dynamic_cast<QVProcessingBlock*>(qvp->getMaster())) != NULL)
00187 block->moveToThread(masterBlock);
00188 connect(block,SIGNAL(finished()),this,SLOT(blockFinished()));
00189
00190 if (block->isSequentialGroupMaster()) {
00191 blockCount++;
00192 block->start();
00193 }
00194 }
00195 }
00196 isRunningFlag = TRUE;
00197 qDebug() << "QVApplication::initItems(): blocks started";
00198 return true;
00199 }
00200
00201 void QVApplication::quit()
00202 {
00203 quitItems();
00204 deregisterGUI();
00205 this->exit(0);
00206 }
00207
00208 void QVApplication::quitItems()
00209 {
00210 qDebug() << "QVApplication::quitItems()";
00211
00212
00213 foreach(QVPropertyContainer* qvp, qvps)
00214 {
00215 QVProcessingBlock* block;
00216 if((block = dynamic_cast<QVProcessingBlock*>(qvp)) != NULL)
00217 block->finish();
00218 }
00219
00220
00221 foreach(QVPropertyContainer* qvp, qvps)
00222 {
00223 QVProcessingBlock* block;
00224 if((block = dynamic_cast<QVProcessingBlock*>(qvp)) != NULL)
00225 {
00226
00227
00228
00229 while(not block->wait(10)) processEvents();
00230 deregisterQVPropertyContainer(block);
00231 }
00232 }
00233 qDebug() << "QVApplication::quitItems(): blocks finished";
00234
00235
00236 foreach(QVPropertyContainer* qvp, qvps)
00237 {
00238 QVImageCanvas* canvas;
00239 if((canvas = dynamic_cast<QVImageCanvas*>(qvp)) != NULL)
00240 {
00241 canvas->hide();
00242 deregisterQVPropertyContainer(canvas);
00243 }
00244 }
00245 qDebug() << "QVApplication::quitItems(): canvas hiden";
00246
00247 #ifdef QVQWT
00248
00249 foreach(QVPropertyContainer* qvp, qvps)
00250 {
00251 QVPlot* plot;
00252 if((plot = dynamic_cast<QVPlot*>(qvp)) != NULL)
00253 {
00254 plot->stop();
00255 plot->hide();
00256 deregisterQVPropertyContainer(plot);
00257 }
00258 }
00259 qDebug() << "QVApplication::quitItems(): plots initied";
00260 #endif
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 isRunningFlag = FALSE;
00275 }
00276
00277 void QVApplication::quitBlock(QVProcessingBlock *block)
00278 {
00279 if (qvps.contains(block)) {
00280 block->finish();
00281
00282 while(not block->wait(10)) processEvents();
00283 deregisterQVPropertyContainer(block);
00284
00285
00286 }
00287 }
00288
00289 QStringList QVApplication::getUnusedArguments()
00290 { return unusedArguments; }
00291
00292 void QVApplication::setArgumentAsUsed(QString argument)
00293 {
00294 qDebug() << "QVApplication::setArgumentAsUsed(QString,bool)";
00295 int index = unusedArguments.indexOf(argument);
00296 if(index != -1)
00297 unusedArguments.removeAt(index);
00298 qDebug() << "QVApplication::setArgumentAsUsed(QString,bool) <- return";
00299 }
00300
00301 #ifndef DOXYGEN_IGNORE_THIS
00302 void QVApplication::registerQVPropertyContainer(QVPropertyContainer *qvp)
00303 {
00304 qDebug() << "QVApplication::registerQVPropertyContainer(" << qvp->getName() << ")";
00305 qvps.insert(qvp);
00306 qDebug() << "QVApplication::registerQVPropertyContainer(" << qvp->getName() << ") -> return";
00307 }
00308
00309 void QVApplication::deregisterQVPropertyContainer(QVPropertyContainer *qvp)
00310 {
00311 qDebug() << "QVApplication::registerQVPropertyContainer(" << qvp->getName() << ")";
00312 qvps.remove(qvp);
00313 qDebug() << "QVApplication::registerQVPropertyContainer(" << qvp->getName() << ") -> return";
00314 }
00315
00316 void QVApplication::registerGUI(QVGUI *visionInterface)
00317 {
00318 this->visionInterface = visionInterface;
00319 }
00320
00321 void QVApplication::deregisterGUI()
00322 {
00323 this->visionInterface = 0;
00324 }
00325 #endif
00326
00327 void QVApplication::printHelp()
00328 {
00329 std::cout << qPrintable(getHelp());
00330 }
00331
00332 void QVApplication::blockFinished()
00333 {
00334 blockCount--;
00335 if(blockCount == 0)
00336 if(terminateOnLastBlock)
00337 quit();
00338 }
00339
00340 QString QVApplication::getHelp()
00341 {
00342 QString help_string;
00343
00344 help_string = "Usage: " + applicationName() + " [OPTIONS]\n\n";
00345
00346 if (info != QString())
00347 help_string = help_string + qPrintable(info) + "\n\n";
00348
00349 QSetIterator<QVPropertyContainer *> iq(qvps);
00350 while (iq.hasNext())
00351 {
00352 QString infoHolder = iq.next()->infoInputProperties();
00353 if(infoHolder != QString() )
00354 help_string += infoHolder + "\n";
00355 }
00356
00357 return help_string;
00358
00359 }