QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsnumericformatselectorwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnumericformatselectorwidget.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
17#include "moc_qgsnumericformatselectorwidget.cpp"
18#include "qgsapplication.h"
20#include "qgsnumericformat.h"
22#include "qgis.h"
23#include "qgsgui.h"
25#include "qgsreadwritecontext.h"
27#include <QDialogButtonBox>
28#include <QPushButton>
29
31 : QgsPanelWidget( parent )
32{
33 setupUi( this );
34
35 mCurrentFormat.reset( QgsApplication::numericFormatRegistry()->fallbackFormat() );
36
37 mPreviewFormat = std::make_unique< QgsBasicNumericFormat >();
38 mPreviewFormat->setShowThousandsSeparator( false );
39 mPreviewFormat->setShowPlusSign( false );
40 mPreviewFormat->setShowTrailingZeros( false );
41 mPreviewFormat->setNumberDecimalPlaces( 12 );
42
43 populateTypes();
44 mCategoryCombo->setCurrentIndex( mCategoryCombo->findData( mCurrentFormat->id() ) );
45
46 connect( mCategoryCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNumericFormatSelectorWidget::formatTypeChanged );
47 updateFormatWidget();
48}
49
51
53{
54 if ( !format )
55 return;
56
57 mCurrentFormat.reset( format->clone() );
58
59 const QString id = mCurrentFormat->id();
60 const int index = mCategoryCombo->findData( id );
61 if ( index < 0 )
62 {
63 whileBlocking( mCategoryCombo )->setCurrentIndex( mCategoryCombo->findData( QStringLiteral( "fallback" ) ) );
64
65 }
66 else
67 mCategoryCombo->setCurrentIndex( index );
68
69 updateFormatWidget();
70
71 emit changed();
72}
73
75{
76 return mCurrentFormat->clone();
77}
78
80{
81 mExpressionContextGenerator = generator;
82 if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
83 w->registerExpressionContextGenerator( mExpressionContextGenerator );
84}
85
86void QgsNumericFormatSelectorWidget::formatTypeChanged()
87{
88 const QString newId = mCategoryCombo->currentData().toString();
89 if ( mCurrentFormat->id() == newId )
90 {
91 return;
92 }
93
94 // keep as much of the current format's properties as possible
95 QVariantMap props = mCurrentFormat->configuration( QgsReadWriteContext() );
96 mCurrentFormat.reset( QgsApplication::numericFormatRegistry()->create( newId, props, QgsReadWriteContext() ) );
97
98 updateFormatWidget();
99 updateSampleText();
100 emit changed();
101}
102
103void QgsNumericFormatSelectorWidget::formatChanged()
104{
105 if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
106 mCurrentFormat.reset( w->format() );
107
108 updateSampleText();
109 emit changed();
110}
111
112void QgsNumericFormatSelectorWidget::populateTypes()
113{
114 QStringList ids = QgsApplication::numericFormatRegistry()->formats();
115
116 std::sort( ids.begin(), ids.end(), [ = ]( const QString & a, const QString & b )->bool
117 {
118 if ( QgsApplication::numericFormatRegistry()->sortKey( a ) < QgsApplication::numericFormatRegistry()->sortKey( b ) )
119 return true;
120 else if ( QgsApplication::numericFormatRegistry()->sortKey( a ) > QgsApplication::numericFormatRegistry()->sortKey( b ) )
121 return false;
122 else
123 {
124 int res = QString::localeAwareCompare( QgsApplication::numericFormatRegistry()->visibleName( a ), QgsApplication::numericFormatRegistry()->visibleName( b ) );
125 if ( res < 0 )
126 return true;
127 else if ( res > 0 )
128 return false;
129 }
130 return false;
131 } );
132
133 for ( const QString &id : std::as_const( ids ) )
134 mCategoryCombo->addItem( QgsApplication::numericFormatRegistry()->visibleName( id ), id );
135}
136
137void QgsNumericFormatSelectorWidget::updateFormatWidget()
138{
139 if ( stackedWidget->currentWidget() != pageDummy )
140 {
141 // stop updating from the original widget
142 if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
143 disconnect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
144 stackedWidget->removeWidget( stackedWidget->currentWidget() );
145 }
146 if ( QgsNumericFormatWidget *w = QgsGui::numericFormatGuiRegistry()->formatConfigurationWidget( mCurrentFormat.get() ) )
147 {
148 w->setFormat( mCurrentFormat->clone() );
149 stackedWidget->addWidget( w );
150 stackedWidget->setCurrentWidget( w );
151 // start receiving updates from widget
152 connect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
153 w->registerExpressionContextGenerator( mExpressionContextGenerator );
154 }
155 else
156 {
157 stackedWidget->setCurrentWidget( pageDummy );
158 }
159
160 updateSampleText();
161}
162
163void QgsNumericFormatSelectorWidget::updateSampleText()
164{
165 const double sampleValue = mCurrentFormat->suggestSampleValue();
166 mSampleLabel->setText( QStringLiteral( "%1 %2 <b>%3</b>" ).arg( mPreviewFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) )
167 .arg( QChar( 0x2192 ) )
168 .arg( mCurrentFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ) );
169}
170
171//
172// QgsNumericFormatSelectorDialog
173//
174
176 : QDialog( parent, fl )
177{
178 setWindowTitle( tr( "Numeric Format" ) );
179
180 mFormatWidget = new QgsNumericFormatSelectorWidget( this );
181 mFormatWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
182
183 QVBoxLayout *layout = new QVBoxLayout( this );
184 layout->addWidget( mFormatWidget );
185
186 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, Qt::Horizontal, this );
187 layout->addWidget( mButtonBox );
188
189 setLayout( layout );
191
192 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QAbstractButton::clicked, this, &QDialog::accept );
193 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QAbstractButton::clicked, this, &QDialog::reject );
194}
195
197{
198 mFormatWidget->setFormat( format );
199}
200
202{
203 return mFormatWidget->format();
204}
205
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
Abstract interface for generating an expression context.
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:209
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
Definition qgsgui.cpp:159
A context for numeric formats.
QStringList formats() const
Returns a list of the format IDs currently contained in the registry.
QgsNumericFormat * format() const
Returns a new format object representing the settings currently configured in the dialog.
void setFormat(const QgsNumericFormat *format)
Sets the format to show in the dialog.
QgsNumericFormatSelectorDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=QgsGuiUtils::ModalDialogFlags)
Constructor for QgsNumericFormatSelectorDialog.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
A widget which allows choice of numeric formats and the properties of them.
QgsNumericFormat * format() const
Returns a new format object representing the settings currently configured in the widget.
~QgsNumericFormatSelectorWidget() override
void changed()
Emitted whenever the format configured55 in the widget is changed.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
QgsNumericFormatSelectorWidget(QWidget *parent=nullptr)
Constructor for QgsNumericFormatSelectorWidget with the specified parent widget.
void setFormat(const QgsNumericFormat *format)
Sets the format to show in the widget.
Base class for widgets which allow control over the properties of QgsNumericFormat subclasses.
void changed()
Emitted whenever the configuration of the numeric format is changed.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
Base class for any widget that can be shown as a inline panel.
The class is used as a container of context for various read/write operations on other objects.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5821