activities tab implemented - not finished yet
[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 <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     // TESTING: load some 'fav' data
57     if(Conference::getAll().count()) // no conference(s) in the DB
58     {
59         int confId = 1;
60         static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
61         favTreeView->reset();
62     }
63
64     if(!Conference::getAll().count()) // no conference(s) in the DB
65         dayNavigator->hide(); // hide DayNavigatorWidget
66     else
67     {
68         int confId = 1;
69         dayNavigator->setDates(Conference::getById(confId).start(),Conference::getById(confId).end());
70     }
71
72     connect(static_cast<EventModel*>(dayTreeView->model()), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), SLOT(updateFavView()));
73     connect(static_cast<EventModel*>(favTreeView->model()), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), SLOT(updateFavViewComplete()));
74 }
75
76 MainWindow::~MainWindow()
77 {
78     if(mSqlEngine)
79     {
80         delete mSqlEngine;
81         mSqlEngine = NULL;
82     }
83     if(mXmlParser)
84     {
85         delete mXmlParser;
86         mXmlParser = NULL;
87     }
88 }
89
90 void MainWindow::importSchedule()
91 {
92     QFile file("../schedule.en.xml");
93     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
94     {
95         QString currPath = QDir::currentPath();
96         qDebug() << "current path: " << currPath;
97         qDebug() << "can't open " << file.fileName();
98         return;
99     }
100
101     QByteArray data = file.readAll();
102     mXmlParser->parseData(data,mSqlEngine);
103
104     if(Conference::getAll().count())
105     {
106         int confId = 1;
107         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
108         dayNavigator->setDates(Conference::getById(confId).start(), Conference::getById(confId).end());
109     }
110 }
111
112 void MainWindow::showParsingProgress(int aStatus)
113 {
114     QString msg = QString("Parsing completed: %1\%").arg(aStatus);
115     statusBar()->showMessage(msg,1000);
116 }
117
118 void MainWindow::aboutApp()
119 {
120     QDialog dialog(this);
121     Ui::AboutDialog ui;
122     ui.setupUi(&dialog);
123     dialog.exec();
124 }
125
126 void MainWindow::updateDayView(const QDate &aDate)
127 {
128     int confId = 1;
129     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId);
130     dayTreeView->reset();
131     dayNavigator->show();
132 }
133
134 void MainWindow::updateFavView()
135 {
136     int confId = 1;
137     static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
138     favTreeView->reset(); //Necessary reset:
139                         //  if favourite event unselected as favourite is the only one in its time, and reset is not produced, crashed
140 }
141
142 void MainWindow::updateFavViewComplete()
143 {
144     int confId = 1;
145     updateFavView();
146     updateDayView(Conference::getById(confId).start());
147 }
148
149 void MainWindow::updateActivitiesDayView(const QDate &aDate)
150 {
151     int confId = 1;
152     static_cast<EventModel*>(activityDayTreeView->model())->loadEventsByActivities(aDate,confId);
153     activityDayTreeView->reset();
154     activityDayNavigator->show();
155 }
156
157 void MainWindow::updateView(int tabIndex)
158 {
159     //TODO korinpa: skraslit ! aj pre ine taby
160     qDebug() << "updateView index: " << tabIndex;
161     if (tabIndex == 2)
162     {
163         QDate date = activityDayNavigator->getCurrentDate();
164         updateActivitiesDayView(date);
165     }
166 }
167