19 #include <QApplication> 20 #include <QMouseEvent> 22 #include <QPushButton> 24 #include <QDoubleSpinBox> 27 : QToolButton( parent )
31 setMinimumSize( QSize( 24, 24 ) );
32 setMaximumWidth( fontMetrics().width( QStringLiteral(
"000" ) ) );
35 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
40 if ( mLocked != locked )
44 void QgsRatioLockButton::buttonClicked()
54 void QgsRatioLockButton::widthSpinBoxChanged(
double value )
57 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
63 double oldRatio = mPrevHeight / mPrevWidth;
64 mUpdatingRatio =
true;
65 mHeightSpinBox->setValue( oldRatio * value );
66 mUpdatingRatio =
false;
70 void QgsRatioLockButton::heightSpinBoxChanged(
double value )
73 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
79 double oldRatio = mPrevWidth / mPrevHeight;
80 mUpdatingRatio =
true;
81 mWidthSpinBox->setValue( oldRatio * value );
82 mUpdatingRatio =
false;
88 if ( e->type() == QEvent::EnabledChange )
92 QToolButton::changeEvent( e );
98 QToolButton::showEvent( e );
103 QToolButton::resizeEvent( event );
107 void QgsRatioLockButton::drawButton()
109 QSize currentIconSize;
112 currentIconSize = QSize( width() - 10, height() - 6 );
114 currentIconSize = QSize( width() - 10, height() - 12 );
117 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
123 pm = QPixmap( currentIconSize );
124 pm.fill( Qt::transparent );
127 QPen pen = ( QColor( 136, 136, 136 ) );
130 painter.begin( &pm );
131 painter.setPen( pen );
133 painter.drawLine( 1, 1, currentIconSize.width() / 2, 1 );
134 painter.drawLine( currentIconSize.width() / 2, 1, currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 );
135 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13, currentIconSize.width() / 2, currentIconSize.height() - 2 );
136 painter.drawLine( currentIconSize.width() / 2, currentIconSize.height() - 2, 1, currentIconSize.height() - 2 );
138 QImage image( mLocked ? QStringLiteral(
":/images/themes/default/lockedGray.svg" ) : QStringLiteral(
":/images/themes/default/unlockedGray.svg" ) );
139 painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image, QRectF( 0, 0, 16, 16 ) );
143 setIconSize( currentIconSize );
149 mWidthSpinBox = widget;
150 mPrevWidth = widget->value();
151 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
156 mHeightSpinBox = widget;
157 mPrevHeight = widget->value();
158 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
163 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
164 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)