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 <QModelIndex>
10#include <QDir>
11
12OfflineDataModel::OfflineDataModel( QObject *parent ) : QSortFilterProxyModel( parent ),
13 m_vehicleTypeFilter( Any )
14{
15 m_newstuffModel.setTargetDirectory(Marble::MarbleDirs::localPath() + QLatin1String("/maps"));
16 m_newstuffModel.setRegistryFile( QDir::homePath() + "/.kde/share/apps/knewstuff3/marble-offline-data.knsregistry", Marble::NewstuffModel::NameTag );
17 m_newstuffModel.setProvider( "http://files.kde.org/marble/newstuff/maps-monav.xml" );
18
19 setSourceModel( &m_newstuffModel );
20 QHash<int,QByteArray> roleNames = m_newstuffModel.roleNames();
21 roleNames[Qt::UserRole+17] = "continent";
22 m_roleNames = roleNames;
23
24 sort( 0 );
25 setDynamicSortFilter( true );
26
27 connect( &m_newstuffModel, SIGNAL(installationProgressed(int,qreal)), this, SLOT(handleInstallationProgress(int,qreal)) );
28 connect( &m_newstuffModel, SIGNAL(installationFinished(int)), this, SLOT(handleInstallationFinished(int)) );
29 connect( &m_newstuffModel, SIGNAL(installationFailed(int,QString)), this, SLOT(handleInstallationFailed(int,QString)) );
30 connect( &m_newstuffModel, SIGNAL(uninstallationFinished(int)), this, SLOT(handleUninstallationFinished(int)) );
31}
32
33int OfflineDataModel::count() const
34{
35 return rowCount();
36}
37
38QHash<int, QByteArray> OfflineDataModel::roleNames() const
39{
40 return m_roleNames;
41}
42
43QVariant OfflineDataModel::data(const QModelIndex &index, int role) const
44{
45 if ( index.isValid() && index.row() >= 0 && index.row() < rowCount() && role == Qt::DisplayRole ) {
47 if ( data.size() > 1 ) {
48 QString result = data.at( 1 );
49 for ( int i=2; i<data.size(); ++i ) {
50 result += QLatin1String(" / ") + data.at(i);
51 }
52 result.remove( QLatin1String( " (Motorcar)" ) );
53 result.remove( QLatin1String( " (Pedestrian)" ) );
54 result.remove( QLatin1String( " (Bicycle)" ) );
55 return result.trimmed();
56 }
57 }
58
59 if ( index.isValid() && index.row() >= 0 && index.row() < rowCount() && role == Qt::UserRole+17 ) {
61 if ( data.size() > 1 ) {
62 return data.first().trimmed();
63 }
64 }
65
66 return QSortFilterProxyModel::data( index, role );
67}
68
69void OfflineDataModel::setVehicleTypeFilter( VehicleTypes filter )
70{
71 m_vehicleTypeFilter = filter;
74}
75
76void OfflineDataModel::install( int index )
77{
78 m_newstuffModel.install( toSource( index ) );
79}
80
81void OfflineDataModel::uninstall( int index )
82{
83 m_newstuffModel.uninstall( toSource( index ) );
84}
85
86void OfflineDataModel::cancel( int index )
87{
88 m_newstuffModel.cancel( toSource( index ) );
89}
90
91int OfflineDataModel::fromSource( int index ) const
92{
93 return mapFromSource( m_newstuffModel.index( index ) ).row();
94}
95
96int OfflineDataModel::toSource(int idx) const
97{
98 return mapToSource( index( idx, 0 ) ).row();
99}
100
101void OfflineDataModel::handleInstallationProgress( int index, qreal progress )
102{
103 emit installationProgressed( fromSource( index ), progress );
104}
105
106void OfflineDataModel::handleInstallationFinished( int index )
107{
108 emit installationFinished( fromSource( index ) );
109}
110
111void OfflineDataModel::handleInstallationFailed( int index, const QString &error )
112{
113 emit installationFailed( fromSource( index ), error );
114}
115
116void OfflineDataModel::handleUninstallationFinished( int index )
117{
118 emit uninstallationFinished( fromSource( index ) );
119}
120
121bool OfflineDataModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
122{
124 QModelIndex const index = sourceModel()->index( source_row, 0, source_parent );
125 QString const data = sourceModel()->data( index, Qt::DisplayRole ).toString();
126 if ((m_vehicleTypeFilter & Motorcar) && data.contains(QLatin1String("(Motorcar)"))) {
127 return true;
128 } else if ((m_vehicleTypeFilter & Bicycle) && data.contains(QLatin1String("(Bicycle)"))) {
129 return true;
130 } else if ((m_vehicleTypeFilter & Pedestrian) && data.contains(QLatin1String("(Pedestrian)"))) {
131 return true;
132 }
133 }
134
135 return false;
136}
137
138#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
T qobject_cast(QObject *object)
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 Jun 21 2024 12:00:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.