activities viewed ordered by activity id and start time
[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 <activity.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(QWidget *parent)
24     : QMainWindow(parent)
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 activity map
43     Activity::updateActivityMap();
44
45     connect(dayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateDayView(const QDate &)));
46     connect(activityDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateActivitiesDayView(const QDate &)));
47     connect(favouriteDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateFavouritesDayView(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     actTreeView->setHeaderHidden(true);
67     actTreeView->setRootIsDecorated(false);
68     actTreeView->setIndentation(0);
69     actTreeView->setAnimated(true);
70     actTreeView->setModel(new EventModel());
71     actTreeView->setItemDelegate(new Delegate(actTreeView));
72
73     // event clicked
74     connect(dayTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
75     connect(favTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
76     connect(actTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
77     // request for map to be displayed
78     connect(dayTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
79     connect(favTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
80     connect(actTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
81
82
83     // TESTING: load some 'fav' data
84     if(Conference::getAll().count()) // no conference(s) in the DB
85     {
86         static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
87         favTreeView->reset();
88     }
89
90     if(!Conference::getAll().count()) // no conference(s) in the DB
91     {
92         dayNavigator->hide(); // hide DayNavigatorWidget
93         activityDayNavigator->hide();
94     }
95     else
96     {
97         QDate aStartDate = Conference::getById(confId).start();
98         QDate aEndDate = Conference::getById(confId).end();
99         dayNavigator->setDates(aStartDate, aEndDate);
100         activityDayNavigator->setDates(aStartDate, aEndDate);
101         favouriteDayNavigator->setDates(aStartDate, aEndDate);
102     }
103
104     connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTab(int)));
105
106 }
107
108 MainWindow::~MainWindow()
109 {
110     if(mSqlEngine)
111     {
112         delete mSqlEngine;
113         mSqlEngine = NULL;
114     }
115     if(mXmlParser)
116     {
117         delete mXmlParser;
118         mXmlParser = NULL;
119     }
120 }
121
122 void MainWindow::importSchedule()
123 {
124     QFile file(":/schedule.en.xml");
125     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
126     {
127         qDebug() << "can't open " << file.fileName();
128         return;
129     }
130
131     QByteArray data = file.readAll();
132     mXmlParser->parseData(data,mSqlEngine);
133
134     if(Conference::getAll().count())
135     {
136         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
137         QDate aStartDate = Conference::getById(confId).start();
138         QDate aEndDate = Conference::getById(confId).end();
139         dayNavigator->setDates(aStartDate, aEndDate);
140         activityDayNavigator->setDates(aStartDate, aEndDate);
141     }
142     //update activity map
143     Activity::updateActivityMap();
144 }
145
146 void MainWindow::showParsingProgress(int aStatus)
147 {
148     QString msg = QString("Parsing completed: %1\%").arg(aStatus);
149     statusBar()->showMessage(msg,1000);
150 }
151
152 void MainWindow::aboutApp()
153 {
154     QDialog dialog(this);
155     Ui::AboutDialog ui;
156     ui.setupUi(&dialog);
157     dialog.exec();
158 }
159
160 void MainWindow::updateDayView(const QDate &aDate)
161 {
162     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId);
163     dayTreeView->reset();
164     dayNavigator->show();
165 }
166
167 void MainWindow::updateTab(const int aIndex)
168 {
169     switch(aIndex)
170     {
171     case 0://index 0 of tabWidget: dayViewTab
172         {
173             static_cast<EventModel*>(dayTreeView->model())->loadEvents(Conference::getById(confId).start(),confId);
174             dayTreeView->reset();
175             dayNavigator->show();
176         }
177         break;
178     case 1: //index 1 of tabWidget: favouritesTab
179         {
180             static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
181             favTreeView->reset();
182             favouriteDayNavigator->show();
183         }
184         break;
185     case 2: //index 2 of tabWidget: activitiesTab
186         {
187             static_cast<EventModel*>(actTreeView->model())->loadEventsByActivities(Conference::getById(confId).start(), confId);
188             actTreeView->reset();
189             activityDayNavigator->show();
190         }
191         break;
192     default:
193         {
194
195         }
196     };
197 }
198
199 void MainWindow::updateActivitiesDayView(const QDate &aDate)
200 {
201     static_cast<EventModel*>(actTreeView->model())->loadEventsByActivities(aDate, confId);
202     actTreeView->reset();
203     activityDayNavigator->show();
204 }
205
206 void MainWindow::updateFavouritesDayView(const QDate &aDate)
207 {
208     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(aDate,confId);
209     favTreeView->reset();
210     favouriteDayNavigator->show();
211 }
212
213 void MainWindow::itemClicked(const QModelIndex &aIndex)
214 {
215     // have to handle only events, not time-groups
216     if(!aIndex.parent().isValid()) // time-group
217         return;
218
219     EventDialog dialog(aIndex,this);
220     dialog.exec();
221 }
222
223 void MainWindow::displayMap(const QModelIndex &aIndex)
224 {
225     Event *event = static_cast<Event*>(aIndex.internalPointer());
226
227     // room names are stored in lower-case format
228     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
229     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
230     if(!QFile::exists(mapPath))
231         mapPath = QString(":/maps/rooms/not-available.png");
232
233     QString roomName;
234     if(mapPath.contains("not-available", Qt::CaseInsensitive))
235         roomName = QString("Map is not available: %1").arg(event->room());
236     else
237         roomName = event->room();
238
239     QPixmap map(mapPath);
240     MapWindow window(map,roomName,this);
241     window.exec();
242 }