28#include "moc_qgsrangewidgetwrapper.cpp"
30using namespace Qt::StringLiterals;
39 constexpr int DEFAULT_PRECISION_DOUBLE = 4;
40 const int fieldPrecision =
field.precision();
41 switch (
field.type() )
43 case QMetaType::Type::Float:
44 case QMetaType::Type::Double:
45 return fieldPrecision > 0 ? fieldPrecision : DEFAULT_PRECISION_DOUBLE;
48 case QMetaType::Type::LongLong:
55 return fieldPrecision;
60 QWidget *editor =
nullptr;
62 if (
config( u
"Style"_s ).toString() ==
"Dial"_L1 )
66 else if (
config( u
"Style"_s ).toString() ==
"Slider"_L1 )
68 editor =
new QgsSlider( Qt::Horizontal, parent );
74 case QMetaType::Type::Double:
77 case QMetaType::Type::LongLong:
80 static_cast<QgsDoubleSpinBox *
>( editor )->setLineEditAlignment( Qt::AlignRight );
84 case QMetaType::Type::Int:
87 static_cast<QgsSpinBox *
>( editor )->setLineEditAlignment( Qt::AlignRight );
95template<
class T>
static void setupIntEditor(
const QVariant &min,
const QVariant &max,
const QVariant &step, T *slider,
QgsRangeWidgetWrapper *wrapper )
98 slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest() );
99 slider->setMaximum( max.isValid() ? max.toInt() : std::numeric_limits<int>::max() );
100 slider->setSingleStep( step.isValid() ? step.toInt() : 1 );
104template<
class T>
static void setupVariantEditor(
const QVariant &min,
const QVariant &max,
const QVariant &step, T *slider,
QgsRangeWidgetWrapper *wrapper )
107 slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest() );
108 slider->setMaximum( max.isValid() ? max.toInt() : std::numeric_limits<int>::max() );
109 slider->setSingleStep( step.isValid() ? step.toInt() : 1 );
115 mDoubleSpinBox = qobject_cast<QDoubleSpinBox *>( editor );
116 mIntSpinBox = qobject_cast<QSpinBox *>( editor );
118 mDial = qobject_cast<QDial *>( editor );
119 mSlider = qobject_cast<QSlider *>( editor );
120 mQgsDial = qobject_cast<QgsDial *>( editor );
121 mQgsSlider = qobject_cast<QgsSlider *>( editor );
123 const bool allowNull =
config( u
"AllowNull"_s,
true ).toBool();
125 QVariant min(
config( u
"Min"_s ) );
126 QVariant max(
config( u
"Max"_s ) );
127 QVariant step(
config( u
"Step"_s ) );
128 const QVariant precision(
config( u
"Precision"_s ) );
130 if ( mDoubleSpinBox )
132 const double stepval = step.isValid() ? step.toDouble() : 1.0;
133 double minval = min.isValid() ? min.toDouble() : std::numeric_limits<double>::lowest();
134 const double maxval = max.isValid() ? max.toDouble() : std::numeric_limits<double>::max();
138 mDoubleSpinBox->setDecimals( precisionval );
140 QgsDoubleSpinBox *qgsWidget = qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox );
149 if ( precisionval > 0 )
151 decr = std::pow( 10, -precisionval );
159 mDoubleSpinBox->setMinimum( minval );
160 mDoubleSpinBox->setValue( minval );
161 QgsDoubleSpinBox *doubleSpinBox( qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox ) );
167 mDoubleSpinBox->setMinimum( minval );
168 mDoubleSpinBox->setMaximum( maxval );
169 mDoubleSpinBox->setSingleStep( stepval );
170 if (
config( u
"Suffix"_s ).isValid() )
171 mDoubleSpinBox->setSuffix(
config( u
"Suffix"_s ).toString() );
173 connect( mDoubleSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, [
this](
double ) {
emitValueChanged(); } );
175 else if ( mIntSpinBox )
177 QgsSpinBox *qgsWidget = qobject_cast<QgsSpinBox *>( mIntSpinBox );
180 int minval = min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest();
181 const int maxval = max.isValid() ? max.toInt() : std::numeric_limits<int>::max();
182 const uint stepval = step.isValid() ? step.toUInt() : 1;
186 const int minvalOverflow = uint( minval ) - stepval;
187 if ( minvalOverflow < minval )
189 minval = minvalOverflow;
191 mIntSpinBox->setValue( minval );
192 QgsSpinBox *intSpinBox( qobject_cast<QgsSpinBox *>( mIntSpinBox ) );
198 setupIntEditor( minval, maxval, stepval, mIntSpinBox,
this );
199 if (
config( u
"Suffix"_s ).isValid() )
200 mIntSpinBox->setSuffix(
config( u
"Suffix"_s ).toString() );
208 setupVariantEditor( min, max, step, mQgsDial,
this );
209 else if ( mQgsSlider )
210 setupVariantEditor( min, max, step, mQgsSlider,
this );
212 setupIntEditor( min, max, step, mDial,
this );
214 setupIntEditor( min, max, step, mSlider,
this );
220 return mSlider || mDial || mQgsDial || mQgsSlider || mIntSpinBox || mDoubleSpinBox;
223void QgsRangeWidgetWrapper::valueChangedVariant(
const QVariant &v )
225 if ( v.userType() == QMetaType::Type::Int )
232 else if ( v.userType() == QMetaType::Type::LongLong )
239 else if ( v.userType() == QMetaType::Type::Double )
252 if ( mDoubleSpinBox )
254 const QMetaType::Type fieldType =
field().
type();
257 case QMetaType::Type::Double:
258 value = mDoubleSpinBox->value();
261 case QMetaType::Type::LongLong:
262 value =
static_cast<long long>( mDoubleSpinBox->value() );
269 if (
value == mDoubleSpinBox->minimum() &&
config( u
"AllowNull"_s,
true ).toBool() )
274 else if ( mIntSpinBox )
276 value = mIntSpinBox->value();
277 if (
value == mIntSpinBox->minimum() &&
config( u
"AllowNull"_s,
true ).toBool() )
284 value = mQgsDial->variantValue();
286 else if ( mQgsSlider )
288 value = mQgsSlider->variantValue();
292 value = mDial->value();
296 value = mSlider->value();
302void QgsRangeWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
304 if ( mDoubleSpinBox )
308 mDoubleSpinBox->setValue( mDoubleSpinBox->minimum() );
312 mDoubleSpinBox->setValue(
value.toDouble() );
320 mIntSpinBox->setValue( mIntSpinBox->minimum() );
324 mIntSpinBox->setValue(
value.toInt() );
330 mQgsDial->setValue(
value );
332 else if ( mQgsSlider )
334 mQgsSlider->setValue(
value );
338 mDial->setValue(
value.toInt() );
342 mSlider->setValue(
value.toInt() );
348 if ( mDoubleSpinBox )
350 mDoubleSpinBox->setReadOnly( !enabled );
351 mDoubleSpinBox->setFrame( enabled );
353 else if ( mIntSpinBox )
355 mIntSpinBox->setReadOnly( !enabled );
356 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