21#include <QApplication>
26#include <QDoubleSpinBox>
29 : QToolButton( parent )
31 setMinimumSize( QSize( 24, 24 ) );
32 setMaximumWidth( fontMetrics().horizontalAdvance(
'0' ) * 3 );
35 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
44void QgsRatioLockButton::buttonClicked()
54void QgsRatioLockButton::widthSpinBoxChanged(
double value )
57 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
63 const double oldRatio = mPrevHeight / mPrevWidth;
64 mUpdatingRatio =
true;
65 mHeightSpinBox->setValue( oldRatio * value );
66 mUpdatingRatio =
false;
70void QgsRatioLockButton::heightSpinBoxChanged(
double value )
73 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
79 const 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 );
107void 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 )
122 const double pixelRatio = devicePixelRatioF();
123 QPixmap pm( currentIconSize * pixelRatio );
124 pm.setDevicePixelRatio( pixelRatio );
125 pm.fill( Qt::transparent );
128 QPen pen = QPen( QColor( 136, 136, 136 ) );
131 painter.begin( &pm );
132 painter.setRenderHint( QPainter::Antialiasing,
true );
133 painter.setPen( pen );
135 painter.drawLine( QPointF( 1, 1 ), QPointF( currentIconSize.width() / 2, 1 ) );
136 painter.drawLine( QPointF( currentIconSize.width() / 2, 1 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 ) );
137 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ) );
138 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ), QPointF( 1, currentIconSize.height() - 2 ) );
140 const QString imageSource = mLocked ? QStringLiteral(
":/images/themes/default/lockedGray.svg" ) : QStringLiteral(
":/images/themes/default/unlockedGray.svg" );
141 bool fitsInCache =
false;
143 imageSource, 16 * pixelRatio, QColor(), QColor(), 0, 1, fitsInCache
145 image.setDevicePixelRatio( pixelRatio );
146 painter.drawImage( QRectF(
147 currentIconSize.width() / 2 - 8,
148 currentIconSize.height() / 2 - 8,
155 setIconSize( currentIconSize );
161 mWidthSpinBox = widget;
162 mPrevWidth = widget->value();
163 connect( mWidthSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::widthSpinBoxChanged );
168 mHeightSpinBox = widget;
169 mPrevHeight = widget->value();
170 connect( mHeightSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsRatioLockButton::heightSpinBoxChanged );
175 mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
176 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)