22#include <QApplication>
23#include <QDoubleSpinBox>
29#include "moc_qgsratiolockbutton.cpp"
32 : QToolButton( parent )
34 setMinimumSize( QSize( 24, 24 ) );
35 setMaximumWidth( fontMetrics().horizontalAdvance(
'0' ) * 3 );
38 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
47void QgsRatioLockButton::buttonClicked()
50 setChecked( mLocked );
57void QgsRatioLockButton::widthSpinBoxChanged(
double value )
60 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
66 const double oldRatio = mPrevHeight / mPrevWidth;
67 mUpdatingRatio =
true;
68 mHeightSpinBox->setValue( oldRatio * value );
69 mUpdatingRatio =
false;
73void QgsRatioLockButton::heightSpinBoxChanged(
double value )
76 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
82 const double oldRatio = mPrevWidth / mPrevHeight;
83 mUpdatingRatio =
true;
84 mWidthSpinBox->setValue( oldRatio * value );
85 mUpdatingRatio =
false;
91 if ( e->type() == QEvent::EnabledChange )
95 QToolButton::changeEvent( e );
101 QToolButton::showEvent( e );
106 QToolButton::resizeEvent( event );
110void QgsRatioLockButton::drawButton()
112 QSize currentIconSize;
115 currentIconSize = QSize( width() - 10, height() - 6 );
117 currentIconSize = QSize( width() - 10, height() - 12 );
120 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
125 const double pixelRatio = devicePixelRatioF();
126 QPixmap pm( currentIconSize * pixelRatio );
127 pm.setDevicePixelRatio( pixelRatio );
128 pm.fill( Qt::transparent );
131 QPen pen = QPen( QColor( 136, 136, 136 ) );
134 painter.begin( &pm );
135 painter.setRenderHint( QPainter::Antialiasing,
true );
136 painter.setPen( pen );
138 painter.drawLine( QPointF( 1, 1 ), QPointF( currentIconSize.width() / 2, 1 ) );
139 painter.drawLine( QPointF( currentIconSize.width() / 2, 1 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 ) );
140 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ) );
141 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ), QPointF( 1, currentIconSize.height() - 2 ) );
143 const QString imageSource = mLocked ? QStringLiteral(
":/images/themes/default/lockedGray.svg" ) : QStringLiteral(
":/images/themes/default/unlockedGray.svg" );
144 bool fitsInCache =
false;
146 imageSource, 16 * pixelRatio, QColor(), QColor(), 0, 1, fitsInCache
148 image.setDevicePixelRatio( pixelRatio );
149 painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image );
153 setIconSize( currentIconSize );
159 mWidthSpinBox = widget;
160 mPrevWidth = widget->value();
161 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
166 mHeightSpinBox = widget;
167 mPrevHeight = widget->value();
168 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
173 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
174 mPrevHeight = mHeightSpinBox ? mHeightSpinBox->value() : 0.0;
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
QImage svgAsImage(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > ¶meters=QMap< QString, QString >())
Returns an SVG drawing as a QImage.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).