Skip to content

Commit

Permalink
Add buttons in the QgsProjectionTree to clear recent CRS
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ committed Apr 24, 2023
1 parent 08bf81d commit b44d991
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 54 deletions.
Expand Up @@ -155,6 +155,14 @@ Marks the current selected projection for push to front of recent projections li
Has no effect since QGIS 3.20
%End


void clearRecentCrs();
%Docstring
Clear the list of recent projections.

.. versionadded:: 3.32
%End

signals:

void crsSelected();
Expand Down
97 changes: 65 additions & 32 deletions src/gui/qgsprojectionselectiontreewidget.cpp
Expand Up @@ -51,6 +51,7 @@ QgsProjectionSelectionTreeWidget::QgsProjectionSelectionTreeWidget( QWidget *par
connect( lstRecent, &QTreeWidget::currentItemChanged, this, &QgsProjectionSelectionTreeWidget::lstRecent_currentItemChanged );
connect( cbxHideDeprecated, &QCheckBox::stateChanged, this, &QgsProjectionSelectionTreeWidget::updateFilter );
connect( leSearch, &QgsFilterLineEdit::textChanged, this, &QgsProjectionSelectionTreeWidget::updateFilter );
connect( mClearRecentCrs, &QToolButton::clicked, this, &QgsProjectionSelectionTreeWidget::clearRecentCrs );

mAreaCanvas->setVisible( mShowMap );

Expand All @@ -63,14 +64,16 @@ QgsProjectionSelectionTreeWidget::QgsProjectionSelectionTreeWidget( QWidget *par

// Hide (internal) ID column
lstCoordinateSystems->setColumnHidden( QgisCrsIdColumn, true );

lstRecent->header()->setSectionResizeMode( AuthidColumn, QHeaderView::Stretch );
lstRecent->header()->resizeSection( QgisCrsIdColumn, 0 );
lstRecent->header()->setSectionResizeMode( QgisCrsIdColumn, QHeaderView::Fixed );

// Hide (internal) ID column
lstRecent->setColumnHidden( QgisCrsIdColumn, true );

// Clear Crs Column
lstRecent->header()->setMinimumSectionSize( 10 );
lstRecent->header()->setStretchLastSection( false );
lstRecent->header()->resizeSection( ClearColumn, 20 );

mRecentProjections = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems();

mCheckBoxNoProjection->setHidden( true );
Expand Down Expand Up @@ -112,9 +115,10 @@ void QgsProjectionSelectionTreeWidget::resizeEvent( QResizeEvent *event )
lstCoordinateSystems->header()->resizeSection( AuthidColumn, 240 );
lstCoordinateSystems->header()->resizeSection( QgisCrsIdColumn, 0 );

lstRecent->header()->resizeSection( NameColumn, event->size().width() - 240 );
lstRecent->header()->resizeSection( NameColumn, event->size().width() - 260 );
lstRecent->header()->resizeSection( AuthidColumn, 240 );
lstRecent->header()->resizeSection( QgisCrsIdColumn, 0 );
lstRecent->header()->resizeSection( ClearColumn, 20 );
}

void QgsProjectionSelectionTreeWidget::showEvent( QShowEvent *event )
Expand Down Expand Up @@ -253,13 +257,25 @@ void QgsProjectionSelectionTreeWidget::insertRecent( const QgsCoordinateReferenc
if ( nodes.isEmpty() )
return;

lstRecent->insertTopLevelItem( 0, new QTreeWidgetItem( lstRecent, QStringList()
<< nodes.first()->text( NameColumn )
<< nodes.first()->text( AuthidColumn )
<< nodes.first()->text( QgisCrsIdColumn ) ) );
QTreeWidgetItem *item = new QTreeWidgetItem( lstRecent, QStringList()
<< nodes.first()->text( NameColumn )
<< nodes.first()->text( AuthidColumn )
<< nodes.first()->text( QgisCrsIdColumn ) );

// Insert clear button in the last column
QToolButton *clearButton = new QToolButton();
clearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClearItem.svg" ) );
clearButton->setAutoRaise( true );
clearButton->setToolTip( tr( "Clear from recently used coordinate reference systems" ) );
connect( clearButton, &QToolButton::clicked, this, [this, item] { removeRecentCrsItem( item ); } );
lstRecent->setItemWidget( item, ClearColumn, clearButton );


lstRecent->insertTopLevelItem( 0, item );

}

//note this line just returns the projection name!
// note this line just returns the projection name!
QString QgsProjectionSelectionTreeWidget::selectedName()
{
// return the selected wkt name from the list view
Expand Down Expand Up @@ -307,25 +323,19 @@ QgsRectangle QgsProjectionSelectionTreeWidget::previewRect() const
return mAreaCanvas->canvasRect();
}

QString QgsProjectionSelectionTreeWidget::getSelectedExpression( const QString &expression ) const
QString QgsProjectionSelectionTreeWidget::expressionForItem( QTreeWidgetItem *item, const QString &expression ) const
{
// Only return the attribute if there is a node in the tree
// selected that has an srs_id. This prevents error if the user
// selects a top-level node rather than an actual coordinate
// system
//
// Get the selected node and make sure it is a srs andx
// not a top-level projection node
QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
if ( !lvi || lvi->text( QgisCrsIdColumn ).isEmpty() )
// Only return the attribute if the selected item that has an srs_id.
// This prevents error if the user selects a top-level node rather
// than an actual coordinate system
if ( !item || item->text( QgisCrsIdColumn ).isEmpty() )
return QString();

//

// Determine if this is a user projection or a system on
// user projection defs all have srs_id >= 100000
//
QString databaseFileName;
if ( lvi->text( QgisCrsIdColumn ).toLong() >= USER_CRS_START_ID )
if ( item->text( QgisCrsIdColumn ).toLong() >= USER_CRS_START_ID )
{
databaseFileName = QgsApplication::qgisUserDatabaseFilePath();
if ( !QFileInfo::exists( databaseFileName ) )
Expand All @@ -338,7 +348,6 @@ QString QgsProjectionSelectionTreeWidget::getSelectedExpression( const QString &
databaseFileName = mSrsDatabaseFileName;
}

//
// set up the database
// XXX We could probably hold the database open for the life of this object,
// assuming that it will never be used anywhere else. Given the low overhead,
Expand All @@ -358,7 +367,7 @@ QString QgsProjectionSelectionTreeWidget::getSelectedExpression( const QString &
sqlite3_stmt *stmt = nullptr;
QString sql = QStringLiteral( "select %1 from tbl_srs where srs_id=%2" )
.arg( expression,
lvi->text( QgisCrsIdColumn ) );
item->text( QgisCrsIdColumn ) );

QgsDebugMsgLevel( QStringLiteral( "Finding selected attribute using : %1" ).arg( sql ), 4 );
rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
Expand All @@ -379,36 +388,42 @@ QString QgsProjectionSelectionTreeWidget::getSelectedExpression( const QString &
return attributeValue;
}

QgsCoordinateReferenceSystem QgsProjectionSelectionTreeWidget::crs() const


QgsCoordinateReferenceSystem QgsProjectionSelectionTreeWidget::crsForItem( QTreeWidgetItem *item ) const
{
if ( mCheckBoxNoProjection->isEnabled() && mCheckBoxNoProjection->isChecked() )
return QgsCoordinateReferenceSystem();

if ( !mInitialized && mDeferredLoadCrs.isValid() )
return mDeferredLoadCrs;

const QString srsIdString = getSelectedExpression( QStringLiteral( "srs_id" ) );
const QString srsIdString = expressionForItem( item, QStringLiteral( "srs_id" ) );
if ( !srsIdString.isEmpty() )
{
int srid = srsIdString.toLong();
if ( srid >= USER_CRS_START_ID )
return QgsCoordinateReferenceSystem::fromOgcWmsCrs( QStringLiteral( "USER:%1" ).arg( srid ) );
else
return QgsCoordinateReferenceSystem::fromOgcWmsCrs( getSelectedExpression( QStringLiteral( "upper(auth_name||':'||auth_id)" ) ) );
return QgsCoordinateReferenceSystem::fromOgcWmsCrs( expressionForItem( item, QStringLiteral( "upper(auth_name||':'||auth_id)" ) ) );
}
else
{
// custom CRS
QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
if ( lvi && lvi->data( 0, RoleWkt ).isValid() )
return QgsCoordinateReferenceSystem::fromWkt( lvi->data( 0, RoleWkt ).toString() );
else if ( lvi && lvi->data( 0, RoleProj ).isValid() )
return QgsCoordinateReferenceSystem::fromProj( lvi->data( 0, RoleProj ).toString() );
if ( item && item->data( 0, RoleWkt ).isValid() )
return QgsCoordinateReferenceSystem::fromWkt( item->data( 0, RoleWkt ).toString() );
else if ( item && item->data( 0, RoleProj ).isValid() )
return QgsCoordinateReferenceSystem::fromProj( item->data( 0, RoleProj ).toString() );
else
return QgsCoordinateReferenceSystem();
}
}

QgsCoordinateReferenceSystem QgsProjectionSelectionTreeWidget::crs() const
{
return crsForItem( lstCoordinateSystems->currentItem() );
}

void QgsProjectionSelectionTreeWidget::setShowNoProjection( bool show )
{
mCheckBoxNoProjection->setVisible( show );
Expand Down Expand Up @@ -1053,3 +1068,21 @@ void QgsProjectionSelectionTreeWidget::showDBMissingWarning( const QString &file
"Because of this the projection selector will not work…" )
.arg( fileName ) );
}

void QgsProjectionSelectionTreeWidget::clearRecentCrs()
{
QgsCoordinateReferenceSystem::clearRecentCoordinateReferenceSystems();
lstRecent->clear();
}


void QgsProjectionSelectionTreeWidget::removeRecentCrsItem( QTreeWidgetItem *item )
{
int index = lstRecent->indexOfTopLevelItem( item );
if ( index == -1 )
return;
QgsCoordinateReferenceSystem crs = crsForItem( item );
QgsCoordinateReferenceSystem::removeRecentCoordinateReferenceSystem( crs );
lstRecent->takeTopLevelItem( index );
delete item;
}
23 changes: 16 additions & 7 deletions src/gui/qgsprojectionselectiontreewidget.h
Expand Up @@ -145,6 +145,14 @@ class GUI_EXPORT QgsProjectionSelectionTreeWidget : public QWidget, private Ui::
*/
Q_DECL_DEPRECATED void pushProjectionToFront() SIP_DEPRECATED;


/**
* Clear the list of recent projections.
*
* \since QGIS 3.32
*/
void clearRecentCrs();

signals:

/**
Expand Down Expand Up @@ -236,12 +244,11 @@ class GUI_EXPORT QgsProjectionSelectionTreeWidget : public QWidget, private Ui::
*/
void applySelection( int column = QgsProjectionSelectionTreeWidget::None, QString value = QString() );

/**
* \brief gets an arbitrary sqlite3 expression from the selection
*
* \param e The sqlite3 expression (typically "srid" or "sridid")
*/
QString getSelectedExpression( const QString &e ) const;
//! gets an arbitrary sqlite3 expression from the given item
QString expressionForItem( QTreeWidgetItem *item, const QString &expression ) const;

//! Returns the CRS of the given item
QgsCoordinateReferenceSystem crsForItem( QTreeWidgetItem *item ) const;

QString selectedName();

Expand Down Expand Up @@ -290,7 +297,7 @@ class GUI_EXPORT QgsProjectionSelectionTreeWidget : public QWidget, private Ui::
//! Has the Recent Projection List been populated?
bool mRecentProjListDone = false;

enum Columns { NameColumn, AuthidColumn, QgisCrsIdColumn, None };
enum Columns { NameColumn, AuthidColumn, QgisCrsIdColumn, ClearColumn, None };
int mSearchColumn = QgsProjectionSelectionTreeWidget::None;
QString mSearchValue;

Expand Down Expand Up @@ -320,6 +327,8 @@ class GUI_EXPORT QgsProjectionSelectionTreeWidget : public QWidget, private Ui::
void lstCoordinateSystems_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem *prev );
void lstRecent_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem *prev );
void updateFilter();

void removeRecentCrsItem( QTreeWidgetItem *item );
};

#endif
71 changes: 56 additions & 15 deletions src/ui/qgsprojectionselectorbase.ui
Expand Up @@ -82,17 +82,51 @@
</layout>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Recently Used Coordinate Reference Systems</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Recently Used Coordinate Reference Systems</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="mClearRecentCrs">
<property name="toolTip">
<string>Clear all recently used coordinate reference systems</string>
</property>
<property name="text">
<string>Clear</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/console/iconClearConsole.svg</normaloff>:/images/themes/default/console/iconClearConsole.svg</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QSplitter" name="mSplitter">
Expand Down Expand Up @@ -125,7 +159,7 @@
<bool>true</bool>
</property>
<property name="columnCount">
<number>3</number>
<number>4</number>
</property>
<column>
<property name="text">
Expand All @@ -142,8 +176,13 @@
<string>ID</string>
</property>
</column>
<column>
<property name="text">
<string/>
</property>
</column>
</widget>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<layout class="QGridLayout" name="gridLayout">
<property name="horizontalSpacing">
<number>0</number>
Expand Down Expand Up @@ -223,7 +262,7 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="topMargin">
<number>0</number>
Expand Down Expand Up @@ -314,6 +353,8 @@
<tabstop>cbxHideDeprecated</tabstop>
<tabstop>lstCoordinateSystems</tabstop>
</tabstops>
<resources/>
<resources>
<include location="../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit b44d991

Please sign in to comment.