NowTreeView refresh modified
[toast/confclerk.git] / src / gui / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <appsettings.h>
3
4 #include <QTreeView>
5 #include <QDirModel>
6
7 #include <sqlengine.h>
8
9 #include <track.h>
10 #include <eventmodel.h>
11 #include <delegate.h>
12
13 #include <conference.h>
14
15 #include <QDialog>
16 #include <QMessageBox>
17 #include <QTimer>
18 #include "ui_about.h"
19 #include "eventdialog.h"
20 #include "daynavigatorwidget.h"
21 #include "importscheduledialog.h"
22 #include "mapwindow.h"
23
24 MainWindow::MainWindow(int aEventId, QWidget *aParent)
25     : QMainWindow(aParent)
26 {
27     setupUi(this);
28
29     // create "SQLITE" DB instance/connection
30     // opens DB connection (needed for EventModel)
31     mSqlEngine = new SqlEngine(this);
32     mSqlEngine->initialize();
33
34     // Sanity check for existence of any Conference in the DB
35     // it AppSettings::confId() is 0, but there are any Conference(s) in the DB
36     // set the confId in the AppSettings for the ID of the first conference in the DB
37     QList<Conference> confs = Conference::getAll();
38     if(!confs.count()) // no conference(s) in the DB
39     {
40         AppSettings::setConfId(0); // no conference in the DB
41     }
42     else
43     {
44         if(AppSettings::confId() == 0)
45             AppSettings::setConfId(confs[0].id());
46     }
47
48     // connect Menu actions
49     connect(actionImportSchedule, SIGNAL(triggered()), SLOT(importSchedule()));
50     connect(actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
51     connect(actionAboutApplication, SIGNAL(triggered()), SLOT(aboutApp()));
52
53     connect(dayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateDayView(const QDate &)));
54     connect(trackDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateTracksView(const QDate &)));
55     connect(favouriteDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateFavouritesView(const QDate &)));
56     connect(searchDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateSearchView(const QDate &)));
57     connect(roomDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateRoomView(const QDate &)));
58
59     // DAY EVENTS View
60     dayTreeView->setHeaderHidden(true);
61     dayTreeView->setRootIsDecorated(false);
62     dayTreeView->setIndentation(0);
63     dayTreeView->setAnimated(true);
64     dayTreeView->setModel(new EventModel());
65     dayTreeView->setItemDelegate(new Delegate(dayTreeView));
66
67     // FAVOURITIES View
68     favTreeView->setHeaderHidden(true);
69     favTreeView->setRootIsDecorated(false);
70     favTreeView->setIndentation(0);
71     favTreeView->setAnimated(true);
72     favTreeView->setModel(new EventModel());
73     favTreeView->setItemDelegate(new Delegate(favTreeView));
74
75     // TRACKS View
76     trackTreeView->setHeaderHidden(true);
77     trackTreeView->setRootIsDecorated(false);
78     trackTreeView->setIndentation(0);
79     trackTreeView->setAnimated(true);
80     trackTreeView->setModel(new EventModel());
81     trackTreeView->setItemDelegate(new Delegate(trackTreeView));
82
83     // SEARCH EVENTS View
84         searchTreeView->setHeaderHidden(true);
85         searchTreeView->setRootIsDecorated(false);
86         searchTreeView->setIndentation(0);
87         searchTreeView->setAnimated(true);
88         searchTreeView->setModel(new EventModel());
89         searchTreeView->setItemDelegate(new Delegate(searchTreeView));
90
91     // NOW View
92         nowTreeView->setHeaderHidden(true);
93         nowTreeView->setRootIsDecorated(false);
94         nowTreeView->setIndentation(0);
95         nowTreeView->setAnimated(true);
96         nowTreeView->setModel(new EventModel());
97         nowTreeView->setItemDelegate(new Delegate(nowTreeView));
98
99         // NOW View refresh timer
100     QTimer *timer = new QTimer( this );
101     connect( timer, SIGNAL(timeout()), SLOT(timerUpdateNowView()) );
102     timer->start( 30000); // 30 seconds timer
103
104     // ROOMS View
105     roomTreeView->setHeaderHidden(true);
106     roomTreeView->setRootIsDecorated(false);
107     roomTreeView->setIndentation(0);
108     roomTreeView->setAnimated(true);
109     roomTreeView->setModel(new EventModel());
110     roomTreeView->setItemDelegate(new Delegate(roomTreeView));
111
112     // event details have changed
113     connect(dayTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
114     connect(favTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
115     connect(trackTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
116     connect(searchTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
117     connect(nowTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
118     connect(roomTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
119
120     // event clicked
121     connect(dayTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
122     connect(favTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
123     connect(trackTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
124     connect(searchTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
125     connect(nowTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
126     connect(roomTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
127     // request for map to be displayed
128     connect(dayTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
129     connect(favTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
130     connect(trackTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
131     connect(searchTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
132     connect(nowTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
133     connect(roomTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
134     // request for warning to be displayed
135     connect(dayTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
136     connect(favTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
137     connect(trackTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
138     connect(searchTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
139     connect(nowTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
140     connect(roomTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
141     // event search button clicked
142     connect(searchButton, SIGNAL(clicked()), SLOT(searchClicked()));
143     connect(searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
144     // event conference map button clicked
145     connect(showMapButton, SIGNAL(clicked()), SLOT(conferenceMapClicked()));
146     //
147     connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(tabHasChanged(int)));
148     connect(tabWidget, SIGNAL(infoIconClicked()), SLOT(aboutApp()));
149
150     if(!Conference::getAll().count()) // no conference(s) in the DB
151     {
152         dayNavigator->hide(); // hide DayNavigatorWidget
153         trackDayNavigator->hide();
154         roomDayNavigator->hide();
155     }
156     else
157     {
158         QDate aStartDate = Conference::getById(AppSettings::confId()).start();
159         QDate aEndDate = Conference::getById(AppSettings::confId()).end();
160         dayNavigator->setDates(aStartDate, aEndDate);
161         trackDayNavigator->setDates(aStartDate, aEndDate);
162         favouriteDayNavigator->setDates(aStartDate, aEndDate);
163         searchDayNavigator->setDates(aStartDate, aEndDate);
164         roomDayNavigator->setDates(aStartDate, aEndDate);
165         //
166         conferenceTitle->setText(Conference::getById(AppSettings::confId()).title());
167         conferenceSubtitle->setText(Conference::getById(AppSettings::confId()).subtitle());
168         conferenceWhere->setText(Conference::getById(AppSettings::confId()).city() + ", " + Conference::getById(AppSettings::confId()).venue());
169         conferenceWhen->setText(
170                 Conference::getById(AppSettings::confId()).start().toString("dd-MM-yyyy")
171                 + ", " +
172                 Conference::getById(AppSettings::confId()).end().toString("dd-MM-yyyy"));
173     }
174
175     searchTreeView->hide();
176     searchVerticalWidget->hide();
177     searchHead->show();
178
179     // open dialog for given Event ID
180     // this is used in case Alarm Dialog request application to start
181     if(aEventId)
182     {
183         try
184         {
185             EventDialog dialog(aEventId,this);
186             dialog.exec();
187         }
188         catch(OrmNoObjectException&) {} // just start application
189         catch(...) {} // just start application
190     }
191 }
192
193 MainWindow::~MainWindow()
194 {
195     if(mSqlEngine)
196     {
197         delete mSqlEngine;
198         mSqlEngine = NULL;
199     }
200 }
201
202 void MainWindow::importSchedule()
203 {
204     ImportScheduleDialog dialog(mSqlEngine,this);
205     dialog.exec();
206
207     QList<Conference> confs = Conference::getAll();
208     if(!confs.count()) // no conference(s) in the DB
209     {
210         AppSettings::setConfId(0); // no conference in the DB
211     }
212     else
213     {
214         if(AppSettings::confId() == 0)
215             AppSettings::setConfId(confs[0].id());
216
217         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
218         QDate aStartDate = Conference::getById(AppSettings::confId()).start();
219         QDate aEndDate = Conference::getById(AppSettings::confId()).end();
220         dayNavigator->setDates(aStartDate, aEndDate);
221         trackDayNavigator->setDates(aStartDate, aEndDate);
222         roomDayNavigator->setDates(aStartDate, aEndDate);
223     }
224 }
225
226 void MainWindow::aboutApp()
227 {
228     QDialog dialog(this);
229     Ui::AboutDialog ui;
230     ui.setupUi(&dialog);
231     dialog.exec();
232 }
233
234 void MainWindow::updateDayView(const QDate &aDate)
235 {
236     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,AppSettings::confId());
237     dayTreeView->reset();
238     dayNavigator->show();
239 }
240
241 void MainWindow::updateTracksView(const QDate &aDate)
242 {
243     static_cast<EventModel*>(trackTreeView->model())->loadEventsByTrack(aDate, AppSettings::confId());
244     trackTreeView->reset();
245     trackDayNavigator->show();
246 }
247
248 void MainWindow::updateFavouritesView(const QDate &aDate)
249 {
250     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(aDate,AppSettings::confId());
251     favTreeView->reset();
252     favouriteDayNavigator->show();
253 }
254
255 void MainWindow::updateSearchView(const QDate &aDate)
256 {
257     qDebug() << "MainWindow::updateSearchView(), aDate: " << aDate.toString() ;
258     searchTreeView->reset();
259     int eventsCount = static_cast<EventModel*>(searchTreeView->model())->loadSearchResultEvents(aDate,AppSettings::confId());
260     if( eventsCount ||
261             searchDayNavigator->getCurrentDate() != Conference::getById(AppSettings::confId()).start() ){
262         searchVerticalWidget->show();
263         searchAgainButton->show();
264         searchTreeView->show();
265         searchHead->hide();
266     }
267     else{
268         searchTreeView->hide();
269         searchVerticalWidget->hide();
270         searchHead->show();
271     }
272 }
273
274 void MainWindow::updateNowView()
275 {
276     EventModel *model = static_cast<EventModel*>(nowTreeView->model());
277     model->loadNowEvents(AppSettings::confId());
278     nowTreeView->reset();
279     nowTreeView->setAllExpanded(true);
280 }
281
282 void MainWindow::updateRoomView(const QDate &aDate)
283 {
284     static_cast<EventModel*>(roomTreeView->model())->loadEventsByRoom(aDate, AppSettings::confId());
285     roomTreeView->reset();
286     roomDayNavigator->show();
287 }
288
289 void MainWindow::itemClicked(const QModelIndex &aIndex)
290 {
291     // have to handle only events, not time-groups
292     if(!aIndex.parent().isValid()) // time-group
293         return;
294
295     EventDialog dialog(static_cast<Event*>(aIndex.internalPointer())->id(),this);
296     dialog.exec();
297 }
298
299 void MainWindow::displayMap(const QModelIndex &aIndex)
300 {
301     Event *event = static_cast<Event*>(aIndex.internalPointer());
302
303     // room names are stored in lower-case format
304     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
305     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
306     if(!QFile::exists(mapPath))
307         mapPath = QString(":/maps/rooms/not-available.png");
308
309     QString roomName;
310     if(mapPath.contains("not-available", Qt::CaseInsensitive))
311         roomName = QString("Map is not available: %1").arg(event->room());
312     else
313         roomName = event->room();
314
315     QPixmap map(mapPath);
316     MapWindow window(map,roomName,this);
317     window.exec();
318 }
319
320 void MainWindow::searchClicked()
321 {
322     QHash<QString,QString> columns;
323
324     if( searchTitle->isChecked() )
325         columns.insertMulti("EVENT", "title");
326     if( searchAbstract->isChecked() )
327         columns.insertMulti("EVENT", "abstract");
328     if( searchTag->isChecked() )
329         columns.insertMulti("EVENT", "tag");
330     if( searchSpeaker->isChecked() )
331         columns["PERSON"] = "name";
332     if( searchRoom->isChecked() )
333         columns["ROOM"] = "name";
334
335     QString keyword = searchEdit->text().replace( QString("%"), QString("\\%") );
336     qDebug() << "\nKeyword to search: " << keyword;
337     mSqlEngine->searchEvent( AppSettings::confId(), columns, keyword );
338
339     updateSearchView( Conference::getById(AppSettings::confId()).start() );
340 }
341
342 void MainWindow::searchAgainClicked()
343 {
344     searchHead->show();
345     searchAgainButton->hide();
346 }
347
348 void MainWindow::conferenceMapClicked()
349 {
350
351     QString mapPath = QString(":/maps/campus.png");
352     if(!QFile::exists(mapPath))
353         mapPath = QString(":/maps/rooms/not-available.png");
354
355     QString roomName;
356
357     QPixmap map(mapPath);
358     MapWindow window(map,roomName,this);
359     window.exec();
360 }
361
362 void MainWindow::displayWarning(const QModelIndex &aIndex)
363 {
364     Q_UNUSED(aIndex);
365
366     QMessageBox::warning(
367     this,
368     tr("Time Conflict Warning"),
369     tr("This event happens at the same time than another one of your favourites.") );
370 }
371
372 void MainWindow::eventHasChanged(int aEventId)
373 {
374     static_cast<EventModel*>(dayTreeView->model())->updateModel(aEventId);
375     static_cast<EventModel*>(favTreeView->model())->updateModel(aEventId);
376     static_cast<EventModel*>(trackTreeView->model())->updateModel(aEventId);
377     static_cast<EventModel*>(searchTreeView->model())->updateModel(aEventId);
378     static_cast<EventModel*>(nowTreeView->model())->updateModel(aEventId);
379     static_cast<EventModel*>(roomTreeView->model())->updateModel(aEventId);
380 }
381
382 void MainWindow::tabHasChanged(int aIndex)
383 {
384     Q_UNUSED(aIndex);
385
386     // TODO: only if it changed to favourities tab
387     updateFavouritesView(favouriteDayNavigator->getCurrentDate());
388     // TODO: only if it changed to now tab
389     updateNowView();
390 }
391
392 void MainWindow::timerUpdateNowView()
393 {
394         QWidget * pCurrentWidget = tabWidget->widget(tabWidget->currentIndex());
395
396         if( pCurrentWidget != NULL )
397         {
398                 if( pCurrentWidget == tab )
399                     updateNowView();
400         }
401 }