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 )
68 const double oldRatio = mPrevHeight / mPrevWidth;
69 mUpdatingRatio =
true;
70 mHeightSpinBox->setValue( oldRatio * value );
71 mUpdatingRatio =
false;
75void QgsRatioLockButton::heightSpinBoxChanged(
double value )
83 const double oldRatio = mPrevWidth / mPrevHeight;
84 mUpdatingRatio =
true;
85 mWidthSpinBox->setValue( oldRatio * value );
86 mUpdatingRatio =
false;
92 if ( e->type() == QEvent::EnabledChange )
96 QToolButton::changeEvent( e );
102 QToolButton::showEvent( e );
107 QToolButton::resizeEvent( event );
111void QgsRatioLockButton::drawButton()
113 QSize currentIconSize;
116 currentIconSize = QSize( width() - 10, height() - 6 );
118 currentIconSize = QSize( width() - 10, height() - 12 );
121 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
126 const double pixelRatio = devicePixelRatioF();
127 QPixmap pm( currentIconSize * pixelRatio );
128 pm.setDevicePixelRatio( pixelRatio );
129 pm.fill( Qt::transparent );
132 QPen pen = QPen( QColor( 136, 136, 136 ) );
135 painter.begin( &pm );
136 painter.setRenderHint( QPainter::Antialiasing,
true );
137 painter.setPen( pen );
139 painter.drawLine( QPointF( 1, 1 ), QPointF( currentIconSize.width() / 2, 1 ) );
140 painter.drawLine( QPointF( currentIconSize.width() / 2, 1 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 ) );
141 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ) );
142 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ), QPointF( 1, currentIconSize.height() - 2 ) );
144 const QString imageSource = mLocked ? u
":/images/themes/default/lockedGray.svg"_s : u
":/images/themes/default/unlockedGray.svg"_s;
145 bool fitsInCache =
false;
147 image.setDevicePixelRatio( pixelRatio );
148 painter.drawImage( QRectF( currentIconSize.width() / 2 - 8, currentIconSize.height() / 2 - 8, 16, 16 ), image );
152 setIconSize( currentIconSize );
158 mWidthSpinBox = widget;
159 mPrevWidth = widget->value();
160 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
165 mHeightSpinBox = widget;
166 mPrevHeight = widget->value();
167 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
172 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
173 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).