Skip to content

Commit

Permalink
CRS selection dialog can now be opened inlined
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 11, 2022
1 parent 6e7e8d3 commit adc7b6a
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 39 deletions.
6 changes: 0 additions & 6 deletions python/gui/auto_generated/qgsprojectionselectiondialog.sip.in
Expand Up @@ -23,12 +23,6 @@ A generic widget allowing users to pick a Coordinate Reference System (or define
%End
public:

enum class CrsType
{
Predefined,
Custom,
};

QgsCrsSelectionWidget( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsCrsSelectionWidget, with the specified ``parent`` widget.
Expand Down
18 changes: 18 additions & 0 deletions python/gui/auto_generated/qgsprojectionselectionwidget.sip.in
Expand Up @@ -125,6 +125,24 @@ ensemble datum, regardless of the ensemble's accuracy.
.. seealso:: :py:func:`setSourceEnsemble`

.. versionadded:: 3.20
%End

void setDialogTitle( const QString &title );
%Docstring
Sets the ``title`` for the CRS selector dialog window.

.. seealso:: :py:func:`dialogTitle`

.. versionadded:: 3.24
%End

QString dialogTitle() const;
%Docstring
Returns the title for the CRS selector dialog window.

.. seealso:: :py:func:`setDialogTitle`

.. versionadded:: 3.24
%End

signals:
Expand Down
1 change: 1 addition & 0 deletions src/gui/layout/qgslayoutmapgridwidget.cpp
Expand Up @@ -115,6 +115,7 @@ QgsLayoutMapGridWidget::QgsLayoutMapGridWidget( QgsLayoutItemMapGrid *mapGrid, Q

mMapGridCrsSelector->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, true );
mMapGridCrsSelector->setNotSetText( tr( "Use Map CRS" ) );
mMapGridCrsSelector->setDialogTitle( tr( "Grid CRS" ) );

connect( mMapGridCrsSelector, &QgsProjectionSelectionWidget::crsChanged, this, &QgsLayoutMapGridWidget::mapGridCrsChanged );

Expand Down
1 change: 1 addition & 0 deletions src/gui/layout/qgslayoutmapwidget.cpp
Expand Up @@ -137,6 +137,7 @@ QgsLayoutMapWidget::QgsLayoutMapWidget( QgsLayoutItemMap *item, QgsMapCanvas *ma

mCrsSelector->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, true );
mCrsSelector->setNotSetText( tr( "Use Project CRS" ) );
mCrsSelector->setDialogTitle( tr( "Map Item CRS" ) );

mOverviewFrameStyleButton->setSymbolType( Qgis::SymbolType::Fill );

Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsprojectionselectiondialog.cpp
Expand Up @@ -124,6 +124,7 @@ void QgsCrsSelectionWidget::setShowNoCrs( bool show )
if ( mShowNoCrsOption == show )
return;

mBlockSignals++;
mShowNoCrsOption = show;
if ( mShowNoCrsOption )
{
Expand All @@ -139,6 +140,11 @@ void QgsCrsSelectionWidget::setShowNoCrs( bool show )
mComboCrsType->setCurrentIndex( 0 );
}

mBlockSignals--;

if ( mDeferedInvalidCrsSet )
emit crsChanged();

mDeferedInvalidCrsSet = false;

emit hasValidSelectionChanged( hasValidSelection() );
Expand All @@ -165,6 +171,8 @@ bool QgsCrsSelectionWidget::hasValidSelection() const
{
if ( !mComboCrsType->currentData().isValid() )
return true;
else if ( mDeferedInvalidCrsSet )
return false;
else
{
switch ( static_cast< CrsType >( mComboCrsType->currentData().toInt() ) )
Expand Down Expand Up @@ -197,6 +205,7 @@ QgsCoordinateReferenceSystem QgsCrsSelectionWidget::crs() const

void QgsCrsSelectionWidget::setCrs( const QgsCoordinateReferenceSystem &crs )
{
mBlockSignals++;
if ( !crs.isValid() )
{
if ( mShowNoCrsOption )
Expand All @@ -219,6 +228,7 @@ void QgsCrsSelectionWidget::setCrs( const QgsCoordinateReferenceSystem &crs )
mStackedWidget->setCurrentWidget( mPageDatabase );
}
}
mBlockSignals--;

emit crsChanged();
emit hasValidSelectionChanged( hasValidSelection() );
Expand Down
18 changes: 9 additions & 9 deletions src/gui/qgsprojectionselectiondialog.h
Expand Up @@ -41,15 +41,6 @@ class GUI_EXPORT QgsCrsSelectionWidget : public QgsPanelWidget, private Ui::QgsG
Q_OBJECT
public:

/**
* CRS types
*/
enum class CrsType
{
Predefined, //!< Predefined (from database )
Custom, //!< Custom CRS
};

/**
* Constructor for QgsCrsSelectionWidget, with the specified \a parent widget.
*/
Expand Down Expand Up @@ -137,6 +128,15 @@ class GUI_EXPORT QgsCrsSelectionWidget : public QgsPanelWidget, private Ui::QgsG

private:

/**
* CRS types
*/
enum class CrsType
{
Predefined, //!< Predefined (from database )
Custom, //!< Custom CRS
};

QString mNotSetText;
bool mShowNoCrsOption = false;
bool mDeferedInvalidCrsSet = false;
Expand Down
102 changes: 81 additions & 21 deletions src/gui/qgsprojectionselectionwidget.cpp
Expand Up @@ -26,7 +26,9 @@

QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent )
: QWidget( parent )
, mDialogTitle( tr( "Select CRS" ) )
{

mCrsComboBox = new QgsHighlightableComboBox( this );
mCrsComboBox->addItem( tr( "invalid projection" ), QgsProjectionSelectionWidget::CurrentCrs );
mCrsComboBox->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
Expand Down Expand Up @@ -197,33 +199,75 @@ bool QgsProjectionSelectionWidget::optionVisible( QgsProjectionSelectionWidget::

void QgsProjectionSelectionWidget::selectCrs()
{
//find out crs id of current proj4 string
QgsProjectionSelectionDialog dlg( this );
if ( !mMessage.isEmpty() )
dlg.setMessage( mMessage );
dlg.setCrs( mCrs );
QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
if ( panel && panel->dockMode() )
{
mActivePanel = new QgsCrsSelectionWidget( this );
if ( !mMessage.isEmpty() )
mActivePanel->setMessage( mMessage );
mActivePanel->setCrs( mCrs );

if ( !mNotSetText.isEmpty() )
dlg.setNotSetText( mNotSetText );
if ( !mNotSetText.isEmpty() )
mActivePanel->setNotSetText( mNotSetText );

if ( optionVisible( QgsProjectionSelectionWidget::CrsOption::CrsNotSet ) )
{
dlg.setShowNoProjection( true );
}
dlg.setRequireValidSelection();
mActivePanel->setPanelTitle( mDialogTitle );

if ( dlg.exec() )
{
mCrsComboBox->blockSignals( true );
mCrsComboBox->setCurrentIndex( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ) );
mCrsComboBox->blockSignals( false );
const QgsCoordinateReferenceSystem crs = dlg.crs();
setCrs( crs );
emit crsChanged( crs );
if ( optionVisible( QgsProjectionSelectionWidget::CrsOption::CrsNotSet ) )
{
mActivePanel->setShowNoCrs( true );
}

connect( mActivePanel, &QgsCrsSelectionWidget::crsChanged, this, [ this ]
{
if ( mIgnorePanelSignals )
return;

if ( !mActivePanel->hasValidSelection() )
return;

mCrsComboBox->blockSignals( true );
mCrsComboBox->setCurrentIndex( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ) );
mCrsComboBox->blockSignals( false );
const QgsCoordinateReferenceSystem crs = mActivePanel->crs();

mIgnorePanelSignals++;
setCrs( crs );
mIgnorePanelSignals--;

emit crsChanged( crs );
} );
panel->openPanel( mActivePanel );
}
else
{
QApplication::restoreOverrideCursor();
QgsProjectionSelectionDialog dlg( this );
if ( !mMessage.isEmpty() )
dlg.setMessage( mMessage );
dlg.setCrs( mCrs );
dlg.setWindowTitle( mDialogTitle );

if ( !mNotSetText.isEmpty() )
dlg.setNotSetText( mNotSetText );

if ( optionVisible( QgsProjectionSelectionWidget::CrsOption::CrsNotSet ) )
{
dlg.setShowNoProjection( true );
}
dlg.setRequireValidSelection();

if ( dlg.exec() )
{
mCrsComboBox->blockSignals( true );
mCrsComboBox->setCurrentIndex( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ) );
mCrsComboBox->blockSignals( false );
const QgsCoordinateReferenceSystem crs = dlg.crs();
setCrs( crs );
emit crsChanged( crs );
}
else
{
QApplication::restoreOverrideCursor();
}
}
}

Expand Down Expand Up @@ -294,6 +338,16 @@ QString QgsProjectionSelectionWidget::sourceEnsemble() const
return mSourceEnsemble;
}

void QgsProjectionSelectionWidget::setDialogTitle( const QString &title )
{
mDialogTitle = title;
}

QString QgsProjectionSelectionWidget::dialogTitle() const
{
return mDialogTitle;
}

void QgsProjectionSelectionWidget::setSourceEnsemble( const QString &ensemble )
{
if ( mSourceEnsemble == ensemble )
Expand Down Expand Up @@ -448,6 +502,12 @@ void QgsProjectionSelectionWidget::setCrs( const QgsCoordinateReferenceSystem &c
crsOptionText( crs ) );
}
}
if ( mActivePanel && !mIgnorePanelSignals )
{
mIgnorePanelSignals++;
mActivePanel->setCrs( crs );
mIgnorePanelSignals--;
}
if ( mCrs != crs )
{
mCrs = crs;
Expand Down
23 changes: 21 additions & 2 deletions src/gui/qgsprojectionselectionwidget.h
Expand Up @@ -22,12 +22,13 @@
#include <QLineEdit>
#include <QToolButton>
#include <QComboBox>
#include <QPointer>

#include "qgscoordinatereferencesystem.h"
#include "qgis_gui.h"

class QgsProjectionSelectionDialog;
class QgsHighlightableComboBox;
class QgsCrsSelectionWidget;
class QLabel;

/**
Expand Down Expand Up @@ -141,6 +142,20 @@ class GUI_EXPORT QgsProjectionSelectionWidget : public QWidget
*/
QString sourceEnsemble() const;

/**
* Sets the \a title for the CRS selector dialog window.
* \see dialogTitle()
* \since QGIS 3.24
*/
void setDialogTitle( const QString &title );

/**
* Returns the title for the CRS selector dialog window.
* \see setDialogTitle()
* \since QGIS 3.24
*/
QString dialogTitle() const;

signals:

/**
Expand Down Expand Up @@ -188,7 +203,6 @@ class GUI_EXPORT QgsProjectionSelectionWidget : public QWidget
QgsCoordinateReferenceSystem mDefaultCrs;
QgsHighlightableComboBox *mCrsComboBox = nullptr;
QToolButton *mButton = nullptr;
QgsProjectionSelectionDialog *mDialog = nullptr;
QString mNotSetText;
QString mMessage;

Expand All @@ -198,6 +212,11 @@ class GUI_EXPORT QgsProjectionSelectionWidget : public QWidget
QWidget *mWarningLabelContainer = nullptr;
QLabel *mWarningLabel = nullptr;

QPointer< QgsCrsSelectionWidget > mActivePanel;
int mIgnorePanelSignals = 0;

QString mDialogTitle;

void addNotSetOption();
void addProjectCrsOption();
void addDefaultCrsOption();
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -62,6 +62,7 @@ ADD_PYTHON_TEST(PyQgsConditionalStyle test_qgsconditionalstyle.py)
ADD_PYTHON_TEST(PyQgsConnectionRegistry test_qgsconnectionregistry.py)
ADD_PYTHON_TEST(PyQgsCoordinateTransformContext test_qgscoordinatetransformcontext.py)
ADD_PYTHON_TEST(PyQgsCrsDefinitionWidget test_qgscrsdefinitionwidget.py)
ADD_PYTHON_TEST(PyQgsCrsSelectionWidget test_qgscrsselectionwidget.py)
ADD_PYTHON_TEST(PyQgsDefaultValue test_qgsdefaultvalue.py)
ADD_PYTHON_TEST(PyQgsXmlUtils test_qgsxmlutils.py)
ADD_PYTHON_TEST(PyQgsCore test_qgscore.py)
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgscrsdefinitionwidget.py
Expand Up @@ -19,7 +19,7 @@
start_app()


class TestQgsCrsDefinitionWidge(unittest.TestCase):
class TestQgsCrsDefinitionWidget(unittest.TestCase):

def testWidget(self):
"""
Expand Down

0 comments on commit adc7b6a

Please sign in to comment.