19 #include <QApplication> 20 #include <QMouseEvent> 22 #include <QPushButton> 24 #include <QDoubleSpinBox> 27 : QToolButton( parent )
29 setMinimumSize( QSize( 24, 24 ) );
30 setMaximumWidth( fontMetrics().width( QStringLiteral(
"000" ) ) );
33 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
38 if ( mLocked != locked )
42 void QgsRatioLockButton::buttonClicked()
52 void QgsRatioLockButton::widthSpinBoxChanged(
double value )
55 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
61 double oldRatio = mPrevHeight / mPrevWidth;
62 mUpdatingRatio =
true;
63 mHeightSpinBox->setValue( oldRatio * value );
64 mUpdatingRatio =
false;
68 void QgsRatioLockButton::heightSpinBoxChanged(
double value )
71 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
77 double oldRatio = mPrevWidth / mPrevHeight;
78 mUpdatingRatio =
true;
79 mWidthSpinBox->setValue( oldRatio * value );
80 mUpdatingRatio =
false;
86 if ( e->type() == QEvent::EnabledChange )
90 QToolButton::changeEvent( e );
96 QToolButton::showEvent( e );
101 QToolButton::resizeEvent( event );
105 void QgsRatioLockButton::drawButton()
107 QSize currentIconSize;
110 currentIconSize = QSize( width() - 10, height() - 6 );
112 currentIconSize = QSize( width() - 10, height() - 12 );
115 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
121 pm = QPixmap( currentIconSize );
122 pm.fill( Qt::transparent );
125 QPen pen = ( QColor( 136, 136, 136 ) );
128 painter.begin( &pm );
129 painter.setPen( pen );
131 painter.drawLine( 1, 1, currentIconSize.width() / 2, 1 );
132 painter.drawLine( currentIconSize.width() / 2, 1, currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 );
133 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13, currentIconSize.width() / 2, currentIconSize.height() - 2 );
134 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() - 2, 1, currentIconSize.height() - 2 );
136 QImage image( mLocked ? QStringLiteral(
":/images/themes/default/lockedGray.svg" ) : QStringLiteral(
":/images/themes/default/unlockedGray.svg" ) );
137 painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image, QRectF( 0, 0, 16, 16 ) );
141 setIconSize( currentIconSize );
147 mWidthSpinBox = widget;
148 mPrevWidth = widget->value();
149 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
154 mHeightSpinBox = widget;
155 mPrevHeight = widget->value();
156 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
161 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
162 mPrevHeight = mHeightSpinBox ? mHeightSpinBox->value() : 0.0;
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)