QGIS API Documentation 3.39.0-Master (d85f3c2a281)
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 "qgsapplication.h"
19#include "qgsnumericformat.h"
21#include "qgis.h"
22#include "qgsgui.h"
24#include "qgsreadwritecontext.h"
26#include <QDialogButtonBox>
27#include <QPushButton>
28
30 : QgsPanelWidget( parent )
31{
32 setupUi( this );
33
34 mCurrentFormat.reset( QgsApplication::numericFormatRegistry()->fallbackFormat() );
35
36 mPreviewFormat = std::make_unique< QgsBasicNumericFormat >();
37 mPreviewFormat->setShowThousandsSeparator( false );
38 mPreviewFormat->setShowPlusSign( false );
39 mPreviewFormat->setShowTrailingZeros( false );
40 mPreviewFormat->setNumberDecimalPlaces( 12 );
41
42 populateTypes();
43 mCategoryCombo->setCurrentIndex( mCategoryCombo->findData( mCurrentFormat->id() ) );
44
45 connect( mCategoryCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNumericFormatSelectorWidget::formatTypeChanged );
46 updateFormatWidget();
47}
48
50
52{
53 if ( !format )
54 return;
55
56 mCurrentFormat.reset( format->clone() );
57
58 const QString id = mCurrentFormat->id();
59 const int index = mCategoryCombo->findData( id );
60 if ( index < 0 )
61 {
62 whileBlocking( mCategoryCombo )->setCurrentIndex( mCategoryCombo->findData( QStringLiteral( "fallback" ) ) );
63
64 }
65 else
66 mCategoryCombo->setCurrentIndex( index );
67
68 updateFormatWidget();
69
70 emit changed();
71}
72
74{
75 return mCurrentFormat->clone();
76}
77
79{
80 mExpressionContextGenerator = generator;
81 if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
82 w->registerExpressionContextGenerator( mExpressionContextGenerator );
83}
84
85void QgsNumericFormatSelectorWidget::formatTypeChanged()
86{
87 const QString newId = mCategoryCombo->currentData().toString();
88 if ( mCurrentFormat->id() == newId )
89 {
90 return;
91 }
92
93 // keep as much of the current format's properties as possible
94 QVariantMap props = mCurrentFormat->configuration( QgsReadWriteContext() );
95 mCurrentFormat.reset( QgsApplication::numericFormatRegistry()->create( newId, props, QgsReadWriteContext() ) );
96
97 updateFormatWidget();
98 updateSampleText();
99 emit changed();
100}
101
102void QgsNumericFormatSelectorWidget::formatChanged()
103{
104 if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
105 mCurrentFormat.reset( w->format() );
106
107 updateSampleText();
108 emit changed();
109}
110
111void QgsNumericFormatSelectorWidget::populateTypes()
112{
113 QStringList ids = QgsApplication::numericFormatRegistry()->formats();
114
115 std::sort( ids.begin(), ids.end(), [ = ]( const QString & a, const QString & b )->bool
116 {
117 if ( QgsApplication::numericFormatRegistry()->sortKey( a ) < QgsApplication::numericFormatRegistry()->sortKey( b ) )
118 return true;
119 else if ( QgsApplication::numericFormatRegistry()->sortKey( a ) > QgsApplication::numericFormatRegistry()->sortKey( b ) )
120 return false;
121 else
122 {
123 int res = QString::localeAwareCompare( QgsApplication::numericFormatRegistry()->visibleName( a ), QgsApplication::numericFormatRegistry()->visibleName( b ) );
124 if ( res < 0 )
125 return true;
126 else if ( res > 0 )
127 return false;
128 }
129 return false;
130 } );
131
132 for ( const QString &id : std::as_const( ids ) )
133 mCategoryCombo->addItem( QgsApplication::numericFormatRegistry()->visibleName( id ), id );
134}
135
136void QgsNumericFormatSelectorWidget::updateFormatWidget()
137{
138 if ( stackedWidget->currentWidget() != pageDummy )
139 {
140 // stop updating from the original widget
141 if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
142 disconnect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
143 stackedWidget->removeWidget( stackedWidget->currentWidget() );
144 }
145 if ( QgsNumericFormatWidget *w = QgsGui::numericFormatGuiRegistry()->formatConfigurationWidget( mCurrentFormat.get() ) )
146 {
147 w->setFormat( mCurrentFormat->clone() );
148 stackedWidget->addWidget( w );
149 stackedWidget->setCurrentWidget( w );
150 // start receiving updates from widget
151 connect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
152 w->registerExpressionContextGenerator( mExpressionContextGenerator );
153 }
154 else
155 {
156 stackedWidget->setCurrentWidget( pageDummy );
157 }
158
159 updateSampleText();
160}
161
162void QgsNumericFormatSelectorWidget::updateSampleText()
163{
164 const double sampleValue = mCurrentFormat->suggestSampleValue();
165 mSampleLabel->setText( QStringLiteral( "%1 %2 <b>%3</b>" ).arg( mPreviewFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) )
166 .arg( QChar( 0x2192 ) )
167 .arg( mCurrentFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ) );
168}
169
170//
171// QgsNumericFormatSelectorDialog
172//
173
175 : QDialog( parent, fl )
176{
177 setWindowTitle( tr( "Numeric Format" ) );
178
179 mFormatWidget = new QgsNumericFormatSelectorWidget( this );
180 mFormatWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
181
182 QVBoxLayout *layout = new QVBoxLayout( this );
183 layout->addWidget( mFormatWidget );
184
185 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, Qt::Horizontal, this );
186 layout->addWidget( mButtonBox );
187
188 setLayout( layout );
190
191 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QAbstractButton::clicked, this, &QDialog::accept );
192 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QAbstractButton::clicked, this, &QDialog::reject );
193}
194
196{
197 mFormatWidget->setFormat( format );
198}
199
201{
202 return mFormatWidget->format();
203}
204
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:208
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
Definition qgsgui.cpp:158
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:5761