source: src/gui/mainwindow.cpp @ b1fc17a

qt5
Last change on this file since b1fc17a was b1fc17a, checked in by pavelpa <pavelpa@…>, 12 years ago

handled the case when the map is not available

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