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