67440e2cf894ad4f0775399c6cb63fda6d9ce6c8
[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
57     // DAY EVENTS View
58     dayTreeView->setHeaderHidden(true);
59     dayTreeView->setRootIsDecorated(false);
60     dayTreeView->setIndentation(0);
61     dayTreeView->setAnimated(true);
62     dayTreeView->setModel(new EventModel());
63     dayTreeView->setItemDelegate(new Delegate(dayTreeView));
64
65     // FAVOURITIES View
66     favTreeView->setHeaderHidden(true);
67     favTreeView->setRootIsDecorated(false);
68     favTreeView->setIndentation(0);
69     favTreeView->setAnimated(true);
70     favTreeView->setModel(new EventModel());
71     favTreeView->setItemDelegate(new Delegate(favTreeView));
72
73     // TRACKS View
74     trackTreeView->setHeaderHidden(true);
75     trackTreeView->setRootIsDecorated(false);
76     trackTreeView->setIndentation(0);
77     trackTreeView->setAnimated(true);
78     trackTreeView->setModel(new EventModel());
79     trackTreeView->setItemDelegate(new Delegate(trackTreeView));
80
81     // SEARCH EVENTS View
82         searchTreeView->setHeaderHidden(true);
83         searchTreeView->setRootIsDecorated(false);
84         searchTreeView->setIndentation(0);
85         searchTreeView->setAnimated(true);
86         searchTreeView->setModel(new EventModel());
87         searchTreeView->setItemDelegate(new Delegate(searchTreeView));
88
89     // NOW View
90         nowTreeView->setHeaderHidden(true);
91         nowTreeView->setRootIsDecorated(false);
92         nowTreeView->setIndentation(0);
93         nowTreeView->setAnimated(true);
94         nowTreeView->setModel(new EventModel());
95         nowTreeView->setItemDelegate(new Delegate(nowTreeView));
96
97     // event details have changed
98     connect(dayTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
99     connect(favTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
100     connect(trackTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
101     connect(searchTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
102     connect(nowTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
103
104     // event clicked
105     connect(dayTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
106     connect(favTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
107     connect(trackTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
108     connect(searchTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
109     connect(nowTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
110     // request for map to be displayed
111     connect(dayTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
112     connect(favTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
113     connect(trackTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
114     connect(searchTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
115     connect(nowTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
116     // request for warning to be displayed
117     connect(dayTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
118     connect(favTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
119     connect(trackTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
120     connect(searchTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
121     connect(nowTreeView, SIGNAL(requestForWarning(const QModelIndex &)), SLOT(displayWarning(const QModelIndex &)));
122     // event search button clicked
123     connect(searchButton, SIGNAL(clicked()), SLOT(searchClicked()));
124     //
125     connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(tabHasChanged(int)));
126
127     if(!Conference::getAll().count()) // no conference(s) in the DB
128     {
129         dayNavigator->hide(); // hide DayNavigatorWidget
130         trackDayNavigator->hide();
131     }
132     else
133     {
134         QDate aStartDate = Conference::getById(AppSettings::confId()).start();
135         QDate aEndDate = Conference::getById(AppSettings::confId()).end();
136         dayNavigator->setDates(aStartDate, aEndDate);
137         trackDayNavigator->setDates(aStartDate, aEndDate);
138         favouriteDayNavigator->setDates(aStartDate, aEndDate);
139         searchDayNavigator->setDates(aStartDate, aEndDate);
140         //
141         conferenceTitle->setText(Conference::getById(AppSettings::confId()).title());
142         conferenceSubtitle->setText(Conference::getById(AppSettings::confId()).subtitle());
143         conferenceWhere->setText(Conference::getById(AppSettings::confId()).city() + ", " + Conference::getById(AppSettings::confId()).venue());
144         conferenceWhen->setText(
145                 Conference::getById(AppSettings::confId()).start().toString("dd-MM-yyyy")
146                 + ", " +
147                 Conference::getById(AppSettings::confId()).end().toString("dd-MM-yyyy"));
148     }
149
150     searchTreeView->hide();
151     searchDayNavigator->hide();
152
153     // open dialog for given Event ID
154     // this is used in case Alarm Dialog request application to start
155     if(aEventId)
156     {
157         try
158         {
159             EventDialog dialog(aEventId,this);
160             dialog.exec();
161         }
162         catch(OrmNoObjectException) {} // just start application
163         catch(...) {} // just start application
164     }
165 }
166
167 MainWindow::~MainWindow()
168 {
169     if(mSqlEngine)
170     {
171         delete mSqlEngine;
172         mSqlEngine = NULL;
173     }
174 }
175
176 void MainWindow::importSchedule()
177 {
178     ImportScheduleDialog dialog(mSqlEngine,this);
179     dialog.exec();
180     
181     if(Conference::getAll().count())
182     {
183         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
184         QDate aStartDate = Conference::getById(AppSettings::confId()).start();
185         QDate aEndDate = Conference::getById(AppSettings::confId()).end();
186         dayNavigator->setDates(aStartDate, aEndDate);
187         trackDayNavigator->setDates(aStartDate, aEndDate);
188     }
189 }
190
191 void MainWindow::aboutApp()
192 {
193     QDialog dialog(this);
194     Ui::AboutDialog ui;
195     ui.setupUi(&dialog);
196     dialog.exec();
197 }
198
199 void MainWindow::updateDayView(const QDate &aDate)
200 {
201     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,AppSettings::confId());
202     dayTreeView->reset();
203     dayNavigator->show();
204 }
205
206 void MainWindow::updateTracksView(const QDate &aDate)
207 {
208     static_cast<EventModel*>(trackTreeView->model())->loadEventsByTrack(aDate, AppSettings::confId());
209     trackTreeView->reset();
210     trackDayNavigator->show();
211 }
212
213 void MainWindow::updateFavouritesView(const QDate &aDate)
214 {
215     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(aDate,AppSettings::confId());
216     favTreeView->reset();
217     favouriteDayNavigator->show();
218 }
219
220 void MainWindow::updateSearchView(const QDate &aDate)
221 {
222     searchTreeView->reset();
223     int eventsCount = static_cast<EventModel*>(searchTreeView->model())->loadSearchResultEvents(aDate,AppSettings::confId());
224     if( eventsCount ){
225         searchDayNavigator->show();
226         searchTreeView->show();
227     }
228     else{
229         searchTreeView->hide();
230         searchDayNavigator->hide();
231     }
232 }
233
234 void MainWindow::updateNowView()
235 {
236     EventModel *model = static_cast<EventModel*>(nowTreeView->model());
237     model->loadNowEvents(AppSettings::confId());
238     nowTreeView->reset();
239     nowTreeView->setAllExpanded(true);
240 }
241
242 void MainWindow::itemClicked(const QModelIndex &aIndex)
243 {
244     // have to handle only events, not time-groups
245     if(!aIndex.parent().isValid()) // time-group
246         return;
247
248     EventDialog dialog(static_cast<Event*>(aIndex.internalPointer())->id(),this);
249     dialog.exec();
250 }
251
252 void MainWindow::displayMap(const QModelIndex &aIndex)
253 {
254     Event *event = static_cast<Event*>(aIndex.internalPointer());
255
256     // room names are stored in lower-case format
257     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
258     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
259     if(!QFile::exists(mapPath))
260         mapPath = QString(":/maps/rooms/not-available.png");
261
262     QString roomName;
263     if(mapPath.contains("not-available", Qt::CaseInsensitive))
264         roomName = QString("Map is not available: %1").arg(event->room());
265     else
266         roomName = event->room();
267
268     QPixmap map(mapPath);
269     MapWindow window(map,roomName,this);
270     window.exec();
271 }
272
273 void MainWindow::searchClicked()
274 {
275     QList<QString> columns;
276
277     if( searchTitle->isChecked() )
278         columns.append( "title" );
279     if( searchAbstract->isChecked() )
280         columns.append( "abstract" );
281
282     mSqlEngine->searchEvent( AppSettings::confId(), columns, searchEdit->text() );
283     updateSearchView( Conference::getById(AppSettings::confId()).start() );
284 }
285
286 void MainWindow::displayWarning(const QModelIndex &aIndex)
287 {
288     Q_UNUSED(aIndex);
289
290     QMessageBox::warning(
291     this,
292     tr("Time Conflict Warning"),
293     tr("This event happens at the same time than another one of your favourites.") );
294 }
295
296 void MainWindow::eventHasChanged(int aEventId)
297 {
298     static_cast<EventModel*>(dayTreeView->model())->updateModel(aEventId);
299     static_cast<EventModel*>(favTreeView->model())->updateModel(aEventId);
300     static_cast<EventModel*>(trackTreeView->model())->updateModel(aEventId);
301     static_cast<EventModel*>(searchTreeView->model())->updateModel(aEventId);
302     static_cast<EventModel*>(nowTreeView->model())->updateModel(aEventId);
303 }
304
305 void MainWindow::tabHasChanged(int aIndex)
306 {
307     Q_UNUSED(aIndex);
308
309     // TODO: only if it changed to favourities tab
310     updateFavouritesView(favouriteDayNavigator->getCurrentDate());
311     // TODO: only if it changed to now tab
312     updateNowView();
313 }
314