QGIS API Documentation  3.12.1-București (121cc00ff0)
qgsnumericformatwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnumericformatwidget.cpp
3  --------------------------
4  begin : January 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsnumericformatwidget.h"
17 #include "qgsbasicnumericformat.h"
22 #include "qgsgui.h"
23 #include "qgis.h"
24 #include <QDialogButtonBox>
25 
26 //
27 // QgsBasicNumericFormatWidget
28 //
30  : QgsNumericFormatWidget( parent )
31 {
32  setupUi( this );
33  setFormat( format->clone() );
34 
35  mThousandsLineEdit->setShowClearButton( true );
36  mDecimalLineEdit->setShowClearButton( true );
37 
38  connect( mShowPlusCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
39  {
40  mFormat->setShowPlusSign( checked );
41  if ( !mBlockSignals )
42  emit changed();
43  } );
44 
45  connect( mShowTrailingZerosCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
46  {
47  mFormat->setShowTrailingZeros( checked );
48  if ( !mBlockSignals )
49  emit changed();
50  } );
51 
52  connect( mShowThousandsCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
53  {
54  mFormat->setShowThousandsSeparator( checked );
55  if ( !mBlockSignals )
56  emit changed();
57  } );
58 
59  connect( mDecimalsSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int value )
60  {
61  mFormat->setNumberDecimalPlaces( value );
62  if ( !mBlockSignals )
63  emit changed();
64  } );
65 
66  connect( mRadDecimalPlaces, &QRadioButton::toggled, this, [ = ]( bool checked )
67  {
68  if ( !checked )
69  return;
70 
71  mFormat->setRoundingType( QgsBasicNumericFormat::DecimalPlaces );
72  if ( !mBlockSignals )
73  emit changed();
74  } );
75 
76  connect( mRadSignificantFigures, &QRadioButton::toggled, this, [ = ]( bool checked )
77  {
78  if ( !checked )
79  return;
80 
81  mFormat->setRoundingType( QgsBasicNumericFormat::SignificantFigures );
82  if ( !mBlockSignals )
83  emit changed();
84  } );
85 
86  connect( mThousandsLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & text )
87  {
88  mFormat->setThousandsSeparator( text.isEmpty() ? QChar() : text.at( 0 ) );
89  if ( !mBlockSignals )
90  emit changed();
91  } );
92 
93  connect( mDecimalLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & text )
94  {
95  mFormat->setDecimalSeparator( text.isEmpty() ? QChar() : text.at( 0 ) );
96  if ( !mBlockSignals )
97  emit changed();
98  } );
99 }
100 
102 
104 {
105  mFormat.reset( static_cast< QgsBasicNumericFormat * >( format ) );
106 
107  mBlockSignals = true;
108  mDecimalsSpinBox->setValue( mFormat->numberDecimalPlaces() );
109  mShowPlusCheckBox->setChecked( mFormat->showPlusSign() );
110  mShowTrailingZerosCheckBox->setChecked( mFormat->showTrailingZeros() );
111  mShowThousandsCheckBox->setChecked( mFormat->showThousandsSeparator() );
112  mThousandsLineEdit->setText( mFormat->thousandsSeparator().isNull() ? QString() : mFormat->thousandsSeparator() );
113  mDecimalLineEdit->setText( mFormat->decimalSeparator().isNull() ? QString() : mFormat->decimalSeparator() );
114  switch ( mFormat->roundingType() )
115  {
117  mRadDecimalPlaces->setChecked( true );
118  break;
119 
121  mRadSignificantFigures->setChecked( true );
122  break;
123  }
124 
125  mBlockSignals = false;
126 }
127 
129 {
130  return mFormat->clone();
131 }
132 
133 //
134 // QgsBearingNumericFormatWidget
135 //
136 
138  : QgsNumericFormatWidget( parent )
139 {
140  setupUi( this );
141 
142  mFormatComboBox->addItem( QObject::tr( "0 to 180°, with E/W suffix" ), QgsBearingNumericFormat::UseRange0To180WithEWDirectionalSuffix );
143  mFormatComboBox->addItem( QObject::tr( "-180 to +180°" ), QgsBearingNumericFormat::UseRangeNegative180ToPositive180 );
144  mFormatComboBox->addItem( QObject::tr( "0 to 360°" ), QgsBearingNumericFormat::UseRange0To360 );
145 
146  setFormat( format->clone() );
147 
148  connect( mShowTrailingZerosCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
149  {
150  mFormat->setShowTrailingZeros( checked );
151  if ( !mBlockSignals )
152  emit changed();
153  } );
154 
155  connect( mDecimalsSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int value )
156  {
157  mFormat->setNumberDecimalPlaces( value );
158  if ( !mBlockSignals )
159  emit changed();
160  } );
161 
162  connect( mFormatComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int )
163  {
164  mFormat->setDirectionFormat( static_cast < QgsBearingNumericFormat::FormatDirectionOption >( mFormatComboBox->currentData().toInt() ) );
165  if ( !mBlockSignals )
166  emit changed();
167  } );
168 }
169 
171 
173 {
174  mFormat.reset( static_cast< QgsBearingNumericFormat * >( format ) );
175 
176  mBlockSignals = true;
177  mDecimalsSpinBox->setValue( mFormat->numberDecimalPlaces() );
178  mShowTrailingZerosCheckBox->setChecked( mFormat->showTrailingZeros() );
179  mFormatComboBox->setCurrentIndex( mFormatComboBox->findData( static_cast< int >( mFormat->directionFormat() ) ) );
180  mBlockSignals = false;
181 }
182 
184 {
185  return mFormat->clone();
186 }
187 
188 //
189 // QgsBearingNumericFormatDialog
190 //
191 
193  : QDialog( parent )
194 {
195  setLayout( new QVBoxLayout() );
196  mWidget = new QgsBearingNumericFormatWidget( format );
197  QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
198 
199  connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
200  connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
201 
202  layout()->addWidget( mWidget );
203  layout()->addWidget( buttonBox );
204 
205  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
206 
207  setObjectName( QStringLiteral( "QgsBearingNumericFormatDialog" ) );
209 }
210 
212 {
213  return static_cast< QgsBearingNumericFormat * >( mWidget->format() );
214 }
215 
216 
217 
218 //
219 // QgsCurrencyNumericFormatWidget
220 //
222  : QgsNumericFormatWidget( parent )
223 {
224  setupUi( this );
225  setFormat( format->clone() );
226 
227  connect( mShowPlusCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
228  {
229  mFormat->setShowPlusSign( checked );
230  if ( !mBlockSignals )
231  emit changed();
232  } );
233 
234  connect( mShowTrailingZerosCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
235  {
236  mFormat->setShowTrailingZeros( checked );
237  if ( !mBlockSignals )
238  emit changed();
239  } );
240 
241  connect( mShowThousandsCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
242  {
243  mFormat->setShowThousandsSeparator( checked );
244  if ( !mBlockSignals )
245  emit changed();
246  } );
247 
248  connect( mDecimalsSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int value )
249  {
250  mFormat->setNumberDecimalPlaces( value );
251  if ( !mBlockSignals )
252  emit changed();
253  } );
254 
255  connect( mPrefixLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & text )
256  {
257  mFormat->setPrefix( text );
258  if ( !mBlockSignals )
259  emit changed();
260  } );
261 
262  connect( mSuffixLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & text )
263  {
264  mFormat->setSuffix( text );
265  if ( !mBlockSignals )
266  emit changed();
267  } );
268 }
269 
271 
273 {
274  mFormat.reset( static_cast< QgsCurrencyNumericFormat * >( format ) );
275 
276  mBlockSignals = true;
277  mDecimalsSpinBox->setValue( mFormat->numberDecimalPlaces() );
278  mShowPlusCheckBox->setChecked( mFormat->showPlusSign() );
279  mShowTrailingZerosCheckBox->setChecked( mFormat->showTrailingZeros() );
280  mShowThousandsCheckBox->setChecked( mFormat->showThousandsSeparator() );
281  mPrefixLineEdit->setText( mFormat->prefix() );
282  mSuffixLineEdit->setText( mFormat->suffix() );
283 
284  mBlockSignals = false;
285 }
286 
288 {
289  return mFormat->clone();
290 }
291 
292 
293 //
294 // QgsPercentageNumericFormatWidget
295 //
296 
298  : QgsNumericFormatWidget( parent )
299 {
300  setupUi( this );
301 
302  mScalingComboBox->addItem( QObject::tr( "Values are Percentages (e.g. 50)" ), QgsPercentageNumericFormat::ValuesArePercentage );
303  mScalingComboBox->addItem( QObject::tr( "Values are Fractions (e.g. 0.5)" ), QgsPercentageNumericFormat::ValuesAreFractions );
304 
305  setFormat( format->clone() );
306 
307  connect( mShowTrailingZerosCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
308  {
309  mFormat->setShowTrailingZeros( checked );
310  if ( !mBlockSignals )
311  emit changed();
312  } );
313 
314  connect( mShowPlusCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
315  {
316  mFormat->setShowPlusSign( checked );
317  if ( !mBlockSignals )
318  emit changed();
319  } );
320 
321  connect( mShowThousandsCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
322  {
323  mFormat->setShowThousandsSeparator( checked );
324  if ( !mBlockSignals )
325  emit changed();
326  } );
327 
328  connect( mDecimalsSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int value )
329  {
330  mFormat->setNumberDecimalPlaces( value );
331  if ( !mBlockSignals )
332  emit changed();
333  } );
334 
335  connect( mScalingComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int )
336  {
337  mFormat->setInputValues( static_cast < QgsPercentageNumericFormat::InputValues >( mScalingComboBox->currentData().toInt() ) );
338  if ( !mBlockSignals )
339  emit changed();
340  } );
341 }
342 
344 
346 {
347  mFormat.reset( static_cast< QgsPercentageNumericFormat * >( format ) );
348 
349  mBlockSignals = true;
350  mDecimalsSpinBox->setValue( mFormat->numberDecimalPlaces() );
351  mShowPlusCheckBox->setChecked( mFormat->showPlusSign() );
352  mShowTrailingZerosCheckBox->setChecked( mFormat->showTrailingZeros() );
353  mShowThousandsCheckBox->setChecked( mFormat->showThousandsSeparator() );
354  mScalingComboBox->setCurrentIndex( mScalingComboBox->findData( static_cast< int >( mFormat->inputValues() ) ) );
355  mBlockSignals = false;
356 }
357 
359 {
360  return mFormat->clone();
361 }
362 
363 //
364 // QgsScientificNumericFormatWidget
365 //
367  : QgsNumericFormatWidget( parent )
368 {
369  setupUi( this );
370  setFormat( format->clone() );
371 
372  connect( mShowPlusCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
373  {
374  mFormat->setShowPlusSign( checked );
375  if ( !mBlockSignals )
376  emit changed();
377  } );
378 
379  connect( mShowTrailingZerosCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
380  {
381  mFormat->setShowTrailingZeros( checked );
382  if ( !mBlockSignals )
383  emit changed();
384  } );
385 
386  connect( mDecimalsSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int value )
387  {
388  mFormat->setNumberDecimalPlaces( value );
389  if ( !mBlockSignals )
390  emit changed();
391  } );
392 }
393 
395 
397 {
398  mFormat.reset( static_cast< QgsScientificNumericFormat * >( format ) );
399 
400  mBlockSignals = true;
401  mDecimalsSpinBox->setValue( mFormat->numberDecimalPlaces() );
402  mShowPlusCheckBox->setChecked( mFormat->showPlusSign() );
403  mShowTrailingZerosCheckBox->setChecked( mFormat->showTrailingZeros() );
404  mBlockSignals = false;
405 }
406 
408 {
409  return mFormat->clone();
410 }
411 
412 
void setFormat(QgsNumericFormat *format) override
Sets the format to show in the widget.
~QgsCurrencyNumericFormatWidget() override
QgsNumericFormat * format() override
Returns the format defined by the current settings in the widget.
A widget which allow control over the properties of a QgsBearingNumericFormat.
Base class for widgets which allow control over the properties of QgsNumericFormat subclasses...
void setFormat(QgsNumericFormat *format) override
Sets the format to show in the widget.
QgsNumericFormat * format() override
Returns the format defined by the current settings in the widget.
void changed()
Emitted whenever the configuration of the numeric format is changed.
A numeric formatter which returns a text representation of a direction/bearing.
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
void setFormat(QgsNumericFormat *format) override
Sets the format to show in the widget.
void setFormat(QgsNumericFormat *format) override
Sets the format to show in the widget.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:62
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsPercentageNumericFormatWidget(const QgsNumericFormat *format, QWidget *parent=nullptr)
Constructor for QgsPercentageNumericFormatWidget, initially showing the specified format...
QgsNumericFormat * format() override
Returns the format defined by the current settings in the widget.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
QgsBearingNumericFormatWidget(const QgsNumericFormat *format, QWidget *parent=nullptr)
Constructor for QgsBearingNumericFormatWidget, initially showing the specified format.
void setFormat(QgsNumericFormat *format) override
Sets the format to show in the widget.
QgsBearingNumericFormat * format()
Returns the format defined by the current settings in the dialog.
Return values between 0 and 180, with a E or W directional suffix.
~QgsBasicNumericFormatWidget() override
QgsCurrencyNumericFormatWidget(const QgsNumericFormat *format, QWidget *parent=nullptr)
Constructor for QgsCurrencyNumericFormatWidget, initially showing the specified format.
~QgsBearingNumericFormatWidget() override
QgsNumericFormat * format() override
Returns the format defined by the current settings in the widget.
Incoming values are percentage values (e.g. 50 for 50%)
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:133
QgsNumericFormat * format() override
Returns the format defined by the current settings in the widget.
Maximum number of significant figures.
Maximum number of decimal places.
QgsBearingNumericFormatDialog(const QgsNumericFormat *format, QWidget *parent=nullptr)
Constructor for QgsBearingNumericFormatDialog, initially showing the specified format.
Incoming values are numeric fractions (e.g. 0.5 for 50%)
QgsScientificNumericFormatWidget(const QgsNumericFormat *format, QWidget *parent=nullptr)
Constructor for QgsScientificNumericFormatWidget, initially showing the specified format...
QgsBasicNumericFormatWidget(const QgsNumericFormat *format, QWidget *parent=nullptr)
Constructor for QgsBasicNumericFormatWidget, initially showing the specified format.