Marble

OfflineDataModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2012 Dennis Nienhüser <nienhueser@kde.org>
4//
5
6#include "OfflineDataModel.h"
7#include "MarbleDirs.h"
8
9#include <QDir>
10#include <QModelIndex>
11
12OfflineDataModel::OfflineDataModel(QObject *parent)
13 : QSortFilterProxyModel(parent)
14 , m_vehicleTypeFilter(Any)
15{
16 m_newstuffModel.setTargetDirectory(Marble::MarbleDirs::localPath() + QLatin1StringView("/maps"));
17 m_newstuffModel.setRegistryFile(QDir::homePath() + QStringLiteral("/.kde/share/apps/knewstuff3/marble-offline-data.knsregistry"),
18 Marble::NewstuffModel::NameTag);
19 m_newstuffModel.setProvider(QStringLiteral("http://files.kde.org/marble/newstuff/maps-monav.xml"));
20
21 setSourceModel(&m_newstuffModel);
22 QHash<int, QByteArray> roleNames = m_newstuffModel.roleNames();
23 roleNames[Qt::UserRole + 17] = "continent";
24 m_roleNames = roleNames;
25
26 sort(0);
27 setDynamicSortFilter(true);
28
29 connect(&m_newstuffModel, &Marble::NewstuffModel::installationProgressed, this, &OfflineDataModel::handleInstallationProgress);
30 connect(&m_newstuffModel, &Marble::NewstuffModel::installationFinished, this, &OfflineDataModel::handleInstallationFinished);
31 connect(&m_newstuffModel, &Marble::NewstuffModel::installationFailed, this, &OfflineDataModel::handleInstallationFailed);
32 connect(&m_newstuffModel, &Marble::NewstuffModel::uninstallationFinished, this, &OfflineDataModel::handleUninstallationFinished);
33}
34
35int OfflineDataModel::count() const
36{
37 return rowCount();
38}
39
40QHash<int, QByteArray> OfflineDataModel::roleNames() const
41{
42 return m_roleNames;
43}
44
45QVariant OfflineDataModel::data(const QModelIndex &index, int role) const
46{
47 if (index.isValid() && index.row() >= 0 && index.row() < rowCount() && role == Qt::DisplayRole) {
49 if (data.size() > 1) {
50 QString result = data.at(1);
51 for (int i = 2; i < data.size(); ++i) {
52 result += QLatin1StringView(" / ") + data.at(i);
53 }
54 result.remove(QLatin1StringView(" (Motorcar)"));
55 result.remove(QLatin1StringView(" (Pedestrian)"));
56 result.remove(QLatin1StringView(" (Bicycle)"));
57 return result.trimmed();
58 }
59 }
60
61 if (index.isValid() && index.row() >= 0 && index.row() < rowCount() && role == Qt::UserRole + 17) {
63 if (data.size() > 1) {
64 return data.first().trimmed();
65 }
66 }
67
69}
70
71void OfflineDataModel::setVehicleTypeFilter(VehicleTypes filter)
72{
73 m_vehicleTypeFilter = filter;
76}
77
78void OfflineDataModel::install(int index)
79{
80 m_newstuffModel.install(toSource(index));
81}
82
83void OfflineDataModel::uninstall(int index)
84{
85 m_newstuffModel.uninstall(toSource(index));
86}
87
88void OfflineDataModel::cancel(int index)
89{
90 m_newstuffModel.cancel(toSource(index));
91}
92
93int OfflineDataModel::fromSource(int index) const
94{
95 return mapFromSource(m_newstuffModel.index(index)).row();
96}
97
98int OfflineDataModel::toSource(int idx) const
99{
100 return mapToSource(index(idx, 0)).row();
101}
102
103void OfflineDataModel::handleInstallationProgress(int index, qreal progress)
104{
105 Q_EMIT installationProgressed(fromSource(index), progress);
106}
107
108void OfflineDataModel::handleInstallationFinished(int index)
109{
110 Q_EMIT installationFinished(fromSource(index));
111}
112
113void OfflineDataModel::handleInstallationFailed(int index, const QString &error)
114{
115 Q_EMIT installationFailed(fromSource(index), error);
116}
117
118void OfflineDataModel::handleUninstallationFinished(int index)
119{
120 Q_EMIT uninstallationFinished(fromSource(index));
121}
122
123bool OfflineDataModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
124{
125 if (QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent)) {
126 QModelIndex const index = sourceModel()->index(source_row, 0, source_parent);
127 QString const data = sourceModel()->data(index, Qt::DisplayRole).toString();
128 if ((m_vehicleTypeFilter & Motorcar) && data.contains(QLatin1StringView("(Motorcar)"))) {
129 return true;
130 } else if ((m_vehicleTypeFilter & Bicycle) && data.contains(QLatin1StringView("(Bicycle)"))) {
131 return true;
132 } else if ((m_vehicleTypeFilter & Pedestrian) && data.contains(QLatin1StringView("(Pedestrian)"))) {
133 return true;
134 }
135 }
136
137 return false;
138}
139
140#include "moc_OfflineDataModel.cpp"
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QString homePath()
const_reference at(qsizetype i) const const
pointer data()
T & first()
qsizetype size() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual int rowCount(const QModelIndex &parent) const const override
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString trimmed() const const
UserRole
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:52:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.