Qt
6.x
The Qt SDK
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
5
class
AppendText
:
public
QUndoCommand
6
{
7
public
:
8
AppendText
(
QString
*doc,
const
QString
&
text
)
9
: m_document(doc), m_text(
text
) {
setText
(
"append text"
); }
10
void
undo
()
override
11
{ m_document->
chop
(m_text.
length
()); }
12
void
redo
()
override
13
{ m_document->
append
(m_text); }
14
private
:
15
QString
*m_document;
16
QString
m_text;
17
};
19
20
22
MyCommand *
command1
=
new
MyCommand();
23
stack->push(
command1
);
24
MyCommand *
command2
=
new
MyCommand();
25
stack->push(
command2
);
26
27
stack->undo();
28
29
MyCommand *
command3
=
new
MyCommand();
30
stack->push(
command3
);
// command2 gets deleted
32
33
35
QUndoCommand
*
insertRed
=
new
QUndoCommand
();
// an empty command
36
insertRed
->
setText
(
"insert red text"
);
37
38
new
InsertText
(document, idx,
text
,
insertRed
);
// becomes child of insertRed
39
new
SetColor
(document, idx,
text
.
length
(),
Qt::red
,
insertRed
);
40
41
stack.push(
insertRed
);
43
44
46
bool
AppendText::mergeWith
(
const
QUndoCommand
*
other
)
47
{
48
if
(
other
->id() !=
id
())
// make sure other is also an AppendText command
49
return
false
;
50
m_text +=
static_cast<
const
AppendText
*
>
(
other
)->m_text;
51
return
true
;
52
}
54
55
57
stack.beginMacro(
"insert red text"
);
58
stack.push(
new
InsertText
(document, idx,
text
));
59
stack.push(
new
SetColor
(document, idx,
text
.
length
(),
Qt::red
));
60
stack.endMacro();
// indexChanged() is emitted
62
63
65
QUndoCommand
*
insertRed
=
new
QUndoCommand
();
// an empty command
66
insertRed
->
setText
(
"insert red text"
);
67
68
new
InsertText
(document, idx,
text
,
insertRed
);
// becomes child of insertRed
69
new
SetColor
(document, idx,
text
.
length
(),
Qt::red
,
insertRed
);
70
71
stack.push(
insertRed
);
AppendText
[0]
Definition
src_gui_util_qundostack.cpp:6
AppendText::AppendText
AppendText(QString *doc, const QString &text)
Definition
src_gui_util_qundostack.cpp:8
AppendText::undo
void undo() override
Reverts a change to the document.
Definition
src_gui_util_qundostack.cpp:10
AppendText::redo
void redo() override
Applies a change to the document.
Definition
src_gui_util_qundostack.cpp:12
QString
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition
qstring.h:127
QString::chop
void chop(qsizetype n)
Removes n characters from the end of the string.
Definition
qstring.cpp:6180
QString::append
QString & append(QChar c)
Definition
qstring.cpp:3227
QString::length
qsizetype length() const
Returns the number of characters in this string.
Definition
qstring.h:187
QUndoCommand
Definition
qundostack.h:20
QUndoCommand::mergeWith
virtual bool mergeWith(const QUndoCommand *other)
Attempts to merge this command with command.
Definition
qundostack.cpp:180
QUndoCommand::setText
void setText(const QString &text)
Sets the command's text to be the text specified.
Definition
qundostack.cpp:267
QUndoCommand::text
QString text() const
Returns a short text string describing what this command does; for example, "insert text".
Definition
qundostack.cpp:230
text
QString text
Definition
doc_src_properties.cpp:102
Qt::red
@ red
Definition
qnamespace.h:34
other
QSharedPointer< T > other(t)
[5]
command2
MyCommand * command2
Definition
src_gui_util_qundostack.cpp:24
SetColor
new SetColor(document, idx, text.length(), Qt::red, insertRed)
insertRed
QUndoCommand * insertRed
[1]
Definition
src_gui_util_qundostack.cpp:35
command3
MyCommand * command3
Definition
src_gui_util_qundostack.cpp:29
InsertText
new InsertText(document, idx, text, insertRed)
command1
MyCommand * command1
[0]
Definition
src_gui_util_qundostack.cpp:22
qtbase
src
gui
doc
snippets
code
src_gui_util_qundostack.cpp
Generated by
1.9.7