PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/qtbase/src/widgets/util/qundostack.cpp

https://gitlab.com/x33n/phantomjs
C++ | 1179 lines | 439 code | 137 blank | 603 comment | 101 complexity | 179ce45a4449add53b676456f218d8db MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtWidgets module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <QtCore/qdebug.h>
  42. #include "qundostack.h"
  43. #include "qundogroup.h"
  44. #include "qundostack_p.h"
  45. #ifndef QT_NO_UNDOCOMMAND
  46. QT_BEGIN_NAMESPACE
  47. /*!
  48. \class QUndoCommand
  49. \brief The QUndoCommand class is the base class of all commands stored on a QUndoStack.
  50. \since 4.2
  51. \inmodule QtWidgets
  52. For an overview of Qt's Undo Framework, see the
  53. \l{Overview of Qt's Undo Framework}{overview document}.
  54. A QUndoCommand represents a single editing action on a document; for example,
  55. inserting or deleting a block of text in a text editor. QUndoCommand can apply
  56. a change to the document with redo() and undo the change with undo(). The
  57. implementations for these functions must be provided in a derived class.
  58. \snippet code/src_gui_util_qundostack.cpp 0
  59. A QUndoCommand has an associated text(). This is a short string
  60. describing what the command does. It is used to update the text
  61. properties of the stack's undo and redo actions; see
  62. QUndoStack::createUndoAction() and QUndoStack::createRedoAction().
  63. QUndoCommand objects are owned by the stack they were pushed on.
  64. QUndoStack deletes a command if it has been undone and a new command is pushed. For example:
  65. \snippet code/src_gui_util_qundostack.cpp 1
  66. In effect, when a command is pushed, it becomes the top-most command
  67. on the stack.
  68. To support command compression, QUndoCommand has an id() and the virtual function
  69. mergeWith(). These functions are used by QUndoStack::push().
  70. To support command macros, a QUndoCommand object can have any number of child
  71. commands. Undoing or redoing the parent command will cause the child
  72. commands to be undone or redone. A command can be assigned
  73. to a parent explicitly in the constructor. In this case, the command
  74. will be owned by the parent.
  75. The parent in this case is usually an empty command, in that it doesn't
  76. provide its own implementation of undo() and redo(). Instead, it uses
  77. the base implementations of these functions, which simply call undo() or
  78. redo() on all its children. The parent should, however, have a meaningful
  79. text().
  80. \snippet code/src_gui_util_qundostack.cpp 2
  81. Another way to create macros is to use the convenience functions
  82. QUndoStack::beginMacro() and QUndoStack::endMacro().
  83. \sa QUndoStack
  84. */
  85. /*!
  86. Constructs a QUndoCommand object with the given \a parent and \a text.
  87. If \a parent is not 0, this command is appended to parent's child list.
  88. The parent command then owns this command and will delete it in its
  89. destructor.
  90. \sa ~QUndoCommand()
  91. */
  92. QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent)
  93. {
  94. d = new QUndoCommandPrivate;
  95. if (parent != 0)
  96. parent->d->child_list.append(this);
  97. setText(text);
  98. }
  99. /*!
  100. Constructs a QUndoCommand object with parent \a parent.
  101. If \a parent is not 0, this command is appended to parent's child list.
  102. The parent command then owns this command and will delete it in its
  103. destructor.
  104. \sa ~QUndoCommand()
  105. */
  106. QUndoCommand::QUndoCommand(QUndoCommand *parent)
  107. {
  108. d = new QUndoCommandPrivate;
  109. if (parent != 0)
  110. parent->d->child_list.append(this);
  111. }
  112. /*!
  113. Destroys the QUndoCommand object and all child commands.
  114. \sa QUndoCommand()
  115. */
  116. QUndoCommand::~QUndoCommand()
  117. {
  118. qDeleteAll(d->child_list);
  119. delete d;
  120. }
  121. /*!
  122. Returns the ID of this command.
  123. A command ID is used in command compression. It must be an integer unique to
  124. this command's class, or -1 if the command doesn't support compression.
  125. If the command supports compression this function must be overridden in the
  126. derived class to return the correct ID. The base implementation returns -1.
  127. QUndoStack::push() will only try to merge two commands if they have the
  128. same ID, and the ID is not -1.
  129. \sa mergeWith(), QUndoStack::push()
  130. */
  131. int QUndoCommand::id() const
  132. {
  133. return -1;
  134. }
  135. /*!
  136. Attempts to merge this command with \a command. Returns \c true on
  137. success; otherwise returns \c false.
  138. If this function returns \c true, calling this command's redo() must have the same
  139. effect as redoing both this command and \a command.
  140. Similarly, calling this command's undo() must have the same effect as undoing
  141. \a command and this command.
  142. QUndoStack will only try to merge two commands if they have the same id, and
  143. the id is not -1.
  144. The default implementation returns \c false.
  145. \snippet code/src_gui_util_qundostack.cpp 3
  146. \sa id(), QUndoStack::push()
  147. */
  148. bool QUndoCommand::mergeWith(const QUndoCommand *command)
  149. {
  150. Q_UNUSED(command);
  151. return false;
  152. }
  153. /*!
  154. Applies a change to the document. This function must be implemented in
  155. the derived class. Calling QUndoStack::push(),
  156. QUndoStack::undo() or QUndoStack::redo() from this function leads to
  157. undefined beahavior.
  158. The default implementation calls redo() on all child commands.
  159. \sa undo()
  160. */
  161. void QUndoCommand::redo()
  162. {
  163. for (int i = 0; i < d->child_list.size(); ++i)
  164. d->child_list.at(i)->redo();
  165. }
  166. /*!
  167. Reverts a change to the document. After undo() is called, the state of
  168. the document should be the same as before redo() was called. This function must
  169. be implemented in the derived class. Calling QUndoStack::push(),
  170. QUndoStack::undo() or QUndoStack::redo() from this function leads to
  171. undefined beahavior.
  172. The default implementation calls undo() on all child commands in reverse order.
  173. \sa redo()
  174. */
  175. void QUndoCommand::undo()
  176. {
  177. for (int i = d->child_list.size() - 1; i >= 0; --i)
  178. d->child_list.at(i)->undo();
  179. }
  180. /*!
  181. Returns a short text string describing what this command does; for example,
  182. "insert text".
  183. The text is used for names of items in QUndoView.
  184. \sa actionText(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()
  185. */
  186. QString QUndoCommand::text() const
  187. {
  188. return d->text;
  189. }
  190. /*!
  191. \since 4.8
  192. Returns a short text string describing what this command does; for example,
  193. "insert text".
  194. The text is used when the text properties of the stack's undo and redo
  195. actions are updated.
  196. \sa text(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()
  197. */
  198. QString QUndoCommand::actionText() const
  199. {
  200. return d->actionText;
  201. }
  202. /*!
  203. Sets the command's text to be the \a text specified.
  204. The specified text should be a short user-readable string describing what this
  205. command does.
  206. If you need to have two different strings for text() and actionText(), separate
  207. them with "\\n" and pass into this function. Even if you do not use this feature
  208. for English strings during development, you can still let translators use two
  209. different strings in order to match specific languages' needs.
  210. The described feature and the function actionText() are available since Qt 4.8.
  211. \sa text(), actionText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()
  212. */
  213. void QUndoCommand::setText(const QString &text)
  214. {
  215. int cdpos = text.indexOf(QLatin1Char('\n'));
  216. if (cdpos > 0) {
  217. d->text = text.left(cdpos);
  218. d->actionText = text.mid(cdpos + 1);
  219. } else {
  220. d->text = text;
  221. d->actionText = text;
  222. }
  223. }
  224. /*!
  225. \since 4.4
  226. Returns the number of child commands in this command.
  227. \sa child()
  228. */
  229. int QUndoCommand::childCount() const
  230. {
  231. return d->child_list.count();
  232. }
  233. /*!
  234. \since 4.4
  235. Returns the child command at \a index.
  236. \sa childCount(), QUndoStack::command()
  237. */
  238. const QUndoCommand *QUndoCommand::child(int index) const
  239. {
  240. if (index < 0 || index >= d->child_list.count())
  241. return 0;
  242. return d->child_list.at(index);
  243. }
  244. #endif // QT_NO_UNDOCOMMAND
  245. #ifndef QT_NO_UNDOSTACK
  246. /*!
  247. \class QUndoStack
  248. \brief The QUndoStack class is a stack of QUndoCommand objects.
  249. \since 4.2
  250. \inmodule QtWidgets
  251. For an overview of Qt's Undo Framework, see the
  252. \l{Overview of Qt's Undo Framework}{overview document}.
  253. An undo stack maintains a stack of commands that have been applied to a
  254. document.
  255. New commands are pushed on the stack using push(). Commands can be
  256. undone and redone using undo() and redo(), or by triggering the
  257. actions returned by createUndoAction() and createRedoAction().
  258. QUndoStack keeps track of the \a current command. This is the command
  259. which will be executed by the next call to redo(). The index of this
  260. command is returned by index(). The state of the edited object can be
  261. rolled forward or back using setIndex(). If the top-most command on the
  262. stack has already been redone, index() is equal to count().
  263. QUndoStack provides support for undo and redo actions, command
  264. compression, command macros, and supports the concept of a
  265. \e{clean state}.
  266. \section1 Undo and Redo Actions
  267. QUndoStack provides convenient undo and redo QAction objects, which
  268. can be inserted into a menu or a toolbar. When commands are undone or
  269. redone, QUndoStack updates the text properties of these actions
  270. to reflect what change they will trigger. The actions are also disabled
  271. when no command is available for undo or redo. These actions
  272. are returned by QUndoStack::createUndoAction() and QUndoStack::createRedoAction().
  273. \section1 Command Compression and Macros
  274. Command compression is useful when several commands can be compressed
  275. into a single command that can be undone and redone in a single operation.
  276. For example, when a user types a character in a text editor, a new command
  277. is created. This command inserts the character into the document at the
  278. cursor position. However, it is more convenient for the user to be able
  279. to undo or redo typing of whole words, sentences, or paragraphs.
  280. Command compression allows these single-character commands to be merged
  281. into a single command which inserts or deletes sections of text.
  282. For more information, see QUndoCommand::mergeWith() and push().
  283. A command macro is a sequence of commands, all of which are undone and
  284. redone in one go. Command macros are created by giving a command a list
  285. of child commands.
  286. Undoing or redoing the parent command will cause the child commands to
  287. be undone or redone. Command macros may be created explicitly
  288. by specifying a parent in the QUndoCommand constructor, or by using the
  289. convenience functions beginMacro() and endMacro().
  290. Although command compression and macros appear to have the same effect to the
  291. user, they often have different uses in an application. Commands that
  292. perform small changes to a document may be usefully compressed if there is
  293. no need to individually record them, and if only larger changes are relevant
  294. to the user.
  295. However, for commands that need to be recorded individually, or those that
  296. cannot be compressed, it is useful to use macros to provide a more convenient
  297. user experience while maintaining a record of each command.
  298. \section1 Clean State
  299. QUndoStack supports the concept of a clean state. When the
  300. document is saved to disk, the stack can be marked as clean using
  301. setClean(). Whenever the stack returns to this state through undoing and
  302. redoing commands, it emits the signal cleanChanged(). This signal
  303. is also emitted when the stack leaves the clean state. This signal is
  304. usually used to enable and disable the save actions in the application,
  305. and to update the document's title to reflect that it contains unsaved
  306. changes.
  307. \sa QUndoCommand, QUndoView
  308. */
  309. #ifndef QT_NO_ACTION
  310. QUndoAction::QUndoAction(const QString &prefix, QObject *parent)
  311. : QAction(parent)
  312. {
  313. m_prefix = prefix;
  314. }
  315. void QUndoAction::setPrefixedText(const QString &text)
  316. {
  317. if (m_defaultText.isEmpty()) {
  318. QString s = m_prefix;
  319. if (!m_prefix.isEmpty() && !text.isEmpty())
  320. s.append(QLatin1Char(' '));
  321. s.append(text);
  322. setText(s);
  323. } else {
  324. if (text.isEmpty())
  325. setText(m_defaultText);
  326. else
  327. setText(m_prefix.arg(text));
  328. }
  329. }
  330. void QUndoAction::setTextFormat(const QString &textFormat, const QString &defaultText)
  331. {
  332. m_prefix = textFormat;
  333. m_defaultText = defaultText;
  334. }
  335. #endif // QT_NO_ACTION
  336. /*! \internal
  337. Sets the current index to \a idx, emitting appropriate signals. If \a clean is true,
  338. makes \a idx the clean index as well.
  339. */
  340. void QUndoStackPrivate::setIndex(int idx, bool clean)
  341. {
  342. Q_Q(QUndoStack);
  343. bool was_clean = index == clean_index;
  344. if (idx != index) {
  345. index = idx;
  346. emit q->indexChanged(index);
  347. emit q->canUndoChanged(q->canUndo());
  348. emit q->undoTextChanged(q->undoText());
  349. emit q->canRedoChanged(q->canRedo());
  350. emit q->redoTextChanged(q->redoText());
  351. }
  352. if (clean)
  353. clean_index = index;
  354. bool is_clean = index == clean_index;
  355. if (is_clean != was_clean)
  356. emit q->cleanChanged(is_clean);
  357. }
  358. /*! \internal
  359. If the number of commands on the stack exceedes the undo limit, deletes commands from
  360. the bottom of the stack.
  361. Returns \c true if commands were deleted.
  362. */
  363. bool QUndoStackPrivate::checkUndoLimit()
  364. {
  365. if (undo_limit <= 0 || !macro_stack.isEmpty() || undo_limit >= command_list.count())
  366. return false;
  367. int del_count = command_list.count() - undo_limit;
  368. for (int i = 0; i < del_count; ++i)
  369. delete command_list.takeFirst();
  370. index -= del_count;
  371. if (clean_index != -1) {
  372. if (clean_index < del_count)
  373. clean_index = -1; // we've deleted the clean command
  374. else
  375. clean_index -= del_count;
  376. }
  377. return true;
  378. }
  379. /*!
  380. Constructs an empty undo stack with the parent \a parent. The
  381. stack will initially be in the clean state. If \a parent is a
  382. QUndoGroup object, the stack is automatically added to the group.
  383. \sa push()
  384. */
  385. QUndoStack::QUndoStack(QObject *parent)
  386. : QObject(*(new QUndoStackPrivate), parent)
  387. {
  388. #ifndef QT_NO_UNDOGROUP
  389. if (QUndoGroup *group = qobject_cast<QUndoGroup*>(parent))
  390. group->addStack(this);
  391. #endif
  392. }
  393. /*!
  394. Destroys the undo stack, deleting any commands that are on it. If the
  395. stack is in a QUndoGroup, the stack is automatically removed from the group.
  396. \sa QUndoStack()
  397. */
  398. QUndoStack::~QUndoStack()
  399. {
  400. #ifndef QT_NO_UNDOGROUP
  401. Q_D(QUndoStack);
  402. if (d->group != 0)
  403. d->group->removeStack(this);
  404. #endif
  405. clear();
  406. }
  407. /*!
  408. Clears the command stack by deleting all commands on it, and returns the stack
  409. to the clean state.
  410. Commands are not undone or redone; the state of the edited object remains
  411. unchanged.
  412. This function is usually used when the contents of the document are
  413. abandoned.
  414. \sa QUndoStack()
  415. */
  416. void QUndoStack::clear()
  417. {
  418. Q_D(QUndoStack);
  419. if (d->command_list.isEmpty())
  420. return;
  421. bool was_clean = isClean();
  422. d->macro_stack.clear();
  423. qDeleteAll(d->command_list);
  424. d->command_list.clear();
  425. d->index = 0;
  426. d->clean_index = 0;
  427. emit indexChanged(0);
  428. emit canUndoChanged(false);
  429. emit undoTextChanged(QString());
  430. emit canRedoChanged(false);
  431. emit redoTextChanged(QString());
  432. if (!was_clean)
  433. emit cleanChanged(true);
  434. }
  435. /*!
  436. Pushes \a cmd on the stack or merges it with the most recently executed command.
  437. In either case, executes \a cmd by calling its redo() function.
  438. If \a cmd's id is not -1, and if the id is the same as that of the
  439. most recently executed command, QUndoStack will attempt to merge the two
  440. commands by calling QUndoCommand::mergeWith() on the most recently executed
  441. command. If QUndoCommand::mergeWith() returns \c true, \a cmd is deleted.
  442. In all other cases \a cmd is simply pushed on the stack.
  443. If commands were undone before \a cmd was pushed, the current command and
  444. all commands above it are deleted. Hence \a cmd always ends up being the
  445. top-most on the stack.
  446. Once a command is pushed, the stack takes ownership of it. There
  447. are no getters to return the command, since modifying it after it has
  448. been executed will almost always lead to corruption of the document's
  449. state.
  450. \sa QUndoCommand::id(), QUndoCommand::mergeWith()
  451. */
  452. void QUndoStack::push(QUndoCommand *cmd)
  453. {
  454. Q_D(QUndoStack);
  455. cmd->redo();
  456. bool macro = !d->macro_stack.isEmpty();
  457. QUndoCommand *cur = 0;
  458. if (macro) {
  459. QUndoCommand *macro_cmd = d->macro_stack.last();
  460. if (!macro_cmd->d->child_list.isEmpty())
  461. cur = macro_cmd->d->child_list.last();
  462. } else {
  463. if (d->index > 0)
  464. cur = d->command_list.at(d->index - 1);
  465. while (d->index < d->command_list.size())
  466. delete d->command_list.takeLast();
  467. if (d->clean_index > d->index)
  468. d->clean_index = -1; // we've deleted the clean state
  469. }
  470. bool try_merge = cur != 0
  471. && cur->id() != -1
  472. && cur->id() == cmd->id()
  473. && (macro || d->index != d->clean_index);
  474. if (try_merge && cur->mergeWith(cmd)) {
  475. delete cmd;
  476. if (!macro) {
  477. emit indexChanged(d->index);
  478. emit canUndoChanged(canUndo());
  479. emit undoTextChanged(undoText());
  480. emit canRedoChanged(canRedo());
  481. emit redoTextChanged(redoText());
  482. }
  483. } else {
  484. if (macro) {
  485. d->macro_stack.last()->d->child_list.append(cmd);
  486. } else {
  487. d->command_list.append(cmd);
  488. d->checkUndoLimit();
  489. d->setIndex(d->index + 1, false);
  490. }
  491. }
  492. }
  493. /*!
  494. Marks the stack as clean and emits cleanChanged() if the stack was
  495. not already clean.
  496. Whenever the stack returns to this state through the use of undo/redo
  497. commands, it emits the signal cleanChanged(). This signal is also
  498. emitted when the stack leaves the clean state.
  499. \sa isClean(), cleanIndex()
  500. */
  501. void QUndoStack::setClean()
  502. {
  503. Q_D(QUndoStack);
  504. if (!d->macro_stack.isEmpty()) {
  505. qWarning("QUndoStack::setClean(): cannot set clean in the middle of a macro");
  506. return;
  507. }
  508. d->setIndex(d->index, true);
  509. }
  510. /*!
  511. If the stack is in the clean state, returns \c true; otherwise returns \c false.
  512. \sa setClean(), cleanIndex()
  513. */
  514. bool QUndoStack::isClean() const
  515. {
  516. Q_D(const QUndoStack);
  517. if (!d->macro_stack.isEmpty())
  518. return false;
  519. return d->clean_index == d->index;
  520. }
  521. /*!
  522. Returns the clean index. This is the index at which setClean() was called.
  523. A stack may not have a clean index. This happens if a document is saved,
  524. some commands are undone, then a new command is pushed. Since
  525. push() deletes all the undone commands before pushing the new command, the stack
  526. can't return to the clean state again. In this case, this function returns -1.
  527. \sa isClean(), setClean()
  528. */
  529. int QUndoStack::cleanIndex() const
  530. {
  531. Q_D(const QUndoStack);
  532. return d->clean_index;
  533. }
  534. /*!
  535. Undoes the command below the current command by calling QUndoCommand::undo().
  536. Decrements the current command index.
  537. If the stack is empty, or if the bottom command on the stack has already been
  538. undone, this function does nothing.
  539. \sa redo(), index()
  540. */
  541. void QUndoStack::undo()
  542. {
  543. Q_D(QUndoStack);
  544. if (d->index == 0)
  545. return;
  546. if (!d->macro_stack.isEmpty()) {
  547. qWarning("QUndoStack::undo(): cannot undo in the middle of a macro");
  548. return;
  549. }
  550. int idx = d->index - 1;
  551. d->command_list.at(idx)->undo();
  552. d->setIndex(idx, false);
  553. }
  554. /*!
  555. Redoes the current command by calling QUndoCommand::redo(). Increments the current
  556. command index.
  557. If the stack is empty, or if the top command on the stack has already been
  558. redone, this function does nothing.
  559. \sa undo(), index()
  560. */
  561. void QUndoStack::redo()
  562. {
  563. Q_D(QUndoStack);
  564. if (d->index == d->command_list.size())
  565. return;
  566. if (!d->macro_stack.isEmpty()) {
  567. qWarning("QUndoStack::redo(): cannot redo in the middle of a macro");
  568. return;
  569. }
  570. d->command_list.at(d->index)->redo();
  571. d->setIndex(d->index + 1, false);
  572. }
  573. /*!
  574. Returns the number of commands on the stack. Macro commands are counted as
  575. one command.
  576. \sa index(), setIndex(), command()
  577. */
  578. int QUndoStack::count() const
  579. {
  580. Q_D(const QUndoStack);
  581. return d->command_list.size();
  582. }
  583. /*!
  584. Returns the index of the current command. This is the command that will be
  585. executed on the next call to redo(). It is not always the top-most command
  586. on the stack, since a number of commands may have been undone.
  587. \sa undo(), redo(), count()
  588. */
  589. int QUndoStack::index() const
  590. {
  591. Q_D(const QUndoStack);
  592. return d->index;
  593. }
  594. /*!
  595. Repeatedly calls undo() or redo() until the current command index reaches
  596. \a idx. This function can be used to roll the state of the document forwards
  597. of backwards. indexChanged() is emitted only once.
  598. \sa index(), count(), undo(), redo()
  599. */
  600. void QUndoStack::setIndex(int idx)
  601. {
  602. Q_D(QUndoStack);
  603. if (!d->macro_stack.isEmpty()) {
  604. qWarning("QUndoStack::setIndex(): cannot set index in the middle of a macro");
  605. return;
  606. }
  607. if (idx < 0)
  608. idx = 0;
  609. else if (idx > d->command_list.size())
  610. idx = d->command_list.size();
  611. int i = d->index;
  612. while (i < idx)
  613. d->command_list.at(i++)->redo();
  614. while (i > idx)
  615. d->command_list.at(--i)->undo();
  616. d->setIndex(idx, false);
  617. }
  618. /*!
  619. Returns \c true if there is a command available for undo; otherwise returns \c false.
  620. This function returns \c false if the stack is empty, or if the bottom command
  621. on the stack has already been undone.
  622. Synonymous with index() == 0.
  623. \sa index(), canRedo()
  624. */
  625. bool QUndoStack::canUndo() const
  626. {
  627. Q_D(const QUndoStack);
  628. if (!d->macro_stack.isEmpty())
  629. return false;
  630. return d->index > 0;
  631. }
  632. /*!
  633. Returns \c true if there is a command available for redo; otherwise returns \c false.
  634. This function returns \c false if the stack is empty or if the top command
  635. on the stack has already been redone.
  636. Synonymous with index() == count().
  637. \sa index(), canUndo()
  638. */
  639. bool QUndoStack::canRedo() const
  640. {
  641. Q_D(const QUndoStack);
  642. if (!d->macro_stack.isEmpty())
  643. return false;
  644. return d->index < d->command_list.size();
  645. }
  646. /*!
  647. Returns the text of the command which will be undone in the next call to undo().
  648. \sa QUndoCommand::actionText(), redoText()
  649. */
  650. QString QUndoStack::undoText() const
  651. {
  652. Q_D(const QUndoStack);
  653. if (!d->macro_stack.isEmpty())
  654. return QString();
  655. if (d->index > 0)
  656. return d->command_list.at(d->index - 1)->actionText();
  657. return QString();
  658. }
  659. /*!
  660. Returns the text of the command which will be redone in the next call to redo().
  661. \sa QUndoCommand::actionText(), undoText()
  662. */
  663. QString QUndoStack::redoText() const
  664. {
  665. Q_D(const QUndoStack);
  666. if (!d->macro_stack.isEmpty())
  667. return QString();
  668. if (d->index < d->command_list.size())
  669. return d->command_list.at(d->index)->actionText();
  670. return QString();
  671. }
  672. #ifndef QT_NO_ACTION
  673. /*!
  674. Creates an undo QAction object with the given \a parent.
  675. Triggering this action will cause a call to undo(). The text of this action
  676. is the text of the command which will be undone in the next call to undo(),
  677. prefixed by the specified \a prefix. If there is no command available for undo,
  678. this action will be disabled.
  679. If \a prefix is empty, the default template "Undo %1" is used instead of prefix.
  680. Before Qt 4.8, the prefix "Undo" was used by default.
  681. \sa createRedoAction(), canUndo(), QUndoCommand::text()
  682. */
  683. QAction *QUndoStack::createUndoAction(QObject *parent, const QString &prefix) const
  684. {
  685. QUndoAction *result = new QUndoAction(prefix, parent);
  686. if (prefix.isEmpty())
  687. result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action"));
  688. result->setEnabled(canUndo());
  689. result->setPrefixedText(undoText());
  690. connect(this, SIGNAL(canUndoChanged(bool)),
  691. result, SLOT(setEnabled(bool)));
  692. connect(this, SIGNAL(undoTextChanged(QString)),
  693. result, SLOT(setPrefixedText(QString)));
  694. connect(result, SIGNAL(triggered()), this, SLOT(undo()));
  695. return result;
  696. }
  697. /*!
  698. Creates an redo QAction object with the given \a parent.
  699. Triggering this action will cause a call to redo(). The text of this action
  700. is the text of the command which will be redone in the next call to redo(),
  701. prefixed by the specified \a prefix. If there is no command available for redo,
  702. this action will be disabled.
  703. If \a prefix is empty, the default template "Redo %1" is used instead of prefix.
  704. Before Qt 4.8, the prefix "Redo" was used by default.
  705. \sa createUndoAction(), canRedo(), QUndoCommand::text()
  706. */
  707. QAction *QUndoStack::createRedoAction(QObject *parent, const QString &prefix) const
  708. {
  709. QUndoAction *result = new QUndoAction(prefix, parent);
  710. if (prefix.isEmpty())
  711. result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action"));
  712. result->setEnabled(canRedo());
  713. result->setPrefixedText(redoText());
  714. connect(this, SIGNAL(canRedoChanged(bool)),
  715. result, SLOT(setEnabled(bool)));
  716. connect(this, SIGNAL(redoTextChanged(QString)),
  717. result, SLOT(setPrefixedText(QString)));
  718. connect(result, SIGNAL(triggered()), this, SLOT(redo()));
  719. return result;
  720. }
  721. #endif // QT_NO_ACTION
  722. /*!
  723. Begins composition of a macro command with the given \a text description.
  724. An empty command described by the specified \a text is pushed on the stack.
  725. Any subsequent commands pushed on the stack will be appended to the empty
  726. command's children until endMacro() is called.
  727. Calls to beginMacro() and endMacro() may be nested, but every call to
  728. beginMacro() must have a matching call to endMacro().
  729. While a macro is composed, the stack is disabled. This means that:
  730. \list
  731. \li indexChanged() and cleanChanged() are not emitted,
  732. \li canUndo() and canRedo() return false,
  733. \li calling undo() or redo() has no effect,
  734. \li the undo/redo actions are disabled.
  735. \endlist
  736. The stack becomes enabled and appropriate signals are emitted when endMacro()
  737. is called for the outermost macro.
  738. \snippet code/src_gui_util_qundostack.cpp 4
  739. This code is equivalent to:
  740. \snippet code/src_gui_util_qundostack.cpp 5
  741. \sa endMacro()
  742. */
  743. void QUndoStack::beginMacro(const QString &text)
  744. {
  745. Q_D(QUndoStack);
  746. QUndoCommand *cmd = new QUndoCommand();
  747. cmd->setText(text);
  748. if (d->macro_stack.isEmpty()) {
  749. while (d->index < d->command_list.size())
  750. delete d->command_list.takeLast();
  751. if (d->clean_index > d->index)
  752. d->clean_index = -1; // we've deleted the clean state
  753. d->command_list.append(cmd);
  754. } else {
  755. d->macro_stack.last()->d->child_list.append(cmd);
  756. }
  757. d->macro_stack.append(cmd);
  758. if (d->macro_stack.count() == 1) {
  759. emit canUndoChanged(false);
  760. emit undoTextChanged(QString());
  761. emit canRedoChanged(false);
  762. emit redoTextChanged(QString());
  763. }
  764. }
  765. /*!
  766. Ends composition of a macro command.
  767. If this is the outermost macro in a set nested macros, this function emits
  768. indexChanged() once for the entire macro command.
  769. \sa beginMacro()
  770. */
  771. void QUndoStack::endMacro()
  772. {
  773. Q_D(QUndoStack);
  774. if (d->macro_stack.isEmpty()) {
  775. qWarning("QUndoStack::endMacro(): no matching beginMacro()");
  776. return;
  777. }
  778. d->macro_stack.removeLast();
  779. if (d->macro_stack.isEmpty()) {
  780. d->checkUndoLimit();
  781. d->setIndex(d->index + 1, false);
  782. }
  783. }
  784. /*!
  785. \since 4.4
  786. Returns a const pointer to the command at \a index.
  787. This function returns a const pointer, because modifying a command,
  788. once it has been pushed onto the stack and executed, almost always
  789. causes corruption of the state of the document, if the command is
  790. later undone or redone.
  791. \sa QUndoCommand::child()
  792. */
  793. const QUndoCommand *QUndoStack::command(int index) const
  794. {
  795. Q_D(const QUndoStack);
  796. if (index < 0 || index >= d->command_list.count())
  797. return 0;
  798. return d->command_list.at(index);
  799. }
  800. /*!
  801. Returns the text of the command at index \a idx.
  802. \sa beginMacro()
  803. */
  804. QString QUndoStack::text(int idx) const
  805. {
  806. Q_D(const QUndoStack);
  807. if (idx < 0 || idx >= d->command_list.size())
  808. return QString();
  809. return d->command_list.at(idx)->text();
  810. }
  811. /*!
  812. \property QUndoStack::undoLimit
  813. \brief the maximum number of commands on this stack.
  814. \since 4.3
  815. When the number of commands on a stack exceedes the stack's undoLimit, commands are
  816. deleted from the bottom of the stack. Macro commands (commands with child commands)
  817. are treated as one command. The default value is 0, which means that there is no
  818. limit.
  819. This property may only be set when the undo stack is empty, since setting it on a
  820. non-empty stack might delete the command at the current index. Calling setUndoLimit()
  821. on a non-empty stack prints a warning and does nothing.
  822. */
  823. void QUndoStack::setUndoLimit(int limit)
  824. {
  825. Q_D(QUndoStack);
  826. if (!d->command_list.isEmpty()) {
  827. qWarning("QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
  828. return;
  829. }
  830. if (limit == d->undo_limit)
  831. return;
  832. d->undo_limit = limit;
  833. d->checkUndoLimit();
  834. }
  835. int QUndoStack::undoLimit() const
  836. {
  837. Q_D(const QUndoStack);
  838. return d->undo_limit;
  839. }
  840. /*!
  841. \property QUndoStack::active
  842. \brief the active status of this stack.
  843. An application often has multiple undo stacks, one for each opened document. The active
  844. stack is the one associated with the currently active document. If the stack belongs
  845. to a QUndoGroup, calls to QUndoGroup::undo() or QUndoGroup::redo() will be forwarded
  846. to this stack when it is active. If the QUndoGroup is watched by a QUndoView, the view
  847. will display the contents of this stack when it is active. If the stack does not belong to
  848. a QUndoGroup, making it active has no effect.
  849. It is the programmer's responsibility to specify which stack is active by
  850. calling setActive(), usually when the associated document window receives focus.
  851. \sa QUndoGroup
  852. */
  853. void QUndoStack::setActive(bool active)
  854. {
  855. #ifdef QT_NO_UNDOGROUP
  856. Q_UNUSED(active);
  857. #else
  858. Q_D(QUndoStack);
  859. if (d->group != 0) {
  860. if (active)
  861. d->group->setActiveStack(this);
  862. else if (d->group->activeStack() == this)
  863. d->group->setActiveStack(0);
  864. }
  865. #endif
  866. }
  867. bool QUndoStack::isActive() const
  868. {
  869. #ifdef QT_NO_UNDOGROUP
  870. return true;
  871. #else
  872. Q_D(const QUndoStack);
  873. return d->group == 0 || d->group->activeStack() == this;
  874. #endif
  875. }
  876. /*!
  877. \fn void QUndoStack::indexChanged(int idx)
  878. This signal is emitted whenever a command modifies the state of the document.
  879. This happens when a command is undone or redone. When a macro
  880. command is undone or redone, or setIndex() is called, this signal
  881. is emitted only once.
  882. \a idx specifies the index of the current command, ie. the command which will be
  883. executed on the next call to redo().
  884. \sa index(), setIndex()
  885. */
  886. /*!
  887. \fn void QUndoStack::cleanChanged(bool clean)
  888. This signal is emitted whenever the stack enters or leaves the clean state.
  889. If \a clean is true, the stack is in a clean state; otherwise this signal
  890. indicates that it has left the clean state.
  891. \sa isClean(), setClean()
  892. */
  893. /*!
  894. \fn void QUndoStack::undoTextChanged(const QString &undoText)
  895. This signal is emitted whenever the value of undoText() changes. It is
  896. used to update the text property of the undo action returned by createUndoAction().
  897. \a undoText specifies the new text.
  898. */
  899. /*!
  900. \fn void QUndoStack::canUndoChanged(bool canUndo)
  901. This signal is emitted whenever the value of canUndo() changes. It is
  902. used to enable or disable the undo action returned by createUndoAction().
  903. \a canUndo specifies the new value.
  904. */
  905. /*!
  906. \fn void QUndoStack::redoTextChanged(const QString &redoText)
  907. This signal is emitted whenever the value of redoText() changes. It is
  908. used to update the text property of the redo action returned by createRedoAction().
  909. \a redoText specifies the new text.
  910. */
  911. /*!
  912. \fn void QUndoStack::canRedoChanged(bool canRedo)
  913. This signal is emitted whenever the value of canRedo() changes. It is
  914. used to enable or disable the redo action returned by createRedoAction().
  915. \a canRedo specifies the new value.
  916. */
  917. QT_END_NAMESPACE
  918. #endif // QT_NO_UNDOSTACK