Qt 4.8
qundoview.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 
42 #include "qundostack.h"
43 #include "qundoview.h"
44 
45 #ifndef QT_NO_UNDOVIEW
46 
47 #include "qundogroup.h"
48 #include <QtCore/qabstractitemmodel.h>
49 #include <QtCore/qpointer.h>
50 #include <QtGui/qicon.h>
51 #include <private/qlistview_p.h>
52 
54 
56 {
57  Q_OBJECT
58 public:
60 
61  QUndoStack *stack() const;
62 
63  virtual QModelIndex index(int row, int column,
64  const QModelIndex &parent = QModelIndex()) const;
65  virtual QModelIndex parent(const QModelIndex &child) const;
66  virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
67  virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
68  virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
69 
70  QModelIndex selectedIndex() const;
72 
73  QString emptyLabel() const;
74  void setEmptyLabel(const QString &label);
75 
76  void setCleanIcon(const QIcon &icon);
77  QIcon cleanIcon() const;
78 
79 public slots:
80  void setStack(QUndoStack *stack);
81 
82 private slots:
83  void stackChanged();
84  void stackDestroyed(QObject *obj);
85  void setStackCurrentIndex(const QModelIndex &index);
86 
87 private:
92 };
93 
95  : QAbstractItemModel(parent)
96 {
97  m_stack = 0;
98  m_sel_model = new QItemSelectionModel(this, this);
101  m_emty_label = tr("<empty>");
102 }
103 
105 {
106  return m_sel_model;
107 }
108 
110 {
111  return m_stack;
112 }
113 
115 {
116  if (m_stack == stack)
117  return;
118 
119  if (m_stack != 0) {
120  disconnect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
121  disconnect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
123  }
124  m_stack = stack;
125  if (m_stack != 0) {
126  connect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
127  connect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
129  }
130 
131  stackChanged();
132 }
133 
135 {
136  if (obj != m_stack)
137  return;
138  m_stack = 0;
139 
140  stackChanged();
141 }
142 
144 {
145  reset();
147 }
148 
150 {
151  if (m_stack == 0)
152  return;
153 
154  if (index == selectedIndex())
155  return;
156 
157  if (index.column() != 0)
158  return;
159 
160  m_stack->setIndex(index.row());
161 }
162 
164 {
165  return m_stack == 0 ? QModelIndex() : createIndex(m_stack->index(), 0);
166 }
167 
168 QModelIndex QUndoModel::index(int row, int column, const QModelIndex &parent) const
169 {
170  if (m_stack == 0)
171  return QModelIndex();
172 
173  if (parent.isValid())
174  return QModelIndex();
175 
176  if (column != 0)
177  return QModelIndex();
178 
179  if (row < 0 || row > m_stack->count())
180  return QModelIndex();
181 
182  return createIndex(row, column);
183 }
184 
186 {
187  return QModelIndex();
188 }
189 
191 {
192  if (m_stack == 0)
193  return 0;
194 
195  if (parent.isValid())
196  return 0;
197 
198  return m_stack->count() + 1;
199 }
200 
202 {
203  return 1;
204 }
205 
207 {
208  if (m_stack == 0)
209  return QVariant();
210 
211  if (index.column() != 0)
212  return QVariant();
213 
214  if (index.row() < 0 || index.row() > m_stack->count())
215  return QVariant();
216 
217  if (role == Qt::DisplayRole) {
218  if (index.row() == 0)
219  return m_emty_label;
220  return m_stack->text(index.row() - 1);
221  } else if (role == Qt::DecorationRole) {
222  if (index.row() == m_stack->cleanIndex() && !m_clean_icon.isNull())
223  return m_clean_icon;
224  return QVariant();
225  }
226 
227  return QVariant();
228 }
229 
231 {
232  return m_emty_label;
233 }
234 
236 {
237  m_emty_label = label;
238  stackChanged();
239 }
240 
242 {
243  m_clean_icon = icon;
244  stackChanged();
245 }
246 
248 {
249  return m_clean_icon;
250 }
251 
275 {
277 public:
279 #ifndef QT_NO_UNDOGROUP
280  group(0),
281 #endif
282  model(0) {}
283 
284 #ifndef QT_NO_UNDOGROUP
286 #endif
288 
289  void init();
290 };
291 
293 {
294  Q_Q(QUndoView);
295 
296  model = new QUndoModel(q);
297  q->setModel(model);
298  q->setSelectionModel(model->selectionModel());
299 }
300 
306  : QListView(*new QUndoViewPrivate(), parent)
307 {
308  Q_D(QUndoView);
309  d->init();
310 }
311 
317  : QListView(*new QUndoViewPrivate(), parent)
318 {
319  Q_D(QUndoView);
320  d->init();
321  setStack(stack);
322 }
323 
324 #ifndef QT_NO_UNDOGROUP
325 
333  : QListView(*new QUndoViewPrivate(), parent)
334 {
335  Q_D(QUndoView);
336  d->init();
337  setGroup(group);
338 }
339 
340 #endif // QT_NO_UNDOGROUP
341 
347 {
348 }
349 
358 {
359  Q_D(const QUndoView);
360  return d->model->stack();
361 }
362 
373 {
374  Q_D(QUndoView);
375 #ifndef QT_NO_UNDOGROUP
376  setGroup(0);
377 #endif
378  d->model->setStack(stack);
379 }
380 
381 #ifndef QT_NO_UNDOGROUP
382 
393 {
394  Q_D(QUndoView);
395 
396  if (d->group == group)
397  return;
398 
399  if (d->group != 0) {
400  disconnect(d->group, SIGNAL(activeStackChanged(QUndoStack*)),
401  d->model, SLOT(setStack(QUndoStack*)));
402  }
403 
404  d->group = group;
405 
406  if (d->group != 0) {
407  connect(d->group, SIGNAL(activeStackChanged(QUndoStack*)),
408  d->model, SLOT(setStack(QUndoStack*)));
409  d->model->setStack(d->group->activeStack());
410  } else {
411  d->model->setStack(0);
412  }
413 }
414 
424 {
425  Q_D(const QUndoView);
426  return d->group;
427 }
428 
429 #endif // QT_NO_UNDOGROUP
430 
444 {
445  Q_D(QUndoView);
446  d->model->setEmptyLabel(label);
447 }
448 
450 {
451  Q_D(const QUndoView);
452  return d->model->emptyLabel();
453 }
454 
469 {
470  Q_D(const QUndoView);
471  d->model->setCleanIcon(icon);
472 
473 }
474 
476 {
477  Q_D(const QUndoView);
478  return d->model->cleanIcon();
479 }
480 
482 
483 #include "qundoview.moc"
484 
485 #endif // QT_NO_UNDOVIEW
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void setStack(QUndoStack *stack)
Sets the stack displayed by this view to stack.
Definition: qundoview.cpp:372
double d
Definition: qnumeric_p.h:62
void setCleanIcon(const QIcon &icon)
Definition: qundoview.cpp:241
QString text(int idx) const
Returns the text of the command at index idx.
The QItemSelectionModel class keeps track of a view&#39;s selected items.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setEmptyLabel(const QString &label)
Definition: qundoview.cpp:443
QString emptyLabel() const
void stackChanged()
Definition: qundoview.cpp:143
QUndoModel(QObject *parent=0)
Definition: qundoview.cpp:94
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
QUndoGroup * group() const
Returns the group displayed by this view.
Definition: qundoview.cpp:423
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QPointer< QUndoGroup > group
Definition: qundoview.cpp:285
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QIcon cleanIcon() const
#define Q_D(Class)
Definition: qglobal.h:2482
void setStack(QUndoStack *stack)
Definition: qundoview.cpp:114
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows under the given parent.
Definition: qundoview.cpp:190
#define Q_Q(Class)
Definition: qglobal.h:2483
The QUndoGroup class is a group of QUndoStack objects.
Definition: qundogroup.h:60
void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Sets the model item index to be the current item, and emits currentChanged().
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QUndoView class displays the contents of a QUndoStack.
Definition: qundoview.h:61
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the item in the model specified by the given row, column and parent index...
Definition: qundoview.cpp:168
QItemSelectionModel * selectionModel() const
Definition: qundoview.cpp:104
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Returns the data stored under the given role for the item referred to by the index.
Definition: qundoview.cpp:206
QItemSelectionModel * m_sel_model
Definition: qundoview.cpp:89
void setStackCurrentIndex(const QModelIndex &index)
Definition: qundoview.cpp:149
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
static bool init
int row() const
Returns the row this model index refers to.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the children of the given parent.
Definition: qundoview.cpp:201
void setCleanIcon(const QIcon &icon)
Definition: qundoview.cpp:468
QUndoView(QWidget *parent=0)
Constructs a new view with parent parent.
Definition: qundoview.cpp:305
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition: qicon.cpp:769
QString emptyLabel() const
Definition: qundoview.cpp:230
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91
QString m_emty_label
Definition: qundoview.cpp:90
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QAbstractItemModel class provides the abstract interface for item model classes.
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
void stackDestroyed(QObject *obj)
Definition: qundoview.cpp:134
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
~QUndoView()
Destroys this view.
Definition: qundoview.cpp:346
The QListView class provides a list or icon view onto a model.
Definition: qlistview.h:57
#define QT_NO_UNDOGROUP
int cleanIndex() const
Returns the clean index.
Definition: qundostack.cpp:694
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QUndoModel * model
Definition: qundoview.cpp:287
The QModelIndex class is used to locate data in a data model.
QUndoStack * stack() const
Returns the stack currently displayed by this view.
Definition: qundoview.cpp:357
QModelIndex selectedIndex() const
Definition: qundoview.cpp:163
QUndoStack * m_stack
Definition: qundoview.cpp:88
int count() const
Returns the number of commands on the stack.
Definition: qundostack.cpp:758
void setIndex(int idx)
Repeatedly calls undo() or redo() until the current command index reaches idx.
Definition: qundostack.cpp:786
QUndoStack * stack() const
Definition: qundoview.cpp:109
QIcon m_clean_icon
Definition: qundoview.cpp:91
#define slots
Definition: qobjectdefs.h:68
QIcon cleanIcon() const
Definition: qundoview.cpp:247
int index() const
Returns the index of the current command.
Definition: qundostack.cpp:772
void setGroup(QUndoGroup *group)
Sets the group displayed by this view to group.
Definition: qundoview.cpp:392
void reset()
Resets the model to its original state in any attached views.
int column() const
Returns the column this model index refers to.
void setEmptyLabel(const QString &label)
Definition: qundoview.cpp:235
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60