Commit e8efe118 authored by Tom on the web's avatar Tom on the web 🚴🏿
Browse files

Allow typing a zoom level in the zoom combo

This fixes a bug that would sometimes disallow typing a zoom level
in the zoom combobox (in the statusbar).

For instance if the user has 400% zoom and wants to manually set
it to 380%, they would select the full text and start typing.
When the first 3 is typed there is a chance that the combobox would
replace the typed '3' with a much higher value based on the mininum
allowed. For instance '13'.
The effect is a 4 digit zoom instead of the one the user wanted.

This commit allows there to be an invalid state which we do not
act on (zoom does not change) while the user is typing.
parent 46b06fbb
......@@ -114,10 +114,18 @@ ZoomComboBox::ZoomComboBox(QWidget *parent)
Qt::MatchFlags matchFlags = startsWithNumber || matches > 1 ? Qt::MatchFixedString : Qt::MatchStartsWith;
const int textIndex = findText(text, matchFlags);
if (textIndex < 0) {
setValue(valueFromText(text));
d->value = valueFromText(text);
}
if (textIndex >= 0 || d->value >= minimum()) {
// update only when doing so would not change the typed text due
// to being below the mininum as that means we can't type a new percentage
// one key at a time.
if (textIndex < 0)
d->lastCustomZoomValue = d->value;
updateDisplayedText();
activateAndChangeZoomTo(textIndex);
lineEdit()->setCursorPosition(lineEdit()->cursorPosition() - 1);
}
activateAndChangeZoomTo(textIndex);
lineEdit()->setCursorPosition(lineEdit()->cursorPosition() - 1);
});
connect(this, qOverload<int>(&ZoomComboBox::highlighted), this, &ZoomComboBox::changeZoomTo);
view()->installEventFilter(this);
......@@ -288,6 +296,19 @@ void ZoomComboBox::mousePressEvent(QMouseEvent *event)
QComboBox::mousePressEvent(event);
}
void ZoomComboBox::focusOutEvent(QFocusEvent *)
{
Q_D(ZoomComboBox);
// Should the user have started typing a custom value
// that was out of our range, then we have a temporary
// state that is illegal. This is needed to allow a user
// to type a zoom with multiple keystrokes, but when the
// user leaves focus we should reset to the last known 'good'
// zoom value.
if (d->lastSelectedIndex == -1)
setValue(d->lastCustomZoomValue);
}
void ZoomComboBox::changeZoomTo(int index)
{
if (index < 0) {
......
......@@ -63,6 +63,8 @@ protected:
void mousePressEvent(QMouseEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
private Q_SLOTS:
/**
* This method changes the zoom mode or value of the currently displayed image in response to
......
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