Skip to content

Commit

Permalink
Fix loss of crs set for QgsProjectionSelectionWidget if widget is never
Browse files Browse the repository at this point in the history
shown, fix missing signals and duplicate signals

(cherry picked from commit 5375fdf)
  • Loading branch information
nyalldawson committed Dec 20, 2019
1 parent 771474e commit 1a88667
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/gui/qgsprojectionselectiontreewidget.cpp
Expand Up @@ -75,7 +75,11 @@ QgsProjectionSelectionTreeWidget::QgsProjectionSelectionTreeWidget( QWidget *par

mCheckBoxNoProjection->setHidden( true );
mCheckBoxNoProjection->setEnabled( false );
connect( mCheckBoxNoProjection, &QCheckBox::toggled, this, &QgsProjectionSelectionTreeWidget::crsSelected );
connect( mCheckBoxNoProjection, &QCheckBox::toggled, this, [ = ]
{
if ( !mBlockSignals )
emit crsSelected();
} );
connect( mCheckBoxNoProjection, &QCheckBox::toggled, this, [ = ]( bool checked )
{
if ( mCheckBoxNoProjection->isEnabled() )
Expand Down Expand Up @@ -159,7 +163,9 @@ void QgsProjectionSelectionTreeWidget::showEvent( QShowEvent *event )
}

// apply deferred selection
mBlockSignals = true; // we've already emitted the signal, when the deferred crs was first set
applySelection();
mBlockSignals = false;

emit initialized();

Expand Down Expand Up @@ -297,8 +303,20 @@ void QgsProjectionSelectionTreeWidget::setCrs( const QgsCoordinateReferenceSyste
}
else
{
bool changed = false;
if ( !mInitialized )
{
changed = mDeferredLoadCrs != crs;
mDeferredLoadCrs = crs;
}
mBlockSignals = true;
mCheckBoxNoProjection->setChecked( false );
mBlockSignals = false;
applySelection( AuthidColumn, crs.authid() );
if ( changed )
{
emit crsSelected();
}
}
}

Expand Down Expand Up @@ -418,6 +436,9 @@ QgsCoordinateReferenceSystem QgsProjectionSelectionTreeWidget::crs() const
if ( mCheckBoxNoProjection->isEnabled() && mCheckBoxNoProjection->isChecked() )
return QgsCoordinateReferenceSystem();

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

int srid = getSelectedExpression( QStringLiteral( "srs_id" ) ).toLong();
if ( srid >= USER_CRS_START_ID )
return QgsCoordinateReferenceSystem::fromOgcWmsCrs( QStringLiteral( "USER:%1" ).arg( srid ) );
Expand Down Expand Up @@ -456,6 +477,8 @@ bool QgsProjectionSelectionTreeWidget::hasValidSelection() const
QTreeWidgetItem *item = lstCoordinateSystems->currentItem();
if ( mCheckBoxNoProjection->isChecked() )
return true;
else if ( !mInitialized && mDeferredLoadCrs.isValid() )
return true;
else
return item && !item->text( QgisCrsIdColumn ).isEmpty();
}
Expand Down Expand Up @@ -718,7 +741,8 @@ void QgsProjectionSelectionTreeWidget::lstCoordinateSystems_currentItemChanged(
if ( current->childCount() == 0 )
{
// Found a real CRS
emit crsSelected();
if ( !mBlockSignals )
emit crsSelected();

updateBoundsPreview();

Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsprojectionselectiontreewidget.h
Expand Up @@ -290,6 +290,8 @@ class GUI_EXPORT QgsProjectionSelectionTreeWidget : public QWidget, private Ui::
bool mShowMap = true;

bool mInitialized = false;
QgsCoordinateReferenceSystem mDeferredLoadCrs;
bool mBlockSignals = false;

private slots:
//! Gets list of authorities
Expand Down
65 changes: 65 additions & 0 deletions tests/src/python/test_qgsprojectionselectionwidgets.py
Expand Up @@ -169,6 +169,71 @@ def testDialogNotSetOption(self):
w.setCrs(QgsCoordinateReferenceSystem())
self.assertFalse(w.crs().isValid())

def testTreeWidgetDeferredLoad(self):
"""
Test that crs setting made before widget is initialized is respected
"""
w = QgsProjectionSelectionTreeWidget()
spy = QSignalSpy(w.crsSelected)
self.assertFalse(w.hasValidSelection())
w.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
self.assertEqual(len(spy), 1)
self.assertTrue(w.hasValidSelection())
self.assertEqual(w.crs().authid(), 'EPSG:3111')
self.assertEqual(len(spy), 1)

w = QgsProjectionSelectionTreeWidget()
spy = QSignalSpy(w.crsSelected)
self.assertFalse(w.hasValidSelection())
w.setCrs(QgsCoordinateReferenceSystem())
self.assertEqual(len(spy), 1)
self.assertTrue(w.hasValidSelection())
self.assertFalse(w.crs().isValid())
self.assertEqual(len(spy), 1)
w.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
self.assertEqual(len(spy), 2)

# expect same behavior if we show
w = QgsProjectionSelectionTreeWidget()
spy = QSignalSpy(w.crsSelected)
self.assertFalse(w.hasValidSelection())
w.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
self.assertEqual(len(spy), 1)
w.show()
self.assertTrue(w.hasValidSelection())
self.assertEqual(w.crs().authid(), 'EPSG:3111')
self.assertEqual(len(spy), 1)

w = QgsProjectionSelectionTreeWidget()
spy = QSignalSpy(w.crsSelected)
self.assertFalse(w.hasValidSelection())
w.setCrs(QgsCoordinateReferenceSystem())
self.assertEqual(len(spy), 1)
w.show()
self.assertTrue(w.hasValidSelection())
self.assertFalse(w.crs().isValid())
self.assertEqual(len(spy), 1)

# no double signals if same crs set
w = QgsProjectionSelectionTreeWidget()
spy = QSignalSpy(w.crsSelected)
self.assertFalse(w.hasValidSelection())
w.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
w.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
self.assertEqual(len(spy), 1)

# no double signals if same crs set
w = QgsProjectionSelectionTreeWidget()
spy = QSignalSpy(w.crsSelected)
self.assertFalse(w.hasValidSelection())
w.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
w.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
w.show()
w.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
self.assertEqual(len(spy), 1)
w.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
self.assertEqual(len(spy), 2)


if __name__ == '__main__':
unittest.main()

0 comments on commit 1a88667

Please sign in to comment.