Skip to content

Commit

Permalink
Fix crash in widget, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 8, 2020
1 parent f5e965a commit 0fc7abb
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/gui/numericformats/qgsnumericformatselectorwidget.cpp
Expand Up @@ -84,6 +84,14 @@ void QgsNumericFormatSelectorWidget::formatTypeChanged()
emit changed();
}

void QgsNumericFormatSelectorWidget::formatChanged()
{
if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
mCurrentFormat.reset( w->format() );

emit changed();
}

void QgsNumericFormatSelectorWidget::populateTypes()
{
QStringList ids = QgsApplication::numericFormatRegistry()->formats();
Expand Down Expand Up @@ -115,16 +123,16 @@ void QgsNumericFormatSelectorWidget::updateFormatWidget()
{
// stop updating from the original widget
if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
disconnect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::changed );
disconnect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
stackedWidget->removeWidget( stackedWidget->currentWidget() );
}
if ( QgsNumericFormatWidget *w = QgsGui::numericFormatGuiRegistry()->formatConfigurationWidget( mCurrentFormat.get() ) )
{
w->setFormat( mCurrentFormat.get() );
w->setFormat( mCurrentFormat->clone() );
stackedWidget->addWidget( w );
stackedWidget->setCurrentWidget( w );
// start receiving updates from widget
connect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::changed );
connect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
return;
}
else
Expand Down
1 change: 1 addition & 0 deletions src/gui/numericformats/qgsnumericformatselectorwidget.h
Expand Up @@ -62,6 +62,7 @@ class GUI_EXPORT QgsNumericFormatSelectorWidget : public QgsPanelWidget, private

private slots:
void formatTypeChanged();
void formatChanged();

private:

Expand Down
98 changes: 98 additions & 0 deletions tests/src/python/test_qgsnumericformatgui.py
Expand Up @@ -133,6 +133,104 @@ def testSelectorWidget(self):
new = w.format()
self.assertEqual(new.xx, 55)

def testBasicFormat(self):
w = QgsNumericFormatSelectorWidget()

original = QgsBasicNumericFormat()
original.setShowPlusSign(True)
original.setShowThousandsSeparator(False)
original.setNumberDecimalPlaces(4)
original.setShowTrailingZeros(True)

w.setFormat(original)
new = w.format()

self.assertIsInstance(new, QgsBasicNumericFormat)
self.assertEqual(new.showPlusSign(), original.showPlusSign())
self.assertEqual(new.showThousandsSeparator(), original.showThousandsSeparator())
self.assertEqual(new.numberDecimalPlaces(), original.numberDecimalPlaces())
self.assertEqual(new.showTrailingZeros(), original.showTrailingZeros())

def testCurrencyFormat(self):
w = QgsNumericFormatSelectorWidget()

original = QgsCurrencyNumericFormat()
original.setShowPlusSign(True)
original.setShowThousandsSeparator(False)
original.setNumberDecimalPlaces(4)
original.setShowTrailingZeros(True)
original.setPrefix('$$')
original.setSuffix('AUD')

w.setFormat(original)
new = w.format()

self.assertIsInstance(new, QgsCurrencyNumericFormat)
self.assertEqual(new.showPlusSign(), original.showPlusSign())
self.assertEqual(new.showThousandsSeparator(), original.showThousandsSeparator())
self.assertEqual(new.numberDecimalPlaces(), original.numberDecimalPlaces())
self.assertEqual(new.showTrailingZeros(), original.showTrailingZeros())
self.assertEqual(new.prefix(), original.prefix())
self.assertEqual(new.suffix(), original.suffix())

def testBearingFormat(self):
w = QgsNumericFormatSelectorWidget()

original = QgsBearingNumericFormat()
original.setNumberDecimalPlaces(4)
original.setShowTrailingZeros(True)
original.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360)

w.setFormat(original)
new = w.format()

self.assertIsInstance(new, QgsBearingNumericFormat)
self.assertEqual(new.numberDecimalPlaces(), original.numberDecimalPlaces())
self.assertEqual(new.showTrailingZeros(), original.showTrailingZeros())
self.assertEqual(new.directionFormat(), original.directionFormat())

def testPercentageFormat(self):
w = QgsNumericFormatSelectorWidget()

original = QgsPercentageNumericFormat()
original.setNumberDecimalPlaces(4)
original.setShowTrailingZeros(True)
original.setInputValues(QgsPercentageNumericFormat.ValuesAreFractions)

w.setFormat(original)
new = w.format()

self.assertIsInstance(new, QgsPercentageNumericFormat)
self.assertEqual(new.numberDecimalPlaces(), original.numberDecimalPlaces())
self.assertEqual(new.showTrailingZeros(), original.showTrailingZeros())
self.assertEqual(new.inputValues(), original.inputValues())

def testScientificFormat(self):
w = QgsNumericFormatSelectorWidget()

original = QgsScientificNumericFormat()
original.setShowPlusSign(True)
original.setNumberDecimalPlaces(4)
original.setShowTrailingZeros(True)

w.setFormat(original)
new = w.format()

self.assertIsInstance(new, QgsScientificNumericFormat)
self.assertEqual(new.showPlusSign(), original.showPlusSign())
self.assertEqual(new.numberDecimalPlaces(), original.numberDecimalPlaces())
self.assertEqual(new.showTrailingZeros(), original.showTrailingZeros())

def testDefaultFormat(self):
w = QgsNumericFormatSelectorWidget()

original = QgsFallbackNumericFormat()

w.setFormat(original)
new = w.format()

self.assertIsInstance(new, QgsFallbackNumericFormat)


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

0 comments on commit 0fc7abb

Please sign in to comment.