00001
00002 #ifndef Impala_Application_TagsLife_TagsLife_h
00003 #define Impala_Application_TagsLife_TagsLife_h
00004
00005 #include "Application/MediaTable/TableDataStore.h"
00006
00007 #include "OglGui/Tabs.h"
00008 #include "OglGui/StringSelector.h"
00009 #include "OglGui/WindowView2D.h"
00010 #include "OglGui/ZoomPanControl.h"
00011 #include "OglGui/TextEdit.h"
00012 #include "OglGui/IntervalChooser.h"
00013 #include "OglGui/ScrollWnd.h"
00014 #include "Basis/String.h"
00015
00016 #include "DirFileNames.h"
00017
00018 #include "TagsWeekPlot.h"
00019 #include "HistogramBars.h"
00020 #include "ViewListenerCircles.h"
00021
00022
00023 #ifdef USE_BOOST_GRAPH
00024 #include "BoostedGraph.h"
00025 #else
00026 #include "LabeledGraph.h"
00027 #endif
00028
00029 #define USE_CURL
00030
00031 #include "RemoteRetriever.h"
00032
00033 #include "TableDataSourceWeekMap.h"
00034
00035 #include "Application/MediaTable/ImageCache.h"
00036 #include "Application/MediaTable/GridWindow.h"
00037 #include "Application/MediaTable/TableWindow.h"
00038
00039 #include "BarPlot.h"
00040 #include "TagsWeekBarPlot.h"
00041
00042 #include "Tag.h"
00043
00044 namespace Impala {
00045 namespace Application {
00046 namespace TagsLife {
00047
00048 using namespace MediaTable;
00049
00050 class TagsLife : public OglGui::Window,
00051 public OglGui::TabsListener,
00052 public OglGui::StringListener,
00053 public OglGui::IntervalChooserListener
00054 {
00055 public:
00056 typedef OglGui::StaticText StaticText;
00057 typedef OglGui::StringSelector StringSelector;
00058 typedef OglGui::ZoomPanControl ZoomPanControl;
00059 typedef OglGui::WindowView2D WindowView2D;
00060 typedef OglGui::IntervalChooser IntervalChooser;
00061
00062 #ifndef USE_BOOST_GRAPH
00063 typedef LabeledGraph BoostedGraph;
00064 #endif
00065
00066 TagsLife(int x, int y, int w, int h) :
00067 OglGui::Window(x,y,w,h)
00068 {
00069 Init(w,h);
00070 }
00071
00072 TagsLife(OglGui::Window* parent, int x, int y, int w, int h) :
00073 OglGui::Window(parent,x,y,w,h)
00074 {
00075 Init(w,h);
00076 }
00077
00078 virtual void
00079 StringSelectionEvent(StringSelector *src, int strIndex, void* listenerData)
00080 {
00081 string tag = "";
00082 if(mTag != 0) tag = mTag->tag;
00083 if(tag.compare(src->GetString(strIndex)) != 0) {
00084 string newTag = src->GetString(strIndex);
00085 ILOG_INFO("Loading new tag: " << newTag);
00086 loadNewTag(newTag);
00087 }
00088 }
00089
00090 virtual void
00091 IntervalChanged(IntervalChooser *src,int minVal,int maxVal,void *userData)
00092 {
00093 mTagsWeekPlotUsed->ShowRange(minVal,maxVal);
00094 mBarPlotViewed->ShowRange(minVal,maxVal);
00095 mHistoBarsUsed->HighLightInterval(minVal,maxVal);
00096 mHistoBarsViewed->HighLightInterval(minVal,maxVal);
00097
00098 int x, y, w, h;
00099 float zoomx, zoomy;
00100
00101 OGLVIEW* v = mGlobeMap->GetOglView();
00102 if(v == 0 || v->im == 0) return;
00103 int imW = v->im->w;
00104 int imH = v->im->h;
00105
00106 float lat2ym65 = lat2y(-65), lat2y85 = lat2y(85);
00107
00108 mViewListenerCircles.Clear();
00109
00110 Tag::geoDataType geoData = mTag->getGeoData(minVal, maxVal);
00111 for(Tag::geoDataType::iterator iter = geoData.begin(); iter != geoData.end(); iter++) {
00112 float radius = 5*sqrt(iter->second->count)/(maxVal-minVal);
00113 radius = cap(radius, 3, 75);
00114 mViewListenerCircles.AddCircle(map(iter->second->centerLongitude, -180, 180, 0, imH),
00115 map(lat2y(iter->second->centerLatitude), lat2ym65, lat2y85, 0, imH),
00116 radius);
00117 }
00118 }
00119
00120 float y2lat(float a) { return 180/M_PI * (2 * atan(exp(a*M_PI/180)) - M_PI/2); }
00121 float lat2y(float a) { return 180/M_PI * log(tan(M_PI/4+a*(M_PI/180)/2)); }
00122
00123
00124 virtual void
00125 TabSelectionEvent(OglGui::Tabs *src, int tabNr, void* data)
00126 {
00127 ULONG normCol = (tabNr==0) ? oglRED : 0xff0090ee;
00128 ULONG highCol = (tabNr==0) ? 0xff800000 : 0xff00385d;
00129 mIntervalChooser->StartThumb()->SetColors(normCol, highCol);
00130 mHistoBarsUsed->SetVisible(tabNr==0);
00131 mHistoBarsViewed->SetVisible(tabNr==1);
00132 }
00133
00134 protected:
00135 void FillTagSelector()
00136 {
00137 vector<string*> files;
00138 DirFileNames("Data", files);
00139 for(int j=0 ; j<files.size() ; j++) {
00140 string filter = "_analysis.xml";
00141 string fileName = *files[j];
00142 if(fileName.size() > filter.size() &&
00143 fileName.compare(fileName.size()-filter.size(), filter.size(), filter) == 0)
00144 mTagSelector->AddString(fileName.substr(0, fileName.size()-filter.size()));
00145 }
00146 }
00147
00148 void
00149 SetTagInfo()
00150 {
00151 ILOG_DEBUG("Setting value for TagInfo, tag=" << mTag->tag);
00152 ILOG_DEBUG("Setting value for TagInfo, nrOfPhotos=" << mTag->nrOfPhotos);
00153 string txt = "I am the " + mTag->tag + " tag.\n"
00154 + "I was first used on " + Tag::formatDate(&mTag->firstUse) + ".\n"
00155 + "Since then, I have been used " + MakeString(mTag->nrOfPhotos) + " times.\n"
00156 + "I was most used in week " + MakeString(mTag->mostUsedWeek) + "\n"
00157 + "with a total of " + MakeString(mTag->mostUsedCount) + " uses.\n"
00158 + "My 3 most popular pictures are:";
00159 mTagInfoTEdit->SetText(txt);
00160 }
00161
00162
00163 protected:
00164
00165
00166
00167
00168
00169
00170
00171
00172 void DoTagSelector(OglGui::Window* parent, int x, int y, int w, int h)
00173 {
00174 mTagSelector = new StringSelector(parent,x,y,w,h);
00175
00176 mTagSelector->SetStringListener(this);
00177 FillTagSelector();
00178 }
00179
00180 void DoTabs(OglGui::Window* parent, int x, int y, int w, int h)
00181 {
00182 mTabs = new OglGui::Tabs(parent,x,y,w,h,120,22);
00183 mTabs->SetTabsListener(this);
00184 mTabs->SetBackground(PaletteMainBackground);
00185 mTabs->SetBorderBackground(PaletteBackground);
00186
00187 mTabUsed = newTab("Used", mTabs);
00188
00189 mTagsWeekPlotUsed =
00190 new TagsWeekPlot(mTabUsed,0,0,mTabUsed->W(),mTabUsed->H());
00191
00192 mTagsWeekPlotUsed->ConnectTo(mTabUsed);
00193 mTagsWeekPlotUsed->BarColors(PaletteForeground1, PaletteForeground1, PaletteForeground1);
00194 mTagsWeekPlotUsed->ShowRange(0,20);
00195
00196 TableDataSource* src = new TableDataSourceWeekMap(mTag->weekStats, mTag->lastWeek);
00197
00198 TableDataStore::GetInstance()->AddTableDataSource("WeekMap", src);
00199 mSource = TableDataStore::GetInstance()->GetTableDataView("WeekMap", "MediaTable");
00200
00201 mTabViewed = newTab("Viewed", mTabs);
00202
00203 mBarPlotViewed =
00204 new TagsWeekBarPlot(mTabViewed,0,0,mTabViewed->W(),mTabViewed->H());
00205 mBarPlotViewed->ConnectTo(mTabViewed);
00206 mBarPlotViewed->BarColors(PaletteForeground2, PaletteForeground2a, PaletteForeground2);
00207 mBarPlotViewed->ShowRange(0,20);
00208
00209 mTabRelated = newTab("Related", mTabs);
00210
00211 mTagsRelatedGraph = new BoostedGraph(mTabRelated, mTabRelated->W(), mTabRelated->H());
00212 mTagsRelatedGraph->ConnectTo(mTabRelated);
00213
00214 mTabTable = newTab("Table", mTabs);
00215 mTabGrid = newTab("Grid", mTabs);
00216 DoVideoExcelTabs();
00217
00218 mTabs->SetForeground(PaletteForeground);
00219 mTabs->SetActiveTab(0);
00220 }
00221
00222 OglGui::Window* newTab(string title, OglGui::Tabs* tabs) {
00223 OglGui::Window* newTab = tabs->CreateTab(title);
00224 newTab->SetBorderType(BEV_ETCHED);
00225 newTab->SetBorderFillShaded(2);
00226
00227
00228
00229
00230
00231
00232
00233
00234 return newTab;
00235 }
00236
00237 void DoVideoExcelTabs() {
00238 mGrid = new GridWindow(mTabGrid,mSource,mTabGrid->W(),mTabGrid->H());
00239 mGrid->SetGridSize(75, 75);
00240 mGrid->ConnectTo(mTabGrid);
00241
00242
00243
00244
00245 mTable = new TableWindow(mTabTable,mSource,mTabTable->W(),mTabTable->H());
00246 mTable->ConnectTo(mTabTable);
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 Window *overlay = new Window(this, 0, 0, W(), H());
00269 overlay->ConnectTo(this);
00270 oglSys.SetNoMouseInput(overlay->GetOGLWND(),1);
00271
00272 mGrid->SetListenTableUpdates(false);
00273 mTable->SetListenTableUpdates(true);
00274
00275
00276
00277
00278 mTable->SetZoomViewTargetWindow(overlay, true);
00279 mGrid->SetZoomViewTargetWindow(overlay, true);
00280 }
00281
00282 void DoTagInfo(OglGui::Window* parent, int x, int y, int w, int h)
00283 {
00284 mTagInfoWnd = new OglGui::Window(parent,x,y,w,h);
00285 mTagInfoWnd->SetBorderType(BEV_ETCHED);
00286 mTagInfoWnd->SetBorderFillShaded(2);
00287
00288
00289 mTagInfoTEdit =
00290 new OglGui::TextEdit(mTagInfoWnd,0,h-90,w,90,"info",0);
00291 mTagInfoTEdit->Editable(false);
00292 mTagInfoTEdit->SetBorderType(0);
00293 mTagInfoTEdit->SetBackground(0);
00294 mTagInfoTEdit->ConnectTo(mTagInfoWnd,OglGui::TOTOP|OglGui::TOLEFTRIGHT);
00295 SetTagInfo();
00296
00297 OglGui::Window* popWnd = new OglGui::Window(mTagInfoWnd,0,0,w,h-90);
00298 for (int i=0; i<3; i++)
00299 {
00300 mPopular[i] = new WindowView2D(popWnd,i*100,1,100,100,0);
00301 mPopular[i]->Texturing(false);
00302 }
00303 popWnd->ConnectTo(mTagInfoWnd);
00304 popWnd->ScaleChildren();
00305
00306 popWnd->SetAllowChildScaling(false);
00307 }
00308
00309 void DoWeekLine(OglGui::Window* parent, int x, int y, int w, int h)
00310 {
00311 mWeekLine = new OglGui::Window(parent,x,y,w,h);
00312 mWeekLine->SetBorderType(-1);
00313 mWeekLine->SetBorderFillShaded(2);
00314 mWeekLine->SetAllowChildScaling(false);
00315
00316 mHistoBarsUsed = new HistogramBars(mWeekLine,0,46,w,h-46);
00317 mHistoBarsUsed->SetColors(PaletteForeground1,PaletteForeground1a);
00318 mHistoBarsUsed->ConnectTo(mWeekLine);
00319
00320 mHistoBarsViewed = new HistogramBars(mWeekLine,0,46,w,h-46);
00321 mHistoBarsViewed->SetColors(PaletteForeground2,PaletteForeground2a);
00322 mHistoBarsViewed->ConnectTo(mWeekLine);
00323
00324 uint barCount = mTag->lastWeek;
00325 mIntervalChooser =
00326 new OglGui::IntervalChooser(mWeekLine,0,16,w,30,0,barCount-1,barCount-10,barCount-1);
00327 mIntervalChooser->SetIntervalChooserListener(this,0);
00328 mIntervalChooser->MinInterval(4);
00329 mIntervalChooser->SetBackground(PaletteBackground);
00330 mIntervalChooser->EndThumb()->SetColors(0xff404040,0xff000000);
00331 mIntervalChooser->SetHighlightBorderType(BEV_RAISED);
00332 mIntervalChooser->ConnectTo(mWeekLine,OglGui::TOLEFTRIGHT);
00333
00334 StaticText* sTxt = new StaticText(mWeekLine,0,0,70,20,"Jan 1, 2004",false);
00335 sTxt->SetAlignOffsets(-2,-2);
00336
00337 sTxt = new StaticText(mWeekLine,w-40,0,40,20,"today",false);
00338 sTxt->SetAlignOffsets(0,-2);
00339 sTxt->ConnectTo(mWeekLine,OglGui::TORIGHT);
00340 }
00341
00342 void DoMap(OglGui::Window* parent, int x, int y, int w, int h)
00343 {
00344 OGLIMAGE* oglIm = TryReadPNG("World.png");
00345
00346 mMapWnd = new OglGui::Window(parent,x,y,w,h);
00347 mGlobeMap = new WindowView2D(mMapWnd,0,92,w,h-92,oglIm);
00348 mGlobeMap->SetBorderType(1);
00349 mGlobeMap->SetScaleToWindow(false);
00350 mGlobeMap->ConnectTo(mMapWnd);
00351 ReleaseOglImage(oglIm);
00352
00353
00354 OGLVIEW* oglView = mGlobeMap->GetOglView();
00355 new OglGui::View(oglView, &mViewListenerCircles);
00356
00357 mZoomPanControl = new ZoomPanControl(mMapWnd,0,0,w,90,mGlobeMap->GetOGLWND(),5,1);
00358 mZoomPanControl->PanWindow()->SetRoundness(0,0,0,0);
00359 mZoomPanControl->ConnectTo(mMapWnd,OglGui::TOBOTTOM|OglGui::TOLEFTRIGHT);
00360
00361
00362 OGLWND* oglWnd = mZoomPanControl->PanWindow()->GetOGLWND();
00363 oglView = (OGLVIEW*) oglWnd->objectList->info;
00364 new OglGui::View(oglView, &mViewListenerCircles);
00365 }
00366
00367
00368 virtual void DisplayFunc()
00369 {
00370 Window::DisplayFunc();
00371
00372 if (mImReadWeek > 0)
00373 {
00374 if(mTag->weekStats.count(mImReadWeek) > 0) {
00375 OGLIMAGE* oglIm = retriever->requestImage(mTag->weekStats[mImReadWeek]->mostPopularPhoto->imageUrl("s"));
00376 mTagsWeekPlotUsed->SetImage(mImReadWeek,oglIm);
00377 }
00378
00379 mImReadWeek--;
00380 SetAlwaysDraw(mImReadWeek > 0);
00381 }
00382 }
00383
00384 private:
00385
00386 void Init(int reqW, int reqH)
00387 {
00388 mOglWnd->topTitle = "TagsLife";
00389
00390
00391 int w = 1024, h = 768;
00392 SetDimensions(w,h);
00393 SetBackground(PaletteMainBackground);
00394
00395
00396 retriever = RemoteRetriever::GetInstance();
00397
00398 mTag = new Tag("obama");
00399
00400 DoTagSelector(this,w-300,h-85,298,83);
00401 DoMap(this,w-300,406,298,h-85-408);
00402 DoWeekLine(this,2,0,w-2,198);
00403 DoTabs(this,2,202,w-304,h-206);
00404 DoTagInfo(this,w-300,202,298,202);
00405
00406 loadNewTag();
00407
00408
00409 ScaleChildren();
00410
00411 SetDimensions(reqW,reqH);
00412
00413 OnLayoutChange();
00414
00415
00416 mZoomPanControl->OnLayoutChange();
00417
00418 mZoomPanControl->ZoomController()->FitButton()->DoButtonSelectionEvent();
00419 }
00420
00421 void loadNewTag(string tag="") {
00422 if(tag.size() != 0) {
00423 delete mTag;
00424 mTag = new Tag(tag);
00425
00426 delete mSource;
00427 TableDataSource* src = new TableDataSourceWeekMap(mTag->weekStats, mTag->lastWeek);
00428
00429 TableDataStore::GetInstance()->AddTableDataSource("WeekMap", src);
00430 mSource = TableDataStore::GetInstance()->GetTableDataView("WeekMap", "MediaTable");
00431 }
00432
00433 mImReadWeek = mTag->lastWeek;
00434 SetTagInfo();
00435 mTagsWeekPlotUsed->Clear();
00436 mBarPlotViewed->Clear();
00437 mHistoBarsUsed->Clear();
00438 mHistoBarsViewed->Clear();
00439
00440 TableDataView* viewBarPlot = TableDataStore::GetInstance()->GetTableDataView("WeekMap", "BarPlot");
00441 mBarPlotViewed->AddColumnsFromDataSource(viewBarPlot, "flickrWeek", "views", "most popular photo", "contribution");
00442
00443
00444 mGrid->AddColumnsFromDataSource();
00445 mTable->AddColumnsFromDataSource();
00446 mSource->SetSortColumn("views", false);
00447
00448
00449
00450 delete mTagsRelatedGraph;
00451 mTagsRelatedGraph = new BoostedGraph(mTabRelated, mTabRelated->W(), mTabRelated->H());
00452 mTagsRelatedGraph->ConnectTo(mTabRelated);
00453
00454 uint mWeekStatCount = 0;
00455 for(Tag::weekStatsType::const_iterator it = mTag->weekStats.begin(); it != mTag->weekStats.end(); ++it) {
00456 for(; mWeekStatCount < it->first; mWeekStatCount++) {
00457
00458 mTagsWeekPlotUsed->AddToHistogram(0);
00459 mHistoBarsUsed->AddToHistogram(0);
00460 }
00461
00462 mWeekStatCount++;
00463 mTagsWeekPlotUsed->AddToHistogram(it->second->usage,0,0);
00464 mHistoBarsUsed->AddToHistogram(it->second->usage);
00465 mHistoBarsViewed->AddToHistogram(it->second->views);
00466 }
00467
00468 for(uint i=0; i < 3; i++) {
00469 mPopular[i]->SetImage(retriever->requestImage(mTag->mostPopularPhotos[i]->imageUrl("s")));
00470 }
00471
00472 while(mTagSelector->GetCurrentString().compare(mTag->tag) != 0)
00473 mTagSelector->CurrentStringUpDown(true, true);
00474
00475 mTag->readRelated();
00476 mTagsRelatedGraph->addNodes(mTag->relatedTags);
00477
00478
00479 uint barCount = mTag->lastWeek;
00480 mIntervalChooser =
00481 new OglGui::IntervalChooser(mWeekLine,0,16,W()-2,30,0,barCount,barCount-9,barCount);
00482 mIntervalChooser->SetIntervalChooserListener(this,0);
00483 mIntervalChooser->MinInterval(4);
00484 mIntervalChooser->SetBackground(PaletteBackground);
00485 mIntervalChooser->EndThumb()->SetColors(0xff404040,0xff000000);
00486 mIntervalChooser->SetHighlightBorderType(BEV_RAISED);
00487 mIntervalChooser->ConnectTo(mWeekLine,OglGui::TOLEFTRIGHT);
00488 }
00489
00490 float map(float v, float vmin, float vmax, float min, float max) {
00491 float r = (v-vmin)/(vmax-vmin);
00492 r *= (max-min);
00493 r += min;
00494 return r;
00495 }
00496
00497 float cap(float v, float min, float max) {
00498 if(v < min) return min;
00499 if(v > max) return max;
00500 return v;
00501 }
00502
00503 Tag* mTag;
00504 string newTag;
00505 RemoteRetriever* retriever;
00506
00507 TableDataView* mSource;
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 enum Palette { PaletteMainBackground = oglGREY,
00519 PaletteBackground = oglDARKGREY,
00520 PaletteText = oglWHITE,
00521 PaletteForeground = PaletteText,
00522 PaletteForegroundShadow = oglGREY,
00523 PaletteForeground1 = oglRED,
00524 PaletteForeground1a = 0xff800000,
00525 PaletteForeground2 = 0xff0090ee,
00526 PaletteForeground2a = 0xff00385d };
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 OglGui::Tabs* mTabs;
00547 OglGui::Window* mTabUsed;
00548 OglGui::Window* mTabViewed;
00549 OglGui::Window* mTabRelated;
00550 OglGui::Window* mTabTable;
00551 OglGui::Window* mTabGrid;
00552 OglGui::Window* mMapWnd;
00553 OglGui::Window* mTagInfoWnd;
00554 OglGui::Window* mWeekLine;
00555
00556 TableWindow* mTable;
00557 GridWindow* mGrid;
00558
00559 OglGui::TextEdit* mTagInfoTEdit;
00560 ZoomPanControl* mZoomPanControl;
00561 WindowView2D* mTitleView2D;
00562 WindowView2D* mGlobeMap;
00563 WindowView2D* mPopular[3];
00564 StringSelector* mTagSelector;
00565
00566 TagsWeekPlot* mTagsWeekPlotUsed;
00567 BarPlot* mBarPlotViewed;
00568 HistogramBars* mHistoBarsUsed;
00569 HistogramBars* mHistoBarsViewed;
00570 IntervalChooser* mIntervalChooser;
00571 BoostedGraph* mTagsRelatedGraph;
00572
00573 ViewListenerCircles mViewListenerCircles;
00574
00575 int mImReadWeek;
00576
00577 ILOG_VAR_DEC;
00578 };
00579
00580 ILOG_VAR_INIT(TagsLife, Impala.Application);
00581
00582 }
00583 }
00584 }
00585
00586 #endif // Impala_Application_TagsLife_TagsLife_h