QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 ) : QDial( parent )
29{
30 setMinimumSize( QSize( 50, 50 ) );
31}
32
33void QgsDial::paintEvent( QPaintEvent *event )
34{
35 QDial::paintEvent( event );
36 QPainter painter( this );
37 const QRect rect = geometry();
38 painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
39 painter.drawText( QRectF( 0, rect.height() * 0.65, rect.width(), rect.height() ),
40 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 &&
77 mMax.userType() == QMetaType::Type::Int &&
78 mStep.userType() == QMetaType::Type::Int &&
79 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 &&
88 mMax.userType() == QMetaType::Type::Double &&
89 mStep.userType() == QMetaType::Type::Double &&
90 mValue.userType() == QMetaType::Type::Double )
91 {
92 if ( minimum() != 0 )
93 QDial::setMinimum( 0 );
94
95 const int max = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
96 if ( maximum() != max )
97 QDial::setMaximum( max );
98
99 if ( singleStep() != 1 )
100 QDial::setSingleStep( 1 );
101
102 QDial::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
103 }
104
105 connect( this, static_cast < void ( QDial::* )( int ) > ( &QDial::valueChanged ), this, &QgsDial::onValueChanged );
106}
107
108QVariant QgsDial::variantValue() const
109{
110 return mValue;
111}
112
113void QgsDial::onValueChanged( int value )
114{
116 {
117 mValue = QVariant();
118 }
119 else if ( mMin.userType() == QMetaType::Type::Int &&
120 mMax.userType() == QMetaType::Type::Int &&
121 mStep.userType() == QMetaType::Type::Int &&
122 mValue.userType() == QMetaType::Type::Int )
123 {
124 mValue = value;
125 }
126 else if ( mMin.userType() == QMetaType::Type::Double &&
127 mMax.userType() == QMetaType::Type::Double &&
128 mStep.userType() == QMetaType::Type::Double &&
129 mValue.userType() == QMetaType::Type::Double )
130 {
131 mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
132 }
133 emit valueChanged( mValue );
134}
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:108
void paintEvent(QPaintEvent *event) override
Definition qgsdial.cpp:33
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.