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