QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgscoordinatereferencesystemmodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscoordinatereferencesystemmodel.h
3 -------------------
4 begin : July 2023
5 copyright : (C) 2023 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#include "qgsstringutils.h"
22
23#include <QFont>
24#include <QString>
25
26#include "moc_qgscoordinatereferencesystemmodel.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QAbstractItemModel( parent )
32 , mRootNode( std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( QString(), QIcon(), QString() ) )
33{
35
36 rebuild();
37
38 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::userCrsAdded, this, &QgsCoordinateReferenceSystemModel::userCrsAdded );
39 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::userCrsRemoved, this, &QgsCoordinateReferenceSystemModel::userCrsRemoved );
40 connect( QgsApplication::coordinateReferenceSystemRegistry(), &QgsCoordinateReferenceSystemRegistry::userCrsChanged, this, &QgsCoordinateReferenceSystemModel::userCrsChanged );
41}
42
43Qt::ItemFlags QgsCoordinateReferenceSystemModel::flags( const QModelIndex &index ) const
44{
45 if ( !index.isValid() )
46 {
47 return Qt::ItemFlags();
48 }
49
50 QgsCoordinateReferenceSystemModelNode *n = index2node( index );
51 if ( !n )
52 return Qt::ItemFlags();
53
54 switch ( n->nodeType() )
55 {
56 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
57 return index.column() == 0 ? Qt::ItemIsEnabled : Qt::ItemFlags();
58 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
59 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
60 }
62}
63
64QVariant QgsCoordinateReferenceSystemModel::data( const QModelIndex &index, int role ) const
65{
66 if ( !index.isValid() )
67 return QVariant();
68
69 QgsCoordinateReferenceSystemModelNode *n = index2node( index );
70 if ( !n )
71 return QVariant();
72
73 if ( role == static_cast<int>( CustomRole::NodeType ) )
74 return n->nodeType();
75
76 switch ( n->nodeType() )
77 {
78 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
79 {
80 QgsCoordinateReferenceSystemModelGroupNode *groupNode = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( n );
81 switch ( role )
82 {
83 case Qt::DecorationRole:
84 switch ( index.column() )
85 {
86 case 0:
87 return groupNode->icon();
88 default:
89 break;
90 }
91 break;
92
93 case Qt::DisplayRole:
94 case Qt::ToolTipRole:
95 switch ( index.column() )
96 {
97 case 0:
98 return groupNode->name();
99
100 default:
101 break;
102 }
103 break;
104
105 case Qt::FontRole:
106 {
107 QFont font;
108 font.setItalic( true );
109 if ( groupNode->parent() == mRootNode.get() )
110 {
111 font.setBold( true );
112 }
113 return font;
114 }
115
116 case static_cast<int>( CustomRole::GroupId ):
117 return groupNode->id();
118 }
119 return QVariant();
120 }
121 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
122 {
123 QgsCoordinateReferenceSystemModelCrsNode *crsNode = qgis::down_cast<QgsCoordinateReferenceSystemModelCrsNode *>( n );
124 switch ( role )
125 {
126 case Qt::DisplayRole:
127 case Qt::ToolTipRole:
128 switch ( index.column() )
129 {
130 case 0:
131 return crsNode->record().description;
132
133 case 1:
134 {
135 if ( crsNode->record().authName == "CUSTOM"_L1 )
136 return QString();
137 return u"%1:%2"_s.arg( crsNode->record().authName, crsNode->record().authId );
138 }
139
140 default:
141 break;
142 }
143 break;
144
145 case static_cast<int>( CustomRole::Name ):
146 return crsNode->record().description;
147
148 case static_cast<int>( CustomRole::AuthId ):
149 if ( !crsNode->record().authId.isEmpty() )
150 return u"%1:%2"_s.arg( crsNode->record().authName, crsNode->record().authId );
151 else
152 return QVariant();
153
154 case static_cast<int>( CustomRole::Deprecated ):
155 return crsNode->record().deprecated;
156
157 case static_cast<int>( CustomRole::Type ):
158 return QVariant::fromValue( crsNode->record().type );
159
160 case static_cast<int>( CustomRole::Wkt ):
161 return crsNode->wkt();
162
163 case static_cast<int>( CustomRole::Proj ):
164 return crsNode->proj();
165
166 case static_cast<int>( CustomRole::Group ):
167 return crsNode->group();
168
169 case static_cast<int>( CustomRole::Projection ):
170 return crsNode->projection();
171
172 default:
173 break;
174 }
175 }
176 }
177 return QVariant();
178}
179
180QVariant QgsCoordinateReferenceSystemModel::headerData( int section, Qt::Orientation orientation, int role ) const
181{
182 if ( orientation == Qt::Horizontal )
183 {
184 switch ( role )
185 {
186 case Qt::DisplayRole:
187 switch ( section )
188 {
189 case 0:
190 return tr( "Coordinate Reference System" );
191 case 1:
192 return tr( "Authority ID" );
193 default:
194 break;
195 }
196 break;
197
198 default:
199 break;
200 }
201 }
202 return QVariant();
203}
204
206{
207 QgsCoordinateReferenceSystemModelNode *n = index2node( parent );
208 if ( !n )
209 return 0;
210
211 return n->children().count();
212}
213
215{
216 return 2;
217}
218
219QModelIndex QgsCoordinateReferenceSystemModel::index( int row, int column, const QModelIndex &parent ) const
220{
221 if ( !hasIndex( row, column, parent ) )
222 return QModelIndex();
223
224 QgsCoordinateReferenceSystemModelNode *n = index2node( parent );
225 if ( !n )
226 return QModelIndex(); // have no children
227
228 return createIndex( row, column, n->children().at( row ) );
229}
230
231QModelIndex QgsCoordinateReferenceSystemModel::parent( const QModelIndex &child ) const
232{
233 if ( !child.isValid() )
234 return QModelIndex();
235
236 if ( QgsCoordinateReferenceSystemModelNode *n = index2node( child ) )
237 {
238 return indexOfParentTreeNode( n->parent() ); // must not be null
239 }
240 else
241 {
242 Q_ASSERT( false ); // no other node types!
243 return QModelIndex();
244 }
245}
246
247QModelIndex QgsCoordinateReferenceSystemModel::authIdToIndex( const QString &authid ) const
248{
249 const QModelIndex startIndex = index( 0, 0 );
250 const QModelIndexList hits = match( startIndex, static_cast<int>( CustomRole::AuthId ), authid, 1, Qt::MatchRecursive );
251 return hits.value( 0 );
252}
253
254void QgsCoordinateReferenceSystemModel::rebuild()
255{
256 beginResetModel();
257
258 mRootNode->deleteChildren();
259
260 for ( const QgsCrsDbRecord &record : std::as_const( mCrsDbRecords ) )
261 {
262 addRecord( record );
263 }
264
265 const QList<QgsCoordinateReferenceSystemRegistry::UserCrsDetails> userCrsList = QgsApplication::coordinateReferenceSystemRegistry()->userCrsList();
266 for ( const QgsCoordinateReferenceSystemRegistry::UserCrsDetails &details : userCrsList )
267 {
268 QgsCrsDbRecord userRecord;
269 userRecord.authName = u"USER"_s;
270 userRecord.authId = QString::number( details.id );
271 userRecord.description = details.name;
272
273 addRecord( userRecord );
274 }
275
276 endResetModel();
277}
278
279void QgsCoordinateReferenceSystemModel::userCrsAdded( const QString &id )
280{
281 const QList<QgsCoordinateReferenceSystemRegistry::UserCrsDetails> userCrsList = QgsApplication::coordinateReferenceSystemRegistry()->userCrsList();
282 for ( const QgsCoordinateReferenceSystemRegistry::UserCrsDetails &details : userCrsList )
283 {
284 if ( u"USER:%1"_s.arg( details.id ) == id )
285 {
286 QgsCrsDbRecord userRecord;
287 userRecord.authName = u"USER"_s;
288 userRecord.authId = QString::number( details.id );
289 userRecord.description = details.name;
290
291 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u"USER"_s );
292 if ( !group )
293 {
294 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( tr( "User-defined" ), QgsApplication::getThemeIcon( u"/user.svg"_s ), u"USER"_s );
295 beginInsertRows( QModelIndex(), mRootNode->children().length(), mRootNode->children().length() );
296 group = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( mRootNode->addChildNode( std::move( newGroup ) ) );
297 endInsertRows();
298 }
299 if ( !group )
300 {
301 break;
302 }
303
304 const QModelIndex parentGroupIndex = node2index( group );
305
306 beginInsertRows( parentGroupIndex, group->children().size(), group->children().size() );
307 QgsCoordinateReferenceSystemModelCrsNode *crsNode = addRecord( userRecord );
308 crsNode->setProj( details.proj );
309 crsNode->setWkt( details.wkt );
310 endInsertRows();
311 break;
312 }
313 }
314}
315
316void QgsCoordinateReferenceSystemModel::userCrsRemoved( long id )
317{
318 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u"USER"_s );
319 if ( group )
320 {
321 for ( int row = 0; row < group->children().size(); ++row )
322 {
323 if ( QgsCoordinateReferenceSystemModelCrsNode *crsNode = dynamic_cast<QgsCoordinateReferenceSystemModelCrsNode *>( group->children().at( row ) ) )
324 {
325 if ( crsNode->record().authId == QString::number( id ) )
326 {
327 const QModelIndex parentIndex = node2index( group );
328 beginRemoveRows( parentIndex, row, row );
329 delete group->takeChild( crsNode );
330 endRemoveRows();
331 return;
332 }
333 }
334 }
335 }
336}
337
338void QgsCoordinateReferenceSystemModel::userCrsChanged( const QString &id )
339{
340 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u"USER"_s );
341 if ( group )
342 {
343 for ( int row = 0; row < group->children().size(); ++row )
344 {
345 if ( QgsCoordinateReferenceSystemModelCrsNode *crsNode = dynamic_cast<QgsCoordinateReferenceSystemModelCrsNode *>( group->children().at( row ) ) )
346 {
347 if ( u"USER:%1"_s.arg( crsNode->record().authId ) == id )
348 {
349 // treat a change as a remove + add operation
350 const QModelIndex parentIndex = node2index( group );
351 beginRemoveRows( parentIndex, row, row );
352 delete group->takeChild( crsNode );
353 endRemoveRows();
354
355 userCrsAdded( id );
356 return;
357 }
358 }
359 }
360 }
361}
362
363QgsCoordinateReferenceSystemModelCrsNode *QgsCoordinateReferenceSystemModel::addRecord( const QgsCrsDbRecord &record )
364{
365 QgsCoordinateReferenceSystemModelGroupNode *parentNode = mRootNode.get();
366 auto crsNode = std::make_unique<QgsCoordinateReferenceSystemModelCrsNode>( record );
367
368 QString groupName;
369 QString groupId;
370 QIcon groupIcon;
371 if ( record.authName == "USER"_L1 )
372 {
373 groupName = tr( "User-defined" );
374 groupId = u"USER"_s;
375 groupIcon = QgsApplication::getThemeIcon( u"/user.svg"_s );
376 }
377 else if ( record.authName == "CUSTOM"_L1 )
378 {
379 // the group is guaranteed to exist at this point
380 groupId = u"CUSTOM"_s;
381 }
382 else
383 {
384 groupId = qgsEnumValueToKey( record.type );
385 switch ( record.type )
386 {
388 break;
390 groupName = tr( "Geodetic" );
391 groupIcon = QgsApplication::getThemeIcon( u"/mIconProjectionEnabled.svg"_s );
392 break;
394 groupName = tr( "Geocentric" );
395 groupIcon = QgsApplication::getThemeIcon( u"/mIconProjectionEnabled.svg"_s );
396 break;
398 groupName = tr( "Geographic (2D)" );
399 groupIcon = QgsApplication::getThemeIcon( u"/mIconProjectionEnabled.svg"_s );
400 break;
401
403 groupName = tr( "Geographic (3D)" );
404 groupIcon = QgsApplication::getThemeIcon( u"/mIconProjectionEnabled.svg"_s );
405 break;
406
408 groupName = tr( "Vertical" );
409 break;
410
413 groupName = tr( "Projected" );
414 groupIcon = QgsApplication::getThemeIcon( u"/transformed.svg"_s );
415 break;
416
418 groupName = tr( "Compound" );
419 break;
420
422 groupName = tr( "Temporal" );
423 break;
424
426 groupName = tr( "Engineering" );
427 break;
428
430 groupName = tr( "Bound" );
431 break;
432
434 groupName = tr( "Other" );
435 break;
436 }
437 }
438 crsNode->setGroup( groupName );
439
440 if ( QgsCoordinateReferenceSystemModelGroupNode *group = parentNode->getChildGroupNode( groupId ) )
441 {
442 parentNode = group;
443 }
444 else
445 {
446 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( groupName, groupIcon, groupId );
447 parentNode = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( parentNode->addChildNode( std::move( newGroup ) ) );
448 }
449
450 if ( ( record.authName != "USER"_L1 && record.authName != "CUSTOM"_L1 ) && ( record.type == Qgis::CrsType::Projected || record.type == Qgis::CrsType::DerivedProjected ) )
451 {
453 if ( projectionName.isEmpty() )
454 projectionName = tr( "Other" );
455 else
456 crsNode->setProjection( projectionName );
457
458 if ( QgsCoordinateReferenceSystemModelGroupNode *group = parentNode->getChildGroupNode( record.projectionAcronym ) )
459 {
460 parentNode = group;
461 }
462 else
463 {
464 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( projectionName, QIcon(), record.projectionAcronym );
465 parentNode = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( parentNode->addChildNode( std::move( newGroup ) ) );
466 }
467 }
468
469 return qgis::down_cast< QgsCoordinateReferenceSystemModelCrsNode * >( parentNode->addChildNode( std::move( crsNode ) ) );
470}
471
473{
474 QgsCrsDbRecord userRecord;
475 userRecord.authName = u"CUSTOM"_s;
476 userRecord.description = crs.description().isEmpty() ? tr( "Custom CRS" ) : crs.description();
477 userRecord.type = crs.type();
478
479 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u"CUSTOM"_s );
480 if ( !group )
481 {
482 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( tr( "Custom" ), QgsApplication::getThemeIcon( u"/user.svg"_s ), u"CUSTOM"_s );
483 beginInsertRows( QModelIndex(), mRootNode->children().length(), mRootNode->children().length() );
484 group = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( mRootNode->addChildNode( std::move( newGroup ) ) );
485 endInsertRows();
486 }
487
488 if ( !group )
489 return QModelIndex();
490
491 const QModelIndex parentGroupIndex = node2index( group );
492
493 const int newRow = group->children().size();
494 beginInsertRows( parentGroupIndex, newRow, newRow );
495 QgsCoordinateReferenceSystemModelCrsNode *node = addRecord( userRecord );
496 node->setWkt( crs.toWkt( Qgis::CrsWktVariant::Preferred ) );
497 node->setProj( crs.toProj() );
498 endInsertRows();
499
500 return index( newRow, 0, parentGroupIndex );
501}
502
503QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModel::index2node( const QModelIndex &index ) const
504{
505 if ( !index.isValid() )
506 return mRootNode.get();
507
508 return reinterpret_cast<QgsCoordinateReferenceSystemModelNode *>( index.internalPointer() );
509}
510
511QModelIndex QgsCoordinateReferenceSystemModel::node2index( QgsCoordinateReferenceSystemModelNode *node ) const
512{
513 if ( !node || !node->parent() )
514 return QModelIndex(); // this is the only root item -> invalid index
515
516 QModelIndex parentIndex = node2index( node->parent() );
517
518 int row = node->parent()->children().indexOf( node );
519 Q_ASSERT( row >= 0 );
520 return index( row, 0, parentIndex );
521}
522
523QModelIndex QgsCoordinateReferenceSystemModel::indexOfParentTreeNode( QgsCoordinateReferenceSystemModelNode *parentNode ) const
524{
525 Q_ASSERT( parentNode );
526
527 QgsCoordinateReferenceSystemModelNode *grandParentNode = parentNode->parent();
528 if ( !grandParentNode )
529 return QModelIndex(); // root node -> invalid index
530
531 int row = grandParentNode->children().indexOf( parentNode );
532 Q_ASSERT( row >= 0 );
533
534 return createIndex( row, 0, parentNode );
535}
536
538QgsCoordinateReferenceSystemModelNode::~QgsCoordinateReferenceSystemModelNode()
539{
540 qDeleteAll( mChildren );
541}
542
543QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModelNode::takeChild( QgsCoordinateReferenceSystemModelNode *node )
544{
545 return mChildren.takeAt( mChildren.indexOf( node ) );
546}
547
548QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModelNode::addChildNode( std::unique_ptr<QgsCoordinateReferenceSystemModelNode> node )
549{
550 if ( !node )
551 return nullptr;
552
553 Q_ASSERT( !node->mParent );
554 node->mParent = this;
555
556 mChildren.append( node.release() );
557 return mChildren.back();
558}
559
560void QgsCoordinateReferenceSystemModelNode::deleteChildren()
561{
562 qDeleteAll( mChildren );
563 mChildren.clear();
564}
565
566QgsCoordinateReferenceSystemModelGroupNode *QgsCoordinateReferenceSystemModelNode::getChildGroupNode( const QString &id )
567{
568 for ( QgsCoordinateReferenceSystemModelNode *node : std::as_const( mChildren ) )
569 {
570 if ( node->nodeType() == NodeGroup )
571 {
572 QgsCoordinateReferenceSystemModelGroupNode *groupNode = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( node );
573 if ( groupNode && groupNode->id() == id )
574 return groupNode;
575 }
576 }
577 return nullptr;
578}
579
580QgsCoordinateReferenceSystemModelGroupNode::QgsCoordinateReferenceSystemModelGroupNode( const QString &name, const QIcon &icon, const QString &id )
581 : mId( id )
582 , mName( name )
583 , mIcon( icon )
584{}
585
586QgsCoordinateReferenceSystemModelCrsNode::QgsCoordinateReferenceSystemModelCrsNode( const QgsCrsDbRecord &record )
587 : mRecord( record )
588{}
590
591
592//
593// QgsCoordinateReferenceSystemProxyModel
594//
595
597 : QSortFilterProxyModel( parent )
598 , mModel( new QgsCoordinateReferenceSystemModel( this ) )
599{
600 setSourceModel( mModel );
601 setDynamicSortFilter( true );
602 setSortLocaleAware( true );
603 setFilterCaseSensitivity( Qt::CaseInsensitive );
604 setRecursiveFilteringEnabled( true );
605 sort( 0 );
606}
607
612
617
619{
620 if ( mFilters == filters )
621 return;
622
623 mFilters = filters;
624 invalidateFilter();
625}
626
628{
629 mFilterString = filter;
630 invalidateFilter();
631}
632
634{
635 if ( mFilterAuthIds == filter )
636 return;
637
638 mFilterAuthIds.clear();
639 mFilterAuthIds.reserve( filter.size() );
640 for ( const QString &id : filter )
641 {
642 mFilterAuthIds.insert( id.toUpper() );
643 }
644 invalidateFilter();
645}
646
648{
649 if ( mFilterDeprecated == filter )
650 return;
651
652 mFilterDeprecated = filter;
653 invalidateFilter();
654}
655
656bool QgsCoordinateReferenceSystemProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
657{
658 if ( mFilterString.trimmed().isEmpty() && !mFilters && !mFilterDeprecated && mFilterAuthIds.isEmpty() )
659 return true;
660
661 const QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
662 const QgsCoordinateReferenceSystemModelNode::NodeType nodeType = static_cast<QgsCoordinateReferenceSystemModelNode::NodeType>(
663 sourceModel()->data( sourceIndex, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::NodeType ) ).toInt()
664 );
665 switch ( nodeType )
666 {
667 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
668 return false;
669 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
670 break;
671 }
672
673 const bool deprecated = sourceModel()->data( sourceIndex, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::Deprecated ) ).toBool();
674 if ( mFilterDeprecated && deprecated )
675 return false;
676
677 if ( mFilters )
678 {
679 const Qgis::CrsType type = sourceModel()->data( sourceIndex, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::Type ) ).value<Qgis::CrsType>();
680 switch ( type )
681 {
684 break;
685
695 if ( !mFilters.testFlag( Filter::FilterHorizontal ) )
696 return false;
697 break;
698
700 if ( !mFilters.testFlag( Filter::FilterVertical ) )
701 return false;
702 break;
703
705 if ( !mFilters.testFlag( Filter::FilterCompound ) )
706 return false;
707 break;
708 }
709 }
710
711 const QString authid = sourceModel()->data( sourceIndex, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::AuthId ) ).toString();
712 if ( !mFilterAuthIds.isEmpty() )
713 {
714 if ( !mFilterAuthIds.contains( authid.toUpper() ) )
715 return false;
716 }
717
718 if ( !mFilterString.trimmed().isEmpty() )
719 {
720 const QString name = sourceModel()->data( sourceIndex, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::Name ) ).toString();
721 QString candidate = name;
722 const QString groupName = sourceModel()->data( sourceIndex, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::Group ) ).toString();
723 if ( !groupName.isEmpty() )
724 candidate += ' ' + groupName;
725 const QString projectionName = sourceModel()->data( sourceIndex, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::Projection ) ).toString();
726 if ( !projectionName.isEmpty() )
727 candidate += ' ' + projectionName;
728
729 if ( !( QgsStringUtils::containsByWord( candidate, mFilterString ) || authid.contains( mFilterString, Qt::CaseInsensitive ) ) )
730 return false;
731 }
732 return true;
733}
734
735bool QgsCoordinateReferenceSystemProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
736{
737 QgsCoordinateReferenceSystemModelNode::NodeType leftType = static_cast<QgsCoordinateReferenceSystemModelNode::NodeType>(
738 sourceModel()->data( left, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::NodeType ) ).toInt()
739 );
740 QgsCoordinateReferenceSystemModelNode::NodeType rightType = static_cast<QgsCoordinateReferenceSystemModelNode::NodeType>(
741 sourceModel()->data( right, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::NodeType ) ).toInt()
742 );
743
744 if ( leftType != rightType )
745 {
746 if ( leftType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
747 return true;
748 else if ( rightType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
749 return false;
750 }
751
752 const QString leftStr = sourceModel()->data( left ).toString().toLower();
753 const QString rightStr = sourceModel()->data( right ).toString().toLower();
754
755 if ( leftType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
756 {
757 // both are groups -- ensure USER group comes last, and CUSTOM group comes first
758 const QString leftGroupId = sourceModel()->data( left, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::GroupId ) ).toString();
759 const QString rightGroupId = sourceModel()->data( right, static_cast<int>( QgsCoordinateReferenceSystemModel::CustomRole::GroupId ) ).toString();
760 if ( leftGroupId == "USER"_L1 )
761 return false;
762 if ( rightGroupId == "USER"_L1 )
763 return true;
764
765 if ( leftGroupId == "CUSTOM"_L1 )
766 return true;
767 if ( rightGroupId == "CUSTOM"_L1 )
768 return false;
769 }
770
771 // default sort is alphabetical order
772 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
773}
CrsType
Coordinate reference system types.
Definition qgis.h:2409
@ Vertical
Vertical CRS.
Definition qgis.h:2415
@ Temporal
Temporal CRS.
Definition qgis.h:2418
@ Compound
Compound (horizontal + vertical) CRS.
Definition qgis.h:2417
@ Projected
Projected CRS.
Definition qgis.h:2416
@ Other
Other type.
Definition qgis.h:2421
@ Bound
Bound CRS.
Definition qgis.h:2420
@ DerivedProjected
Derived projected CRS.
Definition qgis.h:2422
@ Unknown
Unknown type.
Definition qgis.h:2410
@ Engineering
Engineering CRS.
Definition qgis.h:2419
@ Geographic3d
3D geopraphic CRS
Definition qgis.h:2414
@ Geodetic
Geodetic CRS.
Definition qgis.h:2411
@ Geographic2d
2D geographic CRS
Definition qgis.h:2413
@ Geocentric
Geocentric CRS.
Definition qgis.h:2412
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2527
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsCoordinateReferenceSystemRegistry * coordinateReferenceSystemRegistry()
Returns the application's coordinate reference system (CRS) registry, which handles known CRS definit...
A tree model for display of known coordinate reference systems.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex addCustomCrs(const QgsCoordinateReferenceSystem &crs)
Adds a custom crs to the model.
QModelIndex parent(const QModelIndex &index) const override
QgsCoordinateReferenceSystemModel(QObject *parent=nullptr)
Constructor for QgsCoordinateReferenceSystemModel, with the specified parent object.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QModelIndex authIdToIndex(const QString &authId) const
Retrieves the model index corresponding to a CRS with the specified authId.
@ AuthId
The coordinate reference system authority name and id.
@ Wkt
The coordinate reference system's WKT representation. This is only used for non-standard CRS (i....
@ Proj
The coordinate reference system's PROJ representation. This is only used for non-standard CRS (i....
QVariant data(const QModelIndex &index, int role) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
int columnCount(const QModelIndex &=QModelIndex()) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QgsCoordinateReferenceSystemProxyModel(QObject *parent=nullptr)
Constructor for QgsCoordinateReferenceSystemProxyModel, with the given parent object.
void setFilterDeprecated(bool filter)
Sets whether deprecated CRS should be filtered from the results.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Filters filters() const
Returns any filters that affect how CRS are filtered.
@ FilterVertical
Include vertical CRS (excludes compound CRS containing a vertical component).
@ FilterHorizontal
Include horizontal CRS (excludes compound CRS containing a horizontal component).
QgsCoordinateReferenceSystemModel * coordinateReferenceSystemModel()
Returns the underlying source model.
void setFilterString(const QString &filter)
Sets a filter string, such that only coordinate reference systems matching the specified string will ...
void setFilterAuthIds(const QSet< QString > &filter)
Sets a filter list of CRS auth ID strings, such that only coordinate reference systems matching the s...
void setFilters(QgsCoordinateReferenceSystemProxyModel::Filters filters)
Set filters that affect how CRS are filtered.
QList< QgsCrsDbRecord > crsDbRecords() const
Returns the list of records from the QGIS srs db.
void userCrsAdded(const QString &id)
Emitted whenever a new user CRS definition is added.
void userCrsChanged(const QString &id)
Emitted whenever an existing user CRS definition is changed.
void userCrsRemoved(long id)
Emitted when the user CRS with matching id is removed from the database.
QList< QgsCoordinateReferenceSystemRegistry::UserCrsDetails > userCrsList() const
Returns a list containing the details of all registered custom (user-defined) CRSes.
static QString translateProjection(const QString &projection)
Returns a translated string for a projection method.
Represents a coordinate reference system (CRS).
QString toProj() const
Returns a Proj string representation of this CRS.
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Qgis::CrsType type() const
Returns the type of the CRS.
static bool containsByWord(const QString &candidate, const QString &words, Qt::CaseSensitivity sensitivity=Qt::CaseInsensitive)
Given a candidate string, returns true if the candidate contains all the individual words from anothe...
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7157
#define BUILTIN_UNREACHABLE
Definition qgis.h:7540