QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 ***************************************************************************/
19#include "qgsapplication.h"
20
21#include <QFont>
22
23#ifdef ENABLE_MODELTEST
24#include "modeltest.h"
25#endif
26
28 : QAbstractItemModel( parent )
29 , mColumnCount( subclassColumnCount )
30{
31#ifdef ENABLE_MODELTEST
32 new ModelTest( this, this );
33#endif
34
36 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsPushed, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsPushed );
37 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsRemoved, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsRemoved );
38 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::recentCrsCleared, this, &QgsRecentCoordinateReferenceSystemsModel::recentCrsCleared );
39}
40
41Qt::ItemFlags QgsRecentCoordinateReferenceSystemsModel::flags( const QModelIndex &index ) const
42{
43 if ( !index.isValid() )
44 {
45 return Qt::ItemFlags();
46 }
47
48 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
49}
50
51QVariant QgsRecentCoordinateReferenceSystemsModel::data( const QModelIndex &index, int role ) const
52{
54 if ( !crs.isValid() )
55 return QVariant();
56
57 if ( index.column() == 0 )
58 {
59 switch ( role )
60 {
61 case Qt::DisplayRole:
62 case Qt::ToolTipRole:
64
65 case static_cast< int >( CustomRole::Crs ):
66 return crs;
67
68 case static_cast< int >( CustomRole::AuthId ):
69 return crs.authid();
70
71 default:
72 break;
73 }
74 }
75
76 return QVariant();
77}
78
79int QgsRecentCoordinateReferenceSystemsModel::rowCount( const QModelIndex &parent ) const
80{
81 if ( parent.isValid() )
82 return 0;
83
84 return mCrs.size();
85}
86
88{
89 return mColumnCount;
90}
91
92QModelIndex QgsRecentCoordinateReferenceSystemsModel::index( int row, int column, const QModelIndex &parent ) const
93{
94 if ( row < 0 || row >= mCrs.size() || column < 0 || column >= columnCount( parent ) || parent.isValid() )
95 return QModelIndex();
96
97 return createIndex( row, column );
98}
99
100QModelIndex QgsRecentCoordinateReferenceSystemsModel::parent( const QModelIndex & ) const
101{
102 return QModelIndex();
103}
104
106{
107 if ( !index.isValid() )
109
110 return mCrs.value( index.row() );
111}
112
113void QgsRecentCoordinateReferenceSystemsModel::recentCrsPushed( const QgsCoordinateReferenceSystem &crs )
114{
115 const int currentRow = mCrs.indexOf( crs );
116 if ( currentRow > 0 )
117 {
118 // move operation
119 beginMoveRows( QModelIndex(), currentRow, currentRow, QModelIndex(), 0 );
120 mCrs.removeAt( currentRow );
121 mCrs.insert( 0, crs );
122 endMoveRows();
123 }
124 else if ( currentRow < 0 )
125 {
126 // add operation
127 beginInsertRows( QModelIndex(), 0, 0 );
128 mCrs.insert( 0, crs );
129 endInsertRows();
130 }
131}
132
133void QgsRecentCoordinateReferenceSystemsModel::recentCrsRemoved( const QgsCoordinateReferenceSystem &crs )
134{
135 const int currentRow = mCrs.indexOf( crs );
136 if ( currentRow >= 0 )
137 {
138 beginRemoveRows( QModelIndex(), currentRow, currentRow );
139 mCrs.removeAt( currentRow );
140 endRemoveRows();
141 }
142}
143
144void QgsRecentCoordinateReferenceSystemsModel::recentCrsCleared()
145{
146 beginResetModel();
147 mCrs.clear();
148 endResetModel();
149}
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: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