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