File indexing completed on 2024-05-12 04:42:52

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "pathmodel.h"
0008 
0009 using namespace KPublicTransport;
0010 
0011 PathModel::PathModel(QObject *parent)
0012     : QAbstractListModel(parent)
0013 {
0014 }
0015 
0016 PathModel::~PathModel() = default;
0017 
0018 Path PathModel::path() const
0019 {
0020     return m_path;
0021 }
0022 
0023 void PathModel::setPath(const Path &path)
0024 {
0025     beginResetModel();
0026     m_path = path;
0027     endResetModel();
0028 }
0029 
0030 int PathModel::rowCount(const QModelIndex &parent) const
0031 {
0032     if (parent.isValid()) {
0033         return 0;
0034     }
0035     return m_path.sections().size();
0036 }
0037 
0038 QVariant PathModel::data(const QModelIndex &index, int role) const
0039 {
0040     switch (role) {
0041         case PathSectionRole:
0042             return m_path.sections()[index.row()];
0043         case TurnDirectionRole:
0044         {
0045             const auto curDir = m_path.sections()[index.row()].direction();
0046             if (index.row() == 0) {
0047                 return curDir;
0048             }
0049             const auto prevDir = m_path.sections()[index.row() - 1].direction();
0050             if (prevDir >= 0 && curDir >= 0) {
0051                 return (360 + curDir - prevDir) % 360;
0052             }
0053             return curDir;
0054         }
0055     }
0056 
0057     return {};
0058 }
0059 
0060 QHash<int, QByteArray> PathModel::roleNames() const
0061 {
0062     auto r = QAbstractListModel::roleNames();
0063     r.insert(PathSectionRole, "section");
0064     r.insert(TurnDirectionRole, "turnDirection");
0065     return r;
0066 }