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 "daynavigatorwidget.h" |
---|
17 | |
---|
18 | MainWindow::MainWindow(QWidget *parent) |
---|
19 | : QMainWindow(parent) |
---|
20 | { |
---|
21 | setupUi(this); |
---|
22 | |
---|
23 | // connect Menu actions |
---|
24 | connect(actionImportSchedule, SIGNAL(triggered()), SLOT(importSchedule())); |
---|
25 | connect(actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
---|
26 | connect(actionAboutApplication, SIGNAL(triggered()), SLOT(aboutApp())); |
---|
27 | |
---|
28 | // create "SQLITE" DB instance/connection |
---|
29 | // opens DB connection (needed for EventModel) |
---|
30 | mSqlEngine = new SqlEngine(this); |
---|
31 | mSqlEngine->initialize(); |
---|
32 | |
---|
33 | mXmlParser = new ScheduleXmlParser(this); |
---|
34 | connect(mXmlParser, SIGNAL(progressStatus(int)), this, SLOT(showParsingProgress(int))); |
---|
35 | statusBar()->showMessage(tr("Ready")); |
---|
36 | |
---|
37 | connect(dayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateDayView(const QDate &))); |
---|
38 | connect(activityDayNavigator, SIGNAL(dateChanged(const QDate &)), SLOT(updateActivitiesDayView(const QDate &))); |
---|
39 | //connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(updateView(int))); |
---|
40 | |
---|
41 | // DAY EVENTS View |
---|
42 | dayTreeView->setHeaderHidden(true); |
---|
43 | dayTreeView->setRootIsDecorated(false); |
---|
44 | dayTreeView->setIndentation(0); |
---|
45 | dayTreeView->setAnimated(true); |
---|
46 | dayTreeView->setModel(new EventModel()); |
---|
47 | dayTreeView->setItemDelegate(new Delegate(dayTreeView)); |
---|
48 | |
---|
49 | // FAVOURITIES View |
---|
50 | favTreeView->setHeaderHidden(true); |
---|
51 | favTreeView->setRootIsDecorated(false); |
---|
52 | favTreeView->setIndentation(0); |
---|
53 | favTreeView->setAnimated(true); |
---|
54 | favTreeView->setModel(new EventModel()); |
---|
55 | favTreeView->setItemDelegate(new Delegate(favTreeView)); |
---|
56 | |
---|
57 | //ACTIVITIES View |
---|
58 | activityDayTreeView->setHeaderHidden(true); |
---|
59 | activityDayTreeView->setRootIsDecorated(false); |
---|
60 | activityDayTreeView->setIndentation(0); |
---|
61 | activityDayTreeView->setAnimated(true); |
---|
62 | activityDayTreeView->setModel(new EventModel()); |
---|
63 | activityDayTreeView->setItemDelegate(new Delegate(activityDayTreeView)); |
---|
64 | |
---|
65 | // TESTING: load some 'fav' data |
---|
66 | if(Conference::getAll().count()) // no conference(s) in the DB |
---|
67 | { |
---|
68 | int confId = 1; |
---|
69 | static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId); |
---|
70 | favTreeView->reset(); |
---|
71 | } |
---|
72 | |
---|
73 | if(!Conference::getAll().count()) // no conference(s) in the DB |
---|
74 | { |
---|
75 | dayNavigator->hide(); // hide DayNavigatorWidget |
---|
76 | activityDayNavigator->hide(); |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | int confId = 1; |
---|
81 | QDate aStartDate = Conference::getById(confId).start(); |
---|
82 | QDate aEndDate = Conference::getById(confId).end(); |
---|
83 | dayNavigator->setDates(aStartDate, aEndDate); |
---|
84 | activityDayNavigator->setDates(aStartDate, aEndDate); |
---|
85 | } |
---|
86 | connect(static_cast<EventModel*>(dayTreeView->model()), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), SLOT(updateFavView())); |
---|
87 | connect(static_cast<EventModel*>(favTreeView->model()), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), SLOT(updateFavView())); |
---|
88 | /* connect(static_cast<EventModel*>(favTreeView->model()), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), SLOT(updateFavViewComplete()));*/ |
---|
89 | } |
---|
90 | |
---|
91 | MainWindow::~MainWindow() |
---|
92 | { |
---|
93 | if(mSqlEngine) |
---|
94 | { |
---|
95 | delete mSqlEngine; |
---|
96 | mSqlEngine = NULL; |
---|
97 | } |
---|
98 | if(mXmlParser) |
---|
99 | { |
---|
100 | delete mXmlParser; |
---|
101 | mXmlParser = NULL; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | void MainWindow::importSchedule() |
---|
106 | { |
---|
107 | QFile file("../schedule.en.xml"); |
---|
108 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
---|
109 | { |
---|
110 | QString currPath = QDir::currentPath(); |
---|
111 | qDebug() << "current path: " << currPath; |
---|
112 | qDebug() << "can't open " << file.fileName(); |
---|
113 | return; |
---|
114 | } |
---|
115 | |
---|
116 | QByteArray data = file.readAll(); |
---|
117 | mXmlParser->parseData(data,mSqlEngine); |
---|
118 | |
---|
119 | if(Conference::getAll().count()) |
---|
120 | { |
---|
121 | int confId = 1; |
---|
122 | // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates |
---|
123 | dayNavigator->setDates(Conference::getById(confId).start(), Conference::getById(confId).end()); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | void MainWindow::showParsingProgress(int aStatus) |
---|
128 | { |
---|
129 | QString msg = QString("Parsing completed: %1\%").arg(aStatus); |
---|
130 | statusBar()->showMessage(msg,1000); |
---|
131 | } |
---|
132 | |
---|
133 | void MainWindow::aboutApp() |
---|
134 | { |
---|
135 | QDialog dialog(this); |
---|
136 | Ui::AboutDialog ui; |
---|
137 | ui.setupUi(&dialog); |
---|
138 | dialog.exec(); |
---|
139 | } |
---|
140 | |
---|
141 | void MainWindow::updateDayView(const QDate &aDate) |
---|
142 | { |
---|
143 | int confId = 1; |
---|
144 | static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId); |
---|
145 | dayTreeView->reset(); |
---|
146 | dayNavigator->show(); |
---|
147 | } |
---|
148 | |
---|
149 | void MainWindow::updateFavView() |
---|
150 | { |
---|
151 | int confId = 1; |
---|
152 | static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId); |
---|
153 | favTreeView->reset(); //Necessary reset: |
---|
154 | // if favourite event unselected as favourite is the only one in its time, and reset is not produced, crashed |
---|
155 | |
---|
156 | dayNavigator->show(); |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | void MainWindow::updateFavViewComplete() |
---|
161 | { |
---|
162 | int confId = 1; |
---|
163 | updateFavView(); |
---|
164 | updateDayView(Conference::getById(confId).start()); |
---|
165 | } |
---|
166 | */ |
---|
167 | |
---|
168 | void MainWindow::updateActivitiesDayView(const QDate &aDate) |
---|
169 | { |
---|
170 | int confId = 1; |
---|
171 | static_cast<EventModel*>(activityDayTreeView->model())->loadEventsByActivities(aDate,confId); |
---|
172 | activityDayTreeView->reset(); |
---|
173 | activityDayNavigator->show(); |
---|
174 | } |
---|
175 | |
---|
176 | void MainWindow::updateView(int tabIndex) |
---|
177 | { |
---|
178 | //TODO korinpa: change to enum or names ? |
---|
179 | qDebug() << "updateView index: " << tabIndex; |
---|
180 | if (tabIndex == 0) |
---|
181 | { |
---|
182 | QDate date = dayNavigator->getCurrentDate(); |
---|
183 | updateDayView(date); |
---|
184 | } |
---|
185 | else if (tabIndex == 1) |
---|
186 | { |
---|
187 | updateFavView(); |
---|
188 | } |
---|
189 | else if (tabIndex == 2) |
---|
190 | { |
---|
191 | QDate date = activityDayNavigator->getCurrentDate(); |
---|
192 | updateActivitiesDayView(date); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|