QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsdial.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdial.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 "qgsdial.h"
19 #include "qgslogger.h"
20 
21 #include <QPaintEvent>
22 #include <QPainter>
23 #include <QRect>
24 #include <qmath.h>
25 
26 QgsDial::QgsDial( QWidget *parent ) : QDial( parent )
27 {
28  setMinimumSize( QSize( 50, 50 ) );
29 }
30 
32 {
33  QDial::paintEvent( event );
34  QPainter painter( this );
35  QRect rect = geometry();
36  painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
37  painter.drawText( QRectF( 0, rect.height() * 0.65, rect.width(), rect.height() ),
38  Qt::AlignHCenter, variantValue().toString(), 0 );
39  painter.end();
40 }
41 
43 {
44  mMin = min;
45  update();
46 }
47 
49 {
50  mMax = max;
51  update();
52 }
53 
54 void QgsDial::setSingleStep( const QVariant &step )
55 {
56  mStep = step;
57  update();
58 }
59 
60 void QgsDial::setValue( const QVariant &value )
61 {
62  mValue = value;
63  update();
64 }
65 
66 void QgsDial::update()
67 {
68  if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
69  return;
70 
71  if ( mValue.isNull() )
72  mValue = mMin;
73 
74  if ( mMin.type() == QVariant::Int &&
75  mMax.type() == QVariant::Int &&
76  mStep.type() == QVariant::Int &&
77  mValue.type() == QVariant::Int )
78  {
79  QDial::setMinimum( mMin.toInt() );
80  QDial::setMaximum( mMax.toInt() );
81  QDial::setSingleStep( mStep.toInt() );
82  QDial::setValue( mValue.toInt() );
83  }
84 
85  if ( mMin.type() == QVariant::Double &&
86  mMax.type() == QVariant::Double &&
87  mStep.type() == QVariant::Double &&
88  mValue.type() == QVariant::Double )
89  {
90  if ( minimum() != 0 )
91  QDial::setMinimum( 0 );
92 
93  int max = qCeil(( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
94  if ( maximum() != max )
95  QDial::setMaximum( max );
96 
97  if ( singleStep() != 1 )
99 
100  QDial::setValue( qCeil(( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
101  }
102 
103  connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
104 }
105 
107 {
108  return mValue;
109 }
110 
111 void QgsDial::valueChanged( int value )
112 {
113  if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
114  {
115  mValue = QVariant();
116  return;
117  }
118 
119  if ( mMin.type() == QVariant::Int &&
120  mMax.type() == QVariant::Int &&
121  mStep.type() == QVariant::Int &&
122  mValue.type() == QVariant::Int )
123  {
124  mValue = value;
125  return;
126  }
127 
128  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  return;
135  }
136 }
void setMaximum(const QVariant &max)
Definition: qgsdial.cpp:48
const QPalette & palette() const
bool end()
QgsDial(QWidget *parent=0)
Definition: qgsdial.cpp:26
void valueChanged(QVariant)
int height() const
virtual void paintEvent(QPaintEvent *event) override
Definition: qgsdial.cpp:31
void setMinimum(const QVariant &min)
Definition: qgsdial.cpp:42
void setSingleStep(const QVariant &step)
Definition: qgsdial.cpp:54
const QRect & geometry() const
void setMinimumSize(const QSize &)
int toInt(bool *ok) const
bool isNull() const
void setPen(const QColor &color)
QVariant variantValue() const
Definition: qgsdial.cpp:106
void drawText(const QPointF &position, const QString &text)
void setMinimum(int)
QRect rect() const
void setValue(const QVariant &value)
Definition: qgsdial.cpp:60
int min(int a, int b)
Definition: util.h:93
int value() const
int width() const
void setSingleStep(int)
virtual void paintEvent(QPaintEvent *pe)
double toDouble(bool *ok) const
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
int max(int a, int b)
Definition: util.h:87