00001 #ifndef Impala_Application_IDash_AppControlDossier_h
00002 #define Impala_Application_IDash_AppControlDossier_h
00003
00004 #include <sstream>
00005 #include <vector>
00006 #include "Basis/Timer.h"
00007 #include "Basis/Logger.h"
00008 #include "Visualization/AppControl.h"
00009 #include "OglGui/Sys.h"
00010 #include "Core/Database/DataDocument.h"
00011 #include "Core/Database/DataDocumentListener.h"
00012
00013 namespace Impala
00014 {
00015 namespace Application
00016 {
00017 namespace IDash
00018 {
00019
00020 class AppControlDossier : public Visualization::AppControl
00021 {
00022 public:
00023 typedef Core::Database::DataDocument DataDocument;
00024 typedef Core::Database::DataDocumentListener DataDocumentListener;
00025 typedef Core::Database::DocLevel DocLevel;
00026
00027 AppControlDossier(int id, DataDocument* doc) : AppControl(id, false), mTimer(1)
00028 {
00029 mDoc = doc;
00030 mDone = false;
00031 mDoContinuous = false;
00032 mNrItemsDone = 0;
00033 }
00034
00035 void
00036 AddDocListener(DataDocumentListener* listener)
00037 {
00038 Logger::Debug() << "AppControlDoc(" << GetControlId() << ")::AddDocListener" << std::endl;
00039 mDocListeners.push_back(listener);
00040 }
00041
00042 void
00043 SetDoContinuous(bool flag)
00044 {
00045 mDoContinuous = flag;
00046 if (mDoContinuous)
00047 IpsReset();
00048 }
00049
00050 bool
00051 GetDoContinuous()
00052 {
00053 return mDoContinuous;
00054 }
00055
00056 void
00057 IpsReset()
00058 {
00059 mTimer.Start();
00060 mNrItemsDone = 0;
00061 }
00062
00063 std::string
00064 GetIpsString()
00065 {
00066 double timeVal = mTimer.SplitTime();
00067 double ips = (double) mNrItemsDone / timeVal;
00068 std::ostringstream ipsStream;
00069 ipsStream << mNrItemsDone << " items in " << timeVal
00070 << " sec = " << ips << " ips" << std::ends;
00071 return ipsStream.str();
00072 }
00073
00074
00075 void
00076 VisitAll(bool doRightOnBookmarks, int startDir, int numberDir)
00077 {
00078 if (mDoc->HasBookmarks())
00079 {
00080 DocLevel level;
00081 do
00082 {
00083 level = mDoc->CursorNextBookmark();
00084 AllHandleNewCursor(level, true);
00085 if (doRightOnBookmarks && (level != Core::Database::LEVEL_NONE))
00086 {
00087 DocLevel level2;
00088 do {
00089 level2 = mDoc->CursorNextFile();
00090 AllHandleNewCursor(level2, false);
00091 } while (level2 != Core::Database::LEVEL_NONE);
00092 }
00093 AllHandleDoneCursor(level);
00094 } while (level != Core::Database::LEVEL_NONE);
00095 }
00096 else
00097 {
00098 if (numberDir == -1)
00099 numberDir = mDoc->GetDataSet()->NrDirs();
00100 DocLevel level1 = mDoc->CursorToDir(startDir);
00101 AllHandleNewCursor(level1, false);
00102 int nr = 0;
00103 while ((level1 != Core::Database::LEVEL_NONE) && (nr < numberDir))
00104 {
00105 DocLevel level2;
00106 do {
00107 level2 = mDoc->CursorNextFile();
00108 AllHandleNewCursor(level2, false);
00109 } while (level2 != Core::Database::LEVEL_NONE);
00110 AllHandleDoneCursor(level1);
00111 nr++;
00112 if (nr < numberDir)
00113 {
00114 level1 = mDoc->CursorNextDir();
00115 AllHandleNewCursor(level1, false);
00116 }
00117 }
00118 }
00119 AllHandleDoneVisit();
00120 }
00121
00122
00123
00124 virtual void
00125 ComputeCycle(double elapsedTime)
00126 {
00127
00128
00129
00130
00131
00132
00133
00134 if (!GetDoContinuous())
00135 return;
00136 AllHandleNewCursor(mDoc->CursorNextFile(), false);
00137 AllHandleCycleDoc();
00138 mNrItemsDone++;
00139 }
00140
00141 virtual void
00142 HandleActivate()
00143 {
00144 Logger::Debug() << "AppControlDoc(" << GetControlId() << ")::HandleActivate()" << std::endl;
00145
00146 for (int i=0 ; i<mDocListeners.size() ; i++)
00147 {
00148 mDocListeners[i]->HandleNewFile();
00149 mDocListeners[i]->HandleNewBookmarked();
00150 }
00151 }
00152
00153 virtual void
00154 KeyEvent(OglGui::Window* src, int c, int state)
00155 {
00156 if (c == 'Q')
00157 mDone = true;
00158 if (c == 'c')
00159 SetDoContinuous(true);
00160 if (c == 'C')
00161 SetDoContinuous(false);
00162
00163 if (c == oglHOME)
00164 AllHandleNewCursor(mDoc->CursorToDir(0), false);
00165 if (c == oglPAGEUP)
00166 AllHandleNewCursor(mDoc->CursorPrevDir(), false);
00167 if (c == oglPAGEDOWN)
00168 AllHandleNewCursor(mDoc->CursorNextDir(), false);
00169 if (c == oglLEFT)
00170 AllHandleNewCursor(mDoc->CursorPrevFile(), false);
00171 if (c == oglRIGHT)
00172 AllHandleNewCursor(mDoc->CursorNextFile(), false);
00173 if (c == oglUP)
00174 AllHandleNewCursor(mDoc->CursorPrevShot(), false);
00175 if (c == oglDOWN)
00176 AllHandleNewCursor(mDoc->CursorNextShot(), false);
00177 if (c == 'n')
00178 AllHandleNewCursor(mDoc->CursorNextBookmark(), true);
00179 if (c == 'N')
00180 AllHandleNewCursor(mDoc->CursorPrevBookmark(), true);
00181
00182
00183 if (c == 'b')
00184 AddCursorToBookmarked();
00185 if (c == 'B')
00186 RemoveCursorFromBookmarked();
00187 for (int i=0 ; i<mDocListeners.size() ; i++)
00188 mDocListeners[i]->HandleNewKey(c, state);
00189 }
00190
00191 virtual bool
00192 Done()
00193 {
00194 return mDone;
00195 }
00196
00197
00198
00199 virtual void
00200 AddCursorToBookmarked()
00201 {
00202 }
00203
00204 virtual void
00205 RemoveCursorFromBookmarked()
00206 {
00207 }
00208
00209 protected:
00210
00211 void
00212 AllHandleNewCursor(DocLevel docLevel, bool doBookmark)
00213 {
00214 if (docLevel == 0)
00215 return;
00216 for (int i=0 ; i<mDocListeners.size() ; i++)
00217 {
00218 if (doBookmark)
00219 mDocListeners[i]->HandleCursorBookmarked();
00220 switch (docLevel) {
00221 case Core::Database::LEVEL_DIR:
00222 mDocListeners[i]->HandleNewDir();
00223 break;
00224 case Core::Database::LEVEL_FILE:
00225 mDocListeners[i]->HandleNewFile();
00226 break;
00227 case Core::Database::LEVEL_SHOT:
00228 mDocListeners[i]->HandleNewShot();
00229 break;
00230 case Core::Database::LEVEL_FRAME:
00231 mDocListeners[i]->HandleNewFrame();
00232 break;
00233 }
00234 }
00235 }
00236
00237 void
00238 AllHandleDoneCursor(DocLevel docLevel)
00239 {
00240 if (docLevel == 0)
00241 return;
00242 for (int i=0 ; i<mDocListeners.size() ; i++)
00243 {
00244 switch (docLevel) {
00245 case Core::Database::LEVEL_DIR:
00246 mDocListeners[i]->HandleDoneDir();
00247 break;
00248 case Core::Database::LEVEL_FILE:
00249 mDocListeners[i]->HandleDoneFile();
00250 break;
00251 }
00252 }
00253 }
00254
00255 void
00256 AllHandleDoneVisit()
00257 {
00258 for (int i=0 ; i<mDocListeners.size() ; i++)
00259 mDocListeners[i]->HandleDoneVisit();
00260 }
00261
00262 void
00263 AllHandleNewBookmarked()
00264 {
00265 for (int i=0 ; i<mDocListeners.size() ; i++)
00266 mDocListeners[i]->HandleNewBookmarked();
00267 }
00268
00269 public:
00270 void
00271 AllHandleBookmarkEvent(int shotid, bool selected)
00272 {
00273 for (int i=0 ; i<mDocListeners.size() ; i++)
00274 mDocListeners[i]->HandleBookmarkEvent(shotid, selected);
00275 }
00276
00277 void
00278 AllHandleAddedBookmark()
00279 {
00280 for (int i=0 ; i<mDocListeners.size() ; i++)
00281 mDocListeners[i]->HandleAddedBookmark();
00282 }
00283
00284 void
00285 AllHandleRemovedBookmark()
00286 {
00287 for (int i=0 ; i<mDocListeners.size() ; i++)
00288 mDocListeners[i]->HandleRemovedBookmark();
00289 }
00290
00291 protected:
00292 void
00293 AllHandleCycleDoc()
00294 {
00295 for (int i=0 ; i<mDocListeners.size() ; i++)
00296 mDocListeners[i]->HandleCycleDoc();
00297 }
00298
00299 private:
00300
00301 DataDocument* mDoc;
00302 std::vector<DataDocumentListener*> mDocListeners;
00303
00304 Timer mTimer;
00305 bool mDone;
00306 bool mDoContinuous;
00307 int mNrItemsDone;
00308
00309 };
00310
00311 }
00312 }
00313 }
00314
00315 #endif