Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
6.2.2
-
None
Description
When setting the prefix to "Undo (%1)" the resulting text in the action will be "Undo (%1) <command text>" instead of "Undo (<command text>)". The reason for that is that variable "defaultText", see below, cannot be set by the user and will always be empty and the command text will be added to the end in function QUndoStackPrivate::setPrefixedText.
All 4 functions have the same problem:
QUndoGroup::createUndoAction
QUndoGroup::createRedoAction
QUndoStack::createUndoAction
QUndoStack::createRedoAction
QUndoStackPrivate::setPrefixedText must be fixed too, see below for an example.
Current implementation:
QAction *QUndoStack::createUndoAction(QObject *parent, const QString &prefix) const { QAction *action = new QAction(parent); action->setEnabled(canUndo()); QString effectivePrefix = prefix; QString defaultText; if (prefix.isEmpty()) { effectivePrefix = tr("Undo %1"); defaultText = tr("Undo", "Default text for undo action"); } QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, undoText()); connect(this, &QUndoStack::canUndoChanged, action, &QAction::setEnabled); connect(this, &QUndoStack::undoTextChanged, action, [=](const QString &text) { QUndoStackPrivate::setPrefixedText(action, effectivePrefix, defaultText, text); }); connect(action, &QAction::triggered, this, &QUndoStack::undo); return action; } }} void QUndoStackPrivate::setPrefixedText(QAction *action, const QString &prefix, const QString &defaultText, const QString &text) { if (defaultText.isEmpty()) { QString s = prefix; if (!prefix.isEmpty() && !text.isEmpty()) s.append(QLatin1Char(' ')); s.append(text); action->setText(s); } else { if (text.isEmpty()) action->setText(defaultText); else action->setText(prefix.arg(text)); } };
I think the proper implementation of the functions should be more like this to allow the user to customize the default and prefix text:
QAction *QUndoStack::createUndoAction(QObject *parent, const QString &prefix, const QString &defaultText) const { QAction *action = new QAction(parent); action->setEnabled(canUndo()); if (prefix.isEmpty()) prefix = tr("Undo %1"); if (defaultText.isEmpty()) defaultText = tr("Undo", "Default text for undo action"); QUndoStackPrivate::setPrefixedText(action, prefix, defaultText, undoText()); connect(this, &QUndoStack::canUndoChanged, action, &QAction::setEnabled); connect(this, &QUndoStack::undoTextChanged, action, [=](const QString &text) { QUndoStackPrivate::setPrefixedText(action, prefix, defaultText, text); }); connect(action, &QAction::triggered, this, &QUndoStack::undo); return action; } void QUndoStackPrivate::setPrefixedText(QAction *action, const QString &prefix, const QString &defaultText, const QString &text) { if (text.isEmpty()) action->setText(defaultText); else if (prefix.contains("%1")) action->setText(prefix.arg(text)); else action->setText(prefix); }