QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <qmath.h>
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 
37 {
38  QSlider::paintEvent( event );
39  QPainter painter( this );
40  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 
48 {
49  mMin = min;
50  update();
51 }
52 
54 {
55  mMax = max;
56  update();
57 }
58 
60 {
61  mStep = step;
62  update();
63 }
64 
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 )
97 
98  int max = qCeil(( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
99  if ( maximum() != max )
100  QSlider::setMaximum( max );
101 
102  if ( singleStep() != 1 )
104 
105  QSlider::setValue( qCeil(( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
106  }
107 
108  connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
109 }
110 
112 {
113  return mValue;
114 }
115 
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 setValue(const QVariant &value)
Definition: qgsslider.cpp:65
virtual bool event(QEvent *event)
const QPalette & palette() const
bool end()
void setMaximum(const QVariant &max)
Definition: qgsslider.cpp:53
void setSingleStep(const QVariant &step)
Definition: qgsslider.cpp:59
int height() const
void setMinimum(const QVariant &min)
Definition: qgsslider.cpp:47
const QRect & geometry() const
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
void setMinimumSize(const QSize &)
QgsSlider(QWidget *parent=nullptr)
Definition: qgsslider.cpp:26
Qt::Orientation orientation() const
int toInt(bool *ok) const
bool isNull() const
void setPen(const QColor &color)
void drawText(const QPointF &position, const QString &text)
virtual void paintEvent(QPaintEvent *ev)
void setMinimum(int)
QRect rect() const
int value() const
void valueChanged(const QVariant &)
int width() const
void setSingleStep(int)
double toDouble(bool *ok) const
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
virtual void paintEvent(QPaintEvent *event) override
Definition: qgsslider.cpp:36
Type type() const
void setMaximum(int)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QString toString() const
QVariant variantValue() const
Definition: qgsslider.cpp:111