Commit d17f48fa authored by Nicolas Fella's avatar Nicolas Fella

Initial commit

parents
build/
*.kdev4
cmake_minimum_required(VERSION 3.0)
find_package(Qt5 REQUIRED COMPONENTS Gui Qml Widgets)
find_package(KPublicTransport REQUIRED)
set(CMAKE_AUTOMOC ON)
add_subdirectory(src)
qt5_add_resources(ktrip_SRCS resources.qrc)
add_executable(ktrip main.cpp querycontroller.cpp locationquerymodel.cpp ${ktrip_SRCS})
target_link_libraries(ktrip Qt5::Qml Qt5::Gui Qt5::Widgets KPublicTransport)
#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("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;
}
#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;
};
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "locationquerymodel.h"
#include "querycontroller.h"
#include <KPublicTransport/JourneyQueryModel>
#include <KPublicTransport/Manager>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine(QUrl(QStringLiteral("qrc:/qml/main.qml")));
qmlRegisterType<LocationQueryModel>("org.kde.ktrip", 0, 1, "LocationQueryModel");
qmlRegisterType<KPublicTransport::JourneyQueryModel>("org.kde.ktrip", 0, 1, "JourneyQueryModel");
QueryController queryController;
engine.rootContext()->setContextProperty("_queryController", &queryController);
return app.exec();
}
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.4
import org.kde.kirigami 2.4 as Kirigami
import org.kde.ktrip 0.1
Kirigami.Page
{
title: "Details"
property var journey
Kirigami.CardsListView {
anchors.fill: parent
model: journey.sections
delegate: Kirigami.Card {
height: Kirigami.Units.gridUnit * 4
Column {
Label {
anchors.leftMargin: Kirigami.Units.largeSpacing
text: modelData.from.name + " " + modelData.scheduledDepartureTime
}
Label {
text: modelData.to.name + " " + modelData.scheduledArrivalTime
}
}
}
}
}
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.4
import org.kde.kirigami 2.4 as Kirigami
import org.kde.ktrip 0.1
Kirigami.Page
{
title: "Connections"
header: Label {
text: _queryController.journeyModel.errorMessage
}
ListView {
anchors.fill: parent
model: _queryController.journeyModel
delegate: Kirigami.BasicListItem {
text: journey.sections[0].from.name + " - " + journey.sections[journey.sections.length - 1].to.name + " (" + journey.numberOfChanges + " changes)"
onClicked: pageStack.push(Qt.resolvedUrl("ConnectionDetailsPage.qml"), {journey: journey})
}
}
}
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.4
import org.kde.kirigami 2.0 as Kirigami
import org.kde.ktrip 0.1
Kirigami.Page
{
property string type
title: {
if (type == "start") {
return "Search for start location"
}
if (type == "destination") {
return "Search for destination location"
}
return "deadbeef"
}
header: TextField {
id: queryTextField
placeholderText: "Search..."
onAccepted: queryModel.query = text
}
ListView {
anchors.fill: parent
model: LocationQueryModel {
id: queryModel
}
delegate: Kirigami.BasicListItem {
text: name
onClicked: {
if (type == "start") {
_queryController.start = object
} else if (type == "destination") {
_queryController.destination = object
}
pageStack.pop()
}
}
}
}
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.4
import org.kde.kirigami 2.0 as Kirigami
Kirigami.Page
{
id: rootPage
property var start;
title: "Start journey"
actions.main: Kirigami.Action {
icon.name: "search"
text: "Search"
onTriggered: pageStack.push(Qt.resolvedUrl("ConnectionsPage.qml"))
}
ColumnLayout {
width: parent.width
Label {
text: "From:"
}
Button {
Layout.fillWidth: true
text: _queryController.start.name ? _queryController.start.name : "Pick start"
onClicked: pageStack.push(Qt.resolvedUrl("LocationQueryPage.qml"), {type: "start"})
}
Label {
text: "To:"
}
Button {
Layout.fillWidth: true
text: _queryController.start.name ? _queryController.destination.name : "Pick destination"
onClicked: pageStack.push(Qt.resolvedUrl("LocationQueryPage.qml"), {type: "destination"})
}
RowLayout {
width: parent.width
Button {
text: "Pick date"
Layout.fillWidth: true
}
Button {
text: "Pick time"
Layout.fillWidth: true
}
}
}
}
import QtQuick 2.2
import QtQuick.Controls 2.4
import org.kde.kirigami 2.0 as Kirigami
Kirigami.ApplicationWindow
{
width: 480
height: 720
pageStack.initialPage: Qt.resolvedUrl("StartPage.qml")
}
#include "querycontroller.h"
#include <QDebug>
#include <QDateTime>
QueryController::QueryController(QObject* parent)
: QObject(parent)
, m_start()
, m_destination()
, m_journeyModel(new KPublicTransport::JourneyQueryModel)
, m_manager()
{
m_journeyModel->setManager(&m_manager);
connect(this, &QueryController::startChanged, this, &QueryController::createJourneyRequest);
connect(this, &QueryController::destinationChanged, this, &QueryController::createJourneyRequest);
}
void QueryController::setStart(const KPublicTransport::Location start)
{
m_start = start;
Q_EMIT startChanged();
}
KPublicTransport::Location QueryController::start() const
{
return m_start;
}
void QueryController::setDestination(const KPublicTransport::Location destination)
{
m_destination = destination;
Q_EMIT destinationChanged();
}
KPublicTransport::Location QueryController::destination() const
{
return m_destination;
}
KPublicTransport::JourneyQueryModel * QueryController::journeyModel() const
{
return m_journeyModel;
}
void QueryController::createJourneyRequest()
{
if (m_start.isEmpty() || m_destination.isEmpty()) {
return;
}
KPublicTransport::JourneyRequest req;
req.setFrom(m_start);
req.setTo(m_destination);
req.setDepartureTime(QDateTime::currentDateTime());
m_journeyModel->setJourneyRequest(req);
}
#pragma once
#include <QObject>
#include <KPublicTransport/Location>
#include <KPublicTransport/JourneyRequest>
#include <KPublicTransport/JourneyQueryModel>
#include <KPublicTransport/Manager>
class QueryController : public QObject {
Q_OBJECT
Q_PROPERTY(KPublicTransport::Location start READ start WRITE setStart NOTIFY startChanged)
Q_PROPERTY(KPublicTransport::Location destination READ destination WRITE setDestination NOTIFY destinationChanged)
Q_PROPERTY(KPublicTransport::JourneyQueryModel* journeyModel READ journeyModel CONSTANT)
public:
explicit QueryController(QObject *parent=nullptr);
KPublicTransport::Location start() const;
void setStart(const KPublicTransport::Location start);
KPublicTransport::Location destination() const;
void setDestination(const KPublicTransport::Location destination);
KPublicTransport::JourneyQueryModel * journeyModel() const;
Q_SIGNALS:
void startChanged();
void destinationChanged();
private Q_SLOTS:
void createJourneyRequest();
private:
KPublicTransport::Location m_start;
KPublicTransport::Location m_destination;
KPublicTransport::JourneyQueryModel *m_journeyModel;
KPublicTransport::Manager m_manager;
};
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>qml/main.qml</file>
<file>qml/StartPage.qml</file>
<file>qml/ConnectionsPage.qml</file>
<file>qml/LocationQueryPage.qml</file>
<file>qml/ConnectionDetailsPage.qml</file>
</qresource>
</RCC>
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