QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  else
90  {
91  if ( minimum() != 0 )
92  QSlider::setMinimum( 0 );
93 
94  const int max = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
95  if ( maximum() != max )
96  QSlider::setMaximum( max );
97 
98  if ( singleStep() != 1 )
99  QSlider::setSingleStep( 1 );
100 
101  QSlider::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
102  }
103 
104  connect( this, &QSlider::valueChanged, this, &QgsSlider::onValueChanged );
105 }
106 
107 QVariant QgsSlider::variantValue() const
108 {
109  return mValue;
110 }
111 
112 void QgsSlider::onValueChanged( int value )
113 {
114  if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
115  {
116  mValue = QVariant();
117  }
118  else if ( mMin.type() == QVariant::Int &&
119  mMax.type() == QVariant::Int &&
120  mStep.type() == QVariant::Int &&
121  mValue.type() == QVariant::Int )
122  {
123  mValue = value;
124  }
125  else
126  {
127  mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
128  }
129 
130  emit valueChanged( mValue );
131 }
QgsSlider::setMinimum
void setMinimum(const QVariant &min)
Definition: qgsslider.cpp:47
QgsSlider::variantValue
QVariant variantValue() const
Definition: qgsslider.cpp:107
QgsSlider::setMaximum
void setMaximum(const QVariant &max)
Definition: qgsslider.cpp:53
QgsSlider::valueChanged
void valueChanged(const QVariant &)
QgsSlider::setSingleStep
void setSingleStep(const QVariant &step)
Definition: qgsslider.cpp:59
QgsSlider::QgsSlider
QgsSlider(QWidget *parent=nullptr)
Constructor for QgsSlider.
Definition: qgsslider.cpp:26
QgsSlider::setValue
void setValue(const QVariant &value)
Definition: qgsslider.cpp:65
QgsSlider::paintEvent
void paintEvent(QPaintEvent *event) override
Definition: qgsslider.cpp:36
qgslogger.h
qgsslider.h