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