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