temp commit for search tab
[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     // 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(actTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
86     connect(searchTreeView, SIGNAL(doubleClicked(const QModelIndex &)), SLOT(itemDoubleClicked(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(actTreeView, 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         activityDayNavigator->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         activityDayNavigator->setDates(aStartDate, aEndDate);
113         favouriteDayNavigator->setDates(aStartDate, aEndDate);
114     }
115
116     connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTab(int)));
117
118 }
119
120 MainWindow::~MainWindow()
121 {
122     if(mSqlEngine)
123     {
124         delete mSqlEngine;
125         mSqlEngine = NULL;
126     }
127     if(mXmlParser)
128     {
129         delete mXmlParser;
130         mXmlParser = NULL;
131     }
132 }
133
134 void MainWindow::importSchedule()
135 {
136     QFile file(":/schedule.en.xml");
137     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
138     {
139         qDebug() << "can't open " << file.fileName();
140         return;
141     }
142
143     QByteArray data = file.readAll();
144     mXmlParser->parseData(data,mSqlEngine);
145
146     if(Conference::getAll().count())
147     {
148         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
149         QDate aStartDate = Conference::getById(confId).start();
150         QDate aEndDate = Conference::getById(confId).end();
151         dayNavigator->setDates(aStartDate, aEndDate);
152         activityDayNavigator->setDates(aStartDate, aEndDate);
153     }
154     //update activity map
155     Activity::updateActivityMap();
156 }
157
158 void MainWindow::showParsingProgress(int aStatus)
159 {
160     QString msg = QString("Parsing completed: %1\%").arg(aStatus);
161     statusBar()->showMessage(msg,1000);
162 }
163
164 void MainWindow::aboutApp()
165 {
166     QDialog dialog(this);
167     Ui::AboutDialog ui;
168     ui.setupUi(&dialog);
169     dialog.exec();
170 }
171
172 void MainWindow::updateDayView(const QDate &aDate)
173 {
174     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId);
175     dayTreeView->reset();
176     dayNavigator->show();
177 }
178
179 void MainWindow::updateTab(const int aIndex)
180 {
181     switch(aIndex)
182     {
183     case 0://index 0 of tabWidget: dayViewTab
184         {
185             static_cast<EventModel*>(dayTreeView->model())->loadEvents(Conference::getById(confId).start(),confId);
186             dayTreeView->reset();
187             dayNavigator->show();
188         }
189         break;
190     case 1: //index 1 of tabWidget: favouritesTab
191         {
192             static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
193             favTreeView->reset();
194             favouriteDayNavigator->show();
195         }
196         break;
197     case 2: //index 2 of tabWidget: activitiesTab
198         {
199             static_cast<EventModel*>(actTreeView->model())->loadEventsByActivities(Conference::getById(confId).start(), confId);
200             actTreeView->reset();
201             activityDayNavigator->show();
202         }
203         break;
204     default:
205         {
206
207         }
208     };
209 }
210
211 void MainWindow::updateActivitiesDayView(const QDate &aDate)
212 {
213     static_cast<EventModel*>(actTreeView->model())->loadEventsByActivities(aDate, confId);
214     actTreeView->reset();
215     activityDayNavigator->show();
216 }
217
218 void MainWindow::updateFavouritesDayView(const QDate &aDate)
219 {
220     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(aDate,confId);
221     favTreeView->reset();
222     favouriteDayNavigator->show();
223 }
224
225 void MainWindow::itemClicked(const QModelIndex &aIndex)
226 {
227     // have to handle only events, not time-groups
228     if(!aIndex.parent().isValid()) // time-group
229         return;
230
231     EventDialog dialog(aIndex,this);
232     dialog.exec();
233 }
234
235 void MainWindow::displayMap(const QModelIndex &aIndex)
236 {
237     Event *event = static_cast<Event*>(aIndex.internalPointer());
238
239     // room names are stored in lower-case format
240     // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
241     QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
242     if(!QFile::exists(mapPath))
243         mapPath = QString(":/maps/rooms/not-available.png");
244
245     QString roomName;
246     if(mapPath.contains("not-available", Qt::CaseInsensitive))
247         roomName = QString("Map is not available: %1").arg(event->room());
248     else
249         roomName = event->room();
250
251     QPixmap map(mapPath);
252     MapWindow window(map,roomName,this);
253     window.exec();
254 }
255
256 void MainWindow::searchClicked()
257 {
258     QList<QString> columns;
259
260     if( searchTitle->isChecked() )
261         columns.append( "title" );
262     if( searchAbstract->isChecked() )
263         columns.append( "abstract" );
264
265     if( mSqlEngine->searchEvent( confId, columns, searchEdit->text() ) > 0 ){
266         searchTreeView->setVisible(true);
267         searchDayNavigator->setVisible(true);
268     }
269 }
270