Bump version to 0.9.0
[quandoparte] / application / view.cpp
1 /*
2
3 Copyright (C) 2011, 2013 Luciano Montanaro <mikelima@cirulla.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20 */
21
22 #include "view.h"
23 #include "settings.h"
24 #include "dataprovider.h"
25 #include "stationitem.h"
26 #include "stationscheduleitem.h"
27 #include "stationschedulemodel.h"
28 #include "stationlistproxymodel.h"
29 #include "stationlistmodel.h"
30
31 #include <QtGlobal>
32 #include <QtConcurrentRun>
33 #include <QDebug>
34 #include <QDir>
35 #include <QFile>
36 #include <QModelIndex>
37 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
38 #include <QtQuick/QtQuick>
39 #else
40 #include <QtDeclarative>
41 #endif
42
43 // search Paths seem to be broken in Harmattan?
44
45 static QString trueFilePath(const QString &path)
46 {
47     qDebug() << "searching for" << path;
48 #if USE_RESOURCES
49     return path;
50 #else
51     QString searchPathName = path.section(':', 0, 0);
52     qDebug() << "path is" << searchPathName;
53     QString fileName = path.section(':', 1, 1);
54     qDebug() << "filename is" << fileName;
55     QStringList dirs = QDir::searchPaths(searchPathName);
56     foreach(QString dir, dirs) {
57         qDebug() << "searching in" << dir;
58        QDir current(dir);
59        QString absoluteFileName = current.absoluteFilePath(fileName);
60         if (current.exists(fileName)) {
61             qDebug() << "found " << fileName << "in path" << absoluteFileName;
62             return absoluteFileName;
63         }
64     }
65     qDebug() << "file not found";
66     return QString();
67 #endif
68 }
69
70 Q_DECLARE_METATYPE(QList< QPersistentModelIndex >)
71 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
72 Q_DECLARE_METATYPE(QAbstractItemModel::LayoutChangeHint)
73 #endif
74
75 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
76 View::View(QWindow *parent) :
77     QQuickView(parent),
78 #else
79 View::View(QWidget *parent) :
80     QDeclarativeView(parent),
81 #endif
82     stationListModel(new StationListModel(this)),
83     stationListProxyModel(new StationListProxyModel(this))
84 {
85     showFullScreen();
86     future = QtConcurrent::run(
87                 stationListModel, &StationListModel::load,
88                 trueFilePath("stations:stations.qpl"));
89     stationListProxyModel->setSourceModel(stationListModel);
90
91     /* Types to be made accessible to QML */
92 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
93     qRegisterMetaType<QGeoCoordinate>("QGeoCoordinate");
94     qRegisterMetaType<QGeoPositionInfo>("QGeoPositionInfo");
95     qRegisterMetaType< QModelIndex >("QModelIndex");
96     qRegisterMetaType< QList< QPersistentModelIndex > >();
97     qRegisterMetaType< QAbstractItemModel::LayoutChangeHint>();
98 #endif
99     qmlRegisterType< StationListModel >(
100                 "net.cirulla.quandoparte", 1, 0, "StationListModel");
101     qmlRegisterType< StationListProxyModel >(
102                 "net.cirulla.quandoparte", 1, 0, "StationListProxyModel");
103     qmlRegisterType< StationScheduleModel >(
104                 "net.cirulla.quandoparte", 1, 0, "StationScheduleModel");
105
106 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
107     QQmlContext *context = this->rootContext();
108 #else
109     QDeclarativeContext *context = this->rootContext();
110 #endif
111     /* objects to be made accessible to QML */
112     context->setContextProperty("settings", Settings::instance());
113     context->setContextProperty("stationList", stationListModel);
114     context->setContextProperty("stationListProxyModel", stationListProxyModel);
115
116     // This does not seem to work in harmattan. As a workaround, change dir to
117     // the qml dir, then load the file.
118     // m_view->setSource(QUrl::fromLocalFile("qml:main.qml"));
119     setSource(trueFilePath("qml:main.qml"));
120 }
121
122 View::~View()
123 {
124     future.waitForFinished();
125 }