QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgsrecentcoordinatereferencesystemsmodel.cpp"
20#include "qgsapplication.h"
21
22#include <QFont>
23
24#ifdef ENABLE_MODELTEST
25#include "modeltest.h"
26#endif
27
29 : QAbstractItemModel( parent )
30 , mColumnCount( subclassColumnCount )
31{
32#ifdef ENABLE_MODELTEST
33 new ModelTest( this, this );
34#endif
35
37 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsPushed, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsPushed );
38 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsRemoved, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsRemoved );
39 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsCleared, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsCleared );
40}
41
42Qt::ItemFlags QgsRecentCoordinateReferenceSystemsModel::flags( const QModelIndex &index ) const
43{
44 if ( !index.isValid() )
45 {
46 return Qt::ItemFlags();
47 }
48
49 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
50}
51
52QVariant QgsRecentCoordinateReferenceSystemsModel::data( const QModelIndex &index, int role ) const
53{
55 if ( !crs.isValid() )
56 return QVariant();
57
58 if ( index.column() == 0 )
59 {
60 switch ( role )
61 {
62 case Qt::DisplayRole:
63 case Qt::ToolTipRole:
65
66 case static_cast< int >( CustomRole::Crs ):
67 return crs;
68
69 case static_cast< int >( CustomRole::AuthId ):
70 return crs.authid();
71
72 default:
73 break;
74 }
75 }
76
77 return QVariant();
78}
79
80int QgsRecentCoordinateReferenceSystemsModel::rowCount( const QModelIndex &parent ) const
81{
82 if ( parent.isValid() )
83 return 0;
84
85 return mCrs.size();
86}
87
89{
90 return mColumnCount;
91}
92
93QModelIndex QgsRecentCoordinateReferenceSystemsModel::index( int row, int column, const QModelIndex &parent ) const
94{
95 if ( row < 0 || row >= mCrs.size() || column < 0 || column >= columnCount( parent ) || parent.isValid() )
96 return QModelIndex();
97
98 return createIndex( row, column );
99}
100
101QModelIndex QgsRecentCoordinateReferenceSystemsModel::parent( const QModelIndex & ) const
102{
103 return QModelIndex();
104}
105
107{
108 if ( !index.isValid() )
110
111 return mCrs.value( index.row() );
112}
113
114void QgsRecentCoordinateReferenceSystemsModel::recentCrsPushed( const QgsCoordinateReferenceSystem &crs )
115{
116 const int currentRow = mCrs.indexOf( crs );
117 if ( currentRow > 0 )
118 {
119 // move operation
120 beginMoveRows( QModelIndex(), currentRow, currentRow, QModelIndex(), 0 );
121 mCrs.removeAt( currentRow );
122 mCrs.insert( 0, crs );
123 endMoveRows();
124 }
125 else if ( currentRow < 0 )
126 {
127 // add operation
128 beginInsertRows( QModelIndex(), 0, 0 );
129 mCrs.insert( 0, crs );
130 endInsertRows();
131 }
132}
133
134void QgsRecentCoordinateReferenceSystemsModel::recentCrsRemoved( const QgsCoordinateReferenceSystem &crs )
135{
136 const int currentRow = mCrs.indexOf( crs );
137 if ( currentRow >= 0 )
138 {
139 beginRemoveRows( QModelIndex(), currentRow, currentRow );
140 mCrs.removeAt( currentRow );
141 endRemoveRows();
142 }
143}
144
145void QgsRecentCoordinateReferenceSystemsModel::recentCrsCleared()
146{
147 beginResetModel();
148 mCrs.clear();
149 endResetModel();
150}
151
152
153
154//
155// QgsRecentCoordinateReferenceSystemsProxyModel
156//
157
159 : QSortFilterProxyModel( parent )
160 , mModel( new QgsRecentCoordinateReferenceSystemsModel( this, subclassColumnCount ) )
161{
162 setSourceModel( mModel );
163 setDynamicSortFilter( true );
164}
165
170
175
177{
178 if ( mFilters == filters )
179 return;
180
181 mFilters = filters;
182 invalidateFilter();
183}
184
186{
187 if ( mFilterDeprecated == filter )
188 return;
189
190 mFilterDeprecated = filter;
191 invalidateFilter();
192}
193
195{
196 mFilterString = filter;
197 invalidateFilter();
198}
199
200bool QgsRecentCoordinateReferenceSystemsProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
201{
202 if ( !mFilters )
203 return true;
204
205 const QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
206
207 const QgsCoordinateReferenceSystem crs = mModel->crs( sourceIndex );
208 if ( mFilterDeprecated && crs.isDeprecated() )
209 return false;
210
211 const Qgis::CrsType type = crs.type();
212 switch ( type )
213 {
216 break;
217
228 return false;
229 break;
230
233 return false;
234 break;
235
238 return false;
239 break;
240 }
241
242 if ( !mFilterString.trimmed().isEmpty() )
243 {
244 if ( !( crs.description().contains( mFilterString, Qt::CaseInsensitive )
245 || crs.authid().contains( mFilterString, Qt::CaseInsensitive ) ) )
246 return false;
247 }
248
249 return true;
250}
251
253{
254 const QModelIndex sourceIndex = mapToSource( index );
255 return mModel->crs( sourceIndex );
256}
CrsType
Coordinate reference system types.
Definition qgis.h:2159
@ Vertical
Vertical CRS.
@ Temporal
Temporal CRS.
@ Compound
Compound (horizontal + vertical) CRS.
@ Projected
Projected CRS.
@ Other
Other type.
@ Bound
Bound CRS.
@ DerivedProjected
Derived projected CRS.
@ Unknown
Unknown type.
@ Engineering
Engineering CRS.
@ Geographic3d
3D geopraphic CRS
@ Geodetic
Geodetic CRS.
@ Geographic2d
2D geographic CRS
@ Geocentric
Geocentric CRS.
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.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString userFriendlyIdentifier(Qgis::CrsIdentifierType type=Qgis::CrsIdentifierType::MediumString) const
Returns a user friendly identifier for the CRS.
Qgis::CrsType type() const
Returns the type of the CRS.
bool isDeprecated() const
Returns true if the CRS is considered deprecated.
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.
const QgsCoordinateReferenceSystem & crs