17#include "moc_qgsratiolockbutton.cpp"
22#include <QApplication>
27#include <QDoubleSpinBox>
30 : QToolButton( parent )
32 setMinimumSize( QSize( 24, 24 ) );
33 setMaximumWidth( fontMetrics().horizontalAdvance(
'0' ) * 3 );
36 connect(
this, &QPushButton::clicked,
this, &QgsRatioLockButton::buttonClicked );
45void QgsRatioLockButton::buttonClicked()
48 setChecked( mLocked );
55void QgsRatioLockButton::widthSpinBoxChanged(
double value )
58 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mHeightSpinBox || !mLocked )
64 const double oldRatio = mPrevHeight / mPrevWidth;
65 mUpdatingRatio =
true;
66 mHeightSpinBox->setValue( oldRatio * value );
67 mUpdatingRatio =
false;
71void QgsRatioLockButton::heightSpinBoxChanged(
double value )
74 ||
qgsDoubleNear( mPrevHeight, 0.0 ) || !mWidthSpinBox || !mLocked )
80 const double oldRatio = mPrevWidth / mPrevHeight;
81 mUpdatingRatio =
true;
82 mWidthSpinBox->setValue( oldRatio * value );
83 mUpdatingRatio =
false;
89 if ( e->type() == QEvent::EnabledChange )
93 QToolButton::changeEvent( e );
99 QToolButton::showEvent( e );
104 QToolButton::resizeEvent( event );
108void QgsRatioLockButton::drawButton()
110 QSize currentIconSize;
113 currentIconSize = QSize( width() - 10, height() - 6 );
115 currentIconSize = QSize( width() - 10, height() - 12 );
118 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
123 const double pixelRatio = devicePixelRatioF();
124 QPixmap pm( currentIconSize * pixelRatio );
125 pm.setDevicePixelRatio( pixelRatio );
126 pm.fill( Qt::transparent );
129 QPen pen = QPen( QColor( 136, 136, 136 ) );
132 painter.begin( &pm );
133 painter.setRenderHint( QPainter::Antialiasing,
true );
134 painter.setPen( pen );
136 painter.drawLine( QPointF( 1, 1 ), QPointF( currentIconSize.width() / 2, 1 ) );
137 painter.drawLine( QPointF( currentIconSize.width() / 2, 1 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 - 13 ) );
138 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() / 2 + 13 ), QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ) );
139 painter.drawLine( QPointF( currentIconSize.width() / 2, currentIconSize.height() - 2 ), QPointF( 1, currentIconSize.height() - 2 ) );
141 const QString imageSource = mLocked ? QStringLiteral(
":/images/themes/default/lockedGray.svg" ) : QStringLiteral(
":/images/themes/default/unlockedGray.svg" );
142 bool fitsInCache =
false;
144 imageSource, 16 * pixelRatio, QColor(), QColor(), 0, 1, fitsInCache
146 image.setDevicePixelRatio( pixelRatio );
147 painter.drawImage( QRectF(
148 currentIconSize.width() / 2 - 8,
149 currentIconSize.height() / 2 - 8,
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)