QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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(), nullptr );
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 
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 
112 {
113  if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
114  {
115  mValue = QVariant();
116  }
117  else if ( mMin.type() == QVariant::Int &&
118  mMax.type() == QVariant::Int &&
119  mStep.type() == QVariant::Int &&
120  mValue.type() == QVariant::Int )
121  {
122  mValue = value;
123  }
124  else if ( mMin.type() == QVariant::Double &&
125  mMax.type() == QVariant::Double &&
126  mStep.type() == QVariant::Double &&
127  mValue.type() == QVariant::Double )
128  {
129  mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
130  }
131  emit valueChanged( mValue );
132 }
void setMaximum(const QVariant &max)
Definition: qgsdial.cpp:48
const QPalette & palette() const
bool end()
QgsDial(QWidget *parent=nullptr)
Definition: qgsdial.cpp:26
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 valueChanged(const QVariant &)
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)
void drawText(const QPointF &position, const QString &text)
void setMinimum(int)
QRect rect() const
virtual bool event(QEvent *e)
void setValue(const QVariant &value)
Definition: qgsdial.cpp:60
int value() const
int width() const
void setSingleStep(int)
virtual void paintEvent(QPaintEvent *pe)
double toDouble(bool *ok) const
QVariant variantValue() const
Definition: qgsdial.cpp:106
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
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