19 #include <QApplication>
20 #include <QMouseEvent>
22 #include <QPushButton>
24 #include <QDoubleSpinBox>
27 : QToolButton( parent )
29 setMinimumSize( QSize( 24, 24 ) );
30 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
31 setMaximumWidth( fontMetrics().width( QStringLiteral(
"000" ) ) );
33 setMaximumWidth( fontMetrics().horizontalAdvance(
'0' ) * 3 );
37 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
46 void QgsRatioLockButton::buttonClicked()
56 void QgsRatioLockButton::widthSpinBoxChanged(
double value )
59 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
65 double oldRatio = mPrevHeight / mPrevWidth;
66 mUpdatingRatio =
true;
67 mHeightSpinBox->setValue( oldRatio * value );
68 mUpdatingRatio =
false;
72 void QgsRatioLockButton::heightSpinBoxChanged(
double value )
75 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
81 double oldRatio = mPrevWidth / mPrevHeight;
82 mUpdatingRatio =
true;
83 mWidthSpinBox->setValue( oldRatio * value );
84 mUpdatingRatio =
false;
90 if ( e->type() == QEvent::EnabledChange )
94 QToolButton::changeEvent( e );
100 QToolButton::showEvent( e );
105 QToolButton::resizeEvent( event );
109 void QgsRatioLockButton::drawButton()
111 QSize currentIconSize;
114 currentIconSize = QSize( width() - 10, height() - 6 );
116 currentIconSize = QSize( width() - 10, height() - 12 );
119 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
125 pm = QPixmap( currentIconSize );
126 pm.fill( Qt::transparent );
129 QPen pen = ( QColor( 136, 136, 136 ) );
132 painter.begin( &pm );
133 painter.setPen( pen );
135 painter.drawLine( 1, 1, currentIconSize.width() / 2, 1 );
136 painter.drawLine( currentIconSize.width() / 2, 1, currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 );
137 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13, currentIconSize.width() / 2, currentIconSize.height() - 2 );
138 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() - 2, 1, currentIconSize.height() - 2 );
140 QImage image( mLocked ? QStringLiteral(
":/images/themes/default/lockedGray.svg" ) : QStringLiteral(
":/images/themes/default/unlockedGray.svg" ) );
141 painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image, QRectF( 0, 0, 16, 16 ) );
145 setIconSize( currentIconSize );
151 mWidthSpinBox = widget;
152 mPrevWidth = widget->value();
153 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
158 mHeightSpinBox = widget;
159 mPrevHeight = widget->value();
160 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
165 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
166 mPrevHeight = mHeightSpinBox ? mHeightSpinBox->value() : 0.0;