QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 <cmath>
25 
26 QgsDial::QgsDial( QWidget *parent ) : QDial( parent )
27 {
28  setMinimumSize( QSize( 50, 50 ) );
29 }
30 
31 void QgsDial::paintEvent( QPaintEvent *event )
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 
42 void QgsDial::setMinimum( const QVariant &min )
43 {
44  mMin = min;
45  update();
46 }
47 
48 void QgsDial::setMaximum( const QVariant &max )
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 = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
94  if ( maximum() != max )
95  QDial::setMaximum( max );
96 
97  if ( singleStep() != 1 )
98  QDial::setSingleStep( 1 );
99 
100  QDial::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
101  }
102 
103  connect( this, static_cast < void ( QDial::* )( int ) > ( &QDial::valueChanged ), this, &QgsDial::onValueChanged );
104 }
105 
106 QVariant QgsDial::variantValue() const
107 {
108  return mValue;
109 }
110 
111 void QgsDial::onValueChanged( int value )
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 }
QgsDial::paintEvent
void paintEvent(QPaintEvent *event) override
Definition: qgsdial.cpp:31
QgsDial::setSingleStep
void setSingleStep(const QVariant &step)
Definition: qgsdial.cpp:54
QgsDial::variantValue
QVariant variantValue() const
Definition: qgsdial.cpp:106
QgsDial::setMaximum
void setMaximum(const QVariant &max)
Definition: qgsdial.cpp:48
QgsDial::QgsDial
QgsDial(QWidget *parent=nullptr)
Constructor for QgsDial.
Definition: qgsdial.cpp:26
QgsDial::setMinimum
void setMinimum(const QVariant &min)
Definition: qgsdial.cpp:42
QgsDial::setValue
void setValue(const QVariant &value)
Definition: qgsdial.cpp:60
QgsDial::valueChanged
void valueChanged(const QVariant &)
qgsdial.h
qgslogger.h