Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qundoview.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qundoview.h"
5
6#if QT_CONFIG(undogroup)
7#include <QtGui/qundogroup.h>
8#endif
9#include <QtGui/qundostack.h>
10#include <QtCore/qabstractitemmodel.h>
11#include <QtCore/qpointer.h>
12#include <QtGui/qicon.h>
13#include <private/qlistview_p.h>
14
16
18{
20public:
21 QUndoModel(QObject *parent = nullptr);
22
23 QUndoStack *stack() const;
24
25 virtual QModelIndex index(int row, int column,
26 const QModelIndex &parent = QModelIndex()) const override;
27 virtual QModelIndex parent(const QModelIndex &child) const override;
28 virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
29 virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
30 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
31
34
35 QString emptyLabel() const;
36 void setEmptyLabel(const QString &label);
37
38 void setCleanIcon(const QIcon &icon);
39 QIcon cleanIcon() const;
40
41public slots:
42 void setStack(QUndoStack *stack);
43
44private slots:
45 void stackChanged();
46 void stackDestroyed(QObject *obj);
47 void setStackCurrentIndex(const QModelIndex &index);
48
49private:
50 QUndoStack *m_stack;
51 QItemSelectionModel *m_sel_model;
52 QString m_emty_label;
53 QIcon m_clean_icon;
54};
55
57 : QAbstractItemModel(parent)
58{
59 m_stack = nullptr;
60 m_sel_model = new QItemSelectionModel(this, this);
61 connect(m_sel_model, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
62 this, SLOT(setStackCurrentIndex(QModelIndex)));
63 m_emty_label = tr("<empty>");
64}
65
67{
68 return m_sel_model;
69}
70
71QUndoStack *QUndoModel::stack() const
72{
73 return m_stack;
74}
75
76void QUndoModel::setStack(QUndoStack *stack)
77{
78 if (m_stack == stack)
79 return;
80
81 if (m_stack != nullptr) {
82 disconnect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
83 disconnect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
84 disconnect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*)));
85 }
86 m_stack = stack;
87 if (m_stack != nullptr) {
88 connect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
89 connect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
90 connect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*)));
91 }
92
93 stackChanged();
94}
95
96void QUndoModel::stackDestroyed(QObject *obj)
97{
98 if (obj != m_stack)
99 return;
100 m_stack = nullptr;
101
102 stackChanged();
103}
104
105void QUndoModel::stackChanged()
106{
110}
111
112void QUndoModel::setStackCurrentIndex(const QModelIndex &index)
113{
114 if (m_stack == nullptr)
115 return;
116
117 if (index == selectedIndex())
118 return;
119
120 if (index.column() != 0)
121 return;
122
123 m_stack->setIndex(index.row());
124}
125
127{
128 return m_stack == nullptr ? QModelIndex() : createIndex(m_stack->index(), 0);
129}
130
131QModelIndex QUndoModel::index(int row, int column, const QModelIndex &parent) const
132{
133 if (m_stack == nullptr)
134 return QModelIndex();
135
136 if (parent.isValid())
137 return QModelIndex();
138
139 if (column != 0)
140 return QModelIndex();
141
142 if (row < 0 || row > m_stack->count())
143 return QModelIndex();
144
145 return createIndex(row, column);
146}
147
149{
150 return QModelIndex();
151}
152
153int QUndoModel::rowCount(const QModelIndex &parent) const
154{
155 if (m_stack == nullptr)
156 return 0;
157
158 if (parent.isValid())
159 return 0;
160
161 return m_stack->count() + 1;
162}
163
165{
166 return 1;
167}
168
170{
171 if (m_stack == nullptr)
172 return QVariant();
173
174 if (index.column() != 0)
175 return QVariant();
176
177 if (index.row() < 0 || index.row() > m_stack->count())
178 return QVariant();
179
180 if (role == Qt::DisplayRole) {
181 if (index.row() == 0)
182 return m_emty_label;
183 return m_stack->text(index.row() - 1);
184 } else if (role == Qt::DecorationRole) {
185 if (index.row() == m_stack->cleanIndex() && !m_clean_icon.isNull())
186 return m_clean_icon;
187 return QVariant();
188 }
189
190 return QVariant();
191}
192
194{
195 return m_emty_label;
196}
197
199{
200 m_emty_label = label;
201 stackChanged();
202}
203
205{
206 m_clean_icon = icon;
207 stackChanged();
208}
209
211{
212 return m_clean_icon;
213}
214
236{
237 Q_DECLARE_PUBLIC(QUndoView)
238public:
240#if QT_CONFIG(undogroup)
241 group(nullptr),
242#endif
243 model(nullptr) {}
244
245#if QT_CONFIG(undogroup)
246 QPointer<QUndoGroup> group;
247#endif
249
250 void init();
251};
252
254{
255 Q_Q(QUndoView);
256
257 model = new QUndoModel(q);
258 q->setModel(model);
259 q->setSelectionModel(model->selectionModel());
260}
261
267 : QListView(*new QUndoViewPrivate(), parent)
268{
269 Q_D(QUndoView);
270 d->init();
271}
272
277QUndoView::QUndoView(QUndoStack *stack, QWidget *parent)
278 : QListView(*new QUndoViewPrivate(), parent)
279{
280 Q_D(QUndoView);
281 d->init();
283}
284
285#if QT_CONFIG(undogroup)
286
294 : QListView(*new QUndoViewPrivate(), parent)
295{
296 Q_D(QUndoView);
297 d->init();
298 setGroup(group);
299}
300
301#endif // QT_CONFIG(undogroup)
302
310
318QUndoStack *QUndoView::stack() const
319{
320 Q_D(const QUndoView);
321 return d->model->stack();
322}
323
333void QUndoView::setStack(QUndoStack *stack)
334{
335 Q_D(QUndoView);
336#if QT_CONFIG(undogroup)
337 setGroup(nullptr);
338#endif
339 d->model->setStack(stack);
340}
341
342#if QT_CONFIG(undogroup)
343
353void QUndoView::setGroup(QUndoGroup *group)
354{
355 Q_D(QUndoView);
356
357 if (d->group == group)
358 return;
359
360 if (d->group != nullptr) {
361 disconnect(d->group, SIGNAL(activeStackChanged(QUndoStack*)),
362 d->model, SLOT(setStack(QUndoStack*)));
363 }
364
365 d->group = group;
366
367 if (d->group != nullptr) {
368 connect(d->group, SIGNAL(activeStackChanged(QUndoStack*)),
369 d->model, SLOT(setStack(QUndoStack*)));
370 d->model->setStack(d->group->activeStack());
371 } else {
372 d->model->setStack(nullptr);
373 }
374}
375
384QUndoGroup *QUndoView::group() const
385{
386 Q_D(const QUndoView);
387 return d->group;
388}
389
390#endif // QT_CONFIG(undogroup)
391
402{
403 Q_D(QUndoView);
404 d->model->setEmptyLabel(label);
405}
406
408{
409 Q_D(const QUndoView);
410 return d->model->emptyLabel();
411}
412
424{
425 Q_D(const QUndoView);
426 d->model->setCleanIcon(icon);
427
428}
429
431{
432 Q_D(const QUndoView);
433 return d->model->cleanIcon();
434}
435
437
438#include "qundoview.moc"
439#include "moc_qundoview.cpp"
Q_INVOKABLE int const QModelIndex & parent
Returns the parent of the model item with the given index.
void endResetModel()
Completes a model reset operation.
void beginResetModel()
Begins a model reset operation.
QModelIndex createIndex(int row, int column, const void *data=nullptr) const
Creates a model index for the given row and column with the internal pointer ptr.
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition qicon.cpp:1019
virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Sets the model item index to be the current item, and emits currentChanged().
The QListView class provides a list or icon view onto a model.
Definition qlistview.h:17
\inmodule QtCore
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QUndoGroup class is a group of QUndoStack objects.
Definition qundogroup.h:20
QItemSelectionModel * selectionModel() const
Definition qundoview.cpp:66
QUndoModel(QObject *parent=nullptr)
Definition qundoview.cpp:56
void setCleanIcon(const QIcon &icon)
QString emptyLabel() const
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
void setEmptyLabel(const QString &label)
QUndoStack * stack() const
Definition qundoview.cpp:71
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
QModelIndex selectedIndex() const
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the item in the model specified by the given row, column and parent index.
QIcon cleanIcon() const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
void setStack(QUndoStack *stack)
Definition qundoview.cpp:76
QUndoModel * model
The QUndoView class displays the contents of a QUndoStack.
Definition qundoview.h:22
~QUndoView()
Destroys this view.
QIcon cleanIcon
the icon used to represent the clean state.
Definition qundoview.h:26
QString emptyLabel
the label used for the empty state.
Definition qundoview.h:25
QUndoStack * stack() const
Returns the stack currently displayed by this view.
void setCleanIcon(const QIcon &icon)
QUndoView(QWidget *parent=nullptr)
Constructs a new view with parent parent.
void setEmptyLabel(const QString &label)
void setStack(QUndoStack *stack)
Sets the stack displayed by this view to stack.
\inmodule QtCore
Definition qvariant.h:65
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
Combined button and popup list for selecting options.
@ DecorationRole
@ DisplayRole
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint index
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean GLuint group
GLuint GLsizei const GLchar * label
[43]
GLenum GLenum GLsizei void GLsizei void * column
GLhandleARB obj
[2]
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
#define QT_CONFIG(feature)
#define tr(X)
#define Q_OBJECT
#define slots
if(qFloatDistance(a, b)<(1<< 7))
[0]
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QObject::connect nullptr
myObject disconnect()
[26]
QLayoutItem * child
[0]