26#include "moc_qgscoordinatereferencesystemmodel.cpp"
28using namespace Qt::StringLiterals;
31 : QAbstractItemModel(
parent )
32 , mRootNode( std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( QString(), QIcon(), QString() ) )
45 if ( !
index.isValid() )
47 return Qt::ItemFlags();
50 QgsCoordinateReferenceSystemModelNode *n = index2node(
index );
52 return Qt::ItemFlags();
54 switch ( n->nodeType() )
56 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
57 return index.column() == 0 ? Qt::ItemIsEnabled : Qt::ItemFlags();
58 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
59 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
66 if ( !
index.isValid() )
69 QgsCoordinateReferenceSystemModelNode *n = index2node(
index );
76 switch ( n->nodeType() )
78 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
80 QgsCoordinateReferenceSystemModelGroupNode *groupNode = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( n );
83 case Qt::DecorationRole:
84 switch (
index.column() )
87 return groupNode->icon();
95 switch (
index.column() )
98 return groupNode->name();
108 font.setItalic(
true );
109 if ( groupNode->parent() == mRootNode.get() )
111 font.setBold(
true );
117 return groupNode->id();
121 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
123 QgsCoordinateReferenceSystemModelCrsNode *crsNode = qgis::down_cast<QgsCoordinateReferenceSystemModelCrsNode *>( n );
126 case Qt::DisplayRole:
127 case Qt::ToolTipRole:
128 switch (
index.column() )
131 return crsNode->record().description;
135 if ( crsNode->record().authName ==
"CUSTOM"_L1 )
137 return u
"%1:%2"_s.arg( crsNode->record().authName, crsNode->record().authId );
146 return crsNode->record().description;
149 if ( !crsNode->record().authId.isEmpty() )
150 return u
"%1:%2"_s.arg( crsNode->record().authName, crsNode->record().authId );
155 return crsNode->record().deprecated;
158 return QVariant::fromValue( crsNode->record().type );
161 return crsNode->wkt();
164 return crsNode->proj();
167 return crsNode->group();
170 return crsNode->projection();
182 if ( orientation == Qt::Horizontal )
186 case Qt::DisplayRole:
190 return tr(
"Coordinate Reference System" );
192 return tr(
"Authority ID" );
207 QgsCoordinateReferenceSystemModelNode *n = index2node(
parent );
211 return n->children().count();
221 if ( !hasIndex( row, column,
parent ) )
222 return QModelIndex();
224 QgsCoordinateReferenceSystemModelNode *n = index2node(
parent );
226 return QModelIndex();
228 return createIndex( row, column, n->children().at( row ) );
233 if ( !child.isValid() )
234 return QModelIndex();
236 if ( QgsCoordinateReferenceSystemModelNode *n = index2node( child ) )
238 return indexOfParentTreeNode( n->parent() );
243 return QModelIndex();
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 );
254void QgsCoordinateReferenceSystemModel::rebuild()
258 mRootNode->deleteChildren();
260 for (
const QgsCrsDbRecord &record : std::as_const( mCrsDbRecords ) )
266 for (
const QgsCoordinateReferenceSystemRegistry::UserCrsDetails &details : userCrsList )
268 QgsCrsDbRecord userRecord;
270 userRecord.
authId = QString::number( details.id );
273 addRecord( userRecord );
279void QgsCoordinateReferenceSystemModel::userCrsAdded(
const QString &
id )
282 for (
const QgsCoordinateReferenceSystemRegistry::UserCrsDetails &details : userCrsList )
284 if ( u
"USER:%1"_s.arg( details.id ) ==
id )
286 QgsCrsDbRecord userRecord;
288 userRecord.
authId = QString::number( details.id );
291 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u
"USER"_s );
294 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>(
295 tr(
"User-defined" ),
298 beginInsertRows( QModelIndex(), mRootNode->children().length(), mRootNode->children().length() );
299 group = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( mRootNode->addChildNode( std::move( newGroup ) ) );
307 const QModelIndex parentGroupIndex = node2index( group );
309 beginInsertRows( parentGroupIndex, group->children().size(), group->children().size() );
310 QgsCoordinateReferenceSystemModelCrsNode *crsNode = addRecord( userRecord );
311 crsNode->setProj( details.proj );
312 crsNode->setWkt( details.wkt );
319void QgsCoordinateReferenceSystemModel::userCrsRemoved(
long id )
321 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u
"USER"_s );
324 for (
int row = 0; row < group->children().size(); ++row )
326 if ( QgsCoordinateReferenceSystemModelCrsNode *crsNode =
dynamic_cast<QgsCoordinateReferenceSystemModelCrsNode *
>( group->children().at( row ) ) )
328 if ( crsNode->record().authId == QString::number(
id ) )
330 const QModelIndex parentIndex = node2index( group );
331 beginRemoveRows( parentIndex, row, row );
332 delete group->takeChild( crsNode );
341void QgsCoordinateReferenceSystemModel::userCrsChanged(
const QString &
id )
343 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u
"USER"_s );
346 for (
int row = 0; row < group->children().size(); ++row )
348 if ( QgsCoordinateReferenceSystemModelCrsNode *crsNode =
dynamic_cast<QgsCoordinateReferenceSystemModelCrsNode *
>( group->children().at( row ) ) )
350 if ( u
"USER:%1"_s.arg( crsNode->record().authId ) ==
id )
353 const QModelIndex parentIndex = node2index( group );
354 beginRemoveRows( parentIndex, row, row );
355 delete group->takeChild( crsNode );
366QgsCoordinateReferenceSystemModelCrsNode *QgsCoordinateReferenceSystemModel::addRecord(
const QgsCrsDbRecord &record )
368 QgsCoordinateReferenceSystemModelGroupNode *parentNode = mRootNode.get();
369 auto crsNode = std::make_unique<QgsCoordinateReferenceSystemModelCrsNode>( record );
376 groupName = tr(
"User-defined" );
380 else if ( record.
authName ==
"CUSTOM"_L1 )
383 groupId = u
"CUSTOM"_s;
388 switch ( record.
type )
393 groupName = tr(
"Geodetic" );
397 groupName = tr(
"Geocentric" );
401 groupName = tr(
"Geographic (2D)" );
406 groupName = tr(
"Geographic (3D)" );
411 groupName = tr(
"Vertical" );
416 groupName = tr(
"Projected" );
421 groupName = tr(
"Compound" );
425 groupName = tr(
"Temporal" );
429 groupName = tr(
"Engineering" );
433 groupName = tr(
"Bound" );
437 groupName = tr(
"Other" );
441 crsNode->setGroup( groupName );
443 if ( QgsCoordinateReferenceSystemModelGroupNode *group = parentNode->getChildGroupNode( groupId ) )
449 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( groupName, groupIcon, groupId );
450 parentNode = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( parentNode->addChildNode( std::move( newGroup ) ) );
456 if ( projectionName.isEmpty() )
457 projectionName = tr(
"Other" );
459 crsNode->setProjection( projectionName );
461 if ( QgsCoordinateReferenceSystemModelGroupNode *group = parentNode->getChildGroupNode( record.
projectionAcronym ) )
467 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( projectionName, QIcon(), record.
projectionAcronym );
468 parentNode = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( parentNode->addChildNode( std::move( newGroup ) ) );
472 return qgis::down_cast< QgsCoordinateReferenceSystemModelCrsNode * >( parentNode->addChildNode( std::move( crsNode ) ) );
482 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( u
"CUSTOM"_s );
485 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>(
489 beginInsertRows( QModelIndex(), mRootNode->children().length(), mRootNode->children().length() );
490 group = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( mRootNode->addChildNode( std::move( newGroup ) ) );
495 return QModelIndex();
497 const QModelIndex parentGroupIndex = node2index( group );
499 const int newRow = group->children().size();
500 beginInsertRows( parentGroupIndex, newRow, newRow );
501 QgsCoordinateReferenceSystemModelCrsNode *node = addRecord( userRecord );
503 node->setProj( crs.
toProj() );
506 return index( newRow, 0, parentGroupIndex );
509QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModel::index2node(
const QModelIndex &index )
const
511 if ( !
index.isValid() )
512 return mRootNode.get();
514 return reinterpret_cast<QgsCoordinateReferenceSystemModelNode *
>(
index.internalPointer() );
517QModelIndex QgsCoordinateReferenceSystemModel::node2index( QgsCoordinateReferenceSystemModelNode *node )
const
519 if ( !node || !node->parent() )
520 return QModelIndex();
522 QModelIndex parentIndex = node2index( node->parent() );
524 int row = node->parent()->children().indexOf( node );
525 Q_ASSERT( row >= 0 );
526 return index( row, 0, parentIndex );
529QModelIndex QgsCoordinateReferenceSystemModel::indexOfParentTreeNode( QgsCoordinateReferenceSystemModelNode *parentNode )
const
531 Q_ASSERT( parentNode );
533 QgsCoordinateReferenceSystemModelNode *grandParentNode = parentNode->parent();
534 if ( !grandParentNode )
535 return QModelIndex();
537 int row = grandParentNode->children().indexOf( parentNode );
538 Q_ASSERT( row >= 0 );
540 return createIndex( row, 0, parentNode );
544QgsCoordinateReferenceSystemModelNode::~QgsCoordinateReferenceSystemModelNode()
546 qDeleteAll( mChildren );
549QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModelNode::takeChild( QgsCoordinateReferenceSystemModelNode *node )
551 return mChildren.takeAt( mChildren.indexOf( node ) );
554QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModelNode::addChildNode( std::unique_ptr<QgsCoordinateReferenceSystemModelNode> node )
559 Q_ASSERT( !node->mParent );
560 node->mParent =
this;
562 mChildren.append( node.release() );
563 return mChildren.back();
566void QgsCoordinateReferenceSystemModelNode::deleteChildren()
568 qDeleteAll( mChildren );
572QgsCoordinateReferenceSystemModelGroupNode *QgsCoordinateReferenceSystemModelNode::getChildGroupNode(
const QString &
id )
574 for ( QgsCoordinateReferenceSystemModelNode *node : std::as_const( mChildren ) )
576 if ( node->nodeType() == NodeGroup )
578 QgsCoordinateReferenceSystemModelGroupNode *groupNode = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( node );
579 if ( groupNode && groupNode->id() ==
id )
586QgsCoordinateReferenceSystemModelGroupNode::QgsCoordinateReferenceSystemModelGroupNode(
const QString &name,
const QIcon &icon,
const QString &
id )
593QgsCoordinateReferenceSystemModelCrsNode::QgsCoordinateReferenceSystemModelCrsNode(
const QgsCrsDbRecord &record )
605 : QSortFilterProxyModel( parent )
608 setSourceModel( mModel );
609 setDynamicSortFilter(
true );
610 setSortLocaleAware(
true );
611 setFilterCaseSensitivity( Qt::CaseInsensitive );
612 setRecursiveFilteringEnabled(
true );
637 mFilterString = filter;
643 if ( mFilterAuthIds == filter )
646 mFilterAuthIds.clear();
647 mFilterAuthIds.reserve( filter.size() );
648 for (
const QString &
id : filter )
650 mFilterAuthIds.insert(
id.toUpper() );
657 if ( mFilterDeprecated == filter )
660 mFilterDeprecated = filter;
666 if ( mFilterString.trimmed().isEmpty() && !mFilters && !mFilterDeprecated && mFilterAuthIds.isEmpty() )
669 const QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
673 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
675 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
680 if ( mFilterDeprecated && deprecated )
718 if ( !mFilterAuthIds.isEmpty() )
720 if ( !mFilterAuthIds.contains( authid.toUpper() ) )
724 if ( !mFilterString.trimmed().isEmpty() )
727 QString candidate = name;
729 if ( !groupName.isEmpty() )
730 candidate +=
' ' + groupName;
732 if ( !projectionName.isEmpty() )
733 candidate +=
' ' + projectionName;
736 || authid.contains( mFilterString, Qt::CaseInsensitive ) ) )
747 if ( leftType != rightType )
749 if ( leftType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
751 else if ( rightType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
755 const QString leftStr = sourceModel()->data( left ).toString().toLower();
756 const QString rightStr = sourceModel()->data( right ).toString().toLower();
758 if ( leftType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
763 if ( leftGroupId ==
"USER"_L1 )
765 if ( rightGroupId ==
"USER"_L1 )
768 if ( leftGroupId ==
"CUSTOM"_L1 )
770 if ( rightGroupId ==
"CUSTOM"_L1 )
775 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
CrsType
Coordinate reference system types.
@ Compound
Compound (horizontal + vertical) CRS.
@ Projected
Projected CRS.
@ DerivedProjected
Derived projected CRS.
@ Engineering
Engineering CRS.
@ Geographic3d
3D geopraphic CRS
@ Geographic2d
2D geographic CRS
@ Geocentric
Geocentric CRS.
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
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.
@ Deprecated
true if the CRS is deprecated
@ AuthId
The coordinate reference system authority name and id.
@ Projection
Projection name.
@ GroupId
The node ID (for group nodes).
@ Name
The coordinate reference system name.
@ NodeType
Corresponds to the node's type.
@ Wkt
The coordinate reference system's WKT representation. This is only used for non-standard CRS (i....
@ Type
The coordinate reference system type.
@ 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).
@ FilterCompound
Include compound CRS.
@ 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.
#define BUILTIN_UNREACHABLE
QString projectionAcronym