QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20#include <cmath>
21
22#include "qgslogger.h"
23#include "qgsvariantutils.h"
24
25#include <QPaintEvent>
26#include <QPainter>
27#include <QRect>
28
29#include "moc_qgsslider.cpp"
30
31QgsSlider::QgsSlider( QWidget *parent )
32 : QSlider( parent )
33{
34 setMinimumSize( QSize( 100, 40 ) );
35}
36
37QgsSlider::QgsSlider( Qt::Orientation orientation, QWidget *parent )
38 : QSlider( orientation, parent )
39{
40 setMinimumSize( QSize( 100, 40 ) );
41}
42
43void QgsSlider::paintEvent( QPaintEvent *event )
44{
45 QSlider::paintEvent( event );
46 QPainter painter( this );
47 const QRect rect = geometry();
48 painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
49 painter.drawText( QRectF( 0, rect.height() * 0.5, rect.width(), rect.height() ), Qt::AlignHCenter, variantValue().toString(), nullptr );
50 painter.end();
51}
52
53void QgsSlider::setMinimum( const QVariant &min )
54{
55 mMin = min;
56 update();
57}
58
59void QgsSlider::setMaximum( const QVariant &max )
60{
61 mMax = max;
62 update();
63}
64
65void QgsSlider::setSingleStep( const QVariant &step )
66{
67 mStep = step;
68 update();
69}
70
71void QgsSlider::setValue( const QVariant &value )
72{
73 mValue = value;
74 update();
75}
76
77void QgsSlider::update()
78{
80 return;
81
82 if ( QgsVariantUtils::isNull( mValue ) )
83 mValue = mMin;
84
85 if ( mMin.userType() == QMetaType::Type::Int && mMax.userType() == QMetaType::Type::Int && mStep.userType() == QMetaType::Type::Int && mValue.userType() == QMetaType::Type::Int )
86 {
87 QSlider::setMinimum( mMin.toInt() );
88 QSlider::setMaximum( mMax.toInt() );
89 QSlider::setSingleStep( mStep.toInt() );
90 QSlider::setValue( mValue.toInt() );
91 }
92 else
93 {
94 if ( minimum() != 0 )
95 QSlider::setMinimum( 0 );
96
97 const int max = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
98 if ( maximum() != max )
99 QSlider::setMaximum( max );
100
101 if ( singleStep() != 1 )
102 QSlider::setSingleStep( 1 );
103
104 QSlider::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
105 }
106
107 connect( this, &QSlider::valueChanged, this, &QgsSlider::onValueChanged );
108}
109
111{
112 return mValue;
113}
114
115void QgsSlider::onValueChanged( int value )
116{
118 {
119 mValue = QVariant();
120 }
121 else if ( mMin.userType() == QMetaType::Type::Int && mMax.userType() == QMetaType::Type::Int && mStep.userType() == QMetaType::Type::Int && mValue.userType() == QMetaType::Type::Int )
122 {
123 mValue = value;
124 }
125 else
126 {
127 mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
128 }
129
130 emit valueChanged( mValue );
131}
void valueChanged(const QVariant &)
void setSingleStep(const QVariant &step)
Definition qgsslider.cpp:65
void setMinimum(const QVariant &min)
Definition qgsslider.cpp:53
QgsSlider(QWidget *parent=nullptr)
Constructor for QgsSlider.
Definition qgsslider.cpp:31
void setValue(const QVariant &value)
Definition qgsslider.cpp:71
void setMaximum(const QVariant &max)
Definition qgsslider.cpp:59
void paintEvent(QPaintEvent *event) override
Definition qgsslider.cpp:43
QVariant variantValue() const
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.