util/qundoview.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
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 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 -
53QT_BEGIN_NAMESPACE -
54 -
55class QUndoModel : public QAbstractItemModel -
56{ -
57 Q_OBJECT -
58public: -
59 QUndoModel(QObject *parent = 0); -
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; -
71 QItemSelectionModel *selectionModel() const; -
72 -
73 QString emptyLabel() const; -
74 void setEmptyLabel(const QString &label); -
75 -
76 void setCleanIcon(const QIcon &icon); -
77 QIcon cleanIcon() const; -
78 -
79public slots: -
80 void setStack(QUndoStack *stack); -
81 -
82private slots: -
83 void stackChanged(); -
84 void stackDestroyed(QObject *obj); -
85 void setStackCurrentIndex(const QModelIndex &index); -
86 -
87private: -
88 QUndoStack *m_stack; -
89 QItemSelectionModel *m_sel_model; -
90 QString m_emty_label; -
91 QIcon m_clean_icon; -
92}; -
93 -
94QUndoModel::QUndoModel(QObject *parent) -
95 : QAbstractItemModel(parent) -
96{ -
97 m_stack = 0;
never executed (the execution status of this line is deduced): m_stack = 0;
-
98 m_sel_model = new QItemSelectionModel(this, this);
never executed (the execution status of this line is deduced): m_sel_model = new QItemSelectionModel(this, this);
-
99 connect(m_sel_model, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
never executed (the execution status of this line is deduced): connect(m_sel_model, "2""currentChanged(QModelIndex,QModelIndex)",
-
100 this, SLOT(setStackCurrentIndex(QModelIndex)));
never executed (the execution status of this line is deduced): this, "1""setStackCurrentIndex(QModelIndex)");
-
101 m_emty_label = tr("<empty>");
never executed (the execution status of this line is deduced): m_emty_label = tr("<empty>");
-
102}
never executed: }
0
103 -
104QItemSelectionModel *QUndoModel::selectionModel() const -
105{ -
106 return m_sel_model;
never executed: return m_sel_model;
0
107} -
108 -
109QUndoStack *QUndoModel::stack() const -
110{ -
111 return m_stack;
never executed: return m_stack;
0
112} -
113 -
114void QUndoModel::setStack(QUndoStack *stack) -
115{ -
116 if (m_stack == stack)
never evaluated: m_stack == stack
0
117 return;
never executed: return;
0
118 -
119 if (m_stack != 0) {
never evaluated: m_stack != 0
0
120 disconnect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
never executed (the execution status of this line is deduced): disconnect(m_stack, "2""cleanChanged(bool)", this, "1""stackChanged()");
-
121 disconnect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
never executed (the execution status of this line is deduced): disconnect(m_stack, "2""indexChanged(int)", this, "1""stackChanged()");
-
122 disconnect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*)));
never executed (the execution status of this line is deduced): disconnect(m_stack, "2""destroyed(QObject*)", this, "1""stackDestroyed(QObject*)");
-
123 }
never executed: }
0
124 m_stack = stack;
never executed (the execution status of this line is deduced): m_stack = stack;
-
125 if (m_stack != 0) {
never evaluated: m_stack != 0
0
126 connect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
never executed (the execution status of this line is deduced): connect(m_stack, "2""cleanChanged(bool)", this, "1""stackChanged()");
-
127 connect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
never executed (the execution status of this line is deduced): connect(m_stack, "2""indexChanged(int)", this, "1""stackChanged()");
-
128 connect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*)));
never executed (the execution status of this line is deduced): connect(m_stack, "2""destroyed(QObject*)", this, "1""stackDestroyed(QObject*)");
-
129 }
never executed: }
0
130 -
131 stackChanged();
never executed (the execution status of this line is deduced): stackChanged();
-
132}
never executed: }
0
133 -
134void QUndoModel::stackDestroyed(QObject *obj) -
135{ -
136 if (obj != m_stack)
never evaluated: obj != m_stack
0
137 return;
never executed: return;
0
138 m_stack = 0;
never executed (the execution status of this line is deduced): m_stack = 0;
-
139 -
140 stackChanged();
never executed (the execution status of this line is deduced): stackChanged();
-
141}
never executed: }
0
142 -
143void QUndoModel::stackChanged() -
144{ -
145 beginResetModel();
never executed (the execution status of this line is deduced): beginResetModel();
-
146 endResetModel();
never executed (the execution status of this line is deduced): endResetModel();
-
147 m_sel_model->setCurrentIndex(selectedIndex(), QItemSelectionModel::ClearAndSelect);
never executed (the execution status of this line is deduced): m_sel_model->setCurrentIndex(selectedIndex(), QItemSelectionModel::ClearAndSelect);
-
148}
never executed: }
0
149 -
150void QUndoModel::setStackCurrentIndex(const QModelIndex &index) -
151{ -
152 if (m_stack == 0)
never evaluated: m_stack == 0
0
153 return;
never executed: return;
0
154 -
155 if (index == selectedIndex())
never evaluated: index == selectedIndex()
0
156 return;
never executed: return;
0
157 -
158 if (index.column() != 0)
never evaluated: index.column() != 0
0
159 return;
never executed: return;
0
160 -
161 m_stack->setIndex(index.row());
never executed (the execution status of this line is deduced): m_stack->setIndex(index.row());
-
162}
never executed: }
0
163 -
164QModelIndex QUndoModel::selectedIndex() const -
165{ -
166 return m_stack == 0 ? QModelIndex() : createIndex(m_stack->index(), 0);
never executed: return m_stack == 0 ? QModelIndex() : createIndex(m_stack->index(), 0);
0
167} -
168 -
169QModelIndex QUndoModel::index(int row, int column, const QModelIndex &parent) const -
170{ -
171 if (m_stack == 0)
never evaluated: m_stack == 0
0
172 return QModelIndex();
never executed: return QModelIndex();
0
173 -
174 if (parent.isValid())
never evaluated: parent.isValid()
0
175 return QModelIndex();
never executed: return QModelIndex();
0
176 -
177 if (column != 0)
never evaluated: column != 0
0
178 return QModelIndex();
never executed: return QModelIndex();
0
179 -
180 if (row < 0 || row > m_stack->count())
never evaluated: row < 0
never evaluated: row > m_stack->count()
0
181 return QModelIndex();
never executed: return QModelIndex();
0
182 -
183 return createIndex(row, column);
never executed: return createIndex(row, column);
0
184} -
185 -
186QModelIndex QUndoModel::parent(const QModelIndex&) const -
187{ -
188 return QModelIndex();
never executed: return QModelIndex();
0
189} -
190 -
191int QUndoModel::rowCount(const QModelIndex &parent) const -
192{ -
193 if (m_stack == 0)
never evaluated: m_stack == 0
0
194 return 0;
never executed: return 0;
0
195 -
196 if (parent.isValid())
never evaluated: parent.isValid()
0
197 return 0;
never executed: return 0;
0
198 -
199 return m_stack->count() + 1;
never executed: return m_stack->count() + 1;
0
200} -
201 -
202int QUndoModel::columnCount(const QModelIndex&) const -
203{ -
204 return 1;
never executed: return 1;
0
205} -
206 -
207QVariant QUndoModel::data(const QModelIndex &index, int role) const -
208{ -
209 if (m_stack == 0)
never evaluated: m_stack == 0
0
210 return QVariant();
never executed: return QVariant();
0
211 -
212 if (index.column() != 0)
never evaluated: index.column() != 0
0
213 return QVariant();
never executed: return QVariant();
0
214 -
215 if (index.row() < 0 || index.row() > m_stack->count())
never evaluated: index.row() < 0
never evaluated: index.row() > m_stack->count()
0
216 return QVariant();
never executed: return QVariant();
0
217 -
218 if (role == Qt::DisplayRole) {
never evaluated: role == Qt::DisplayRole
0
219 if (index.row() == 0)
never evaluated: index.row() == 0
0
220 return m_emty_label;
never executed: return m_emty_label;
0
221 return m_stack->text(index.row() - 1);
never executed: return m_stack->text(index.row() - 1);
0
222 } else if (role == Qt::DecorationRole) {
never evaluated: role == Qt::DecorationRole
0
223 if (index.row() == m_stack->cleanIndex() && !m_clean_icon.isNull())
never evaluated: index.row() == m_stack->cleanIndex()
never evaluated: !m_clean_icon.isNull()
0
224 return m_clean_icon;
never executed: return m_clean_icon;
0
225 return QVariant();
never executed: return QVariant();
0
226 } -
227 -
228 return QVariant();
never executed: return QVariant();
0
229} -
230 -
231QString QUndoModel::emptyLabel() const -
232{ -
233 return m_emty_label;
never executed: return m_emty_label;
0
234} -
235 -
236void QUndoModel::setEmptyLabel(const QString &label) -
237{ -
238 m_emty_label = label;
never executed (the execution status of this line is deduced): m_emty_label = label;
-
239 stackChanged();
never executed (the execution status of this line is deduced): stackChanged();
-
240}
never executed: }
0
241 -
242void QUndoModel::setCleanIcon(const QIcon &icon) -
243{ -
244 m_clean_icon = icon;
never executed (the execution status of this line is deduced): m_clean_icon = icon;
-
245 stackChanged();
never executed (the execution status of this line is deduced): stackChanged();
-
246}
never executed: }
0
247 -
248QIcon QUndoModel::cleanIcon() const -
249{ -
250 return m_clean_icon;
never executed: return m_clean_icon;
0
251} -
252 -
253/*! -
254 \class QUndoView -
255 \brief The QUndoView class displays the contents of a QUndoStack. -
256 \since 4.2 -
257 -
258 \ingroup advanced -
259 \inmodule QtWidgets -
260 -
261 QUndoView is a QListView which displays the list of commands pushed on an undo stack. -
262 The most recently executed command is always selected. Selecting a different command -
263 results in a call to QUndoStack::setIndex(), rolling the state of the document -
264 backwards or forward to the new command. -
265 -
266 The stack can be set explicitly with setStack(). Alternatively, a QUndoGroup object can -
267 be set with setGroup(). The view will then update itself automatically whenever the -
268 active stack of the group changes. -
269 -
270 \image qundoview.png -
271*/ -
272 -
273class QUndoViewPrivate : public QListViewPrivate -
274{ -
275 Q_DECLARE_PUBLIC(QUndoView) -
276public: -
277 QUndoViewPrivate() : -
278#ifndef QT_NO_UNDOGROUP -
279 group(0), -
280#endif -
281 model(0) {}
never executed: }
0
282 -
283#ifndef QT_NO_UNDOGROUP -
284 QPointer<QUndoGroup> group; -
285#endif -
286 QUndoModel *model; -
287 -
288 void init(); -
289}; -
290 -
291void QUndoViewPrivate::init() -
292{ -
293 Q_Q(QUndoView);
never executed (the execution status of this line is deduced): QUndoView * const q = q_func();
-
294 -
295 model = new QUndoModel(q);
never executed (the execution status of this line is deduced): model = new QUndoModel(q);
-
296 q->setModel(model);
never executed (the execution status of this line is deduced): q->setModel(model);
-
297 q->setSelectionModel(model->selectionModel());
never executed (the execution status of this line is deduced): q->setSelectionModel(model->selectionModel());
-
298}
never executed: }
0
299 -
300/*! -
301 Constructs a new view with parent \a parent. -
302*/ -
303 -
304QUndoView::QUndoView(QWidget *parent) -
305 : QListView(*new QUndoViewPrivate(), parent) -
306{ -
307 Q_D(QUndoView);
never executed (the execution status of this line is deduced): QUndoViewPrivate * const d = d_func();
-
308 d->init();
never executed (the execution status of this line is deduced): d->init();
-
309}
never executed: }
0
310 -
311/*! -
312 Constructs a new view with parent \a parent and sets the observed stack to \a stack. -
313*/ -
314 -
315QUndoView::QUndoView(QUndoStack *stack, QWidget *parent) -
316 : QListView(*new QUndoViewPrivate(), parent) -
317{ -
318 Q_D(QUndoView);
never executed (the execution status of this line is deduced): QUndoViewPrivate * const d = d_func();
-
319 d->init();
never executed (the execution status of this line is deduced): d->init();
-
320 setStack(stack);
never executed (the execution status of this line is deduced): setStack(stack);
-
321}
never executed: }
0
322 -
323#ifndef QT_NO_UNDOGROUP -
324 -
325/*! -
326 Constructs a new view with parent \a parent and sets the observed group to \a group. -
327 -
328 The view will update itself autmiatically whenever the active stack of the group changes. -
329*/ -
330 -
331QUndoView::QUndoView(QUndoGroup *group, QWidget *parent) -
332 : QListView(*new QUndoViewPrivate(), parent) -
333{ -
334 Q_D(QUndoView);
never executed (the execution status of this line is deduced): QUndoViewPrivate * const d = d_func();
-
335 d->init();
never executed (the execution status of this line is deduced): d->init();
-
336 setGroup(group);
never executed (the execution status of this line is deduced): setGroup(group);
-
337}
never executed: }
0
338 -
339#endif // QT_NO_UNDOGROUP -
340 -
341/*! -
342 Destroys this view. -
343*/ -
344 -
345QUndoView::~QUndoView() -
346{ -
347} -
348 -
349/*! -
350 Returns the stack currently displayed by this view. If the view is looking at a -
351 QUndoGroup, this the group's active stack. -
352 -
353 \sa setStack(), setGroup() -
354*/ -
355 -
356QUndoStack *QUndoView::stack() const -
357{ -
358 Q_D(const QUndoView);
never executed (the execution status of this line is deduced): const QUndoViewPrivate * const d = d_func();
-
359 return d->model->stack();
never executed: return d->model->stack();
0
360} -
361 -
362/*! -
363 Sets the stack displayed by this view to \a stack. If \a stack is 0, the view -
364 will be empty. -
365 -
366 If the view was previously looking at a QUndoGroup, the group is set to 0. -
367 -
368 \sa stack(), setGroup() -
369*/ -
370 -
371void QUndoView::setStack(QUndoStack *stack) -
372{ -
373 Q_D(QUndoView);
never executed (the execution status of this line is deduced): QUndoViewPrivate * const d = d_func();
-
374#ifndef QT_NO_UNDOGROUP -
375 setGroup(0);
never executed (the execution status of this line is deduced): setGroup(0);
-
376#endif -
377 d->model->setStack(stack);
never executed (the execution status of this line is deduced): d->model->setStack(stack);
-
378}
never executed: }
0
379 -
380#ifndef QT_NO_UNDOGROUP -
381 -
382/*! -
383 Sets the group displayed by this view to \a group. If \a group is 0, the view will -
384 be empty. -
385 -
386 The view will update itself autmiatically whenever the active stack of the group changes. -
387 -
388 \sa group(), setStack() -
389*/ -
390 -
391void QUndoView::setGroup(QUndoGroup *group) -
392{ -
393 Q_D(QUndoView);
never executed (the execution status of this line is deduced): QUndoViewPrivate * const d = d_func();
-
394 -
395 if (d->group == group)
never evaluated: d->group == group
0
396 return;
never executed: return;
0
397 -
398 if (d->group != 0) {
never evaluated: d->group != 0
0
399 disconnect(d->group, SIGNAL(activeStackChanged(QUndoStack*)),
never executed (the execution status of this line is deduced): disconnect(d->group, "2""activeStackChanged(QUndoStack*)",
-
400 d->model, SLOT(setStack(QUndoStack*)));
never executed (the execution status of this line is deduced): d->model, "1""setStack(QUndoStack*)");
-
401 }
never executed: }
0
402 -
403 d->group = group;
never executed (the execution status of this line is deduced): d->group = group;
-
404 -
405 if (d->group != 0) {
never evaluated: d->group != 0
0
406 connect(d->group, SIGNAL(activeStackChanged(QUndoStack*)),
never executed (the execution status of this line is deduced): connect(d->group, "2""activeStackChanged(QUndoStack*)",
-
407 d->model, SLOT(setStack(QUndoStack*)));
never executed (the execution status of this line is deduced): d->model, "1""setStack(QUndoStack*)");
-
408 d->model->setStack(d->group->activeStack());
never executed (the execution status of this line is deduced): d->model->setStack(d->group->activeStack());
-
409 } else {
never executed: }
0
410 d->model->setStack(0);
never executed (the execution status of this line is deduced): d->model->setStack(0);
-
411 }
never executed: }
0
412} -
413 -
414/*! -
415 Returns the group displayed by this view. -
416 -
417 If the view is not looking at group, this function returns 0. -
418 -
419 \sa setGroup(), setStack() -
420*/ -
421 -
422QUndoGroup *QUndoView::group() const -
423{ -
424 Q_D(const QUndoView);
never executed (the execution status of this line is deduced): const QUndoViewPrivate * const d = d_func();
-
425 return d->group;
never executed: return d->group;
0
426} -
427 -
428#endif // QT_NO_UNDOGROUP -
429 -
430/*! -
431 \property QUndoView::emptyLabel -
432 \brief the label used for the empty state. -
433 -
434 The empty label is the topmost element in the list of commands, which represents -
435 the state of the document before any commands were pushed on the stack. The default -
436 is the string "<empty>". -
437*/ -
438 -
439void QUndoView::setEmptyLabel(const QString &label) -
440{ -
441 Q_D(QUndoView);
never executed (the execution status of this line is deduced): QUndoViewPrivate * const d = d_func();
-
442 d->model->setEmptyLabel(label);
never executed (the execution status of this line is deduced): d->model->setEmptyLabel(label);
-
443}
never executed: }
0
444 -
445QString QUndoView::emptyLabel() const -
446{ -
447 Q_D(const QUndoView);
never executed (the execution status of this line is deduced): const QUndoViewPrivate * const d = d_func();
-
448 return d->model->emptyLabel();
never executed: return d->model->emptyLabel();
0
449} -
450 -
451/*! -
452 \property QUndoView::cleanIcon -
453 \brief the icon used to represent the clean state. -
454 -
455 A stack may have a clean state set with QUndoStack::setClean(). This is usually -
456 the state of the document at the point it was saved. QUndoView can display an -
457 icon in the list of commands to show the clean state. If this property is -
458 a null icon, no icon is shown. The default value is the null icon. -
459*/ -
460 -
461void QUndoView::setCleanIcon(const QIcon &icon) -
462{ -
463 Q_D(const QUndoView);
never executed (the execution status of this line is deduced): const QUndoViewPrivate * const d = d_func();
-
464 d->model->setCleanIcon(icon);
never executed (the execution status of this line is deduced): d->model->setCleanIcon(icon);
-
465 -
466}
never executed: }
0
467 -
468QIcon QUndoView::cleanIcon() const -
469{ -
470 Q_D(const QUndoView);
never executed (the execution status of this line is deduced): const QUndoViewPrivate * const d = d_func();
-
471 return d->model->cleanIcon();
never executed: return d->model->cleanIcon();
0
472} -
473 -
474QT_END_NAMESPACE -
475 -
476#include "qundoview.moc" -
477 -
478#endif // QT_NO_UNDOVIEW -
479 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial