QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
25 QgsDial::QgsDial( QWidget *parent ) : QDial( parent )
26 {
27  setMinimumSize( QSize( 50, 50 ) );
28 }
29 
30 void QgsDial::paintEvent( QPaintEvent *event )
31 {
32  QDial::paintEvent( event );
33  QPainter painter( this );
34  QRect rect = geometry();
35  painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
36  painter.drawText( QRectF( 0, rect.height() * 0.65, rect.width(), rect.height() ),
37  Qt::AlignHCenter, variantValue().toString(), nullptr );
38  painter.end();
39 }
40 
41 void QgsDial::setMinimum( const QVariant &min )
42 {
43  mMin = min;
44  update();
45 }
46 
47 void QgsDial::setMaximum( const QVariant &max )
48 {
49  mMax = max;
50  update();
51 }
52 
53 void QgsDial::setSingleStep( const QVariant &step )
54 {
55  mStep = step;
56  update();
57 }
58 
59 void QgsDial::setValue( const QVariant &value )
60 {
61  mValue = value;
62  update();
63 }
64 
65 void QgsDial::update()
66 {
67  if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
68  return;
69 
70  if ( mValue.isNull() )
71  mValue = mMin;
72 
73  if ( mMin.type() == QVariant::Int &&
74  mMax.type() == QVariant::Int &&
75  mStep.type() == QVariant::Int &&
76  mValue.type() == QVariant::Int )
77  {
78  QDial::setMinimum( mMin.toInt() );
79  QDial::setMaximum( mMax.toInt() );
80  QDial::setSingleStep( mStep.toInt() );
81  QDial::setValue( mValue.toInt() );
82  }
83 
84  if ( mMin.type() == QVariant::Double &&
85  mMax.type() == QVariant::Double &&
86  mStep.type() == QVariant::Double &&
87  mValue.type() == QVariant::Double )
88  {
89  if ( minimum() != 0 )
90  QDial::setMinimum( 0 );
91 
92  int max = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
93  if ( maximum() != max )
94  QDial::setMaximum( max );
95 
96  if ( singleStep() != 1 )
97  QDial::setSingleStep( 1 );
98 
99  QDial::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
100  }
101 
102  connect( this, static_cast < void ( QDial::* )( int ) > ( &QDial::valueChanged ), this, &QgsDial::onValueChanged );
103 }
104 
105 QVariant QgsDial::variantValue() const
106 {
107  return mValue;
108 }
109 
110 void QgsDial::onValueChanged( int value )
111 {
112  if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
113  {
114  mValue = QVariant();
115  }
116  else if ( mMin.type() == QVariant::Int &&
117  mMax.type() == QVariant::Int &&
118  mStep.type() == QVariant::Int &&
119  mValue.type() == QVariant::Int )
120  {
121  mValue = value;
122  }
123  else if ( mMin.type() == QVariant::Double &&
124  mMax.type() == QVariant::Double &&
125  mStep.type() == QVariant::Double &&
126  mValue.type() == QVariant::Double )
127  {
128  mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
129  }
130  emit valueChanged( mValue );
131 }
void setMaximum(const QVariant &max)
Definition: qgsdial.cpp:47
QgsDial(QWidget *parent=nullptr)
Constructor for QgsDial.
Definition: qgsdial.cpp:25
void paintEvent(QPaintEvent *event) override
Definition: qgsdial.cpp:30
void setMinimum(const QVariant &min)
Definition: qgsdial.cpp:41
void setSingleStep(const QVariant &step)
Definition: qgsdial.cpp:53
void valueChanged(const QVariant &)
void setValue(const QVariant &value)
Definition: qgsdial.cpp:59
QVariant variantValue() const
Definition: qgsdial.cpp:105