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