QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 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 if ( QgsApplication::numericFormatRegistry()->sortKey( a ) < QgsApplication::numericFormatRegistry()->sortKey( b ) )
117 return true;
118 else if ( QgsApplication::numericFormatRegistry()->sortKey( a ) > QgsApplication::numericFormatRegistry()->sortKey( b ) )
119 return false;
120 else
121 {
122 int res = QString::localeAwareCompare( QgsApplication::numericFormatRegistry()->visibleName( a ), QgsApplication::numericFormatRegistry()->visibleName( b ) );
123 if ( res < 0 )
124 return true;
125 else if ( res > 0 )
126 return false;
127 }
128 return false;
129 } );
130
131 for ( const QString &id : std::as_const( ids ) )
132 mCategoryCombo->addItem( QgsApplication::numericFormatRegistry()->visibleName( id ), id );
133}
134
135void QgsNumericFormatSelectorWidget::updateFormatWidget()
136{
137 if ( stackedWidget->currentWidget() != pageDummy )
138 {
139 // stop updating from the original widget
140 if ( QgsNumericFormatWidget *w = qobject_cast<QgsNumericFormatWidget *>( stackedWidget->currentWidget() ) )
141 disconnect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
142 stackedWidget->removeWidget( stackedWidget->currentWidget() );
143 }
144 if ( QgsNumericFormatWidget *w = QgsGui::numericFormatGuiRegistry()->formatConfigurationWidget( mCurrentFormat.get() ) )
145 {
146 w->setFormat( mCurrentFormat->clone() );
147 stackedWidget->addWidget( w );
148 stackedWidget->setCurrentWidget( w );
149 // start receiving updates from widget
150 connect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
151 w->registerExpressionContextGenerator( mExpressionContextGenerator );
152 }
153 else
154 {
155 stackedWidget->setCurrentWidget( pageDummy );
156 }
157
158 updateSampleText();
159}
160
161void QgsNumericFormatSelectorWidget::updateSampleText()
162{
163 const double sampleValue = mCurrentFormat->suggestSampleValue();
164 mSampleLabel->setText( QStringLiteral( "%1 %2 <b>%3</b>" ).arg( mPreviewFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ).arg( QChar( 0x2192 ) ).arg( mCurrentFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ) );
165}
166
167//
168// QgsNumericFormatSelectorDialog
169//
170
172 : QDialog( parent, fl )
173{
174 setWindowTitle( tr( "Numeric Format" ) );
175
176 mFormatWidget = new QgsNumericFormatSelectorWidget( this );
177 mFormatWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
178
179 QVBoxLayout *layout = new QVBoxLayout( this );
180 layout->addWidget( mFormatWidget );
181
182 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, Qt::Horizontal, this );
183 layout->addWidget( mButtonBox );
184
185 setLayout( layout );
187
188 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QAbstractButton::clicked, this, &QDialog::accept );
189 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QAbstractButton::clicked, this, &QDialog::reject );
190}
191
193{
194 mFormatWidget->setFormat( format );
195}
196
198{
199 return mFormatWidget->format();
200}
201
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:210
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
Definition qgsgui.cpp:160
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:5928