src/qvcore/qvapplication.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
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); // Application name removed.
00047         }
00048 
00049 int QVApplication::exec()
00050         {
00051         qDebug() << "QVApplication::exec()";
00052 
00053         // If --help parameter was given, show help and exit:
00054         if(unusedArguments.contains("--help")) 
00055                 {
00056                 printHelp();
00057                 return 0;
00058                 }
00059 
00060         // An initialization error of any QVPropertyHolder aborts execution:
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         // If there are unused arguments, show error and exit
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         // Now we will open all cameras:
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         // Connect and send an initial signal for the QVThreads to start running
00104         // later, once the GUI is up and runnig (the initWorkers() slot will be
00105         // lately called by the exec() method):
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         // We order all workers to finish...
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         // ... and then wait for all of them (Warning, it won't work if we try to
00182         // finish and wait in the same loop).
00183         while (iq.hasNext())
00184                 {
00185                 QVPropertyHolder* qvp = iq.next();
00186                 QVWorker* worker;
00187                 if((worker = dynamic_cast<QVWorker*>(qvp)) != NULL)
00188                         // Needed to treat possible pending Qt::BlockingQueuedConnection
00189                         // signals from qvpropertyholder.h:
00190                         while(not worker->wait(10/*ms*/)) processEvents();
00191                 }
00192         qDebug() << "QVApplication::quit(): workers finished";
00193 
00194         // Now we will close all cameras:
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 /* Antiguo QVApplication::ready:
00229 
00230                 qDebug() << "QVApplication::ready(): opening cameras";
00231                 QSetIterator<QVCamera *> ic(cameras);
00232                 while (ic.hasNext())
00233                         {
00234                         // TODO: AQUI ABRIMOS LAS CÁMARAS
00235                         QVCamera * camera = ic.next();
00236 
00237                         if (camera->openCam() == -1)
00238                                 {
00239                                 returnValue = false;
00240                                 std::cerr << "Error: could not open camera" << qPrintable(camera->getName()) << std::endl;
00241                                 }
00242                         else
00243                                 connect(camera,SIGNAL(camClosed()),this,SLOT(finish()));
00244                         }
00245                 }
00246 
00247 
00248                         // Para cada propiedad que contenga una qvimage, la publicamos en el interfaz
00249                         QStringListIterator uCharC1Iterator(worker->getPropertiesByType< QVImage<uChar,1> >());
00250                                 while (uCharC1Iterator.hasNext())
00251                                         {
00252                                         QString property = uCharC1Iterator.next();
00253                                         if (worker->isVisibleProperty(property))
00254                                                 visionInterface->publishQVImage(worker, property);
00255                                         }
00256 
00257                         QStringListIterator uCharC3Iterator(worker->getPropertiesByType< QVImage<uChar,3> >());
00258                                 while (uCharC3Iterator.hasNext())
00259                                         {
00260                                         QString property = uCharC3Iterator.next();
00261                                         if (worker->isVisibleProperty(property))
00262                                                 visionInterface->publishQVImage(worker, property);
00263                                         }
00264                         }
00265 */

Generated on Fri Dec 7 12:20:59 2007 for QVision by  doxygen 1.5.3