Skip to content
Permalink
Browse files

Fix crash in widget, add unit tests

  • Loading branch information
nyalldawson committed Jan 7, 2020
1 parent f5e965a commit 0fc7abb20ba9f1e4880b8c8ccbaf8131b9eb939a
@@ -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();
@@ -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
@@ -62,6 +62,7 @@ class GUI_EXPORT QgsNumericFormatSelectorWidget : public QgsPanelWidget, private

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

private:

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