47bb535be12066db5a4f783311d494bca1dc196b
[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     }
216 }
217
218 void MainWindow::aboutApp()
219 {
220     QDialog dialog(this);
221     Ui::AboutDialog ui;
222     ui.setupUi(&dialog);
223     dialog.exec();
224 }
225
226 void MainWindow::updateDayView(const QDate &aDate)
227 {
228     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,AppSettings::confId());
229     dayTreeView->reset();
230     dayNavigator->show();
231 }
232
233 void MainWindow::updateTracksView(const QDate &aDate)
234 {
235     static_cast<EventModel*>(trackTreeView->model())->loadEventsByTrack(aDate, AppSettings::confId());
236     trackTreeView->reset();
237     trackDayNavigator->show();
238 }
239
240 void MainWindow::updateFavouritesView(const QDate &aDate)
241 {
242     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(aDate,AppSettings::confId());
243     favTreeView->reset();
244     favouriteDayNavigator->show();
245 }
246
247 void MainWindow::updateSearchView(const QDate &aDate)
248 {
249 /*
250     searchTreeView->reset();
251     int eventsCount = static_cast<EventModel*>(searchTreeView->model())->loadSearchResultEvents(aDate,AppSettings::confId());
252     if( eventsCount ){
253         searchVerticalWidget->show();
254         searchAgainButton->show();
255         searchTreeView->show();
256         searchHead->hide();
257     }
258     else{
259         searchTreeView->hide();
260         searchVerticalWidget->hide();
261         searchHead->show();
262     }
263 */
264 }
265
266 void MainWindow::updateNowView()
267 {
268     EventModel *model = static_cast<EventModel*>(nowTreeView->model());
269     model->loadNowEvents(AppSettings::confId());
270     nowTreeView->reset();
271     nowTreeView->setAllExpanded(true);
272 }
273
274 void MainWindow::updateRoomView(const QDate &aDate)
275 {
276     static_cast<EventModel*>(roomTreeView->model())->loadEventsByRoom(aDate, AppSettings::confId());
277     roomTreeView->reset();
278     roomDayNavigator->show();
279 }
280
281 void MainWindow::itemClicked(const QModelIndex &aIndex)
282 {
283     // have to handle only events, not time-groups
284     if(!aIndex.parent().isValid()) // time-group
285         return;
286
287     EventDialog dialog(static_cast<Event*>(aIndex.internalPointer())->id(),this);
288     dialog.exec();
289 }
290
291 void MainWindow::displayMap(const QModelIndex &aIndex)
292 {
293     Event *event = static_cast<Event*>(aIndex.internalPointer());
294
295     // room names are stored in lower-case format
296     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
297     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
298     if(!QFile::exists(mapPath))
299         mapPath = QString(":/maps/rooms/not-available.png");
300
301     QString roomName;
302     if(mapPath.contains("not-available", Qt::CaseInsensitive))
303         roomName = QString("Map is not available: %1").arg(event->room());
304     else
305         roomName = event->room();
306
307     QPixmap map(mapPath);
308     MapWindow window(map,roomName,this);
309     window.exec();
310 }
311
312 void MainWindow::searchClicked()
313 {
314     QList<QString> columns;
315
316     if( searchTitle->isChecked() )
317         columns.append( "title" );
318     if( searchAbstract->isChecked() )
319         columns.append( "abstract" );
320
321     mSqlEngine->searchEvent( AppSettings::confId(), columns, searchEdit->text() );
322     updateSearchView( Conference::getById(AppSettings::confId()).start() );
323 }
324
325 void MainWindow::searchAgainClicked()
326 {
327     searchHead->show();
328     searchAgainButton->hide();
329 }
330
331 void MainWindow::conferenceMapClicked()
332 {
333
334     QString mapPath = QString(":/maps/campus.png");
335     if(!QFile::exists(mapPath))
336         mapPath = QString(":/maps/rooms/not-available.png");
337
338     QString roomName;
339
340     QPixmap map(mapPath);
341     MapWindow window(map,roomName,this);
342     window.exec();
343 }
344
345 void MainWindow::displayWarning(const QModelIndex &aIndex)
346 {
347     Q_UNUSED(aIndex);
348
349     QMessageBox::warning(
350     this,
351     tr("Time Conflict Warning"),
352     tr("This event happens at the same time than another one of your favourites.") );
353 }
354
355 void MainWindow::eventHasChanged(int aEventId)
356 {
357     static_cast<EventModel*>(dayTreeView->model())->updateModel(aEventId);
358     static_cast<EventModel*>(favTreeView->model())->updateModel(aEventId);
359     static_cast<EventModel*>(trackTreeView->model())->updateModel(aEventId);
360     static_cast<EventModel*>(searchTreeView->model())->updateModel(aEventId);
361     static_cast<EventModel*>(nowTreeView->model())->updateModel(aEventId);
362 }
363
364 void MainWindow::tabHasChanged(int aIndex)
365 {
366     Q_UNUSED(aIndex);
367
368     // TODO: only if it changed to favourities tab
369     updateFavouritesView(favouriteDayNavigator->getCurrentDate());
370     // TODO: only if it changed to now tab
371     //updateNowView();
372 }
373