Warning handling
[toast/confclerk.git] / src / gui / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include <QTreeView>
4 #include <QDirModel>
5
6 #include <sqlengine.h>
7
8 #include <track.h>
9 #include <eventmodel.h>
10 #include <delegate.h>
11
12 #include <conference.h>
13
14 #include <QDialog>
15 #include <QMessageBox>
16 #include "ui_about.h"
17 #include "eventdialog.h"
18 #include "daynavigatorwidget.h"
19 #include "importscheduledialog.h"
20 #include "mapwindow.h"
21
22
23 const int confId = 1;
24
25 MainWindow::MainWindow(int aEventId, QWidget *aParent)
26     : QMainWindow(aParent)
27 {
28     setupUi(this);
29
30     // connect Menu actions
31     connect(actionImportSchedule, SIGNAL(triggered()), SLOT(importSchedule()));
32     connect(actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
33     connect(actionAboutApplication, SIGNAL(triggered()), SLOT(aboutApp()));
34
35     // create "SQLITE" DB instance/connection
36     // opens DB connection (needed for EventModel)
37     mSqlEngine = new SqlEngine(this);
38     mSqlEngine->initialize();
39
40     //update track map
41     Track::updateTrackMap();
42
43     connect(dayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateDayView(const QDate &)));
44     connect(trackDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateTracksView(const QDate &)));
45     connect(favouriteDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateFavouritesView(const QDate &)));
46     connect(searchDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateSearchView(const QDate &)));
47
48     // DAY EVENTS View
49     dayTreeView->setHeaderHidden(true);
50     dayTreeView->setRootIsDecorated(false);
51     dayTreeView->setIndentation(0);
52     dayTreeView->setAnimated(true);
53     dayTreeView->setModel(new EventModel());
54     dayTreeView->setItemDelegate(new Delegate(dayTreeView));
55
56     // FAVOURITIES View
57     favTreeView->setHeaderHidden(true);
58     favTreeView->setRootIsDecorated(false);
59     favTreeView->setIndentation(0);
60     favTreeView->setAnimated(true);
61     favTreeView->setModel(new EventModel());
62     favTreeView->setItemDelegate(new Delegate(favTreeView));
63
64     //ACTIVITIES View
65     trackTreeView->setHeaderHidden(true);
66     trackTreeView->setRootIsDecorated(false);
67     trackTreeView->setIndentation(0);
68     trackTreeView->setAnimated(true);
69     trackTreeView->setModel(new EventModel());
70     trackTreeView->setItemDelegate(new Delegate(trackTreeView));
71
72     // SEARCH EVENTS View
73         searchTreeView->setHeaderHidden(true);
74         searchTreeView->setRootIsDecorated(false);
75         searchTreeView->setIndentation(0);
76         searchTreeView->setAnimated(true);
77         searchTreeView->setModel(new EventModel());
78         searchTreeView->setItemDelegate(new Delegate(searchTreeView));
79
80     // event clicked
81     connect(dayTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
82     connect(favTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
83     connect(trackTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
84     connect(searchTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
85     // request for map to be displayed
86     connect(dayTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
87     connect(favTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
88     connect(trackTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
89     connect(searchTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
90     // request for warning to be displayed
91     connect(dayTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
92     connect(favTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
93     connect(trackTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
94     connect(searchTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
95     // event search button clicked
96     connect(searchButton, SIGNAL(clicked()), SLOT(searchClicked()));
97
98     // TESTING: load some 'fav' data
99     if(Conference::getAll().count()) // no conference(s) in the DB
100     {
101         static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
102         favTreeView->reset();
103     }
104
105     if(!Conference::getAll().count()) // no conference(s) in the DB
106     {
107         dayNavigator->hide(); // hide DayNavigatorWidget
108         trackDayNavigator->hide();
109     }
110     else
111     {
112         QDate aStartDate = Conference::getById(confId).start();
113         QDate aEndDate = Conference::getById(confId).end();
114         dayNavigator->setDates(aStartDate, aEndDate);
115         trackDayNavigator->setDates(aStartDate, aEndDate);
116         favouriteDayNavigator->setDates(aStartDate, aEndDate);
117         searchDayNavigator->setDates(aStartDate, aEndDate);
118     }
119
120     connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTab(int)));
121
122     searchTreeView->hide();
123     searchDayNavigator->hide();
124
125     // open dialog for given Event ID
126     // this is used in case Alarm Dialog request application to start
127     if(aEventId)
128     {
129         try
130         {
131             EventDialog dialog(aEventId,this);
132             dialog.exec();
133         }
134         catch(OrmNoObjectException) {} // just start application
135         catch(...) {} // just start application
136     }
137 }
138
139 MainWindow::~MainWindow()
140 {
141     if(mSqlEngine)
142     {
143         delete mSqlEngine;
144         mSqlEngine = NULL;
145     }
146 }
147
148 void MainWindow::importSchedule()
149 {
150     ImportScheduleDialog dialog(mSqlEngine,this);
151     dialog.exec();
152     
153     if(Conference::getAll().count())
154     {
155         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
156         QDate aStartDate = Conference::getById(confId).start();
157         QDate aEndDate = Conference::getById(confId).end();
158         dayNavigator->setDates(aStartDate, aEndDate);
159         //update activity map
160         Track::updateTrackMap();
161         trackDayNavigator->setDates(aStartDate, aEndDate);
162     }
163 }
164
165 void MainWindow::aboutApp()
166 {
167     QDialog dialog(this);
168     Ui::AboutDialog ui;
169     ui.setupUi(&dialog);
170     dialog.exec();
171 }
172
173 void MainWindow::updateDayView(const QDate &aDate)
174 {
175     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId);
176     dayTreeView->reset();
177     dayNavigator->show();
178 }
179
180 void MainWindow::updateTab(const int aIndex)
181 {
182     switch(aIndex)
183     {
184     case 0://index 0 of tabWidget: dayViewTab
185         {
186             updateDayView(dayNavigator->getCurrentDate());
187         }
188         break;
189     case 1: //index 1 of tabWidget: favouritesTab
190         {
191             updateFavouritesView(favouriteDayNavigator->getCurrentDate());
192         }
193         break;
194     case 2: //index 2 of tabWidget: activitiesTab
195         {
196             updateTracksView(trackDayNavigator->getCurrentDate());
197         }
198         break;
199     case 3: //index 3 of tabWidget: searchTab
200        {
201            updateSearchView( searchDayNavigator->getCurrentDate() );
202        }
203        break;
204     default:
205         {
206
207         }
208     };
209 }
210
211 void MainWindow::updateTracksView(const QDate &aDate)
212 {
213     static_cast<EventModel*>(trackTreeView->model())->loadEventsByTrack(aDate, confId);
214     trackTreeView->reset();
215     trackDayNavigator->show();
216 }
217
218 void MainWindow::updateFavouritesView(const QDate &aDate)
219 {
220     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(aDate,confId);
221     favTreeView->reset();
222     favouriteDayNavigator->show();
223 }
224
225 void MainWindow::updateSearchView(const QDate &aDate)
226 {
227     searchTreeView->reset();
228     int eventsCount = static_cast<EventModel*>(searchTreeView->model())->loadSearchResultEvents(aDate,confId);
229     if( eventsCount ){
230         searchDayNavigator->show();
231         searchTreeView->show();
232     }
233     else{
234         searchTreeView->hide();
235         searchDayNavigator->hide();
236     }
237 }
238
239 void MainWindow::itemClicked(const QModelIndex &aIndex)
240 {
241     // have to handle only events, not time-groups
242     if(!aIndex.parent().isValid()) // time-group
243         return;
244
245     EventDialog dialog(static_cast<Event*>(aIndex.internalPointer())->id(),this);
246     dialog.exec();
247 }
248
249 void MainWindow::displayMap(const QModelIndex &aIndex)
250 {
251     Event *event = static_cast<Event*>(aIndex.internalPointer());
252
253     // room names are stored in lower-case format
254     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
255     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
256     if(!QFile::exists(mapPath))
257         mapPath = QString(":/maps/rooms/not-available.png");
258
259     QString roomName;
260     if(mapPath.contains("not-available", Qt::CaseInsensitive))
261         roomName = QString("Map is not available: %1").arg(event->room());
262     else
263         roomName = event->room();
264
265     QPixmap map(mapPath);
266     MapWindow window(map,roomName,this);
267     window.exec();
268 }
269
270 void MainWindow::searchClicked()
271 {
272     QList<QString> columns;
273
274     if( searchTitle->isChecked() )
275         columns.append( "title" );
276     if( searchAbstract->isChecked() )
277         columns.append( "abstract" );
278
279     mSqlEngine->searchEvent( confId, columns, searchEdit->text() );
280     updateSearchView( Conference::getById(confId).start() );
281 }
282
283 void MainWindow::displayWarning(const QModelIndex &aIndex)
284 {
285     QMessageBox::warning(
286     this,
287     tr("Time Conflict Warning"),
288     tr("This event happens at the same time than another one of your favourites.") );
289 }