QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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(), 0 );
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 
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 )
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 
116 void QgsSlider::valueChanged( int value )
117 {
118  if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
119  {
120  mValue = QVariant();
121  return;
122  }
123 
124  if ( mMin.type() == QVariant::Int &&
125  mMax.type() == QVariant::Int &&
126  mStep.type() == QVariant::Int &&
127  mValue.type() == QVariant::Int )
128  {
129  mValue = value;
130  return;
131  }
132 
133  if ( mMin.type() == QVariant::Double &&
134  mMax.type() == QVariant::Double &&
135  mStep.type() == QVariant::Double &&
136  mValue.type() == QVariant::Double )
137  {
138  mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
139  return;
140  }
141 }
void setValue(const QVariant &value)
Definition: qgsslider.cpp:65
const QPalette & palette() const
bool end()
void valueChanged(QVariant)
void setMaximum(const QVariant &max)
Definition: qgsslider.cpp:53
void setSingleStep(const QVariant &step)
Definition: qgsslider.cpp:59
int height() const
QgsSlider(QWidget *parent=0)
Definition: qgsslider.cpp:26
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 &)
int toInt(bool *ok) const
bool isNull() const
void setPen(const QColor &color)
QVariant variantValue() const
Definition: qgsslider.cpp:111
void drawText(const QPointF &position, const QString &text)
virtual void paintEvent(QPaintEvent *ev)
void setMinimum(int)
QRect rect() const
int value() const
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)
QString toString() const