single-click is used to open event dialog
[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 <eventmodel.h>
10 #include <delegate.h>
11
12 #include <conference.h>
13
14 #include <QDialog>
15 #include "ui_about.h"
16 #include "eventdialog.h"
17 #include "daynavigatorwidget.h"
18 #include "mapwindow.h"
19
20 const int confId = 1;
21
22 MainWindow::MainWindow(QWidget *parent)
23     : QMainWindow(parent)
24 {
25     setupUi(this);
26
27     // connect Menu actions
28     connect(actionImportSchedule, SIGNAL(triggered()), SLOT(importSchedule()));
29     connect(actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
30     connect(actionAboutApplication, SIGNAL(triggered()), SLOT(aboutApp()));
31
32     // create "SQLITE" DB instance/connection
33     // opens DB connection (needed for EventModel)
34     mSqlEngine = new SqlEngine(this);
35     mSqlEngine->initialize();
36
37     mXmlParser = new ScheduleXmlParser(this);
38     connect(mXmlParser, SIGNAL(progressStatus(int)), this, SLOT(showParsingProgress(int)));
39     statusBar()->showMessage(tr("Ready"));
40
41     connect(dayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateDayView(const QDate &)));
42     connect(activityDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateActivitiesDayView(const QDate &)));
43     connect(favouriteDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateFavouritesDayView(const QDate &)));
44
45     // DAY EVENTS View
46     dayTreeView->setHeaderHidden(true);
47     dayTreeView->setRootIsDecorated(false);
48     dayTreeView->setIndentation(0);
49     dayTreeView->setAnimated(true);
50     dayTreeView->setModel(new EventModel());
51     dayTreeView->setItemDelegate(new Delegate(dayTreeView));
52
53     // FAVOURITIES View
54     favTreeView->setHeaderHidden(true);
55     favTreeView->setRootIsDecorated(false);
56     favTreeView->setIndentation(0);
57     favTreeView->setAnimated(true);
58     favTreeView->setModel(new EventModel());
59     favTreeView->setItemDelegate(new Delegate(favTreeView));
60
61     //ACTIVITIES View
62     actTreeView->setHeaderHidden(true);
63     actTreeView->setRootIsDecorated(false);
64     actTreeView->setIndentation(0);
65     actTreeView->setAnimated(true);
66     actTreeView->setModel(new EventModel());
67     actTreeView->setItemDelegate(new Delegate(actTreeView));
68
69     // event clicked
70     connect(dayTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
71     connect(favTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
72     connect(actTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
73     // request for map to be displayed
74     connect(dayTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
75     connect(favTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
76     connect(actTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
77
78
79     // TESTING: load some 'fav' data
80     if(Conference::getAll().count()) // no conference(s) in the DB
81     {
82         static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
83         favTreeView->reset();
84     }
85
86     if(!Conference::getAll().count()) // no conference(s) in the DB
87     {
88         dayNavigator->hide(); // hide DayNavigatorWidget
89         activityDayNavigator->hide();
90     }
91     else
92     {
93         QDate aStartDate = Conference::getById(confId).start();
94         QDate aEndDate = Conference::getById(confId).end();
95         dayNavigator->setDates(aStartDate, aEndDate);
96         activityDayNavigator->setDates(aStartDate, aEndDate);
97         favouriteDayNavigator->setDates(aStartDate, aEndDate);
98     }
99
100     connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTab(int)));
101
102 }
103
104 MainWindow::~MainWindow()
105 {
106     if(mSqlEngine)
107     {
108         delete mSqlEngine;
109         mSqlEngine = NULL;
110     }
111     if(mXmlParser)
112     {
113         delete mXmlParser;
114         mXmlParser = NULL;
115     }
116 }
117
118 void MainWindow::importSchedule()
119 {
120     QFile file(":/schedule.en.xml");
121     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
122     {
123         qDebug() << "can't open " << file.fileName();
124         return;
125     }
126
127     QByteArray data = file.readAll();
128     mXmlParser->parseData(data,mSqlEngine);
129
130     if(Conference::getAll().count())
131     {
132         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
133         QDate aStartDate = Conference::getById(confId).start();
134         QDate aEndDate = Conference::getById(confId).end();
135         dayNavigator->setDates(aStartDate, aEndDate);
136         activityDayNavigator->setDates(aStartDate, aEndDate);
137     }
138 }
139
140 void MainWindow::showParsingProgress(int aStatus)
141 {
142     QString msg = QString("Parsing completed: %1\%").arg(aStatus);
143     statusBar()->showMessage(msg,1000);
144 }
145
146 void MainWindow::aboutApp()
147 {
148     QDialog dialog(this);
149     Ui::AboutDialog ui;
150     ui.setupUi(&dialog);
151     dialog.exec();
152 }
153
154 void MainWindow::updateDayView(const QDate &aDate)
155 {
156     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId);
157     dayTreeView->reset();
158     dayNavigator->show();
159 }
160
161 void MainWindow::updateTab(const int aIndex)
162 {
163     switch(aIndex)
164     {
165     case 0://index 0 of tabWidget: dayViewTab
166         {
167             static_cast<EventModel*>(dayTreeView->model())->loadEvents(Conference::getById(confId).start(),confId);
168             dayTreeView->reset();
169             dayNavigator->show();
170         }
171         break;
172     case 1: //index 1 of tabWidget: favouritesTab
173         {
174             static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
175             favTreeView->reset();
176             favouriteDayNavigator->show();
177         }
178         break;
179     case 2: //index 2 of tabWidget: activitiesTab
180         {
181             static_cast<EventModel*>(actTreeView->model())->loadEventsByActivities(Conference::getById(confId).start(),confId);
182             actTreeView->reset();
183             activityDayNavigator->show();
184         }
185         break;
186     default:
187         {
188
189         }
190     };
191 }
192
193 void MainWindow::updateActivitiesDayView(const QDate &aDate)
194 {
195     static_cast<EventModel*>(actTreeView->model())->loadEventsByActivities(aDate,confId);
196     actTreeView->reset();
197     activityDayNavigator->show();
198 }
199
200 void MainWindow::updateFavouritesDayView(const QDate &aDate)
201 {
202     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(aDate,confId);
203     favTreeView->reset();
204     favouriteDayNavigator->show();
205 }
206
207 void MainWindow::itemClicked(const QModelIndex &aIndex)
208 {
209     // have to handle only events, not time-groups
210     if(!aIndex.parent().isValid()) // time-group
211         return;
212
213     EventDialog dialog(aIndex,this);
214     dialog.exec();
215 }
216
217 void MainWindow::displayMap(const QModelIndex &aIndex)
218 {
219     Event *event = static_cast<Event*>(aIndex.internalPointer());
220
221     // room names are stored in lower-case format
222     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
223     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
224     if(!QFile::exists(mapPath))
225         mapPath = QString(":/maps/rooms/not-available.png");
226
227     QString roomName;
228     if(mapPath.contains("not-available", Qt::CaseInsensitive))
229         roomName = QString("Map is not available: %1").arg(event->room());
230     else
231         roomName = event->room();
232
233     QPixmap map(mapPath);
234     MapWindow window(map,roomName,this);
235     window.exec();
236 }
237