/calligra-2.4.92/krita/plugins/extensions/dockers/historydocker/KisUndoView.cpp

# · C++ · 290 lines · 103 code · 51 blank · 136 comment · 7 complexity · d535e6e676a2ca8232d35183b026cb09 MD5 · raw file

  1. /* This file is part of the KDE project
  2. * Copyright (C) 2010 Matus Talcik <matus.talcik@gmail.com>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. /****************************************************************************
  20. **
  21. ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  22. ** All rights reserved.
  23. ** Contact: Nokia Corporation (qt-info@nokia.com)
  24. **
  25. ** This file is part of the QtGui module of the Qt Toolkit.
  26. **
  27. ** $QT_BEGIN_LICENSE:LGPL$
  28. ** No Commercial Usage
  29. ** This file contains pre-release code and may not be distributed.
  30. ** You may use this file in accordance with the terms and conditions
  31. ** contained in the Technology Preview License Agreement accompanying
  32. ** this package.
  33. **
  34. ** GNU Lesser General Public License Usage
  35. ** Alternatively, this file may be used under the terms of the GNU Lesser
  36. ** General Public License version 2.1 as published by the Free Software
  37. ** Foundation and appearing in the file LICENSE.LGPL included in the
  38. ** packaging of this file. Please review the following information to
  39. ** ensure the GNU Lesser General Public License version 2.1 requirements
  40. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  41. **
  42. ** In addition, as a special exception, Nokia gives you certain additional
  43. ** rights. These rights are described in the Nokia Qt LGPL Exception
  44. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  45. **
  46. ** If you have questions regarding the use of this file, please contact
  47. ** Nokia at qt-info@nokia.com.
  48. **
  49. **
  50. **
  51. **
  52. **
  53. **
  54. **
  55. **
  56. ** $QT_END_LICENSE$
  57. **
  58. ****************************************************************************/
  59. #include <kundo2qstack.h>
  60. #include "KisUndoView.h"
  61. #include "KisUndoModel.h"
  62. #ifndef QT_NO_UNDOVIEW
  63. #include <kundo2group.h>
  64. #include <QAbstractItemModel>
  65. #include <QPointer>
  66. #include <QIcon>
  67. /*!
  68. \class KisUndoView
  69. \brief The KisUndoView class displays the contents of a KUndo2QStack.
  70. \since 4.2
  71. \ingroup advanced
  72. KisUndoView is a QListView which displays the list of commands pushed on an undo stack.
  73. The most recently executed command is always selected. Selecting a different command
  74. results in a call to KUndo2QStack::setIndex(), rolling the state of the document
  75. backwards or forward to the new command.
  76. The stack can be set explicitly with setStack(). Alternatively, a KUndo2Group object can
  77. be set with setGroup(). The view will then update itself automatically whenever the
  78. active stack of the group changes.
  79. \image KisUndoView.png
  80. */
  81. class KisUndoViewPrivate
  82. {
  83. public:
  84. KisUndoViewPrivate() :
  85. #ifndef QT_NO_UNDOGROUP
  86. group(0),
  87. #endif
  88. model(0) {}
  89. #ifndef QT_NO_UNDOGROUP
  90. QPointer<KUndo2Group> group;
  91. #endif
  92. KisUndoModel *model;
  93. KisUndoView* q;
  94. void init(KisUndoView* view);
  95. };
  96. void KisUndoViewPrivate::init(KisUndoView* view)
  97. {
  98. q = view;
  99. model = new KisUndoModel(q);
  100. q->setModel(model);
  101. q->setSelectionModel(model->selectionModel());
  102. }
  103. /*!
  104. Constructs a new view with parent \a parent.
  105. */
  106. KisUndoView::KisUndoView(QWidget *parent) : QListView(parent), d(new KisUndoViewPrivate)
  107. {
  108. d->init(this);
  109. }
  110. /*!
  111. Constructs a new view with parent \a parent and sets the observed stack to \a stack.
  112. */
  113. KisUndoView::KisUndoView(KUndo2QStack *stack, QWidget *parent) : QListView(parent), d(new KisUndoViewPrivate)
  114. {
  115. d->init(this);
  116. setStack(stack);
  117. }
  118. #ifndef QT_NO_UNDOGROUP
  119. /*!
  120. Constructs a new view with parent \a parent and sets the observed group to \a group.
  121. The view will update itself autmiatically whenever the active stack of the group changes.
  122. */
  123. KisUndoView::KisUndoView(KUndo2Group *group, QWidget *parent) : QListView(parent), d(new KisUndoViewPrivate)
  124. {
  125. d->init(this);
  126. setGroup(group);
  127. }
  128. #endif // QT_NO_UNDOGROUP
  129. /*!
  130. Destroys this view.
  131. */
  132. KisUndoView::~KisUndoView() {
  133. }
  134. /*!
  135. Returns the stack currently displayed by this view. If the view is looking at a
  136. KUndo2Group, this the group's active stack.
  137. \sa setStack() setGroup()
  138. */
  139. KUndo2QStack *KisUndoView::stack() const
  140. {
  141. return d->model->stack();
  142. }
  143. /*!
  144. Sets the stack displayed by this view to \a stack. If \a stack is 0, the view
  145. will be empty.
  146. If the view was previously looking at a KUndo2Group, the group is set to 0.
  147. \sa stack() setGroup()
  148. */
  149. void KisUndoView::setStack(KUndo2QStack *stack)
  150. {
  151. #ifndef QT_NO_UNDOGROUP
  152. setGroup(0);
  153. #endif
  154. d->model->setStack(stack);
  155. }
  156. #ifndef QT_NO_UNDOGROUP
  157. /*!
  158. Sets the group displayed by this view to \a group. If \a group is 0, the view will
  159. be empty.
  160. The view will update itself autmiatically whenever the active stack of the group changes.
  161. \sa group() setStack()
  162. */
  163. void KisUndoView::setGroup(KUndo2Group *group)
  164. {
  165. if (d->group == group)
  166. return;
  167. if (d->group != 0) {
  168. disconnect(d->group, SIGNAL(activeStackChanged(KUndo2QStack*)),
  169. d->model, SLOT(setStack(KUndo2QStack*)));
  170. }
  171. d->group = group;
  172. if (d->group != 0) {
  173. connect(d->group, SIGNAL(activeStackChanged(KUndo2QStack*)),
  174. d->model, SLOT(setStack(KUndo2QStack*)));
  175. d->model->setStack(d->group->activeStack());
  176. } else {
  177. d->model->setStack(0);
  178. }
  179. }
  180. /*!
  181. Returns the group displayed by this view.
  182. If the view is not looking at group, this function returns 0.
  183. \sa setGroup() setStack()
  184. */
  185. KUndo2Group *KisUndoView::group() const
  186. {
  187. return d->group;
  188. }
  189. #endif // QT_NO_UNDOGROUP
  190. /*!
  191. \property KisUndoView::emptyLabel
  192. \brief the label used for the empty state.
  193. The empty label is the topmost element in the list of commands, which represents
  194. the state of the document before any commands were pushed on the stack. The default
  195. is the string "<empty>".
  196. */
  197. void KisUndoView::setEmptyLabel(const QString &label)
  198. {
  199. d->model->setEmptyLabel(label);
  200. }
  201. QString KisUndoView::emptyLabel() const
  202. {
  203. return d->model->emptyLabel();
  204. }
  205. /*!
  206. \property KisUndoView::cleanIcon
  207. \brief the icon used to represent the clean state.
  208. A stack may have a clean state set with KUndo2QStack::setClean(). This is usually
  209. the state of the document at the point it was saved. KisUndoView can display an
  210. icon in the list of commands to show the clean state. If this property is
  211. a null icon, no icon is shown. The default value is the null icon.
  212. */
  213. void KisUndoView::setCleanIcon(const QIcon &icon)
  214. {
  215. d->model->setCleanIcon(icon);
  216. }
  217. QIcon KisUndoView::cleanIcon() const
  218. {
  219. return d->model->cleanIcon();
  220. }
  221. void KisUndoView::setCanvas(KisCanvas2 *canvas) {
  222. d->model->setCanvas(canvas);
  223. }
  224. #include "KisUndoView.moc"
  225. #endif // QT_NO_UNDOVIEW