Krita Source Code Documentation
Loading...
Searching...
No Matches
kundo2model.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2010 Matus Talcik <matus.talcik@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6/****************************************************************************
7**
8** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
9** All rights reserved.
10** Contact: Nokia Corporation (qt-info@nokia.com)
11**
12** This file is part of the QtGui module of the Qt Toolkit.
13**
14** $QT_BEGIN_LICENSE:LGPL$
15** No Commercial Usage
16** This file contains pre-release code and may not be distributed.
17** You may use this file in accordance with the terms and conditions
18** contained in the Technology Preview License Agreement accompanying
19** this package.
20**
21** GNU Lesser General Public License Usage
22** Alternatively, this file may be used under the terms of the GNU Lesser
23** General Public License version 2.1 as published by the Free Software
24** Foundation and appearing in the file LICENSE.LGPL included in the
25** packaging of this file. Please review the following information to
26** ensure the GNU Lesser General Public License version 2.1 requirements
27** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
28**
29** In addition, as a special exception, Nokia gives you certain additional
30** rights. These rights are described in the Nokia Qt LGPL Exception
31** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32**
33** If you have questions regarding the use of this file, please contact
34** Nokia at qt-info@nokia.com.
35**
36**
37**
38**
39**
40**
41**
42**
43** $QT_END_LICENSE$
44**
45****************************************************************************/
46#include "kundo2model.h"
47#include <klocalizedstring.h>
49 : QAbstractItemModel(parent)
50{
51 m_stack = 0;
52 m_sel_model = new QItemSelectionModel(this, this);
53 connect(m_sel_model, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(setStackCurrentIndex(QModelIndex)));
54 m_empty_label = i18n("<empty>");
55}
56
57QItemSelectionModel *KUndo2Model::selectionModel() const
58{
59 return m_sel_model;
60}
61
63{
64 return m_stack;
65}
66
68{
69 if (m_stack == stack)
70 return;
71
72 if (m_stack != 0) {
73 disconnect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
74 disconnect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
75 disconnect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*)));
76 disconnect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(addImage(int)));
77 }
78 m_stack = stack;
79 if (m_stack != 0) {
80 connect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged()));
81 connect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged()));
82 connect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*)));
83 connect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(addImage(int)));
84 }
85
87}
88
90{
91 if (obj != m_stack)
92 return;
93 m_stack = 0;
94
96}
97
99{
100 beginResetModel();
101 endResetModel();
102 m_sel_model->setCurrentIndex(selectedIndex(), QItemSelectionModel::ClearAndSelect);
103}
104
105void KUndo2Model::setStackCurrentIndex(const QModelIndex &index)
106{
107 if (m_stack == 0)
108 return;
109
110 if (index == selectedIndex())
111 return;
112
113 if (index.column() != 0)
114 return;
115
116 m_stack->setIndex(index.row());
117}
118
119QModelIndex KUndo2Model::selectedIndex() const
120{
121 return m_stack == 0 ? QModelIndex() : createIndex(m_stack->index(), 0);
122}
123
124QModelIndex KUndo2Model::index(int row, int column, const QModelIndex &parent) const
125{
126 if (m_stack == 0)
127 return QModelIndex();
128
129 if (parent.isValid())
130 return QModelIndex();
131
132 if (column != 0)
133 return QModelIndex();
134
135 if (row < 0 || row > m_stack->count())
136 return QModelIndex();
137
138 return createIndex(row, column);
139}
140
141QModelIndex KUndo2Model::parent(const QModelIndex&) const
142{
143 return QModelIndex();
144}
145
146int KUndo2Model::rowCount(const QModelIndex &parent) const
147{
148 if (m_stack == 0)
149 return 0;
150
151 if (parent.isValid())
152 return 0;
153
154 return m_stack->count() + 1;
155}
156
157int KUndo2Model::columnCount(const QModelIndex&) const
158{
159 return 1;
160}
161
162QVariant KUndo2Model::data(const QModelIndex &index, int role) const
163{
164 if (m_stack == 0)
165 return QVariant();
166
167 if (index.column() != 0)
168 return QVariant();
169
170 if (index.row() < 0 || index.row() > m_stack->count())
171 return QVariant();
172
173 if (role == Qt::DisplayRole) {
174 if (index.row() == 0)
175 return m_empty_label;
176 return m_stack->text(index.row() - 1);
177 } else if (role == Qt::DecorationRole) {
178 if (index.row() == m_stack->cleanIndex() && !m_clean_icon.isNull())
179 return m_clean_icon;
180 }
181
182 return QVariant();
183}
184
186{
187 return m_empty_label;
188}
189
190void KUndo2Model::setEmptyLabel(const QString &label)
191{
192 m_empty_label = label;
193 stackChanged();
194}
195
196void KUndo2Model::setCleanIcon(const QIcon &icon)
197{
198 m_clean_icon = icon;
199 stackChanged();
200}
201
203{
204 return m_clean_icon;
205}
206
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void stackChanged()
QModelIndex parent(const QModelIndex &child) const override
KUndo2QStack * m_stack
Definition kundo2model.h:87
QString emptyLabel() const
void setEmptyLabel(const QString &label)
QIcon m_clean_icon
Definition kundo2model.h:90
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void setCleanIcon(const QIcon &icon)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QIcon cleanIcon() const
QModelIndex selectedIndex() const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
KUndo2Model(QObject *parent=0)
QItemSelectionModel * m_sel_model
Definition kundo2model.h:88
void setStack(KUndo2QStack *stack)
QItemSelectionModel * selectionModel() const
QString m_empty_label
Definition kundo2model.h:89
void setStackCurrentIndex(const QModelIndex &index)
KUndo2QStack * stack() const
void stackDestroyed(QObject *obj)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
The KUndo2QStack class is a stack of KUndo2Command objects.
virtual void setIndex(int idx)
int cleanIndex() const
QString text(int idx) const
int index() const
int count() const