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