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