QGIS API Documentation 3.41.0-Master (af5edcb665c)
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// QgsRecentCoordinateReferenceSystemsProxyModel
155//
156
158 : QSortFilterProxyModel( parent )
159 , mModel( new QgsRecentCoordinateReferenceSystemsModel( this, subclassColumnCount ) )
160{
161 setSourceModel( mModel );
162 setDynamicSortFilter( true );
163}
164
169
174
176{
177 if ( mFilters == filters )
178 return;
179
180 mFilters = filters;
181 invalidateFilter();
182}
183
185{
186 if ( mFilterDeprecated == filter )
187 return;
188
189 mFilterDeprecated = filter;
190 invalidateFilter();
191}
192
194{
195 mFilterString = filter;
196 invalidateFilter();
197}
198
199bool QgsRecentCoordinateReferenceSystemsProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
200{
201 if ( !mFilters )
202 return true;
203
204 const QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
205
206 const QgsCoordinateReferenceSystem crs = mModel->crs( sourceIndex );
207 if ( mFilterDeprecated && crs.isDeprecated() )
208 return false;
209
210 const Qgis::CrsType type = crs.type();
211 switch ( type )
212 {
215 break;
216
227 return false;
228 break;
229
232 return false;
233 break;
234
237 return false;
238 break;
239 }
240
241 if ( !mFilterString.trimmed().isEmpty() )
242 {
243 if ( !( crs.description().contains( mFilterString, Qt::CaseInsensitive )
244 || crs.authid().contains( mFilterString, Qt::CaseInsensitive ) ) )
245 return false;
246 }
247
248 return true;
249}
250
252{
253 const QModelIndex sourceIndex = mapToSource( index );
254 return mModel->crs( sourceIndex );
255}
CrsType
Coordinate reference system types.
Definition qgis.h:2222
@ 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