source: src/gui/mainwindow.cpp @ c5324ca

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

Automatic tabs update

M src/gui/mainwindow.ui
M src/gui/mainwindow.cpp
M src/gui/mainwindow.h

  • 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
42    // DAY EVENTS View
43    dayTreeView->setHeaderHidden(true);
44    dayTreeView->setRootIsDecorated(false);
45    dayTreeView->setIndentation(0);
46    dayTreeView->setAnimated(true);
47    dayTreeView->setModel(new EventModel());
48    dayTreeView->setItemDelegate(new Delegate(dayTreeView));
49
50    // FAVOURITIES View
51    favTreeView->setHeaderHidden(true);
52    favTreeView->setRootIsDecorated(false);
53    favTreeView->setIndentation(0);
54    favTreeView->setAnimated(true);
55    favTreeView->setModel(new EventModel());
56    favTreeView->setItemDelegate(new Delegate(favTreeView));
57
58    //ACTIVITIES View
59    actTreeView->setHeaderHidden(true);
60    actTreeView->setRootIsDecorated(false);
61    actTreeView->setIndentation(0);
62    actTreeView->setAnimated(true);
63    actTreeView->setModel(new EventModel());
64    actTreeView->setItemDelegate(new Delegate(actTreeView));
65
66    // event double clicked
67    connect(dayTreeView, SIGNAL(doubleClicked(const QModelIndex &)), SLOT(itemDoubleClicked(const QModelIndex &)));
68    connect(favTreeView, SIGNAL(doubleClicked(const QModelIndex &)), SLOT(itemDoubleClicked(const QModelIndex &)));
69    connect(actTreeView, SIGNAL(doubleClicked(const QModelIndex &)), SLOT(itemDoubleClicked(const QModelIndex &)));
70    // request for map to be displayed
71    connect(dayTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
72    connect(favTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
73    connect(actTreeView, SIGNAL(requestForMap(const QModelIndex &)), SLOT(displayMap(const QModelIndex &)));
74
75
76    // TESTING: load some 'fav' data
77    if(Conference::getAll().count()) // no conference(s) in the DB
78    {
79        int confId = 1;
80        static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
81        favTreeView->reset();
82    }
83
84    if(!Conference::getAll().count()) // no conference(s) in the DB
85    {
86        dayNavigator->hide(); // hide DayNavigatorWidget
87        activityDayNavigator->hide();
88    }
89    else
90    {
91        int confId = 1;
92        QDate aStartDate = Conference::getById(confId).start();
93        QDate aEndDate = Conference::getById(confId).end();
94        dayNavigator->setDates(aStartDate, aEndDate);
95        activityDayNavigator->setDates(aStartDate, aEndDate);
96    }
97
98    connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTab(int)));
99
100}
101
102MainWindow::~MainWindow()
103{
104    if(mSqlEngine)
105    {
106        delete mSqlEngine;
107        mSqlEngine = NULL;
108    }
109    if(mXmlParser)
110    {
111        delete mXmlParser;
112        mXmlParser = NULL;
113    }
114}
115
116void MainWindow::importSchedule()
117{
118    QFile file("../schedule.en.xml");
119    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
120    {
121        qDebug() << "can't open " << file.fileName();
122        return;
123    }
124
125    QByteArray data = file.readAll();
126    mXmlParser->parseData(data,mSqlEngine);
127
128    if(Conference::getAll().count())
129    {
130        int confId = 1;
131        // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
132        QDate aStartDate = Conference::getById(confId).start();
133        QDate aEndDate = Conference::getById(confId).end();
134        dayNavigator->setDates(aStartDate, aEndDate);
135        activityDayNavigator->setDates(aStartDate, aEndDate);
136    }
137}
138
139void MainWindow::showParsingProgress(int aStatus)
140{
141    QString msg = QString("Parsing completed: %1\%").arg(aStatus);
142    statusBar()->showMessage(msg,1000);
143}
144
145void MainWindow::aboutApp()
146{
147    QDialog dialog(this);
148    Ui::AboutDialog ui;
149    ui.setupUi(&dialog);
150    dialog.exec();
151}
152
153void MainWindow::updateDayView(const QDate aDate)
154{
155    int confId = 1;
156    static_cast<EventModel*>(dayTreeView->model())->loadEvents(Conference::getById(confId).start(),confId);
157    dayTreeView->reset();
158    dayNavigator->show();
159}
160
161void MainWindow::updateTab(const int n)
162{
163    int confId = 1;
164    if(n) //index 1 of tabWidget: favouriteTab
165    {
166        static_cast<EventModel*>(favTreeView->model())->loadFavEvents(Conference::getById(confId).start(),confId);
167        favTreeView->reset();
168    }
169    else //index 0 of tabWidget: dayViewTab
170    {
171        static_cast<EventModel*>(dayTreeView->model())->loadEvents(Conference::getById(confId).start(),confId);
172        dayTreeView->reset();
173    }
174    //TODO: update of activitiesTab needed?
175    dayNavigator->show();
176}
177
178
179void MainWindow::updateActivitiesDayView(const QDate &aDate)
180{
181    int confId = 1;
182    static_cast<EventModel*>(actTreeView->model())->loadEventsByActivities(aDate,confId);
183    actTreeView->reset();
184    activityDayNavigator->show();
185}
186
187void MainWindow::itemDoubleClicked(const QModelIndex &aIndex)
188{
189    // have to handle only events, not time-groups
190    if(!aIndex.parent().isValid()) // time-group
191        return;
192
193    EventDialog dialog(aIndex,this);
194    dialog.exec();
195}
196
197void MainWindow::displayMap(const QModelIndex &aIndex)
198{
199    Event *event = static_cast<Event*>(aIndex.internalPointer());
200
201    // room names are stored in lower-case format
202    // room names are stored without dots in the name, eg. "aw.1124.png" -> "aw1124.png"
203    QString mapPath = QString(":/maps/rooms/%1.png").arg(event->room().toLower().remove("."));
204    if(!QFile::exists(mapPath))
205        mapPath = QString(":/maps/rooms/not-available.png");
206
207    QString roomName;
208    if(mapPath.contains("not-available", Qt::CaseInsensitive))
209        roomName = QString("Map is not available: %1").arg(event->room());
210    else
211        roomName = event->room();
212
213    QPixmap map(mapPath);
214    MapWindow window(map,roomName,this);
215    window.exec();
216}
217
Note: See TracBrowser for help on using the repository browser.