Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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]
7
class
AppendText
:
public
QUndoCommand
8
{
9
public
:
10
AppendText
(QString *doc,
const
QString &text)
11
:
m_document
(
doc
),
m_text
(
text
) { setText(
"append text"
); }
12
void
undo
()
override
13
{ m_document->chop(m_text.length()); }
14
void
redo
()
override
15
{ m_document->append(m_text); }
16
bool
mergeWith
(
const
QUndoCommand *other)
override
;
17
private
:
18
QString *m_document;
19
QString m_text;
20
};
21
//! [0]
22
23
struct
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
31
struct
InsertText
:
public
QUndoCommand
32
{
33
InsertText
(QString *doc,
int
idx,
const
QString &text, QUndoCommand *parent =
nullptr
);
34
};
35
36
struct
SetColor
:
public
QUndoCommand
37
{
38
SetColor
(QString *doc,
int
idx,
int
len,
const
Qt::GlobalColor &color, QUndoCommand *parent =
nullptr
);
39
};
40
41
void
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]
73
bool
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
82
void
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
}
AppendText
[0]
Definition
src_gui_util_qundostack.cpp:8
AppendText::mergeWith
bool mergeWith(const QUndoCommand *other) override
[3]
Definition
src_gui_util_qundostack.cpp:73
AppendText::AppendText
AppendText(QString *doc, const QString &text)
Definition
src_gui_util_qundostack.cpp:10
AppendText::undo
void undo() override
Reverts a change to the document.
Definition
src_gui_util_qundostack.cpp:12
AppendText::redo
void redo() override
Applies a change to the document.
Definition
src_gui_util_qundostack.cpp:14
examples
void examples(QUndoStack *stack, QString *document, int idx, const QString &text)
Definition
src_gui_util_qundostack.cpp:41
wrap
void wrap(QUndoStack &stack, QString *document, int idx, const QString &text)
[3]
Definition
src_gui_util_qundostack.cpp:82
InsertText
Definition
src_gui_util_qundostack.cpp:32
InsertText::InsertText
InsertText(QString *doc, int idx, const QString &text, QUndoCommand *parent=nullptr)
MyCommand
[0]
Definition
src_gui_util_qundostack.cpp:24
MyCommand::MyCommand
MyCommand()
Definition
src_gui_util_qundostack.cpp:25
MyCommand::undo
void undo() override
Reverts a change to the document.
Definition
src_gui_util_qundostack.cpp:26
MyCommand::id
int id() const override
Returns the ID of this command.
Definition
src_gui_util_qundostack.cpp:28
MyCommand::redo
void redo() override
Applies a change to the document.
Definition
src_gui_util_qundostack.cpp:27
SetColor
Definition
src_gui_util_qundostack.cpp:37
SetColor::SetColor
SetColor(QString *doc, int idx, int len, const Qt::GlobalColor &color, QUndoCommand *parent=nullptr)
qtbase
src
gui
doc
snippets
code
src_gui_util_qundostack.cpp
Generated on
for Qt by
1.18.0