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