started work on 'favourities'
[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         qDebug() << "can't open " << file.fileName();
91         return;
92     }
93
94     QByteArray data = file.readAll();
95     mXmlParser->parseData(data,mSqlEngine);
96
97     if(Conference::getAll().count())
98     {
99         int confId = 1;
100         // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
101         dayNavigator->setDates(Conference::getById(confId).start(),Conference::getById(confId).end());
102     }
103 }
104
105 void MainWindow::showParsingProgress(int aStatus)
106 {
107     QString msg = QString("Parsing completed: %1\%").arg(aStatus);
108     statusBar()->showMessage(msg,1000);
109 }
110
111 void MainWindow::aboutApp()
112 {
113     QDialog dialog(this);
114     Ui::AboutDialog ui;
115     ui.setupUi(&dialog);
116     dialog.exec();
117 }
118
119 void MainWindow::updateDayView(const QDate &aDate)
120 {
121     int confId = 1;
122     static_cast<EventModel*>(dayTreeView->model())->loadEvents(aDate,confId);
123     dayTreeView->reset();
124     dayNavigator->show();
125 }
126