QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsslider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsslider.cpp
3 -------------------
4 begin : July 2013
5 copyright : (C) 2013 by Daniel Vaz
6 email : danielvaz at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsslider.h"
19#include "moc_qgsslider.cpp"
20#include "qgslogger.h"
21#include "qgsvariantutils.h"
22
23#include <QPaintEvent>
24#include <QPainter>
25#include <QRect>
26#include <cmath>
27
28QgsSlider::QgsSlider( QWidget *parent )
29 : QSlider( parent )
30{
31 setMinimumSize( QSize( 100, 40 ) );
32}
33
34QgsSlider::QgsSlider( Qt::Orientation orientation, QWidget *parent )
35 : QSlider( orientation, parent )
36{
37 setMinimumSize( QSize( 100, 40 ) );
38}
39
40void QgsSlider::paintEvent( QPaintEvent *event )
41{
42 QSlider::paintEvent( event );
43 QPainter painter( this );
44 const QRect rect = geometry();
45 painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
46 painter.drawText( QRectF( 0, rect.height() * 0.5, rect.width(), rect.height() ), Qt::AlignHCenter, variantValue().toString(), nullptr );
47 painter.end();
48}
49
50void QgsSlider::setMinimum( const QVariant &min )
51{
52 mMin = min;
53 update();
54}
55
56void QgsSlider::setMaximum( const QVariant &max )
57{
58 mMax = max;
59 update();
60}
61
62void QgsSlider::setSingleStep( const QVariant &step )
63{
64 mStep = step;
65 update();
66}
67
68void QgsSlider::setValue( const QVariant &value )
69{
70 mValue = value;
71 update();
72}
73
74void QgsSlider::update()
75{
77 return;
78
79 if ( QgsVariantUtils::isNull( mValue ) )
80 mValue = mMin;
81
82 if ( mMin.userType() == QMetaType::Type::Int && mMax.userType() == QMetaType::Type::Int && mStep.userType() == QMetaType::Type::Int && mValue.userType() == QMetaType::Type::Int )
83 {
84 QSlider::setMinimum( mMin.toInt() );
85 QSlider::setMaximum( mMax.toInt() );
86 QSlider::setSingleStep( mStep.toInt() );
87 QSlider::setValue( mValue.toInt() );
88 }
89 else
90 {
91 if ( minimum() != 0 )
92 QSlider::setMinimum( 0 );
93
94 const int max = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
95 if ( maximum() != max )
96 QSlider::setMaximum( max );
97
98 if ( singleStep() != 1 )
99 QSlider::setSingleStep( 1 );
100
101 QSlider::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
102 }
103
104 connect( this, &QSlider::valueChanged, this, &QgsSlider::onValueChanged );
105}
106
108{
109 return mValue;
110}
111
112void QgsSlider::onValueChanged( int value )
113{
115 {
116 mValue = QVariant();
117 }
118 else if ( mMin.userType() == QMetaType::Type::Int && mMax.userType() == QMetaType::Type::Int && mStep.userType() == QMetaType::Type::Int && mValue.userType() == QMetaType::Type::Int )
119 {
120 mValue = value;
121 }
122 else
123 {
124 mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
125 }
126
127 emit valueChanged( mValue );
128}
void valueChanged(const QVariant &)
void setSingleStep(const QVariant &step)
Definition qgsslider.cpp:62
void setMinimum(const QVariant &min)
Definition qgsslider.cpp:50
QgsSlider(QWidget *parent=nullptr)
Constructor for QgsSlider.
Definition qgsslider.cpp:28
void setValue(const QVariant &value)
Definition qgsslider.cpp:68
void setMaximum(const QVariant &max)
Definition qgsslider.cpp:56
void paintEvent(QPaintEvent *event) override
Definition qgsslider.cpp:40
QVariant variantValue() const
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.