00001 #ifndef MediaTable_SelectionCollection_h
00002 #define MediaTable_SelectionCollection_h
00003
00004 #include "SelectionCollectionItem.h"
00005 #include "OglGui/ScrollWnd.h"
00006
00007 #include <fstream>
00008
00009 namespace Impala {
00010 namespace Application {
00011 namespace MediaTable {
00012
00013
00014 class SelectionCollection : public OglGui::Window,
00015 OglGui::ScrollBarListener,
00016 TableDataViewListener,
00017 SelectionCollectionListener
00018 {
00019 public:
00020 typedef OglGui::Window Window;
00021 typedef OglGui::ScrollBar ScrollBar;
00022 typedef OglGui::ScrollWnd ScrollWnd;
00023
00024 SelectionCollection(Window* parent, TableDataView *source, int w, int h) :
00025 Window(parent, w, h)
00026 {
00027 Init(source, w, h);
00028 }
00029
00030 SelectionCollection(Window* parent, TableDataView *source, int x, int y,
00031 int w, int h) :
00032 Window(parent, x, y, w, h)
00033 {
00034 Init(source, w, h);
00035 }
00036
00037
00038 virtual void UpdateSelectionEvent()
00039 {
00040 UpdateItemCounts();
00041 }
00042
00043 virtual void OnScroll(ScrollBar *src, int position, void *userData)
00044 {
00045 }
00046
00047 virtual void MouseFunc(int msg, int but, int state, int x, int y)
00048 {
00049 }
00050
00051 virtual void KeyboardFunc(int c, int state)
00052 {
00053 }
00054
00055 void UpdateItemCounts()
00056 {
00057 ILOG_DEBUG("UpdateItemCounts");
00058 std::map<int, int> marks = mSource->GetMarkedRowsMap();
00059 std::map<int, bool> visible = mSource->GetUnfilteredRowsMap();
00060 std::map<int, int> markcounts;
00061 std::map<int, int> markviscounts;
00062
00063 for (int x = 0; x < 9; x++)
00064 {
00065 markcounts[x] = 0;
00066 markviscounts[x] = 0;
00067 }
00068
00069 for (int x = 0; x < mSource->GetTotalRows(); x++)
00070 {
00071 if(marks.find(x) != marks.end())
00072 {
00073 int mark = marks[x];
00074
00075 if (mark & 1) markcounts[1] += 1;
00076 if (mark & 2) markcounts[2] += 1;
00077 if (mark & 4) markcounts[3] += 1;
00078 if (mark & 8) markcounts[4] += 1;
00079 if (mark & 16) markcounts[5] += 1;
00080 if (mark & 32) markcounts[6] += 1;
00081 if (mark & 64) markcounts[7] += 1;
00082 if (mark & 128) markcounts[8] += 1;
00083 if (visible[x])
00084 {
00085 if (mark & 1) markviscounts[1] += 1;
00086 if (mark & 2) markviscounts[2] += 1;
00087 if (mark & 4) markviscounts[3] += 1;
00088 if (mark & 8) markviscounts[4] += 1;
00089 if (mark & 16) markviscounts[5] += 1;
00090 if (mark & 32) markviscounts[6] += 1;
00091 if (mark & 64) markviscounts[7] += 1;
00092 if (mark & 128) markviscounts[8] += 1;
00093 }
00094 }
00095 else
00096 {
00097 markcounts[0] += 1;
00098 if (visible[x])
00099 markviscounts[0] += 1;
00100 }
00101 }
00102
00103 for (int x=0; x < 9; x++)
00104 mItems[x + 1]->SetItemCounts(markcounts[x], markviscounts[x]);
00105
00106 mItems[0]->SetItemCounts(mSource->GetTotalRows(),
00107 mSource->GetFilteredRows());
00108 ILOG_DEBUG("UpdateItemCounts done");
00109 }
00110
00111
00112
00113 virtual void SelectCollectionEvent(int id)
00114 {
00115 if (id==-1)
00116 mSource->DisableFilterByMark();
00117 else
00118 mSource->EnableFilterByMark(id);
00119 ProcessHighlights();
00120 }
00121
00122 virtual void SelectCollectionInvertedEvent(int id)
00123 {
00124 int everything = mSource->GetMarkFilterForEverything();
00125 mSource->EnableFilterByMark(everything & ~id);
00126 ProcessHighlights();
00127 }
00128
00129 virtual void AddCollectionToSelectionEvent(int id)
00130 {
00131 mSource->EnableFilterByMark(mSource->GetMarkFilter() | id);
00132 ProcessHighlights();
00133 }
00134
00135 virtual void RemoveCollectionFromSelectionEvent(int id)
00136 {
00137 mSource->EnableFilterByMark(mSource->GetMarkFilter() & ~id);
00138 ProcessHighlights();
00139 }
00140
00141 virtual void EmptyCollectionEvent(int id)
00142 {
00143 mSource->TransformMarkedTo(id, 0);
00144 mSource->DoUpdateSelectionEvent();
00145 ProcessHighlights();
00146 }
00147
00148 void SaveCollectionEvent(int id)
00149 {
00150 ILOG_SYSTEM("SaveCollectionEvent");
00151
00152 std::map<int, int> marks = mSource->GetMarkedRowsMap();
00153 if(!marks.size()) return;
00154
00155 std::string filename = "marks.csv";
00156 std::ofstream os(filename.c_str());
00157
00158 if(!os.good())
00159 {
00160 ILOG_ERROR("Can not write to " << filename);
00161 return;
00162 }
00163
00164 #ifndef MEDIATABLE_VIDEOLYMPICS
00165 os << "row,mark,mark1,mark2,mark3,mark4,mark5,mark6,mark7,mark8\n";
00166 #else
00167 os << "row,mark,mark1,mark2,mark3,mark4,mark5,mark6,mark7,mark8,shotname\n";
00168 #endif
00169 std::map<int, int>::iterator iter;
00170 for (iter = marks.begin(); iter != marks.end(); iter++)
00171 {
00172 os << iter->first << ",";
00173 os << iter->second << ",";
00174 os << ((iter->second & 1) ? "1" : "0") << ",";
00175 os << ((iter->second & 2) ? "1" : "0") << ",";
00176 os << ((iter->second & 4) ? "1" : "0") << ",";
00177 os << ((iter->second & 8) ? "1" : "0") << ",";
00178 os << ((iter->second & 16) ? "1" : "0") << ",";
00179 os << ((iter->second & 32) ? "1" : "0") << ",";
00180 os << ((iter->second & 64) ? "1" : "0") << ",";
00181 os << ((iter->second & 128) ? "1" : "0");
00182 #ifdef MEDIATABLE_VIDEOLYMPICS
00183 os << "," << mSource->GetTextDataByID("shotname", iter->first);
00184 #endif
00185 os << "\n";
00186 }
00187 os.close();
00188 ILOG_DEBUG("Written " << marks.size() << " to " << filename);
00189 }
00190
00191 void LoadCollectionEvent(int id)
00192 {
00193 ILOG_SYSTEM("SaveCollectionEvent");
00194
00195 std::string filename = "marks.csv";
00196 std::ifstream is(filename.c_str());
00197
00198 if(!is.good())
00199 {
00200 ILOG_ERROR("Can not read from " << filename);
00201 return;
00202 }
00203
00204 std::string line, cell;
00205 int row, mark;
00206 int marks = 0;
00207 bool first = true;
00208 while(is.good()) {
00209 std::getline(is, line);
00210 if(line.length() == 0) continue;
00211 std::stringstream lineStream(line);
00212
00213 if(first) {
00214 first = false;
00215 if(!std::getline(lineStream,cell,',')) first = true;
00216 if(cell != "row") first = true;
00217 if(!std::getline(lineStream,cell,',')) first = true;
00218 if(cell != "mark") first = true;
00219
00220 if(first)
00221 {
00222 ILOG_ERROR(filename << " does not have correct column header.");
00223 break;
00224 }
00225 } else {
00226 if(!std::getline(lineStream,cell,',')) continue;
00227 row = atoi(cell.c_str());
00228 if(!std::getline(lineStream,cell,',')) continue;
00229 mark = atoi(cell.c_str());
00230
00231 mSource->SetMarkById(row, mark);
00232 marks++;
00233 }
00234 }
00235 UpdateItemCounts();
00236 ILOG_DEBUG("Read " << marks << " from " << filename);
00237 }
00238 void ProcessHighlights()
00239 {
00240 int id = mSource->GetMarkFilter();
00241 for (int x=0; x<mItems.size(); x++)
00242 mItems[x]->SetHighlighted(false);
00243
00244 if (id==-1)
00245 mItems[0]->SetHighlighted(true);
00246 else
00247 {
00248 if (id== 0) mItems[1]->SetHighlighted(true);
00249 if (id & 1) mItems[2]->SetHighlighted(true);
00250 if (id & 2) mItems[3]->SetHighlighted(true);
00251 if (id & 4) mItems[4]->SetHighlighted(true);
00252 if (id & 8) mItems[5]->SetHighlighted(true);
00253 if (id & 16) mItems[6]->SetHighlighted(true);
00254 if (id & 32) mItems[7]->SetHighlighted(true);
00255 if (id & 64) mItems[8]->SetHighlighted(true);
00256 if (id & 128) mItems[9]->SetHighlighted(true);
00257 }
00258 }
00259
00260 private :
00261
00262 int MarkToColor(int m)
00263 {
00264 int r=127,g=127,b=127;
00265 if (m == -1)
00266 return ARGB2COLOR(255, 200, 200, 200);
00267 if (m & 1)
00268 r = g = b = 255;
00269 if (m & 2)
00270 r = g = b = 20;
00271 if (m & 4)
00272 r += 64;
00273 if (m & 8)
00274 g += 64;
00275 if (m & 16)
00276 b += 64;
00277 if (m & 32)
00278 { r += 64; g += 64; }
00279 if (m & 64)
00280 { g += 64; b += 64; }
00281 if (m & 128)
00282 { r += 64; b += 64; }
00283 if (r>255) r = 255;
00284 if (g>255) g = 255;
00285 if (b>255) b = 255;
00286 return ARGB2COLOR(255, r,g,b);
00287 }
00288
00289 SelectionCollectionItem*
00290 MakeSelectionCollectionItem(std::string name, int markID)
00291 {
00292 Window* pane = mItemWindow->ContentPane();
00293 SelectionCollectionItem *item =
00294 new SelectionCollectionItem(pane, markID, MarkToColor(markID));
00295 item->SetItemName(name);
00296 item->SetListener(this);
00297 return item;
00298 }
00299
00300 void Init(TableDataView *source, int w, int h)
00301 {
00302 mSource = source;
00303 mSource->AddTableDataViewListener(this);
00304
00305 SetBorderType(1);
00306 SetBorderFillShaded(-2);
00307
00308 mItemWindow = new ScrollWnd(this, 8, 8, W()-16, H()-16, true, true, false);
00309 mItemWindow->ConnectTo(this);
00310 mItemWindow->ContentPane()->SetDimensions(0, 0, 10 * 90, 90);
00311 mItemWindow->SetBorderType(0);
00312
00313 mItems.push_back(MakeSelectionCollectionItem("everything", -1));
00314 mItems.push_back(MakeSelectionCollectionItem("unknown", 0));
00315 mItems.push_back(MakeSelectionCollectionItem("selected", 1));
00316 mItems.push_back(MakeSelectionCollectionItem("seen", 2));
00317 mItems.push_back(MakeSelectionCollectionItem("red", 4));
00318 mItems.push_back(MakeSelectionCollectionItem("green", 8));
00319 mItems.push_back(MakeSelectionCollectionItem("blue", 16));
00320 mItems.push_back(MakeSelectionCollectionItem("yellow", 32));
00321 mItems.push_back(MakeSelectionCollectionItem("cyan", 64));
00322 #ifndef MEDIATABLE_VIDEOLYMPICS
00323 mItems.push_back(MakeSelectionCollectionItem("other", 128));
00324 #else
00325 #ifdef WIN32
00326 mItems.push_back(MakeSelectionCollectionItem("VideOlympic", 128));
00327 #else
00328 mItems.push_back(MakeSelectionCollectionItem("VideOlympics", 128));
00329 #endif
00330 #endif
00331
00332 mItems[0]->SetHighlighted(true);
00333 }
00334
00335 TableDataView* mSource;
00336 ScrollWnd* mItemWindow;
00337
00338 std::vector<SelectionCollectionItem*> mItems;
00339
00340 ILOG_VAR_DEC;
00341 };
00342
00343 ILOG_VAR_INIT(SelectionCollection, Application.MediaTable);
00344
00345
00346 }
00347 }
00348 }
00349
00350 #endif // SelectionCollection_h