summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/util/qundostack.cpp49
-rw-r--r--src/gui/util/qundostack.h1
-rw-r--r--src/gui/util/qundostack_p.h1
-rw-r--r--tests/auto/qundostack/tst_qundostack.cpp59
4 files changed, 100 insertions, 10 deletions
diff --git a/src/gui/util/qundostack.cpp b/src/gui/util/qundostack.cpp
index 417f02e4b2..1dfab5d1ad 100644
--- a/src/gui/util/qundostack.cpp
+++ b/src/gui/util/qundostack.cpp
@@ -114,7 +114,7 @@ QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent)
d = new QUndoCommandPrivate;
if (parent != 0)
parent->d->child_list.append(this);
- d->text = text;
+ setText(text);
}
/*!
@@ -230,10 +230,9 @@ void QUndoCommand::undo()
Returns a short text string describing what this command does; for example,
"insert text".
- The text is used when the text properties of the stack's undo and redo
- actions are updated.
+ The text is used for names of items in QUndoView.
- \sa setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()
+ \sa actionText(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()
*/
QString QUndoCommand::text() const
@@ -242,17 +241,47 @@ QString QUndoCommand::text() const
}
/*!
+ \since 4.8
+
+ Returns a short text string describing what this command does; for example,
+ "insert text".
+
+ The text is used when the text properties of the stack's undo and redo
+ actions are updated.
+
+ \sa text(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()
+*/
+
+QString QUndoCommand::actionText() const
+{
+ return d->actionText;
+}
+
+/*!
Sets the command's text to be the \a text specified.
The specified text should be a short user-readable string describing what this
command does.
- \sa text() QUndoStack::createUndoAction() QUndoStack::createRedoAction()
+ If you need to have two different strings for text() and actionText(), separate
+ them with "\n" and pass into this function. Even if you do not use this feature
+ for English strings during development, you can still let translators use two
+ different strings in order to match specific languages' needs.
+ The described feature and the function actionText() are available since Qt 4.8.
+
+ \sa text() actionText() QUndoStack::createUndoAction() QUndoStack::createRedoAction()
*/
void QUndoCommand::setText(const QString &text)
{
- d->text = text;
+ int cdpos = text.indexOf(QLatin1Char('\n'));
+ if (cdpos > 0) {
+ d->text = text.left(cdpos);
+ d->actionText = text.mid(cdpos + 1);
+ } else {
+ d->text = text;
+ d->actionText = text;
+ }
}
/*!
@@ -796,7 +825,7 @@ bool QUndoStack::canRedo() const
/*!
Returns the text of the command which will be undone in the next call to undo().
- \sa QUndoCommand::text() redoText()
+ \sa QUndoCommand::actionText() redoText()
*/
QString QUndoStack::undoText() const
@@ -805,14 +834,14 @@ QString QUndoStack::undoText() const
if (!d->macro_stack.isEmpty())
return QString();
if (d->index > 0)
- return d->command_list.at(d->index - 1)->text();
+ return d->command_list.at(d->index - 1)->actionText();
return QString();
}
/*!
Returns the text of the command which will be redone in the next call to redo().
- \sa QUndoCommand::text() undoText()
+ \sa QUndoCommand::actionText() undoText()
*/
QString QUndoStack::redoText() const
@@ -821,7 +850,7 @@ QString QUndoStack::redoText() const
if (!d->macro_stack.isEmpty())
return QString();
if (d->index < d->command_list.size())
- return d->command_list.at(d->index)->text();
+ return d->command_list.at(d->index)->actionText();
return QString();
}
diff --git a/src/gui/util/qundostack.h b/src/gui/util/qundostack.h
index 65941b53c3..700996f005 100644
--- a/src/gui/util/qundostack.h
+++ b/src/gui/util/qundostack.h
@@ -70,6 +70,7 @@ public:
virtual void redo();
QString text() const;
+ QString actionText() const;
void setText(const QString &text);
virtual int id() const;
diff --git a/src/gui/util/qundostack_p.h b/src/gui/util/qundostack_p.h
index 2906fd8564..a10076366a 100644
--- a/src/gui/util/qundostack_p.h
+++ b/src/gui/util/qundostack_p.h
@@ -70,6 +70,7 @@ public:
QUndoCommandPrivate() : id(-1) {}
QList<QUndoCommand*> child_list;
QString text;
+ QString actionText;
int id;
};
diff --git a/tests/auto/qundostack/tst_qundostack.cpp b/tests/auto/qundostack/tst_qundostack.cpp
index bcab43d4af..5aea0a15d3 100644
--- a/tests/auto/qundostack/tst_qundostack.cpp
+++ b/tests/auto/qundostack/tst_qundostack.cpp
@@ -101,6 +101,16 @@ private:
QString m_text;
};
+class IdleCommand : public QUndoCommand
+{
+public:
+ IdleCommand(QUndoCommand *parent = 0);
+ ~IdleCommand();
+
+ virtual void undo();
+ virtual void redo();
+};
+
InsertCommand::InsertCommand(QString *str, int idx, const QString &text,
QUndoCommand *parent)
: QUndoCommand(parent)
@@ -201,6 +211,26 @@ bool AppendCommand::mergeWith(const QUndoCommand *other)
return true;
}
+IdleCommand::IdleCommand(QUndoCommand *parent)
+ : QUndoCommand(parent)
+{
+ // "idle-item" goes to QUndoStack::{redo,undo}Text
+ // "idle-action" goes to all other places (e.g. QUndoView)
+ setText("idle-item\nidle-action");
+}
+
+IdleCommand::~IdleCommand()
+{
+}
+
+void IdleCommand::redo()
+{
+}
+
+void IdleCommand::undo()
+{
+}
+
/******************************************************************************
** tst_QUndoStack
*/
@@ -221,6 +251,7 @@ private slots:
void compression();
void undoLimit();
void commandTextFormat();
+ void separateUndoText();
};
tst_QUndoStack::tst_QUndoStack()
@@ -2970,6 +3001,34 @@ void tst_QUndoStack::commandTextFormat()
qApp->removeTranslator(&translator);
}
+void tst_QUndoStack::separateUndoText()
+{
+ QUndoStack stack;
+ QAction *undo_action = stack.createUndoAction(0);
+ QAction *redo_action = stack.createRedoAction(0);
+
+ QUndoCommand *command1 = new IdleCommand();
+ QUndoCommand *command2 = new IdleCommand();
+ stack.push(command1);
+ stack.push(command2);
+ stack.undo();
+
+ QCOMPARE(undo_action->text(), QString("Undo idle-action"));
+ QCOMPARE(redo_action->text(), QString("Redo idle-action"));
+ QCOMPARE(command1->actionText(), QString("idle-action"));
+
+ QCOMPARE(command1->text(), QString("idle-item"));
+ QCOMPARE(stack.text(0), QString("idle-item"));
+
+ command1->setText("idle");
+ QCOMPARE(command1->actionText(), QString("idle"));
+ QCOMPARE(command1->text(), QString("idle"));
+
+ command1->setText("idle-item\nidle-action");
+ QCOMPARE(command1->actionText(), QString("idle-action"));
+ QCOMPARE(command1->text(), QString("idle-item"));
+}
+
QTEST_MAIN(tst_QUndoStack)
#include "tst_qundostack.moc"