28#include "moc_qgsrangewidgetwrapper.cpp"
30using namespace Qt::StringLiterals;
40 constexpr int DEFAULT_PRECISION_DOUBLE = 4;
41 const int fieldPrecision =
field.precision();
42 switch (
field.type() )
44 case QMetaType::Type::Float:
45 case QMetaType::Type::Double:
46 return fieldPrecision > 0 ? fieldPrecision : DEFAULT_PRECISION_DOUBLE;
49 case QMetaType::Type::LongLong:
56 return fieldPrecision;
61 QWidget *editor =
nullptr;
63 if (
config( u
"Style"_s ).toString() ==
"Dial"_L1 )
67 else if (
config( u
"Style"_s ).toString() ==
"Slider"_L1 )
69 editor =
new QgsSlider( Qt::Horizontal, parent );
75 case QMetaType::Type::Double:
78 case QMetaType::Type::LongLong:
81 static_cast<QgsDoubleSpinBox *
>( editor )->setLineEditAlignment( Qt::AlignRight );
85 case QMetaType::Type::Int:
88 static_cast<QgsSpinBox *
>( editor )->setLineEditAlignment( Qt::AlignRight );
97static void setupIntEditor(
const QVariant &min,
const QVariant &max,
const QVariant &step, T *slider,
QgsRangeWidgetWrapper *wrapper )
100 slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest() );
101 slider->setMaximum( max.isValid() ? max.toInt() : std::numeric_limits<int>::max() );
102 slider->setSingleStep( step.isValid() ? step.toInt() : 1 );
107static void setupVariantEditor(
const QVariant &min,
const QVariant &max,
const QVariant &step, T *slider,
QgsRangeWidgetWrapper *wrapper )
110 slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest() );
111 slider->setMaximum( max.isValid() ? max.toInt() : std::numeric_limits<int>::max() );
112 slider->setSingleStep( step.isValid() ? step.toInt() : 1 );
118 mDoubleSpinBox = qobject_cast<QDoubleSpinBox *>( editor );
119 mIntSpinBox = qobject_cast<QSpinBox *>( editor );
121 mDial = qobject_cast<QDial *>( editor );
122 mSlider = qobject_cast<QSlider *>( editor );
123 mQgsDial = qobject_cast<QgsDial *>( editor );
124 mQgsSlider = qobject_cast<QgsSlider *>( editor );
126 const bool allowNull =
config( u
"AllowNull"_s,
true ).toBool();
128 QVariant min(
config( u
"Min"_s ) );
129 QVariant max(
config( u
"Max"_s ) );
130 QVariant step(
config( u
"Step"_s ) );
131 const QVariant precision(
config( u
"Precision"_s ) );
133 if ( mDoubleSpinBox )
135 const double stepval = step.isValid() ? step.toDouble() : 1.0;
136 double minval = min.isValid() ? min.toDouble() : std::numeric_limits<double>::lowest();
137 const double maxval = max.isValid() ? max.toDouble() : std::numeric_limits<double>::max();
141 mDoubleSpinBox->setDecimals( precisionval );
143 QgsDoubleSpinBox *qgsWidget = qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox );
152 if ( precisionval > 0 )
154 decr = std::pow( 10, -precisionval );
162 mDoubleSpinBox->setMinimum( minval );
163 mDoubleSpinBox->setValue( minval );
164 QgsDoubleSpinBox *doubleSpinBox( qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox ) );
170 mDoubleSpinBox->setMinimum( minval );
171 mDoubleSpinBox->setMaximum( maxval );
172 mDoubleSpinBox->setSingleStep( stepval );
173 if (
config( u
"Suffix"_s ).isValid() )
174 mDoubleSpinBox->setSuffix(
config( u
"Suffix"_s ).toString() );
176 connect( mDoubleSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, [
this](
double ) {
emitValueChanged(); } );
178 else if ( mIntSpinBox )
180 QgsSpinBox *qgsWidget = qobject_cast<QgsSpinBox *>( mIntSpinBox );
183 int minval = min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest();
184 const int maxval = max.isValid() ? max.toInt() : std::numeric_limits<int>::max();
185 const uint stepval = step.isValid() ? step.toUInt() : 1;
189 const int minvalOverflow = uint( minval ) - stepval;
190 if ( minvalOverflow < minval )
192 minval = minvalOverflow;
194 mIntSpinBox->setValue( minval );
195 QgsSpinBox *intSpinBox( qobject_cast<QgsSpinBox *>( mIntSpinBox ) );
201 setupIntEditor( minval, maxval, stepval, mIntSpinBox,
this );
202 if (
config( u
"Suffix"_s ).isValid() )
203 mIntSpinBox->setSuffix(
config( u
"Suffix"_s ).toString() );
211 setupVariantEditor( min, max, step, mQgsDial,
this );
212 else if ( mQgsSlider )
213 setupVariantEditor( min, max, step, mQgsSlider,
this );
215 setupIntEditor( min, max, step, mDial,
this );
217 setupIntEditor( min, max, step, mSlider,
this );
223 return mSlider || mDial || mQgsDial || mQgsSlider || mIntSpinBox || mDoubleSpinBox;
226void QgsRangeWidgetWrapper::valueChangedVariant(
const QVariant &v )
228 if ( v.userType() == QMetaType::Type::Int )
235 else if ( v.userType() == QMetaType::Type::LongLong )
242 else if ( v.userType() == QMetaType::Type::Double )
255 if ( mDoubleSpinBox )
257 const QMetaType::Type fieldType =
field().
type();
260 case QMetaType::Type::Double:
261 value = mDoubleSpinBox->value();
264 case QMetaType::Type::LongLong:
265 value =
static_cast<long long>( mDoubleSpinBox->value() );
272 if (
value == mDoubleSpinBox->minimum() &&
config( u
"AllowNull"_s,
true ).toBool() )
277 else if ( mIntSpinBox )
279 value = mIntSpinBox->value();
280 if (
value == mIntSpinBox->minimum() &&
config( u
"AllowNull"_s,
true ).toBool() )
287 value = mQgsDial->variantValue();
289 else if ( mQgsSlider )
291 value = mQgsSlider->variantValue();
295 value = mDial->value();
299 value = mSlider->value();
305void QgsRangeWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
307 if ( mDoubleSpinBox )
311 mDoubleSpinBox->setValue( mDoubleSpinBox->minimum() );
315 mDoubleSpinBox->setValue(
value.toDouble() );
323 mIntSpinBox->setValue( mIntSpinBox->minimum() );
327 mIntSpinBox->setValue(
value.toInt() );
333 mQgsDial->setValue(
value );
335 else if ( mQgsSlider )
337 mQgsSlider->setValue(
value );
341 mDial->setValue(
value.toInt() );
345 mSlider->setValue(
value.toInt() );
351 if ( mDoubleSpinBox )
353 mDoubleSpinBox->setReadOnly( !enabled );
354 mDoubleSpinBox->setFrame( enabled );
356 else if ( mIntSpinBox )
358 mIntSpinBox->setReadOnly( !enabled );
359 mIntSpinBox->setFrame( enabled );
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
A QDial subclass with additional refinements.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
void setSpecialValueText(const QString &txt)
Set the special-value text to be txt If set, the spin box will display this text instead of a numeric...
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
Encapsulate a field in an attribute table or data source.
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
A custom QSlider with additional refinements.
A spin box with a clear button that will set the value to the defined clear value.
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
void setSpecialValueText(const QString &txt)
Set the special-value text to be txt If set, the spin box will display this text instead of a numeric...
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based dataset.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH