00001 /* 00002 * Copyright (C) 2007, 2008. 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 #ifndef QVWORKER_H 00026 #define QVWORKER_H 00027 00028 #include <QStringList> 00029 #include <QThread> 00030 #include <QTime> 00031 00032 #include <qvutils/qvcpustatcontroler.h> 00033 #include <QVPropertyContainer> 00034 00045 class QVWorker: public QThread, public QVPropertyContainer 00046 { 00047 Q_OBJECT 00048 00049 public: 00051 typedef enum 00052 { 00056 Running, 00059 RunningOneStep, 00063 Paused, 00065 Finished 00066 } TWorkerStatus; 00067 00072 QVWorker(const QString name = QString()); 00073 00075 ~QVWorker(); 00076 00081 virtual void iterate() { std::cerr << "WARNING: redefinition of worker() is deprecated. Redefine iterate() instead." << std::endl; worker(); }; 00082 00086 virtual void worker() {}; 00087 00099 void setMinimumDelay(int ms) { minms = ms; }; 00100 00103 bool isFinished() const { return status == Finished; } 00104 00107 bool isPaused() const { return status == Paused; } 00108 00111 bool isRunning() const { return status == Running; } 00112 00115 TWorkerStatus getStatus() const { return status; } 00116 /* 00120 void unlink(); 00121 */ 00124 int getIteration() const { return numIterations; } 00125 00128 bool isStatsEnabled() const { return statsEnabled; } 00129 00140 QVStat getCpuStat() const { 00141 if (statsEnabled) return cpuStatControler->value(); 00142 else return QVStat(); 00143 } 00144 00153 void printStat(int freq) { 00154 if (statsEnabled) { 00155 cpuStatControler->setFreq(freq); 00156 cpuStatControler->setWorkerName(getName()); 00157 } 00158 } 00159 00178 void addTrigger(QString name) { triggerList.push_back(name); } 00179 00181 const QStringList getTriggerList() const { return triggerList; } 00182 00183 public slots: 00186 void pause() { qDebug() << "QVWorker::pause()"; status = Paused; emit statusUpdate(status); } 00187 00190 void unPause() { qDebug() << "QVWorker::unPause()"; status = Running; emit statusUpdate(status); } 00191 00194 void step() { qDebug() << "QVWorker::step()"; status = RunningOneStep; emit statusUpdate(status); } 00195 00198 void finish() { qDebug() << "QVWorker::finish()"; status = Finished; emit statusUpdate(status); } 00199 00214 virtual void processTrigger(QString name) { Q_UNUSED(name); } 00215 00216 signals: 00219 void startIteration(); 00220 00223 void endIteration(); 00224 00227 void endIteration(uint id, int iteration); 00228 00231 void statusUpdate(QVWorker::TWorkerStatus); 00232 00233 private: 00234 bool statsEnabled; 00235 QVStatControler *cpuStatControler; 00236 int numIterations, maxIterations; 00237 TWorkerStatus status; 00238 QStringList triggerList; 00239 QTime iterationTime; 00240 int curms,minms; 00241 00242 protected: 00243 void run(); 00244 00257 void timeFlag(const QString flag) 00258 { 00259 qDebug() << "QVWorker::timeFlag("<< flag <<")"; 00260 if (statsEnabled) cpuStatControler->setFlag(flag); 00261 } 00262 }; 00263 00264 Q_DECLARE_METATYPE(QVStat); 00265 #endif