18 #include <QApplication> 19 #include <QMouseEvent> 21 #include <QPushButton> 23 #include <QDoubleSpinBox> 26 : QToolButton( parent )
30 setMinimumSize( QSize( 24, 24 ) );
31 setMaximumWidth( fontMetrics().width( QStringLiteral(
"000" ) ) );
34 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
39 if ( mLocked != locked )
43 void QgsRatioLockButton::buttonClicked()
53 void QgsRatioLockButton::widthSpinBoxChanged(
double value )
56 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
62 double oldRatio = mPrevHeight / mPrevWidth;
63 mUpdatingRatio =
true;
64 mHeightSpinBox->setValue( oldRatio * value );
65 mUpdatingRatio =
false;
69 void QgsRatioLockButton::heightSpinBoxChanged(
double value )
72 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
78 double oldRatio = mPrevWidth / mPrevHeight;
79 mUpdatingRatio =
true;
80 mWidthSpinBox->setValue( oldRatio * value );
81 mUpdatingRatio =
false;
87 if ( e->type() == QEvent::EnabledChange )
91 QToolButton::changeEvent( e );
97 QToolButton::showEvent( e );
102 QToolButton::resizeEvent( event );
106 void QgsRatioLockButton::drawButton()
108 QSize currentIconSize;
111 currentIconSize = QSize( width() - 10, height() - 6 );
113 currentIconSize = QSize( width() - 10, height() - 12 );
116 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
122 pm = QPixmap( currentIconSize );
123 pm.fill( Qt::transparent );
126 QPen pen = ( QColor( 136, 136, 136 ) );
129 painter.begin( &pm );
130 painter.setPen( pen );
132 painter.drawLine( 1, 1, currentIconSize.width() / 2, 1 );
133 painter.drawLine( currentIconSize.width() / 2, 1, currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 );
134 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13, currentIconSize.width() / 2, currentIconSize.height() - 2 );
135 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() - 2, 1, currentIconSize.height() - 2 );
137 QImage image( mLocked ? QStringLiteral(
":/images/themes/default/lockedGray.svg" ) : QStringLiteral(
":/images/themes/default/unlockedGray.svg" ) );
138 painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image, QRectF( 0, 0, 16, 16 ) );
142 setIconSize( currentIconSize );
148 mWidthSpinBox = widget;
149 mPrevWidth = widget->value();
150 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
155 mHeightSpinBox = widget;
156 mPrevHeight = widget->value();
157 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
162 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
163 mPrevHeight = mHeightSpinBox ? mHeightSpinBox->value() : 0.0;
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)