Skip to content

Commit

Permalink
fix #279967 part 1: image resize not correctly honoring aspect ratio
Browse files Browse the repository at this point in the history
A new member variable `_aspectRatio` of `InspectorImage` is added to remember the image's original ratio. It is also updated when "Lock aspect ratio" is checked. The new two slots help changing the value other than the one changed by user.
  • Loading branch information
Harmoniker1 committed Feb 18, 2020
1 parent 384f904 commit 493bce7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
53 changes: 52 additions & 1 deletion mscore/inspector/inspectorImage.cpp
Expand Up @@ -47,6 +47,12 @@ InspectorImage::InspectorImage(QWidget* parent)
};
const std::vector<InspectorPanel> ppList = { { b.title, b.panel } };

updateAspectRatio(); // initiate aspectRatio
connect(b.lockAspectRatio, &QCheckBox::toggled, this, &InspectorImage::lockAspectRatioClicked);

connect(b.size->xVal, QOverload<qreal>::of(&QDoubleSpinBox::valueChanged), this, &InspectorImage::widthChanged);
connect(b.size->yVal, QOverload<qreal>::of(&QDoubleSpinBox::valueChanged), this, &InspectorImage::heightChanged);

mapSignals(iiList, ppList);
}

Expand All @@ -56,10 +62,55 @@ InspectorImage::InspectorImage(QWidget* parent)

void InspectorImage::valueChanged(int idx)
{
InspectorBase::valueChanged(idx);
InspectorElementBase::valueChanged(idx);
setElement(); // DEBUG
}

//---------------------------------------------------------
// updateAspectRatio
//---------------------------------------------------------

void InspectorImage::updateAspectRatio()
{
Image* image = toImage(inspector->element());
qreal widthVal = image->size().width();
qreal heightVal = image->size().height();

_aspectRatio = heightVal != 0.0 ? widthVal / heightVal : 1.0;
}

//---------------------------------------------------------
// lockAspectRatioClicked
//---------------------------------------------------------

void InspectorImage::lockAspectRatioClicked(bool checked)
{
if (checked)
updateAspectRatio();
}

//---------------------------------------------------------
// widthChanged
//---------------------------------------------------------

void InspectorImage::widthChanged(qreal val)
{
Image* image = toImage(inspector->element());
if (image->lockAspectRatio() && val != 0.0) // to avoid stack overflow
b.size->yVal->setValue(val / _aspectRatio);
}

//---------------------------------------------------------
// heightChanged
//---------------------------------------------------------

void InspectorImage::heightChanged(qreal val)
{
Image* image = toImage(inspector->element());
if (image->lockAspectRatio() && val != 0.0) // to avoid stack overflow
b.size->xVal->setValue(val * _aspectRatio);
}

//---------------------------------------------------------
// postInit
//---------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions mscore/inspector/inspectorImage.h
Expand Up @@ -27,14 +27,21 @@ class InspectorImage : public InspectorElementBase {
Q_OBJECT

Ui::InspectorImage b;
qreal _aspectRatio; // used for widthChanged/heightChanged

virtual void postInit();

protected slots:
virtual void valueChanged(int idx) override;
void lockAspectRatioClicked(bool checked);
void widthChanged(qreal val);
void heightChanged(qreal val);

public:
InspectorImage(QWidget* parent);

private:
void updateAspectRatio();
};


Expand Down

0 comments on commit 493bce7

Please sign in to comment.