28#include <QDialogButtonBox>
31#include "moc_qgsnumericformatselectorwidget.cpp"
40 mPreviewFormat = std::make_unique<QgsBasicNumericFormat>();
41 mPreviewFormat->setShowThousandsSeparator(
false );
42 mPreviewFormat->setShowPlusSign(
false );
43 mPreviewFormat->setShowTrailingZeros(
false );
44 mPreviewFormat->setNumberDecimalPlaces( 12 );
47 mCategoryCombo->setCurrentIndex( mCategoryCombo->findData( mCurrentFormat->id() ) );
49 connect( mCategoryCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNumericFormatSelectorWidget::formatTypeChanged );
60 mCurrentFormat.reset(
format->clone() );
62 const QString
id = mCurrentFormat->id();
63 const int index = mCategoryCombo->findData(
id );
66 whileBlocking( mCategoryCombo )->setCurrentIndex( mCategoryCombo->findData( QStringLiteral(
"fallback" ) ) );
69 mCategoryCombo->setCurrentIndex( index );
78 return mCurrentFormat->
clone();
83 mExpressionContextGenerator = generator;
84 if (
QgsNumericFormatWidget *w = qobject_cast<QgsNumericFormatWidget *>( stackedWidget->currentWidget() ) )
85 w->registerExpressionContextGenerator( mExpressionContextGenerator );
88void QgsNumericFormatSelectorWidget::formatTypeChanged()
90 const QString newId = mCategoryCombo->currentData().toString();
91 if ( mCurrentFormat->id() == newId )
97 QVariantMap props = mCurrentFormat->configuration( QgsReadWriteContext() );
100 updateFormatWidget();
105void QgsNumericFormatSelectorWidget::formatChanged()
107 if ( QgsNumericFormatWidget *w = qobject_cast<QgsNumericFormatWidget *>( stackedWidget->currentWidget() ) )
108 mCurrentFormat.reset( w->format() );
114void QgsNumericFormatSelectorWidget::populateTypes()
118 std::sort( ids.begin(), ids.end(), [](
const QString &a,
const QString &b ) ->
bool {
119 if ( QgsApplication::numericFormatRegistry()->sortKey( a ) < QgsApplication::numericFormatRegistry()->sortKey( b ) )
121 else if ( QgsApplication::numericFormatRegistry()->sortKey( a ) > QgsApplication::numericFormatRegistry()->sortKey( b ) )
125 int res = QString::localeAwareCompare( QgsApplication::numericFormatRegistry()->visibleName( a ), QgsApplication::numericFormatRegistry()->visibleName( b ) );
134 for (
const QString &
id : std::as_const( ids ) )
138void QgsNumericFormatSelectorWidget::updateFormatWidget()
140 if ( stackedWidget->currentWidget() != pageDummy )
143 if ( QgsNumericFormatWidget *w = qobject_cast<QgsNumericFormatWidget *>( stackedWidget->currentWidget() ) )
145 stackedWidget->removeWidget( stackedWidget->currentWidget() );
149 w->setFormat( mCurrentFormat->clone() );
150 stackedWidget->addWidget( w );
151 stackedWidget->setCurrentWidget( w );
154 w->registerExpressionContextGenerator( mExpressionContextGenerator );
158 stackedWidget->setCurrentWidget( pageDummy );
164void QgsNumericFormatSelectorWidget::updateSampleText()
166 const double sampleValue = mCurrentFormat->suggestSampleValue();
167 mSampleLabel->setText( QStringLiteral(
"%1 %2 <b>%3</b>" ).arg( mPreviewFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ).arg( QChar( 0x2192 ) ).arg( mCurrentFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ) );
175 : QDialog( parent, fl )
177 setWindowTitle( tr(
"Numeric Format" ) );
180 mFormatWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
182 QVBoxLayout *layout =
new QVBoxLayout(
this );
183 layout->addWidget( mFormatWidget );
185 mButtonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, Qt::Horizontal,
this );
186 layout->addWidget( mButtonBox );
191 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QAbstractButton::clicked,
this, &QDialog::accept );
192 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QAbstractButton::clicked,
this, &QDialog::reject );
197 mFormatWidget->setFormat(
format );
202 return mFormatWidget->format();
207 mFormatWidget->registerExpressionContextGenerator( generator );
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...
static QgsNumericFormatGuiRegistry * numericFormatGuiRegistry()
Returns the global numeric format gui registry, used for registering the GUI widgets associated with ...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.