Updated qmake file, bumped version
[quandoparte] / application / view.cpp
1 /*
2
3 Copyright (C) 2011 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 "stationschedulemodel.h"
26 #include "stationlistproxymodel.h"
27 #include "stationlistmodel.h"
28
29 #include <QtGlobal>
30 #include <QtConcurrentRun>
31 #include <QDebug>
32 #include <QDir>
33 #include <QFile>
34 #include <QModelIndex>
35 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
36 #include <QtQuick/QtQuick>
37 #else
38 #include <QtDeclarative>
39 #endif
40
41 // search Paths seem to be broken in Harmattan?
42
43 static QString trueFilePath(const QString &path)
44 {
45     qDebug() << "searching for" << path;
46 #if USE_RESOURCES
47     return path;
48 #else
49     QString searchPathName = path.section(':', 0, 0);
50     qDebug() << "path is" << searchPathName;
51     QString fileName = path.section(':', 1, 1);
52     qDebug() << "filename is" << fileName;
53     QStringList dirs = QDir::searchPaths(searchPathName);
54     foreach(QString dir, dirs) {
55         qDebug() << "searching in" << dir;
56        QDir current(dir);
57        QString absoluteFileName = current.absoluteFilePath(fileName);
58         if (current.exists(fileName)) {
59             qDebug() << "found " << fileName << "in path" << absoluteFileName;
60             return absoluteFileName;
61         }
62     }
63     qDebug() << "file not found";
64     return QString();
65 #endif
66 }
67
68 Q_DECLARE_METATYPE(QList< QPersistentModelIndex >)
69 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
70 Q_DECLARE_METATYPE(QAbstractItemModel::LayoutChangeHint)
71 #endif
72
73 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
74 View::View(QWindow *parent) :
75     QQuickView(parent),
76 #else
77 View::View(QWidget *parent) :
78     QDeclarativeView(parent),
79 #endif
80     stationListModel(new StationListModel(this)),
81     stationListProxyModel(new StationListProxyModel(this))
82 {
83     showFullScreen();
84     future = QtConcurrent::run(
85                 stationListModel, &StationListModel::load,
86                 trueFilePath("stations:stations.qpl"));
87     stationListProxyModel->setSourceModel(stationListModel);
88
89     /* Types to be made accessible to QML */
90 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
91     qRegisterMetaType< QModelIndex >();
92     qRegisterMetaType< QList< QPersistentModelIndex > >();
93     qRegisterMetaType< QAbstractItemModel::LayoutChangeHint>();
94 #endif
95     qmlRegisterType< StationListProxyModel >(
96                 "net.cirulla.quandoparte", 1, 0, "StationListProxyModel");
97     qmlRegisterType< StationScheduleModel >(
98                 "net.cirulla.quandoparte", 1, 0, "StationScheduleModel");
99
100 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
101     QQmlContext *context = this->rootContext();
102 #else
103     QDeclarativeContext *context = this->rootContext();
104 #endif
105     /* objects to be made accessible to QML */
106     context->setContextProperty("settings", Settings::instance());
107     context->setContextProperty("stationList", stationListModel);
108     context->setContextProperty("stationListProxyModel", stationListProxyModel);
109
110     // This does not seem to work in harmattan. As a workaround, change dir to
111     // the qml dir, then load the file.
112     // m_view->setSource(QUrl::fromLocalFile("qml:main.qml"));
113     setSource(trueFilePath("qml:main.qml"));
114 }
115
116 View::~View()
117 {
118     future.waitForFinished();
119 }