File: qundocommand.html

package info (click to toggle)
python-qt4 4.7.3-1%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,504 kB
  • ctags: 4,680
  • sloc: python: 28,738; cpp: 8,897; sh: 245; xml: 243; makefile: 150
file content (99 lines) | stat: -rw-r--r-- 12,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QUndoCommand Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QUndoCommand Class Reference<br /><sup><sup>[<a href="qtgui.html">QtGui</a> module]</sup></sup></h1><p>The QUndoCommand class is the base class of all commands stored on a <a href="qundostack.html">QUndoStack</a>. <a href="#details">More...</a></p>
<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qundocommand.html#QUndoCommand">__init__</a></b> (<i>self</i>, QUndoCommand&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" /><b><a href="qundocommand.html#QUndoCommand-2">__init__</a></b> (<i>self</i>, QString, QUndoCommand&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" />QUndoCommand <b><a href="qundocommand.html#child">child</a></b> (<i>self</i>, int)</li><li><div class="fn" />int <b><a href="qundocommand.html#childCount">childCount</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qundocommand.html#id">id</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qundocommand.html#mergeWith">mergeWith</a></b> (<i>self</i>, QUndoCommand)</li><li><div class="fn" /><b><a href="qundocommand.html#redo">redo</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qundocommand.html#setText">setText</a></b> (<i>self</i>, QString)</li><li><div class="fn" />QString <b><a href="qundocommand.html#text">text</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qundocommand.html#undo">undo</a></b> (<i>self</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QUndoCommand class is the base class of all commands stored on a <a href="qundostack.html">QUndoStack</a>.</p>
<p>For an overview of Qt's Undo Framework, see the <a href="qundo.html">overview document</a>.</p>
<p>A QUndoCommand represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. QUndoCommand can apply a change to the document with <a href="qundocommand.html#redo">redo</a>() and undo the change with <a href="qundocommand.html#undo">undo</a>(). The implementations for these functions must be provided in a derived class.</p>
<pre> class AppendText : public QUndoCommand
 {
 public:
     AppendText(QString *doc, const QString &amp;text)
         : m_document(doc), m_text(text) { setText("append text"); }
     virtual void undo()
         { m_document-&gt;chop(m_text.length()); }
     virtual void redo()
         { m_document-&gt;append(m_text); }
 private:
     QString *m_document;
     QString m_text;
 };</pre>
<p>A QUndoCommand has an associated <a href="qundocommand.html#text">text</a>(). This is a short string describing what the command does. It is used to update the text properties of the stack's undo and redo actions; see <a href="qundostack.html#createUndoAction">QUndoStack.createUndoAction</a>() and <a href="qundostack.html#createRedoAction">QUndoStack.createRedoAction</a>().</p>
<p>QUndoCommand objects are owned by the stack they were pushed on. <a href="qundostack.html">QUndoStack</a> deletes a command if it has been undone and a new command is pushed. For example:</p>
<pre> MyCommand *command1 = new MyCommand();
 stack-&gt;push(command1);
 MyCommand *command2 = new MyCommand();
 stack-&gt;push(command2);

 stack-&gt;undo();

 MyCommand *command3 = new MyCommand();
 stack-&gt;push(command3); <span class="comment">// command2 gets deleted</span></pre>
<p>In effect, when a command is pushed, it becomes the top-most command on the stack.</p>
<p>To support command compression, QUndoCommand has an <a href="qundocommand.html#id">id</a>() and the virtual function <a href="qundocommand.html#mergeWith">mergeWith</a>(). These functions are used by <a href="qundostack.html#push">QUndoStack.push</a>().</p>
<p>To support command macros, a QUndoCommand object can have any number of child commands. Undoing or redoing the parent command will cause the child commands to be undone or redone. A command can be assigned to a parent explicitly in the constructor. In this case, the command will be owned by the parent.</p>
<p>The parent in this case is usually an empty command, in that it doesn't provide its own implementation of <a href="qundocommand.html#undo">undo</a>() and <a href="qundocommand.html#redo">redo</a>(). Instead, it uses the base implementations of these functions, which simply call <a href="qundocommand.html#undo">undo</a>() or <a href="qundocommand.html#redo">redo</a>() on all its children. The parent should, however, have a meaningful <a href="qundocommand.html#text">text</a>().</p>
<pre> QUndoCommand *insertRed = new QUndoCommand(); <span class="comment">// an empty command</span>
 insertRed-&gt;setText("insert red text");

 new InsertText(document, idx, text, insertRed); <span class="comment">// becomes child of insertRed</span>
 new SetColor(document, idx, text.length(), Qt.red, insertRed);

 stack.push(insertRed);</pre>
<p>Another way to create macros is to use the convenience functions <a href="qundostack.html#beginMacro">QUndoStack.beginMacro</a>() and <a href="qundostack.html#endMacro">QUndoStack.endMacro</a>().</p>
<p>See also <a href="qundostack.html">QUndoStack</a>.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QUndoCommand" />QUndoCommand.__init__ (<i>self</i>, <a href="qundocommand.html">QUndoCommand</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qundocommand.html">QUndoCommand</a> object with parent <i>parent</i>.</p>
<p>If <i>parent</i> is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.</p>
<p>See also <a href="qundocommand.html#dtor.QUndoCommand">~QUndoCommand</a>().</p>
<h3 class="fn"><a name="QUndoCommand-2" />QUndoCommand.__init__ (<i>self</i>, QString, <a href="qundocommand.html">QUndoCommand</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qundocommand.html">QUndoCommand</a> object with the given <i>parent</i> and <i>text</i>.</p>
<p>If <i>parent</i> is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.</p>
<p>See also <a href="qundocommand.html#dtor.QUndoCommand">~QUndoCommand</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/~QUndoCommand" />
<h3 class="fn"><a name="child" /><a href="qundocommand.html">QUndoCommand</a> QUndoCommand.child (<i>self</i>, int)</h3><p>Returns the child command at <i>index</i>.</p>
<p>This function was introduced in Qt 4.4.</p>
<p>See also <a href="qundocommand.html#childCount">childCount</a>() and <a href="qundostack.html#command">QUndoStack.command</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/childCount" />
<h3 class="fn"><a name="childCount" />int QUndoCommand.childCount (<i>self</i>)</h3><p>Returns the number of child commands in this command.</p>
<p>This function was introduced in Qt 4.4.</p>
<p>See also <a href="qundocommand.html#child">child</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/id" />
<h3 class="fn"><a name="id" />int QUndoCommand.id (<i>self</i>)</h3><p>Returns the ID of this command.</p>
<p>A command ID is used in command compression. It must be an integer unique to this command's class, or -1 if the command doesn't support compression.</p>
<p>If the command supports compression this function must be overridden in the derived class to return the correct ID. The base implementation returns -1.</p>
<p><a href="qundostack.html#push">QUndoStack.push</a>() will only try to merge two commands if they have the same ID, and the ID is not -1.</p>
<p>See also <a href="qundocommand.html#mergeWith">mergeWith</a>() and <a href="qundostack.html#push">QUndoStack.push</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/mergeWith" />
<h3 class="fn"><a name="mergeWith" />bool QUndoCommand.mergeWith (<i>self</i>, <a href="qundocommand.html">QUndoCommand</a>)</h3><p>Attempts to merge this command with <i>command</i>. Returns true on success; otherwise returns false.</p>
<p>If this function returns true, calling this command's <a href="qundocommand.html#redo">redo</a>() must have the same effect as redoing both this command and <i>command</i>. Similarly, calling this command's <a href="qundocommand.html#undo">undo</a>() must have the same effect as undoing <i>command</i> and this command.</p>
<p><a href="qundostack.html">QUndoStack</a> will only try to merge two commands if they have the same id, and the id is not -1.</p>
<p>The default implementation returns false.</p>
<pre> bool AppendText.mergeWith(const QUndoCommand *other)
 {
     if (other-&gt;id() != id()) <span class="comment">// make sure other is also an AppendText command</span>
         return false;
     m_text += static_cast&lt;const AppendText*&gt;(other)-&gt;m_text;
     return true;
 }</pre>
<p>See also <a href="qundocommand.html#id">id</a>() and <a href="qundostack.html#push">QUndoStack.push</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/redo" />
<h3 class="fn"><a name="redo" />QUndoCommand.redo (<i>self</i>)</h3><p>Applies a change to the document. This function must be implemented in the derived class. Calling <a href="qundostack.html#push">QUndoStack.push</a>(), <a href="qundostack.html#undo">QUndoStack.undo</a>() or <a href="qundostack.html#redo">QUndoStack.redo</a>() from this function leads to undefined beahavior.</p>
<p>The default implementation calls redo() on all child commands.</p>
<p>See also <a href="qundocommand.html#undo">undo</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/setText" />
<h3 class="fn"><a name="setText" />QUndoCommand.setText (<i>self</i>, QString)</h3><p>Sets the command's text to be the <i>text</i> specified.</p>
<p>The specified text should be a short user-readable string describing what this command does.</p>
<p>See also <a href="qundocommand.html#text">text</a>(), <a href="qundostack.html#createUndoAction">QUndoStack.createUndoAction</a>(), and <a href="qundostack.html#createRedoAction">QUndoStack.createRedoAction</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/text" />
<h3 class="fn"><a name="text" />QString QUndoCommand.text (<i>self</i>)</h3><p>Returns a short text string describing what this command does; for example, "insert text".</p>
<p>The text is used when the text properties of the stack's undo and redo actions are updated.</p>
<p>See also <a href="qundocommand.html#setText">setText</a>(), <a href="qundostack.html#createUndoAction">QUndoStack.createUndoAction</a>(), and <a href="qundostack.html#createRedoAction">QUndoStack.createRedoAction</a>().</p>
<a name="//apple_ref/cpp/instm/QUndoCommand/undo" />
<h3 class="fn"><a name="undo" />QUndoCommand.undo (<i>self</i>)</h3><p>Reverts a change to the document. After undo() is called, the state of the document should be the same as before <a href="qundocommand.html#redo">redo</a>() was called. This function must be implemented in the derived class. Calling <a href="qundostack.html#push">QUndoStack.push</a>(), <a href="qundostack.html#undo">QUndoStack.undo</a>() or <a href="qundostack.html#redo">QUndoStack.redo</a>() from this function leads to undefined beahavior.</p>
<p>The default implementation calls undo() on all child commands in reverse order.</p>
<p>See also <a href="qundocommand.html#redo">redo</a>().</p>
<p /><address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.7.3 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2010</td><td align="right" width="25%">Qt&#160;4.6.2</td></tr></table></div></address></body></html>