25#include "moc_qgscoordinatereferencesystemmodel.cpp"
28 : QAbstractItemModel(
parent )
29 , mRootNode( std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( QString(), QIcon(), QString() ) )
42 if ( !
index.isValid() )
44 return Qt::ItemFlags();
47 QgsCoordinateReferenceSystemModelNode *n = index2node(
index );
49 return Qt::ItemFlags();
51 switch ( n->nodeType() )
53 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
54 return index.column() == 0 ? Qt::ItemIsEnabled : Qt::ItemFlags();
55 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
56 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
63 if ( !
index.isValid() )
66 QgsCoordinateReferenceSystemModelNode *n = index2node(
index );
73 switch ( n->nodeType() )
75 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
77 QgsCoordinateReferenceSystemModelGroupNode *groupNode = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( n );
80 case Qt::DecorationRole:
81 switch (
index.column() )
84 return groupNode->icon();
92 switch (
index.column() )
95 return groupNode->name();
105 font.setItalic(
true );
106 if ( groupNode->parent() == mRootNode.get() )
108 font.setBold(
true );
114 return groupNode->id();
118 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
120 QgsCoordinateReferenceSystemModelCrsNode *crsNode = qgis::down_cast<QgsCoordinateReferenceSystemModelCrsNode *>( n );
123 case Qt::DisplayRole:
124 case Qt::ToolTipRole:
125 switch (
index.column() )
128 return crsNode->record().description;
132 if ( crsNode->record().authName == QLatin1String(
"CUSTOM" ) )
134 return QStringLiteral(
"%1:%2" ).arg( crsNode->record().authName, crsNode->record().authId );
143 return crsNode->record().description;
146 if ( !crsNode->record().authId.isEmpty() )
147 return QStringLiteral(
"%1:%2" ).arg( crsNode->record().authName, crsNode->record().authId );
152 return crsNode->record().deprecated;
155 return QVariant::fromValue( crsNode->record().type );
158 return crsNode->wkt();
161 return crsNode->proj();
164 return crsNode->group();
167 return crsNode->projection();
179 if ( orientation == Qt::Horizontal )
183 case Qt::DisplayRole:
187 return tr(
"Coordinate Reference System" );
189 return tr(
"Authority ID" );
204 QgsCoordinateReferenceSystemModelNode *n = index2node(
parent );
208 return n->children().count();
218 if ( !hasIndex( row, column,
parent ) )
219 return QModelIndex();
221 QgsCoordinateReferenceSystemModelNode *n = index2node(
parent );
223 return QModelIndex();
225 return createIndex( row, column, n->children().at( row ) );
230 if ( !child.isValid() )
231 return QModelIndex();
233 if ( QgsCoordinateReferenceSystemModelNode *n = index2node( child ) )
235 return indexOfParentTreeNode( n->parent() );
240 return QModelIndex();
246 const QModelIndex startIndex =
index( 0, 0 );
247 const QModelIndexList hits = match( startIndex,
static_cast<int>(
CustomRole::AuthId ), authid, 1, Qt::MatchRecursive );
248 return hits.value( 0 );
251void QgsCoordinateReferenceSystemModel::rebuild()
255 mRootNode->deleteChildren();
257 for (
const QgsCrsDbRecord &record : std::as_const( mCrsDbRecords ) )
263 for (
const QgsCoordinateReferenceSystemRegistry::UserCrsDetails &details : userCrsList )
265 QgsCrsDbRecord userRecord;
266 userRecord.
authName = QStringLiteral(
"USER" );
267 userRecord.
authId = QString::number( details.id );
270 addRecord( userRecord );
276void QgsCoordinateReferenceSystemModel::userCrsAdded(
const QString &
id )
279 for (
const QgsCoordinateReferenceSystemRegistry::UserCrsDetails &details : userCrsList )
281 if ( QStringLiteral(
"USER:%1" ).arg( details.id ) ==
id )
283 QgsCrsDbRecord userRecord;
284 userRecord.
authName = QStringLiteral(
"USER" );
285 userRecord.
authId = QString::number( details.id );
288 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( QStringLiteral(
"USER" ) );
291 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>(
292 tr(
"User-defined" ),
295 beginInsertRows( QModelIndex(), mRootNode->children().length(), mRootNode->children().length() );
296 group = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( mRootNode->addChildNode( std::move( newGroup ) ) );
304 const QModelIndex parentGroupIndex = node2index( group );
306 beginInsertRows( parentGroupIndex, group->children().size(), group->children().size() );
307 QgsCoordinateReferenceSystemModelCrsNode *crsNode = addRecord( userRecord );
308 crsNode->setProj( details.proj );
309 crsNode->setWkt( details.wkt );
316void QgsCoordinateReferenceSystemModel::userCrsRemoved(
long id )
318 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( QStringLiteral(
"USER" ) );
321 for (
int row = 0; row < group->children().size(); ++row )
323 if ( QgsCoordinateReferenceSystemModelCrsNode *crsNode =
dynamic_cast<QgsCoordinateReferenceSystemModelCrsNode *
>( group->children().at( row ) ) )
325 if ( crsNode->record().authId == QString::number(
id ) )
327 const QModelIndex parentIndex = node2index( group );
328 beginRemoveRows( parentIndex, row, row );
329 delete group->takeChild( crsNode );
338void QgsCoordinateReferenceSystemModel::userCrsChanged(
const QString &
id )
340 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( QStringLiteral(
"USER" ) );
343 for (
int row = 0; row < group->children().size(); ++row )
345 if ( QgsCoordinateReferenceSystemModelCrsNode *crsNode =
dynamic_cast<QgsCoordinateReferenceSystemModelCrsNode *
>( group->children().at( row ) ) )
347 if ( QStringLiteral(
"USER:%1" ).arg( crsNode->record().authId ) ==
id )
350 const QModelIndex parentIndex = node2index( group );
351 beginRemoveRows( parentIndex, row, row );
352 delete group->takeChild( crsNode );
363QgsCoordinateReferenceSystemModelCrsNode *QgsCoordinateReferenceSystemModel::addRecord(
const QgsCrsDbRecord &record )
365 QgsCoordinateReferenceSystemModelGroupNode *parentNode = mRootNode.get();
366 auto crsNode = std::make_unique<QgsCoordinateReferenceSystemModelCrsNode>( record );
371 if ( record.
authName == QLatin1String(
"USER" ) )
373 groupName = tr(
"User-defined" );
374 groupId = QStringLiteral(
"USER" );
377 else if ( record.
authName == QLatin1String(
"CUSTOM" ) )
380 groupId = QStringLiteral(
"CUSTOM" );
385 switch ( record.
type )
390 groupName = tr(
"Geodetic" );
394 groupName = tr(
"Geocentric" );
398 groupName = tr(
"Geographic (2D)" );
403 groupName = tr(
"Geographic (3D)" );
408 groupName = tr(
"Vertical" );
413 groupName = tr(
"Projected" );
418 groupName = tr(
"Compound" );
422 groupName = tr(
"Temporal" );
426 groupName = tr(
"Engineering" );
430 groupName = tr(
"Bound" );
434 groupName = tr(
"Other" );
438 crsNode->setGroup( groupName );
440 if ( QgsCoordinateReferenceSystemModelGroupNode *group = parentNode->getChildGroupNode( groupId ) )
446 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( groupName, groupIcon, groupId );
447 parentNode = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( parentNode->addChildNode( std::move( newGroup ) ) );
453 if ( projectionName.isEmpty() )
454 projectionName = tr(
"Other" );
456 crsNode->setProjection( projectionName );
458 if ( QgsCoordinateReferenceSystemModelGroupNode *group = parentNode->getChildGroupNode( record.
projectionAcronym ) )
464 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>( projectionName, QIcon(), record.
projectionAcronym );
465 parentNode = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( parentNode->addChildNode( std::move( newGroup ) ) );
469 return qgis::down_cast< QgsCoordinateReferenceSystemModelCrsNode * >( parentNode->addChildNode( std::move( crsNode ) ) );
475 userRecord.
authName = QStringLiteral(
"CUSTOM" );
479 QgsCoordinateReferenceSystemModelGroupNode *group = mRootNode->getChildGroupNode( QStringLiteral(
"CUSTOM" ) );
482 auto newGroup = std::make_unique<QgsCoordinateReferenceSystemModelGroupNode>(
486 beginInsertRows( QModelIndex(), mRootNode->children().length(), mRootNode->children().length() );
487 group = qgis::down_cast< QgsCoordinateReferenceSystemModelGroupNode * >( mRootNode->addChildNode( std::move( newGroup ) ) );
492 return QModelIndex();
494 const QModelIndex parentGroupIndex = node2index( group );
496 const int newRow = group->children().size();
497 beginInsertRows( parentGroupIndex, newRow, newRow );
498 QgsCoordinateReferenceSystemModelCrsNode *node = addRecord( userRecord );
500 node->setProj( crs.
toProj() );
503 return index( newRow, 0, parentGroupIndex );
506QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModel::index2node(
const QModelIndex &index )
const
508 if ( !
index.isValid() )
509 return mRootNode.get();
511 return reinterpret_cast<QgsCoordinateReferenceSystemModelNode *
>(
index.internalPointer() );
514QModelIndex QgsCoordinateReferenceSystemModel::node2index( QgsCoordinateReferenceSystemModelNode *node )
const
516 if ( !node || !node->parent() )
517 return QModelIndex();
519 QModelIndex parentIndex = node2index( node->parent() );
521 int row = node->parent()->children().indexOf( node );
522 Q_ASSERT( row >= 0 );
523 return index( row, 0, parentIndex );
526QModelIndex QgsCoordinateReferenceSystemModel::indexOfParentTreeNode( QgsCoordinateReferenceSystemModelNode *parentNode )
const
528 Q_ASSERT( parentNode );
530 QgsCoordinateReferenceSystemModelNode *grandParentNode = parentNode->parent();
531 if ( !grandParentNode )
532 return QModelIndex();
534 int row = grandParentNode->children().indexOf( parentNode );
535 Q_ASSERT( row >= 0 );
537 return createIndex( row, 0, parentNode );
541QgsCoordinateReferenceSystemModelNode::~QgsCoordinateReferenceSystemModelNode()
543 qDeleteAll( mChildren );
546QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModelNode::takeChild( QgsCoordinateReferenceSystemModelNode *node )
548 return mChildren.takeAt( mChildren.indexOf( node ) );
551QgsCoordinateReferenceSystemModelNode *QgsCoordinateReferenceSystemModelNode::addChildNode( std::unique_ptr<QgsCoordinateReferenceSystemModelNode> node )
556 Q_ASSERT( !node->mParent );
557 node->mParent =
this;
559 mChildren.append( node.release() );
560 return mChildren.back();
563void QgsCoordinateReferenceSystemModelNode::deleteChildren()
565 qDeleteAll( mChildren );
569QgsCoordinateReferenceSystemModelGroupNode *QgsCoordinateReferenceSystemModelNode::getChildGroupNode(
const QString &
id )
571 for ( QgsCoordinateReferenceSystemModelNode *node : std::as_const( mChildren ) )
573 if ( node->nodeType() == NodeGroup )
575 QgsCoordinateReferenceSystemModelGroupNode *groupNode = qgis::down_cast<QgsCoordinateReferenceSystemModelGroupNode *>( node );
576 if ( groupNode && groupNode->id() ==
id )
583QgsCoordinateReferenceSystemModelGroupNode::QgsCoordinateReferenceSystemModelGroupNode(
const QString &name,
const QIcon &icon,
const QString &
id )
590QgsCoordinateReferenceSystemModelCrsNode::QgsCoordinateReferenceSystemModelCrsNode(
const QgsCrsDbRecord &record )
602 : QSortFilterProxyModel( parent )
605 setSourceModel( mModel );
606 setDynamicSortFilter(
true );
607 setSortLocaleAware(
true );
608 setFilterCaseSensitivity( Qt::CaseInsensitive );
609 setRecursiveFilteringEnabled(
true );
634 mFilterString = filter;
640 if ( mFilterAuthIds == filter )
643 mFilterAuthIds.clear();
644 mFilterAuthIds.reserve( filter.size() );
645 for (
const QString &
id : filter )
647 mFilterAuthIds.insert(
id.toUpper() );
654 if ( mFilterDeprecated == filter )
657 mFilterDeprecated = filter;
663 if ( mFilterString.trimmed().isEmpty() && !mFilters && !mFilterDeprecated && mFilterAuthIds.isEmpty() )
666 const QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
670 case QgsCoordinateReferenceSystemModelNode::NodeGroup:
672 case QgsCoordinateReferenceSystemModelNode::NodeCrs:
677 if ( mFilterDeprecated && deprecated )
715 if ( !mFilterAuthIds.isEmpty() )
717 if ( !mFilterAuthIds.contains( authid.toUpper() ) )
721 if ( !mFilterString.trimmed().isEmpty() )
724 QString candidate = name;
726 if ( !groupName.isEmpty() )
727 candidate +=
' ' + groupName;
729 if ( !projectionName.isEmpty() )
730 candidate +=
' ' + projectionName;
733 || authid.contains( mFilterString, Qt::CaseInsensitive ) ) )
744 if ( leftType != rightType )
746 if ( leftType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
748 else if ( rightType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
752 const QString leftStr = sourceModel()->data( left ).toString().toLower();
753 const QString rightStr = sourceModel()->data( right ).toString().toLower();
755 if ( leftType == QgsCoordinateReferenceSystemModelNode::NodeGroup )
760 if ( leftGroupId == QLatin1String(
"USER" ) )
762 if ( rightGroupId == QLatin1String(
"USER" ) )
765 if ( leftGroupId == QLatin1String(
"CUSTOM" ) )
767 if ( rightGroupId == QLatin1String(
"CUSTOM" ) )
772 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