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