09263da05f907ae24434df8c320dcb819e876db4
[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
39     // DAY EVENTS View
40     dayTreeView->setHeaderHidden(true);
41     dayTreeView->setRootIsDecorated(false);
42     dayTreeView->setIndentation(0);
43     dayTreeView->setAnimated(true);
44     dayTreeView->setModel(new EventModel());
45     dayTreeView->setItemDelegate(new Delegate(dayTreeView));
46
47     // FAVOURITIES View
48     favTreeView->setHeaderHidden(true);
49     favTreeView->setRootIsDecorated(false);
50     favTreeView->setIndentation(0);
51     favTreeView->setAnimated(true);
52     favTreeView->setModel(new EventModel());
53     favTreeView->setItemDelegate(new Delegate(favTreeView));
54     // TESTING: load some 'fav' data
55     if(Conference::getAll().count()) // no conference(s) in the DB
56     {
57         int confId = 1;
58         static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
59         favTreeView->reset();
60     }
61
62     if(!Conference::getAll().count()) // no conference(s) in the DB
63         dayNavigator->hide(); // hide DayNavigatorWidget
64     else
65     {
66         int confId = 1;
67         dayNavigator->setDates(Conference::getById(confId).start(),Conference::getById(confId).end());
68     }
69 }
70
71 MainWindow::~MainWindow()
72 {
73     if(mSqlEngine)
74     {
75         delete mSqlEngine;
76         mSqlEngine = NULL;
77     }
78     if(mXmlParser)
79     {
80         delete mXmlParser;
81         mXmlParser = NULL;
82     }
83 }
84
85 void MainWindow::importSchedule()
86 {
87     QFile file("../schedule.en.xml");
88     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
89     {
90         QString currPath = QDir::currentPath();
91         qDebug() << "current path: " << currPath;
92         qDebug() << "can't open " << file.fileName();
93         return;
94     }
95
96     QByteArray data = file.readAll();
97     mXmlParser->parseData(data,mSqlEngine);
98
99     if(Conference::getAll().count())
100     {
101         int confId = 1;
102         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
103         dayNavigator->setDates(Conference::getById(confId).start(),Conference::getById(confId).end());
104     }
105 }
106
107 void MainWindow::showParsingProgress(int aStatus)
108 {
109     QString msg = QString("Parsing completed: %1\%").arg(aStatus);
110     statusBar()->showMessage(msg,1000);
111 }
112
113 void MainWindow::aboutApp()
114 {
115     QDialog dialog(this);
116     Ui::AboutDialog ui;
117     ui.setupUi(&dialog);
118     dialog.exec();
119 }
120
121 void MainWindow::updateDayView(const QDate &aDate)
122 {
123     int confId = 1;
124     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId);
125     dayTreeView->reset();
126     dayNavigator->show();
127 }
128