implemented 'tab container' widget, which groups daynavigator with treeview
[toast/confclerk.git] / src / gui / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <appsettings.h>
3
4 #include <QTreeView>
5 #include <QFile>
6
7 #include <sqlengine.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 <QMessageBox>
17 #include "ui_about.h"
18 #include <eventdialog.h>
19 #include "daynavigatorwidget.h"
20 #include "importschedulewidget.h"
21 #include "mapwindow.h"
22
23 #include <tabcontainer.h>
24
25 MainWindow::MainWindow(int aEventId, QWidget *aParent)
26     : QMainWindow(aParent)
27 {
28     setupUi(this);
29
30     // create "SQLITE" DB instance/connection
31     // opens DB connection (needed for EventModel)
32     mSqlEngine = new SqlEngine(this);
33     //mSqlEngine->initialize();
34     importScheduleWidget->setSqlEngine(mSqlEngine);
35
36     // Sanity check for existence of any Conference in the DB
37     // it AppSettings::confId() is 0, but there are any Conference(s) in the DB
38     // set the confId in the AppSettings for the ID of the first conference in the DB
39     QList<Conference> confs = Conference::getAll();
40     if(!confs.count()) // no conference(s) in the DB
41     {
42         AppSettings::setConfId(0); // no conference in the DB
43     }
44     else
45     {
46         if(AppSettings::confId() == 0)
47             AppSettings::setConfId(confs[0].id());
48
49         setWindowTitle(confs[0].title());
50     }
51
52     dayTabContainer->setType(TabContainer::EContainerTypeDay);
53     favsTabContainer->setType(TabContainer::EContainerTypeFavs);
54     tracksTabContainer->setType(TabContainer::EContainerTypeTracks);
55     nowTabContainer->setType(TabContainer::EContainerTypeNow);
56     roomsTabContainer->setType(TabContainer::EContainerTypeRooms);
57
58     connect(importScheduleWidget, SIGNAL(scheduleImported(int)), SLOT(scheduleImported(int)));
59
60     connect(searchDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateSearchView(const QDate &)));
61
62     // SEARCH EVENTS View
63         searchTreeView->setHeaderHidden(true);
64         searchTreeView->setRootIsDecorated(false);
65         searchTreeView->setIndentation(0);
66         searchTreeView->setAnimated(true);
67         searchTreeView->setModel(new EventModel());
68         searchTreeView->setItemDelegate(new Delegate(searchTreeView));
69
70     // event details have changed
71     connect(dayTabContainer, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
72     connect(favsTabContainer, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
73     connect(favsTabContainer, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
74     connect(roomsTabContainer, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
75     connect(nowTabContainer, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
76
77     connect(searchTreeView, SIGNAL(eventHasChanged(int)), SLOT(eventHasChanged(int)));
78
79     // event clicked
80     connect(searchTreeView, SIGNAL(clicked(const QModelIndex &)), SLOT(itemClicked(const QModelIndex &)));
81
82     // event search button clicked
83     connect(searchButton, SIGNAL(clicked()), SLOT(searchClicked()));
84     connect(searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
85
86     // event conference map button clicked
87     connect(showMapButton, SIGNAL(clicked()), SLOT(conferenceMapClicked()));
88     //
89     connect(tabWidget, SIGNAL(infoIconClicked()), SLOT(aboutApp()));
90
91     if(Conference::getAll().count()) // no conference(s) in the DB
92     {
93         QDate aStartDate = Conference::getById(AppSettings::confId()).start();
94         QDate aEndDate = Conference::getById(AppSettings::confId()).end();
95         searchDayNavigator->setDates(aStartDate, aEndDate);
96         //
97         dayTabContainer->setDates(aStartDate, aEndDate);
98         tracksTabContainer->setDates(aStartDate, aEndDate);
99         roomsTabContainer->setDates(aStartDate, aEndDate);
100         favsTabContainer->setDates(aStartDate, aEndDate);
101         //
102         conferenceTitle->setText(Conference::getById(AppSettings::confId()).title());
103         conferenceSubtitle->setText(Conference::getById(AppSettings::confId()).subtitle());
104         conferenceWhere->setText(Conference::getById(AppSettings::confId()).city() + ", " + Conference::getById(AppSettings::confId()).venue());
105         conferenceWhen->setText(
106                 Conference::getById(AppSettings::confId()).start().toString("dd-MM-yyyy")
107                 + ", " +
108                 Conference::getById(AppSettings::confId()).end().toString("dd-MM-yyyy"));
109     }
110
111     searchTreeView->hide();
112     searchVerticalWidget->hide();
113     searchHead->show();
114
115     // open dialog for given Event ID
116     // this is used in case Alarm Dialog request application to start
117     if(aEventId)
118     {
119         try
120         {
121             EventDialog dialog(aEventId,this);
122             dialog.exec();
123         }
124         catch(OrmNoObjectException&) {} // just start application
125         catch(...) {} // just start application
126     }
127 }
128
129 MainWindow::~MainWindow()
130 {
131     if(mSqlEngine)
132     {
133         delete mSqlEngine;
134         mSqlEngine = NULL;
135     }
136 }
137
138 void MainWindow::scheduleImported(int aConfId)
139 {
140     Q_UNUSED(aConfId);
141
142     QList<Conference> confs = Conference::getAll();
143     if(!confs.count()) // no conference(s) in the DB
144     {
145         AppSettings::setConfId(0); // no conference in the DB
146     }
147     else
148     {
149         if(AppSettings::confId() == 0)
150             AppSettings::setConfId(confs[0].id());
151
152         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
153         QDate startDate = Conference::getById(AppSettings::confId()).start();
154         QDate endDate = Conference::getById(AppSettings::confId()).end();
155         dayTabContainer->setDates(startDate, endDate);
156         tracksTabContainer->setDates(startDate, endDate);
157         roomsTabContainer->setDates(startDate, endDate);
158         favsTabContainer->setDates(startDate, endDate);
159     }
160 }
161
162 void MainWindow::aboutApp()
163 {
164     QDialog dialog(this);
165     Ui::AboutDialog ui;
166     ui.setupUi(&dialog);
167     dialog.exec();
168 }
169
170 void MainWindow::updateSearchView(const QDate &aDate)
171 {
172     qDebug() << "MainWindow::updateSearchView(), aDate: " << aDate.toString() ;
173     searchTreeView->reset();
174     int eventsCount = static_cast<EventModel*>(searchTreeView->model())->loadSearchResultEvents(aDate,AppSettings::confId());
175     if( eventsCount ||
176             searchDayNavigator->getCurrentDate() != Conference::getById(AppSettings::confId()).start() ){
177         searchVerticalWidget->show();
178         //searchAgainButton->show();
179         searchTreeView->show();
180         searchHead->hide();
181     }
182     else{
183         searchTreeView->hide();
184         searchVerticalWidget->hide();
185         searchHead->show();
186     }
187 }
188
189 void MainWindow::searchClicked()
190 {
191     QHash<QString,QString> columns;
192
193     if( searchTitle->isChecked() )
194         columns.insertMulti("EVENT", "title");
195     if( searchAbstract->isChecked() )
196         columns.insertMulti("EVENT", "abstract");
197     if( searchTag->isChecked() )
198         columns.insertMulti("EVENT", "tag");
199     if( searchSpeaker->isChecked() )
200         columns["PERSON"] = "name";
201     if( searchRoom->isChecked() )
202         columns["ROOM"] = "name";
203
204     QString keyword = searchEdit->text().replace( QString("%"), QString("\\%") );
205     qDebug() << "\nKeyword to search: " << keyword;
206     mSqlEngine->searchEvent( AppSettings::confId(), columns, keyword );
207
208     QDate aStartDate = Conference::getById(AppSettings::confId()).start();
209     QDate aEndDate = Conference::getById(AppSettings::confId()).end();
210     searchDayNavigator->setDates(aStartDate, aEndDate);
211     updateSearchView( Conference::getById(AppSettings::confId()).start() );
212 }
213
214 void MainWindow::searchAgainClicked()
215 {
216     searchHead->show();
217     //searchAgainButton->hide();
218     searchVerticalWidget->hide();
219     searchTreeView->hide();
220 }
221
222 void MainWindow::conferenceMapClicked()
223 {
224     QString mapPath = QString(":/maps/campus.png");
225     if(!QFile::exists(mapPath))
226         mapPath = QString(":/maps/rooms/not-available.png");
227
228     QString roomName;
229
230     QPixmap map(mapPath);
231     MapWindow window(map,roomName,this);
232     window.exec();
233 }
234
235 void MainWindow::eventHasChanged(int aEventId)
236 {
237     dayTabContainer->updateTreeViewModel(aEventId);
238     favsTabContainer->updateTreeViewModel(aEventId);
239     tracksTabContainer->updateTreeViewModel(aEventId);
240     nowTabContainer->updateTreeViewModel(aEventId);
241     roomsTabContainer->updateTreeViewModel(aEventId);
242
243     static_cast<EventModel*>(searchTreeView->model())->updateModel(aEventId);
244 }
245