Skip to content
Permalink
Browse files

Fix loss of crs set for QgsProjectionSelectionWidget if widget is never

shown, fix missing signals and duplicate signals

(cherry picked from commit 5375fdf)
  • Loading branch information
nyalldawson committed Dec 16, 2019
1 parent 771474e commit 1a886671a8b80763c58b5e279286a1565ba7e54b
@@ -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() )
@@ -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();

@@ -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();
}
}
}

@@ -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 ) );
@@ -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();
}
@@ -718,7 +741,8 @@ void QgsProjectionSelectionTreeWidget::lstCoordinateSystems_currentItemChanged(
if ( current->childCount() == 0 )
{
// Found a real CRS
emit crsSelected();
if ( !mBlockSignals )
emit crsSelected();

updateBoundsPreview();

@@ -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
@@ -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.
You can’t perform that action at this time.