22#include <QApplication>
23#include <QDoubleSpinBox>
30#include "moc_qgsratiolockbutton.cpp"
32using namespace Qt::StringLiterals;
35 : QToolButton( parent )
37 setMinimumSize( QSize( 24, 24 ) );
38 setMaximumWidth( fontMetrics().horizontalAdvance(
'0' ) * 3 );
41 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
50void QgsRatioLockButton::buttonClicked()
53 setChecked( mLocked );
60void QgsRatioLockButton::widthSpinBoxChanged(
double value )
63 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
69 const double oldRatio = mPrevHeight / mPrevWidth;
70 mUpdatingRatio =
true;
71 mHeightSpinBox->setValue( oldRatio * value );
72 mUpdatingRatio =
false;
76void QgsRatioLockButton::heightSpinBoxChanged(
double value )
79 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
85 const double oldRatio = mPrevWidth / mPrevHeight;
86 mUpdatingRatio =
true;
87 mWidthSpinBox->setValue( oldRatio * value );
88 mUpdatingRatio =
false;
94 if ( e->type() == QEvent::EnabledChange )
98 QToolButton::changeEvent( e );
104 QToolButton::showEvent( e );
109 QToolButton::resizeEvent( event );
113void QgsRatioLockButton::drawButton()
115 QSize currentIconSize;
118 currentIconSize = QSize( width() - 10, height() - 6 );
120 currentIconSize = QSize( width() - 10, height() - 12 );
123 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
128 const double pixelRatio = devicePixelRatioF();
129 QPixmap pm( currentIconSize * pixelRatio );
130 pm.setDevicePixelRatio( pixelRatio );
131 pm.fill( Qt::transparent );
134 QPen pen = QPen( QColor( 136, 136, 136 ) );
137 painter.begin( &pm );
138 painter.setRenderHint( QPainter::Antialiasing,
true );
139 painter.setPen( pen );
141 painter.drawLine( QPointF( 1, 1 ), QPointF( currentIconSize.width() / 2, 1 ) );
142 painter.drawLine( QPointF( currentIconSize.width() / 2, 1 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 ) );
143 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ) );
144 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ), QPointF( 1, currentIconSize.height() - 2 ) );
146 const QString imageSource = mLocked ? u
":/images/themes/default/lockedGray.svg"_s : u
":/images/themes/default/unlockedGray.svg"_s;
147 bool fitsInCache =
false;
149 imageSource, 16 * pixelRatio, QColor(), QColor(), 0, 1, fitsInCache
151 image.setDevicePixelRatio( pixelRatio );
152 painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image );
156 setIconSize( currentIconSize );
162 mWidthSpinBox = widget;
163 mPrevWidth = widget->value();
164 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
169 mHeightSpinBox = widget;
170 mPrevHeight = widget->value();
171 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
176 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
177 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).