OpenCoverage

qundoview.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9