28#include <QDialogButtonBox>
32#include "moc_qgsnumericformatselectorwidget.cpp"
34using namespace Qt::StringLiterals;
43 mPreviewFormat = std::make_unique<QgsBasicNumericFormat>();
44 mPreviewFormat->setShowThousandsSeparator(
false );
45 mPreviewFormat->setShowPlusSign(
false );
46 mPreviewFormat->setShowTrailingZeros(
false );
47 mPreviewFormat->setNumberDecimalPlaces( 12 );
50 mCategoryCombo->setCurrentIndex( mCategoryCombo->findData( mCurrentFormat->id() ) );
52 connect( mCategoryCombo,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNumericFormatSelectorWidget::formatTypeChanged );
63 mCurrentFormat.reset(
format->clone() );
65 const QString
id = mCurrentFormat->id();
66 const int index = mCategoryCombo->findData(
id );
69 whileBlocking( mCategoryCombo )->setCurrentIndex( mCategoryCombo->findData( u
"fallback"_s ) );
72 mCategoryCombo->setCurrentIndex( index );
81 return mCurrentFormat->
clone();
86 mExpressionContextGenerator = generator;
87 if (
QgsNumericFormatWidget *w = qobject_cast<QgsNumericFormatWidget *>( stackedWidget->currentWidget() ) )
88 w->registerExpressionContextGenerator( mExpressionContextGenerator );
91void QgsNumericFormatSelectorWidget::formatTypeChanged()
93 const QString newId = mCategoryCombo->currentData().toString();
94 if ( mCurrentFormat->id() == newId )
100 QVariantMap props = mCurrentFormat->configuration( QgsReadWriteContext() );
103 updateFormatWidget();
108void QgsNumericFormatSelectorWidget::formatChanged()
110 if ( QgsNumericFormatWidget *w = qobject_cast<QgsNumericFormatWidget *>( stackedWidget->currentWidget() ) )
111 mCurrentFormat.reset( w->format() );
117void QgsNumericFormatSelectorWidget::populateTypes()
121 std::sort( ids.begin(), ids.end(), [](
const QString &a,
const QString &b ) ->
bool {
122 if ( QgsApplication::numericFormatRegistry()->sortKey( a ) < QgsApplication::numericFormatRegistry()->sortKey( b ) )
124 else if ( QgsApplication::numericFormatRegistry()->sortKey( a ) > QgsApplication::numericFormatRegistry()->sortKey( b ) )
128 int res = QString::localeAwareCompare( QgsApplication::numericFormatRegistry()->visibleName( a ), QgsApplication::numericFormatRegistry()->visibleName( b ) );
137 for (
const QString &
id : std::as_const( ids ) )
141void QgsNumericFormatSelectorWidget::updateFormatWidget()
143 if ( stackedWidget->currentWidget() != pageDummy )
146 if ( QgsNumericFormatWidget *w = qobject_cast<QgsNumericFormatWidget *>( stackedWidget->currentWidget() ) )
148 stackedWidget->removeWidget( stackedWidget->currentWidget() );
152 w->setFormat( mCurrentFormat->clone() );
153 stackedWidget->addWidget( w );
154 stackedWidget->setCurrentWidget( w );
157 w->registerExpressionContextGenerator( mExpressionContextGenerator );
161 stackedWidget->setCurrentWidget( pageDummy );
167void QgsNumericFormatSelectorWidget::updateSampleText()
169 const double sampleValue = mCurrentFormat->suggestSampleValue();
170 mSampleLabel->setText( u
"%1 %2 <b>%3</b>"_s.arg( mPreviewFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ).arg( QChar( 0x2192 ) ).arg( mCurrentFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ) );
178 : QDialog( parent, fl )
180 setWindowTitle( tr(
"Numeric Format" ) );
183 mFormatWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
185 QVBoxLayout *layout =
new QVBoxLayout(
this );
186 layout->addWidget( mFormatWidget );
188 mButtonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, Qt::Horizontal,
this );
189 layout->addWidget( mButtonBox );
194 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QAbstractButton::clicked,
this, &QDialog::accept );
195 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QAbstractButton::clicked,
this, &QDialog::reject );
200 mFormatWidget->setFormat(
format );
205 return mFormatWidget->format();
210 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.