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
00029 #include <qvcore/qvapplication.h>
00030 #include <qvgui/qvisioninterface.h>
00031 #include <qvcore/qvpropertyholder.h>
00032
00033 QVApplication::QVApplication (int &argc,char **argv, QString infoString,bool GUIenabled) : QApplication(argc,argv,GUIenabled), info(infoString), unusedArguments(), qvps(), visionInterface(NULL), isRunningFlag(FALSE)
00034 {
00035 qRegisterMetaType< QVariant >("QVariant");
00036 qRegisterMetaType< QVCamera::TCameraStatus >("QVCamera::TCameraStatus");
00037 qRegisterMetaType< QVWorker::TWorkerStatus >("QVWorker::TWorkerStatus");
00038 qRegisterMetaType< QVImage<uChar,1> >("QVImage<uChar,1>");
00039 qRegisterMetaType< QVImage<sShort,1> >("QVImage<sShort,1>");
00040 qRegisterMetaType< QVImage<sFloat,1> >("QVImage<sFloat,1>");
00041 qRegisterMetaType< QVImage<uChar,3> >("QVImage<uChar,3>");
00042 qRegisterMetaType< QVImage<sShort,3> >("QVImage<sShort,3>");
00043 qRegisterMetaType< QVImage<sFloat,3> >("QVImage<sFloat,3>");
00044
00045 unusedArguments = arguments();
00046 unusedArguments.removeAt(0);
00047 }
00048
00049 int QVApplication::exec()
00050 {
00051 qDebug() << "QVApplication::exec()";
00052
00053
00054 if(unusedArguments.contains("--help"))
00055 {
00056 printHelp();
00057 return 0;
00058 }
00059
00060
00061 QSetIterator<QVPropertyHolder *> iq(qvps);
00062 while (iq.hasNext())
00063 {
00064 QVPropertyHolder* qvp = iq.next();
00065 QString lastError;
00066 if((lastError = qvp->getLastError()) != QString())
00067 {
00068 std::cerr << "Error initializing QVApplication: "
00069 << qPrintable(lastError) << std::endl;
00070 return -1;
00071 }
00072 }
00073
00074
00075 if(not unusedArguments.isEmpty())
00076 {
00077 QListIterator<QString> i(unusedArguments);
00078 while (i.hasNext())
00079 std::cerr << "Error initializing QVApplication: "
00080 << "unknown command line parameter: "
00081 << qPrintable(i.next()) << std::endl;
00082 return -1;
00083 }
00084
00085
00086 iq.toFront();
00087 while (iq.hasNext())
00088 {
00089 QVPropertyHolder* qvp = iq.next();
00090 QVCamera* camera;
00091 if((camera = dynamic_cast<QVCamera*>(qvp)) != NULL)
00092 if(camera->isClosed())
00093 if(not camera->openCam())
00094 {
00095 std::cerr << "Error initializing QVApplication: "
00096 << "could not open camera: "
00097 << qPrintable(camera->getName()) << std::endl;
00098 return -1;
00099 }
00100 }
00101 qDebug() << "QVApplication::exec(): cameras opened";
00102
00103
00104
00105
00106 connect(this,SIGNAL(inited()),this,SLOT(initWorkers()));
00107 emit inited();
00108
00109 qDebug() << "Entering in QApplication::exec()";
00110 isRunningFlag = TRUE;
00111 int returnvalue = QApplication::exec();
00112 qDebug() << "Back from QApplication::exec()";
00113
00114 qDebug() << "QVApplication::exec() <- return";
00115 return returnvalue;
00116 }
00117
00118 QStringList QVApplication::getUnusedArguments()
00119 { return unusedArguments; }
00120
00121 void QVApplication::setArgumentAsUsed(QString argument)
00122 {
00123 qDebug() << "QVApplication::setArgumentAsUsed(QString,bool)";
00124 int index = unusedArguments.indexOf(argument);
00125 if(index != -1)
00126 unusedArguments.removeAt(index);
00127 qDebug() << "QVApplication::setArgumentAsUsed(QString,bool) <- return";
00128 }
00129
00130 void QVApplication::registerQVPropertyHolder(QVPropertyHolder *qvp)
00131 {
00132 qDebug() << "QVApplication::registerQVPropertyHolder(QVWorker)";
00133 qvps.insert(qvp);
00134 qDebug() << "QVApplication::registerQVPropertyHolder(QVWorker) -> return";
00135 }
00136
00137 void QVApplication::deregisterQVPropertyHolder(QVPropertyHolder *qvp)
00138 {
00139 qDebug() << "QVApplication::registerQVPropertyHolder(QVWorker)";
00140 qvps.remove(qvp);
00141 qDebug() << "QVApplication::registerQVPropertyHolder(QVWorker) -> return";
00142 }
00143
00144 void QVApplication::registerGUI(QVisionInterface *visionInterface)
00145 {
00146 this->visionInterface = visionInterface;
00147 }
00148
00149 void QVApplication::printHelp() const
00150 {
00151 qDebug() << "QVApplication::printHelp()";
00152
00153 std::cout << "Usage: " << qPrintable(arguments().first())
00154 << " [OPTIONS]" << std::endl;
00155 if (info != QString())
00156 std::cout << qPrintable(info) << std::endl;
00157 std::cout << std::endl;
00158 QSetIterator<QVPropertyHolder *> iq(qvps);
00159 while (iq.hasNext())
00160 {
00161 QString infoHolder = iq.next()->infoInputProperties();
00162 if(infoHolder != QString() )
00163 std::cout << qPrintable(infoHolder) << std::endl;
00164 }
00165 qDebug() << "QVApplication::printHelp() <~ return";
00166 }
00167
00168 void QVApplication::quit()
00169 {
00170 qDebug() << "QVApplication::quit()";
00171
00172 QSetIterator<QVPropertyHolder *> iq(qvps);
00173 while (iq.hasNext())
00174 {
00175 QVPropertyHolder* qvp = iq.next();
00176 QVWorker* worker;
00177 if((worker = dynamic_cast<QVWorker*>(qvp)) != NULL)
00178 worker->finish();
00179 }
00180 iq.toFront();
00181
00182
00183 while (iq.hasNext())
00184 {
00185 QVPropertyHolder* qvp = iq.next();
00186 QVWorker* worker;
00187 if((worker = dynamic_cast<QVWorker*>(qvp)) != NULL)
00188
00189
00190 while(not worker->wait(10)) processEvents();
00191 }
00192 qDebug() << "QVApplication::quit(): workers finished";
00193
00194
00195 iq.toFront();
00196 while (iq.hasNext())
00197 {
00198 QVPropertyHolder* qvp = iq.next();
00199 QVCamera* camera;
00200 if((camera = dynamic_cast<QVCamera*>(qvp)) != NULL)
00201 if(!camera->isClosed())
00202 camera->closeCam();
00203 }
00204 qDebug() << "QVApplication::finish(): cameras closed";
00205
00206 this->exit(0);
00207 }
00208
00209
00210 void QVApplication::initWorkers()
00211 {
00212 qDebug() << "QVApplication::initWorkers()";
00213 QSetIterator<QVPropertyHolder *> iq(qvps);
00214 while (iq.hasNext())
00215 {
00216 QVPropertyHolder* qvp = iq.next();
00217 QVWorker* worker;
00218 if((worker = dynamic_cast<QVWorker*>(qvp)) != NULL)
00219 {
00220 worker->start();
00221 }
00222 }
00223 qDebug() << "QVApplication::initWorkers() <~ return";
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265