27#include "moc_qgsrangewidgetwrapper.cpp"
37 constexpr int DEFAULT_PRECISION_DOUBLE = 4;
38 const int fieldPrecision =
field.precision();
39 switch (
field.type() )
41 case QMetaType::Type::Float:
42 case QMetaType::Type::Double:
43 return fieldPrecision > 0 ? fieldPrecision : DEFAULT_PRECISION_DOUBLE;
46 case QMetaType::Type::LongLong:
53 return fieldPrecision;
58 QWidget *editor =
nullptr;
60 if (
config( QStringLiteral(
"Style" ) ).toString() == QLatin1String(
"Dial" ) )
64 else if (
config( QStringLiteral(
"Style" ) ).toString() == QLatin1String(
"Slider" ) )
66 editor =
new QgsSlider( Qt::Horizontal, parent );
72 case QMetaType::Type::Double:
75 case QMetaType::Type::LongLong:
78 static_cast<QgsDoubleSpinBox *
>( editor )->setLineEditAlignment( Qt::AlignRight );
82 case QMetaType::Type::Int:
85 static_cast<QgsSpinBox *
>( editor )->setLineEditAlignment( Qt::AlignRight );
94static void setupIntEditor(
const QVariant &min,
const QVariant &max,
const QVariant &step, T *slider,
QgsRangeWidgetWrapper *wrapper )
97 slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest() );
98 slider->setMaximum( max.isValid() ? max.toInt() : std::numeric_limits<int>::max() );
99 slider->setSingleStep( step.isValid() ? step.toInt() : 1 );
105 mDoubleSpinBox = qobject_cast<QDoubleSpinBox *>( editor );
106 mIntSpinBox = qobject_cast<QSpinBox *>( editor );
108 mDial = qobject_cast<QDial *>( editor );
109 mSlider = qobject_cast<QSlider *>( editor );
110 mQgsDial = qobject_cast<QgsDial *>( editor );
111 mQgsSlider = qobject_cast<QgsSlider *>( editor );
113 const bool allowNull =
config( QStringLiteral(
"AllowNull" ),
true ).toBool();
115 QVariant min(
config( QStringLiteral(
"Min" ) ) );
116 QVariant max(
config( QStringLiteral(
"Max" ) ) );
117 QVariant step(
config( QStringLiteral(
"Step" ) ) );
118 const QVariant precision(
config( QStringLiteral(
"Precision" ) ) );
120 if ( mDoubleSpinBox )
122 const double stepval = step.isValid() ? step.toDouble() : 1.0;
123 double minval = min.isValid() ? min.toDouble() : std::numeric_limits<double>::lowest();
124 const double maxval = max.isValid() ? max.toDouble() : std::numeric_limits<double>::max();
128 mDoubleSpinBox->setDecimals( precisionval );
130 QgsDoubleSpinBox *qgsWidget = qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox );
139 if ( precisionval > 0 )
141 decr = std::pow( 10, -precisionval );
149 mDoubleSpinBox->setMinimum( minval );
150 mDoubleSpinBox->setValue( minval );
151 QgsDoubleSpinBox *doubleSpinBox( qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox ) );
157 mDoubleSpinBox->setMinimum( minval );
158 mDoubleSpinBox->setMaximum( maxval );
159 mDoubleSpinBox->setSingleStep( stepval );
160 if (
config( QStringLiteral(
"Suffix" ) ).isValid() )
161 mDoubleSpinBox->setSuffix(
config( QStringLiteral(
"Suffix" ) ).toString() );
163 connect( mDoubleSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, [
this](
double ) {
emitValueChanged(); } );
165 else if ( mIntSpinBox )
167 QgsSpinBox *qgsWidget = qobject_cast<QgsSpinBox *>( mIntSpinBox );
170 int minval = min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest();
171 const int maxval = max.isValid() ? max.toInt() : std::numeric_limits<int>::max();
172 const uint stepval = step.isValid() ? step.toUInt() : 1;
176 const int minvalOverflow = uint( minval ) - stepval;
177 if ( minvalOverflow < minval )
179 minval = minvalOverflow;
181 mIntSpinBox->setValue( minval );
182 QgsSpinBox *intSpinBox( qobject_cast<QgsSpinBox *>( mIntSpinBox ) );
188 setupIntEditor( minval, maxval, stepval, mIntSpinBox,
this );
189 if (
config( QStringLiteral(
"Suffix" ) ).isValid() )
190 mIntSpinBox->setSuffix(
config( QStringLiteral(
"Suffix" ) ).toString() );
198 setupIntEditor<QDial>( min, max, step, mQgsDial,
this );
199 else if ( mQgsSlider )
200 setupIntEditor<QSlider>( min, max, step, mQgsSlider,
this );
202 setupIntEditor( min, max, step, mDial,
this );
204 setupIntEditor( min, max, step, mSlider,
this );
210 return mSlider || mDial || mQgsDial || mQgsSlider || mIntSpinBox || mDoubleSpinBox;
213void QgsRangeWidgetWrapper::valueChangedVariant(
const QVariant &v )
215 if ( v.userType() == QMetaType::Type::Int )
222 else if ( v.userType() == QMetaType::Type::LongLong )
229 else if ( v.userType() == QMetaType::Type::Double )
242 if ( mDoubleSpinBox )
244 const QMetaType::Type fieldType =
field().
type();
247 case QMetaType::Type::Double:
248 value = mDoubleSpinBox->value();
251 case QMetaType::Type::LongLong:
252 value =
static_cast<long long>( mDoubleSpinBox->value() );
259 if (
value == mDoubleSpinBox->minimum() &&
config( QStringLiteral(
"AllowNull" ),
true ).toBool() )
264 else if ( mIntSpinBox )
266 value = mIntSpinBox->value();
267 if (
value == mIntSpinBox->minimum() &&
config( QStringLiteral(
"AllowNull" ),
true ).toBool() )
274 value = mQgsDial->variantValue();
276 else if ( mQgsSlider )
278 value = mQgsSlider->variantValue();
282 value = mDial->value();
286 value = mSlider->value();
292void QgsRangeWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
294 if ( mDoubleSpinBox )
298 mDoubleSpinBox->setValue( mDoubleSpinBox->minimum() );
302 mDoubleSpinBox->setValue(
value.toDouble() );
310 mIntSpinBox->setValue( mIntSpinBox->minimum() );
314 mIntSpinBox->setValue(
value.toInt() );
320 mQgsDial->setValue(
value );
322 else if ( mQgsSlider )
324 mQgsSlider->setValue(
value );
328 mDial->setValue(
value.toInt() );
332 mSlider->setValue(
value.toInt() );
338 if ( mDoubleSpinBox )
340 mDoubleSpinBox->setReadOnly( !enabled );
341 mDoubleSpinBox->setFrame( enabled );
343 else if ( mIntSpinBox )
345 mIntSpinBox->setReadOnly( !enabled );
346 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