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