9eebee14c6b3ba0d66a7056ee1ac1e84afd30ef4
[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 "stationlistmodel.h"
26 #include "stationlistproxymodel.h"
27 #include "stationschedulemodel.h"
28
29 #include <QDebug>
30 #include <QDir>
31 #include <QFile>
32 #include <QModelIndex>
33 #include <QtConcurrentRun>
34 #include <QtDeclarative>
35
36 // search Paths seem to be broken in Harmattan?
37
38 static QString trueFilePath(const QString &path)
39 {
40     qDebug() << "searching for" << path;
41 #if USE_RESOURCES
42     return path;
43 #else
44     QString searchPathName = path.section(':', 0, 0);
45     qDebug() << "path is" << searchPathName;
46     QString fileName = path.section(':', 1, 1);
47     qDebug() << "filename is" << fileName;
48     QStringList dirs = QDir::searchPaths(searchPathName);
49     foreach(QString dir, dirs) {
50         qDebug() << "searching in" << dir;
51        QDir current(dir);
52        QString absoluteFileName = current.absoluteFilePath(fileName);
53         if (current.exists(fileName)) {
54             qDebug() << "found " << fileName << "in path" << absoluteFileName;
55             return absoluteFileName;
56         }
57     }
58     qDebug() << "file not found";
59     return QString();
60 #endif
61 }
62
63 View::View(QWidget *parent) :
64     QDeclarativeView(parent),
65     stationListModel(new StationListModel(this)),
66     stationListProxyModel(new StationListProxyModel(this))
67 {
68     showFullScreen();
69     future = QtConcurrent::run(
70                 stationListModel, &StationListModel::load,
71                 trueFilePath("stations:stations.qpl"));
72     stationListProxyModel->setSourceModel(stationListModel);
73
74     /* Types to be made accessible to QML */
75     qRegisterMetaType<QModelIndex>("QModelIndex");
76     qmlRegisterType<Settings>("net.cirulla.quandoparte", 1, 0, "Settings");
77     qmlRegisterType<StationListProxyModel>(
78                 "net.cirulla.quandoparte", 1, 0, "StationListProxyModel");
79     qmlRegisterType<StationScheduleModel>(
80                 "net.cirulla.quandoparte", 1, 0, "StationScheduleModel");
81
82     QDeclarativeContext *context = this->rootContext();
83     /* objects to be made accessible to QML */
84     context->setContextProperty("settings", Settings::instance());
85     context->setContextProperty("stationList", stationListModel);
86     context->setContextProperty("stationListProxyModel", stationListProxyModel);
87
88     // This does not seem ot work in harmattan. As a workaround, change dir to
89     // the qml dir, then load the file.
90     // m_view->setSource(QUrl::fromLocalFile("qml:main.qml"));
91     setSource(trueFilePath("qml:main.qml"));
92 }
93
94 View::~View()
95 {
96     future.waitForFinished();
97 }