Commit 76e159a3 authored by Nicolas Fella's avatar Nicolas Fella

Use LocationQueryModel from KPT

parent 5a474fc7
......@@ -3,7 +3,6 @@ add_subdirectory(dateandtime)
set(ktrip_SRCS
main.cpp
querycontroller.cpp
locationquerymodel.cpp
locationcache.cpp
formatter.cpp
)
......
/**
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "locationquerymodel.h"
#include <KPublicTransport/LocationRequest>
#include <KPublicTransport/LocationReply>
LocationQueryModel::LocationQueryModel(QObject *parent) : QAbstractListModel(parent), m_manager()
{
connect(this, &LocationQueryModel::queryChanged, this, &LocationQueryModel::triggerQuery);
}
void LocationQueryModel::triggerQuery()
{
KPublicTransport::LocationRequest request;
request.setName(m_query);
const KPublicTransport::LocationReply *reply = m_manager.queryLocation(request);
connect(reply, &KPublicTransport::LocationReply::finished, this, [this, reply] {
beginResetModel();
m_locations = reply->result();
endResetModel();
});
}
QVariant LocationQueryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= rowCount()) {
return QVariant();
}
switch (role) {
case NameRole:
return m_locations[index.row()].name();
case ObjectRole:
return QVariant::fromValue(m_locations[index.row()]);
default:
return QVariant(QStringLiteral("deadbeef"));
}
}
QString LocationQueryModel::query() const
{
return m_query;
}
int LocationQueryModel::rowCount(const QModelIndex &parent) const
{
return m_locations.size();
}
void LocationQueryModel::setQuery(const QString query)
{
if (m_query != query) {
m_query = query;
Q_EMIT queryChanged();
}
}
QHash<int, QByteArray> LocationQueryModel::roleNames() const
{
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
names.insert(NameRole, "name");
names.insert(ObjectRole, "object");
return names;
}
/**
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QObject>
#include <QAbstractListModel>
#include <QVariant>
#include <KPublicTransport/Manager>
#include <KPublicTransport/Location>
class LocationQueryModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
public:
enum Roles { NameRole = Qt::DisplayRole, ObjectRole = Qt::UserRole + 1 };
explicit LocationQueryModel(QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
QString query() const;
void setQuery(const QString query);
Q_SIGNALS:
void queryChanged();
private Q_SLOTS:
void triggerQuery();
private:
KPublicTransport::Manager m_manager;
QString m_query;
std::vector<KPublicTransport::Location> m_locations;
};
......@@ -19,7 +19,6 @@
*/
#include "androidutils.h"
#include "locationquerymodel.h"
#include "querycontroller.h"
#include "locationcache.h"
#include "formatter.h"
......@@ -34,6 +33,7 @@
#endif
#include <KPublicTransport/Manager>
#include <KPublicTransport/LocationRequest>
#include <KLocalizedContext>
#ifdef Q_OS_ANDROID
......@@ -59,7 +59,7 @@ int main(int argc, char *argv[])
QQmlApplicationEngine engine(QUrl(QStringLiteral("qrc:/qml/main.qml")));
engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
qmlRegisterType<LocationQueryModel>("org.kde.ktrip", 0, 1, "LocationQueryModel");
qRegisterMetaType<KPublicTransport::LocationRequest>();
QueryController queryController;
engine.rootContext()->setContextProperty(QStringLiteral("_queryController"), &queryController);
......
......@@ -109,6 +109,7 @@ Kirigami.Page
}
RowLayout {
width: parent.width
Label {
text: theData.scheduledArrivalTime.toLocaleTimeString(Locale.ShortFormat)
}
......@@ -126,6 +127,7 @@ Kirigami.Page
Label {
text: theData.scheduledArrivalPlatform
}
}
......
......@@ -23,6 +23,7 @@ import QtQuick.Layouts 1.1
import QtQuick.Controls 2.4
import org.kde.kirigami 2.8 as Kirigami
import org.kde.ktrip 0.1
import org.kde.kpublictransport 1.0 as KPT
Kirigami.Page
{
......@@ -32,7 +33,7 @@ Kirigami.Page
header: Kirigami.SearchField {
id: queryTextField
onAccepted: {
queryModel.query = text
queryModel.request = _queryController.createLocationRequest(text)
showCached = false
}
}
......@@ -53,8 +54,9 @@ Kirigami.Page
}
}
LocationQueryModel {
KPT.LocationQueryModel {
id: queryModel
manager: _manager
}
ListView {
......@@ -63,14 +65,19 @@ Kirigami.Page
model: queryModel
delegate: Kirigami.BasicListItem {
text: name
text: location.name
reserveSpaceForIcon: false
onClicked: {
_locationCache.addCachedLocation(object)
callback(object)
_locationCache.addCachedLocation(location)
callback(location)
pageStack.pop()
}
}
}
BusyIndicator {
running: queryModel.loading
anchors.centerIn: parent
}
}
......@@ -93,3 +93,11 @@ void QueryController::setDepartureTime(const QString &time)
Q_EMIT departureTimeChanged();
}
}
KPublicTransport::LocationRequest QueryController::createLocationRequest(const QString name)
{
KPublicTransport::LocationRequest req;
req.setName(name);
return req;
}
......@@ -21,8 +21,10 @@
#pragma once
#include <QObject>
#include <QVariant>
#include <KPublicTransport/Location>
#include <KPublicTransport/LocationRequest>
#include <KPublicTransport/JourneyRequest>
class QueryController : public QObject
......@@ -50,6 +52,7 @@ public:
void setDepartureTime(const QString &time);
Q_INVOKABLE KPublicTransport::JourneyRequest createJourneyRequest();
Q_INVOKABLE KPublicTransport::LocationRequest createLocationRequest(const QString name);
Q_SIGNALS:
void startChanged();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment