/
qgsratiolockbutton.cpp
158 lines (131 loc) · 4.77 KB
/
qgsratiolockbutton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/***************************************************************************
qgsratiolockbutton.cpp - Lock button
--------------------------------------
Date : July, 2017
Copyright : (C) 2017 by Mathieu Pellerin
Email : nirvn dot asia at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsratiolockbutton.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QPushButton>
#include <QWidget>
#include <QDoubleSpinBox>
QgsRatioLockButton::QgsRatioLockButton( QWidget *parent )
: QToolButton( parent )
, mLocked( false )
{
setMinimumSize( QSize( 24, 24 ) );
setMaximumWidth( fontMetrics().width( "000" ) );
setCheckable( true );
setAutoRaise( true );
connect( this, &QPushButton::clicked, this, &QgsRatioLockButton::buttonClicked );
}
void QgsRatioLockButton::setLocked( const bool locked )
{
if ( mLocked != locked )
buttonClicked();
}
void QgsRatioLockButton::buttonClicked()
{
mLocked = !mLocked;
setDown( mLocked );
emit lockChanged( mLocked );
drawButton();
}
void QgsRatioLockButton::widthSpinBoxChanged( double value )
{
if ( mUpdatingRatio || qgsDoubleNear( value, 0.0 ) || qgsDoubleNear( mPrevWidth, 0.0 )
|| qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
{
mPrevWidth = value;
return;
}
double oldRatio = mPrevHeight / mPrevWidth;
mUpdatingRatio = true;
mHeightSpinBox->setValue( oldRatio * value );
mUpdatingRatio = false;
mPrevWidth = value;
}
void QgsRatioLockButton::heightSpinBoxChanged( double value )
{
if ( mUpdatingRatio || qgsDoubleNear( value, 0.0 ) || qgsDoubleNear( mPrevWidth, 0.0 )
|| qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
{
mPrevHeight = value;
return;
}
double oldRatio = mPrevWidth / mPrevHeight;
mUpdatingRatio = true;
mWidthSpinBox->setValue( oldRatio * value );
mUpdatingRatio = false;
mPrevHeight = value;
}
void QgsRatioLockButton::changeEvent( QEvent *e )
{
if ( e->type() == QEvent::EnabledChange )
{
drawButton();
}
QToolButton::changeEvent( e );
}
void QgsRatioLockButton::showEvent( QShowEvent *e )
{
drawButton();
QToolButton::showEvent( e );
}
void QgsRatioLockButton::resizeEvent( QResizeEvent *event )
{
QToolButton::resizeEvent( event );
drawButton();
}
void QgsRatioLockButton::drawButton()
{
QSize currentIconSize;
#ifdef Q_OS_WIN
currentIconSize = QSize( width() - 10, height() - 6 );
#else
currentIconSize = QSize( width() - 10, height() - 12 );
#endif
if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
{
return;
}
QPixmap pm;
pm = QPixmap( currentIconSize );
pm.fill( Qt::transparent );
QPainter painter;
QPen pen = ( QColor( 136, 136, 136 ) );
pen.setWidth( 2 );
painter.begin( &pm );
painter.setPen( pen );
painter.drawLine( 1, 1, currentIconSize.width() / 2, 1 );
painter.drawLine( currentIconSize.width() / 2, 1, currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 );
painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13, currentIconSize.width() / 2, currentIconSize.height() - 2 );
painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() - 2, 1, currentIconSize.height() - 2 );
QImage image( mLocked ? QStringLiteral( ":/images/themes/default/lockedGray.svg" ) : QStringLiteral( ":/images/themes/default/unlockedGray.svg" ) );
painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image, QRectF( 0, 0, 16, 16 ) );
painter.end();
setIconSize( currentIconSize );
setIcon( pm );
}
void QgsRatioLockButton::setWidthSpinBox( QDoubleSpinBox *widget )
{
mWidthSpinBox = widget;
mPrevWidth = widget->value();
connect( mWidthSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsRatioLockButton::widthSpinBoxChanged );
}
void QgsRatioLockButton::setHeightSpinBox( QDoubleSpinBox *widget )
{
mHeightSpinBox = widget;
mPrevHeight = widget->value();
connect( mHeightSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsRatioLockButton::heightSpinBoxChanged );
}