Skip to content

Commit

Permalink
Addressed a few usability issues
Browse files Browse the repository at this point in the history
* Cursor no longer jumps to the end when editing string properties.

* Global undo/redo shortcuts are no longer overridden by those local to
  property edit widgets.

* Since global undo/redo is now relied upon, the keyboard tracking is
  enabled again for spin boxes.
  • Loading branch information
bjorn committed Oct 29, 2024
1 parent 6fc6c5d commit dd0784a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,11 @@ class ClassNameProperty : public StringProperty
editor->addItems(classNamesFor(*mObject));
auto syncEditor = [this, editor] {
const QSignalBlocker blocker(editor);
editor->setCurrentText(value());

// Avoid affecting cursor position when the text is the same
const auto v = value();
if (editor->currentText() != v)
editor->setCurrentText(v);
};
syncEditor();
connect(this, &Property::valueChanged, editor, syncEditor);
Expand Down
53 changes: 47 additions & 6 deletions src/tiled/propertyeditorwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@

namespace Tiled {

/**
* Returns whether the given event is a shortcut override event for the undo or
* redo shortcuts. We generally want to use the global undo and redo shortcuts
* instead.
*/
static bool isUndoRedoShortcutOverride(QEvent *event)
{
if (event->type() == QEvent::ShortcutOverride) {
auto ke = static_cast<QKeyEvent *>(event);
return (ke == QKeySequence::Redo || ke == QKeySequence::Undo);
}
return false;
}

/**
* Strips a floating point number representation of redundant trailing zeros.
* Examples:
Expand All @@ -55,6 +69,15 @@ static QString removeRedundantTrialingZeros(const QString &text)
}


bool LineEdit::event(QEvent *event)
{
if (isUndoRedoShortcutOverride(event))
return false;

return QLineEdit::event(event);
}


ComboBox::ComboBox(QWidget *parent)
: QComboBox(parent)
{
Expand All @@ -66,6 +89,14 @@ ComboBox::ComboBox(QWidget *parent)
setFocusPolicy(Qt::StrongFocus);
}

bool ComboBox::event(QEvent *event)
{
if (isUndoRedoShortcutOverride(event)) // relevant when editable
return false;

return QComboBox::event(event);
}

void ComboBox::wheelEvent(QWheelEvent *event)
{
if (!hasFocus())
Expand All @@ -85,9 +116,6 @@ SpinBox::SpinBox(QWidget *parent)
// Don't take focus by mouse wheel
setFocusPolicy(Qt::StrongFocus);

// Don't respond to keyboard input immediately.
setKeyboardTracking(false);

// Allow the widget to shrink horizontally.
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}
Expand All @@ -100,6 +128,14 @@ QSize SpinBox::minimumSizeHint() const
return hint;
}

bool SpinBox::event(QEvent *event)
{
if (isUndoRedoShortcutOverride(event)) // relevant when editable
return false;

return QSpinBox::event(event);
}

void SpinBox::wheelEvent(QWheelEvent *event)
{
if (!hasFocus())
Expand All @@ -122,9 +158,6 @@ DoubleSpinBox::DoubleSpinBox(QWidget *parent)
// Don't take focus by mouse wheel
setFocusPolicy(Qt::StrongFocus);

// Don't respond to keyboard input immediately.
setKeyboardTracking(false);

// Allow the widget to shrink horizontally.
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}
Expand All @@ -148,6 +181,14 @@ QString DoubleSpinBox::textFromValue(double val) const
return text;
}

bool DoubleSpinBox::event(QEvent *event)
{
if (isUndoRedoShortcutOverride(event)) // relevant when editable
return false;

return QDoubleSpinBox::event(event);
}

void DoubleSpinBox::wheelEvent(QWheelEvent *event)
{
if (!hasFocus())
Expand Down
22 changes: 21 additions & 1 deletion src/tiled/propertyeditorwidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,31 @@

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QSpinBox>

class QLabel;

namespace Tiled {

/**
* A combo box that doesn't respond to wheel events when not focused.
* A line edit that doesn't override global undo/redo shortcuts.
*/
class LineEdit : public QLineEdit
{
Q_OBJECT

public:
using QLineEdit::QLineEdit;

protected:
bool event(QEvent *event) override;
};


/**
* A combo box that doesn't respond to wheel events when not focused and
* doesn't override global undo/redo shortcuts.
*/
class ComboBox : public QComboBox
{
Expand All @@ -39,6 +56,7 @@ class ComboBox : public QComboBox
ComboBox(QWidget *parent = nullptr);

protected:
bool event(QEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
};

Expand All @@ -57,6 +75,7 @@ class SpinBox : public QSpinBox
QSize minimumSizeHint() const override;

protected:
bool event(QEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
};

Expand All @@ -79,6 +98,7 @@ class DoubleSpinBox : public QDoubleSpinBox
QString textFromValue(double val) const override;

protected:
bool event(QEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
};

Expand Down
7 changes: 5 additions & 2 deletions src/tiled/varianteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ void StringProperty::setPlaceholderText(const QString &placeholderText)

QWidget *StringProperty::createEditor(QWidget *parent)
{
auto editor = new QLineEdit(parent);
auto editor = new LineEdit(parent);
editor->setPlaceholderText(m_placeholderText);

auto syncEditor = [=] {
editor->setText(value());
// Avoid affecting cursor position when the text is the same
const QString v = value();
if (editor->text() != v)
editor->setText(v);
};
syncEditor();

Expand Down

0 comments on commit dd0784a

Please sign in to comment.