QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsrecentcoordinatereferencesystemsmodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrecentcoordinatereferencesystemsmodel.cpp
3 -------------------
4 begin : January 2024
5 copyright : (C) 2024 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18
19#include "qgsapplication.h"
21
22#include <QFont>
23
24#include "moc_qgsrecentcoordinatereferencesystemsmodel.cpp"
25
26#ifdef ENABLE_MODELTEST
27#include "modeltest.h"
28#endif
29
31 : QAbstractItemModel( parent )
32 , mColumnCount( subclassColumnCount )
33{
34#ifdef ENABLE_MODELTEST
35 new ModelTest( this, this );
36#endif
37
39 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsPushed, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsPushed );
40 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsRemoved, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsRemoved );
41 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsCleared, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsCleared );
42}
43
44Qt::ItemFlags QgsRecentCoordinateReferenceSystemsModel::flags( const QModelIndex &index ) const
45{
46 if ( !index.isValid() )
47 {
48 return Qt::ItemFlags();
49 }
50
51 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
52}
53
54QVariant QgsRecentCoordinateReferenceSystemsModel::data( const QModelIndex &index, int role ) const
55{
57 if ( !crs.isValid() )
58 return QVariant();
59
60 if ( index.column() == 0 )
61 {
62 switch ( role )
63 {
64 case Qt::DisplayRole:
65 case Qt::ToolTipRole:
66 return crs.userFriendlyIdentifier();
67
68 case static_cast<int>( CustomRole::Crs ):
69 return crs;
70
71 case static_cast<int>( CustomRole::AuthId ):
72 return crs.authid();
73
74 default:
75 break;
76 }
77 }
78
79 return QVariant();
80}
81
83{
84 if ( parent.isValid() )
85 return 0;
86
87 return mCrs.size();
88}
89
91{
92 return mColumnCount;
93}
94
95QModelIndex QgsRecentCoordinateReferenceSystemsModel::index( int row, int column, const QModelIndex &parent ) const
96{
97 if ( row < 0 || row >= mCrs.size() || column < 0 || column >= columnCount( parent ) || parent.isValid() )
98 return QModelIndex();
99
100 return createIndex( row, column );
101}
102
103QModelIndex QgsRecentCoordinateReferenceSystemsModel::parent( const QModelIndex & ) const
104{
105 return QModelIndex();
106}
107
109{
110 if ( !index.isValid() )
112
113 return mCrs.value( index.row() );
114}
115
116void QgsRecentCoordinateReferenceSystemsModel::recentCrsPushed( const QgsCoordinateReferenceSystem &crs )
117{
118 const int currentRow = mCrs.indexOf( crs );
119 if ( currentRow > 0 )
120 {
121 // move operation
122 beginMoveRows( QModelIndex(), currentRow, currentRow, QModelIndex(), 0 );
123 mCrs.removeAt( currentRow );
124 mCrs.insert( 0, crs );
125 endMoveRows();
126 }
127 else if ( currentRow < 0 )
128 {
129 // add operation
130 beginInsertRows( QModelIndex(), 0, 0 );
131 mCrs.insert( 0, crs );
132 endInsertRows();
133 }
134}
135
136void QgsRecentCoordinateReferenceSystemsModel::recentCrsRemoved( const QgsCoordinateReferenceSystem &crs )
137{
138 const int currentRow = mCrs.indexOf( crs );
139 if ( currentRow >= 0 )
140 {
141 beginRemoveRows( QModelIndex(), currentRow, currentRow );
142 mCrs.removeAt( currentRow );
143 endRemoveRows();
144 }
145}
146
147void QgsRecentCoordinateReferenceSystemsModel::recentCrsCleared()
148{
149 beginResetModel();
150 mCrs.clear();
151 endResetModel();
152}
153
154
155//
156// QgsRecentCoordinateReferenceSystemsProxyModel
157//
158
160 : QSortFilterProxyModel( parent )
161 , mModel( new QgsRecentCoordinateReferenceSystemsModel( this, subclassColumnCount ) )
162{
163 setSourceModel( mModel );
164 setDynamicSortFilter( true );
165}
166
171
176
178{
179 if ( mFilters == filters )
180 return;
181
182 mFilters = filters;
183 invalidateFilter();
184}
185
187{
188 if ( mFilterDeprecated == filter )
189 return;
190
191 mFilterDeprecated = filter;
192 invalidateFilter();
193}
194
196{
197 mFilterString = filter;
198 invalidateFilter();
199}
200
201bool QgsRecentCoordinateReferenceSystemsProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
202{
203 if ( !mFilters )
204 return true;
205
206 const QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
207
208 const QgsCoordinateReferenceSystem crs = mModel->crs( sourceIndex );
209 if ( mFilterDeprecated && crs.isDeprecated() )
210 return false;
211
212 const Qgis::CrsType type = crs.type();
213 switch ( type )
214 {
217 break;
218
229 return false;
230 break;
231
234 return false;
235 break;
236
239 return false;
240 break;
241 }
242
243 if ( !mFilterString.trimmed().isEmpty() )
244 {
245 if ( !( crs.description().contains( mFilterString, Qt::CaseInsensitive )
246 || crs.authid().contains( mFilterString, Qt::CaseInsensitive ) ) )
247 return false;
248 }
249
250 return true;
251}
252
254{
255 const QModelIndex sourceIndex = mapToSource( index );
256 return mModel->crs( sourceIndex );
257}
CrsType
Coordinate reference system types.
Definition qgis.h:2327
@ Vertical
Vertical CRS.
Definition qgis.h:2333
@ Temporal
Temporal CRS.
Definition qgis.h:2336
@ Compound
Compound (horizontal + vertical) CRS.
Definition qgis.h:2335
@ Projected
Projected CRS.
Definition qgis.h:2334
@ Other
Other type.
Definition qgis.h:2339
@ Bound
Bound CRS.
Definition qgis.h:2338
@ DerivedProjected
Derived projected CRS.
Definition qgis.h:2340
@ Unknown
Unknown type.
Definition qgis.h:2328
@ Engineering
Engineering CRS.
Definition qgis.h:2337
@ Geographic3d
3D geopraphic CRS
Definition qgis.h:2332
@ Geodetic
Geodetic CRS.
Definition qgis.h:2329
@ Geographic2d
2D geographic CRS
Definition qgis.h:2331
@ Geocentric
Geocentric CRS.
Definition qgis.h:2330
static QgsCoordinateReferenceSystemRegistry * coordinateReferenceSystemRegistry()
Returns the application's coordinate reference system (CRS) registry, which handles known CRS definit...
@ FilterVertical
Include vertical CRS (excludes compound CRS containing a vertical component).
@ FilterHorizontal
Include horizontal CRS (excludes compound CRS containing a horizontal component).
void recentCrsRemoved(const QgsCoordinateReferenceSystem &crs)
Emitted when a recently used CRS has been removed from the recent CRS list.
void recentCrsCleared()
Emitted when the list of recently used CRS has been cleared.
void recentCrsPushed(const QgsCoordinateReferenceSystem &crs)
Emitted when a recently used CRS has been pushed to the top of the recent CRS list.
QList< QgsCoordinateReferenceSystem > recentCrs()
Returns a list of recently used CRS.
Represents a coordinate reference system (CRS).
A model for display of recently used coordinate reference systems.
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsCoordinateReferenceSystem crs(const QModelIndex &index) const
Returns the CRS for the corresponding index.
QModelIndex parent(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QgsRecentCoordinateReferenceSystemsModel(QObject *parent=nullptr, int subclassColumnCount=1)
Constructor for QgsRecentCoordinateReferenceSystemsModel, with the specified parent object.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &=QModelIndex()) const override
QgsCoordinateReferenceSystem crs(const QModelIndex &index) const
Returns the CRS for the corresponding index.
void setFilters(QgsCoordinateReferenceSystemProxyModel::Filters filters)
Set filters that affect how CRS are filtered.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
QgsCoordinateReferenceSystemProxyModel::Filters filters() const
Returns any filters that affect how CRS are filtered.
QgsRecentCoordinateReferenceSystemsProxyModel(QObject *parent=nullptr, int subclassColumnCount=1)
Constructor for QgsRecentCoordinateReferenceSystemsProxyModel, with the given parent object.
void setFilterString(const QString &filter)
Sets a filter string, such that only coordinate reference systems matching the specified string will ...
void setFilterDeprecated(bool filter)
Sets whether deprecated CRS should be filtered from the results.
QgsRecentCoordinateReferenceSystemsModel * recentCoordinateReferenceSystemsModel()
Returns the underlying source model.