Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_gui_util_qundostack.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QUndoCommand>
5
6//! [0]
7class AppendText : public QUndoCommand
8{
9public:
10 AppendText(QString *doc, const QString &text)
11 : m_document(doc), m_text(text) { setText("append text"); }
13 { m_document->chop(m_text.length()); }
15 { m_document->append(m_text); }
16 bool mergeWith(const QUndoCommand *other) override;
17private:
18 QString *m_document;
19 QString m_text;
20};
21//! [0]
22
23struct MyCommand : public QUndoCommand
24{
25 MyCommand() { setText("My Command"); }
26 void undo() override {}
27 void redo() override {}
28 int id() const override { return 123; } // unique id for this command
29};
30
31struct InsertText : public QUndoCommand
32{
33 InsertText(QString *doc, int idx, const QString &text, QUndoCommand *parent = nullptr);
34};
35
36struct SetColor : public QUndoCommand
37{
38 SetColor(QString *doc, int idx, int len, const Qt::GlobalColor &color, QUndoCommand *parent = nullptr);
39};
40
41void examples(QUndoStack *stack, QString *document, int idx, const QString &text)
42{
43 {
44 //! [1]
45 MyCommand *command1 = new MyCommand();
46 stack->push(command1);
47 MyCommand *command2 = new MyCommand();
48 stack->push(command2);
49
50 stack->undo();
51
52 MyCommand *command3 = new MyCommand();
53 stack->push(command3); // command2 gets deleted
54 //! [1]
55 }
56
57 {
58 QUndoStack stack;
59
60 //! [2]
61 QUndoCommand *insertRed = new QUndoCommand(); // an empty command
62 insertRed->setText("insert red text");
63
64 new InsertText(document, idx, text, insertRed); // becomes child of insertRed
65 new SetColor(document, idx, text.length(), Qt::red, insertRed);
66
67 stack.push(insertRed);
68 //! [2]
69 }
70}
71
72//! [3]
73bool AppendText::mergeWith(const QUndoCommand *other)
74{
75 if (other->id() != id()) // make sure other is also an AppendText command
76 return false;
77 m_text += static_cast<const AppendText*>(other)->m_text;
78 return true;
79}
80//! [3]
81
82void wrap( QUndoStack &stack, QString *document, int idx, const QString &text)
83{
84 //! [4]
85 stack.beginMacro("insert red text");
86 stack.push(new InsertText(document, idx, text));
87 stack.push(new SetColor(document, idx, text.length(), Qt::red));
88 stack.endMacro(); // indexChanged() is emitted
89 //! [4]
90
91
92 //! [5]
93 QUndoCommand *insertRed = new QUndoCommand(); // an empty command
94 insertRed->setText("insert red text");
95
96 new InsertText(document, idx, text, insertRed); // becomes child of insertRed
97 new SetColor(document, idx, text.length(), Qt::red, insertRed);
98
99 stack.push(insertRed);
100 //! [5]
101}
bool mergeWith(const QUndoCommand *other) override
[3]
AppendText(QString *doc, const QString &text)
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
void examples(QUndoStack *stack, QString *document, int idx, const QString &text)
void wrap(QUndoStack &stack, QString *document, int idx, const QString &text)
[3]
InsertText(QString *doc, int idx, const QString &text, QUndoCommand *parent=nullptr)
void undo() override
Reverts a change to the document.
int id() const override
Returns the ID of this command.
void redo() override
Applies a change to the document.
SetColor(QString *doc, int idx, int len, const Qt::GlobalColor &color, QUndoCommand *parent=nullptr)