Commit df6bdad0 authored by Eugene Popov's avatar Eugene Popov
Browse files

[ZoomComboBox] Override showPopup instead of mousePressEvent

There is no need to override mousePressEvent to perform some actions exactly before the popup is displayed, as there is a special virtual function showPopup for that. Using this function (instead of mousePressEvent) makes the code more descriptive and avoids possible issues.
parent 77947f34
......@@ -266,42 +266,43 @@ void ZoomComboBox::updateDisplayedText()
}
}
bool ZoomComboBox::eventFilter(QObject * /* watched */, QEvent *event)
void Gwenview::ZoomComboBox::showPopup()
{
if (event->type() == QEvent::Hide) {
Q_D(ZoomComboBox);
changeZoomTo(d->lastSelectedIndex);
Q_D(ZoomComboBox);
if (d->mZoomToFitAction->isChecked()) {
setCurrentIndex(0);
d->lastSelectedIndex = 0;
} else if (d->mZoomToFillAction->isChecked()) {
setCurrentIndex(1);
d->lastSelectedIndex = 1;
} else if (d->mActualSizeAction->isChecked()) {
const int actualSizeActionIndex = findData(QVariant::fromValue(d->mActualSizeAction));
setCurrentIndex(actualSizeActionIndex);
d->lastSelectedIndex = actualSizeActionIndex;
} else {
// Now is a good time to save the zoom value that was selected before the user changes it through the popup.
d->lastCustomZoomValue = d->value;
// Highlight the correct index if the current zoom value exists as an option in the popup.
// If it doesn't exist, it is set to -1.
d->lastSelectedIndex = findText(textFromValue(d->value));
setCurrentIndex(d->lastSelectedIndex);
}
return false;
// We don't want to emit a QComboBox::highlighted event just because the popup is opened.
const QSignalBlocker blocker(this);
QComboBox::showPopup();
}
void ZoomComboBox::mousePressEvent(QMouseEvent *event)
bool ZoomComboBox::eventFilter(QObject * /* watched */, QEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (event->type() == QEvent::Hide) {
Q_D(ZoomComboBox);
if (d->mZoomToFitAction->isChecked()) {
setCurrentIndex(0);
d->lastSelectedIndex = 0;
} else if (d->mZoomToFillAction->isChecked()) {
setCurrentIndex(1);
d->lastSelectedIndex = 1;
} else if (d->mActualSizeAction->isChecked()) {
const int actualSizeActionIndex = findData(QVariant::fromValue(d->mActualSizeAction));
setCurrentIndex(actualSizeActionIndex);
d->lastSelectedIndex = actualSizeActionIndex;
} else {
// Now is a good time to save the zoom value that was selected before the user changes it through the popup.
d->lastCustomZoomValue = d->value;
// Highlight the correct index if the current zoom value exists as an option in the popup.
// If it doesn't exist, it is set to -1.
d->lastSelectedIndex = findText(textFromValue(d->value));
setCurrentIndex(d->lastSelectedIndex);
}
changeZoomTo(d->lastSelectedIndex);
}
// We don't want to emit a QComboBox::highlighted event just because the popup is opened.
const QSignalBlocker blocker(this);
QComboBox::mousePressEvent(event);
return false;
}
void ZoomComboBox::focusOutEvent(QFocusEvent *)
......
......@@ -55,14 +55,13 @@ public:
void updateDisplayedText();
void showPopup() override;
Q_SIGNALS:
void zoomChanged(qreal zoom);
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
private Q_SLOTS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment