File indexing completed on 2024-05-12 15:56:14
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2010 Matus Talcik <matus.talcik@gmail.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 /**************************************************************************** 0007 ** 0008 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 0009 ** All rights reserved. 0010 ** Contact: Nokia Corporation (qt-info@nokia.com) 0011 ** 0012 ** This file is part of the QtGui module of the Qt Toolkit. 0013 ** 0014 ** $QT_BEGIN_LICENSE:LGPL$ 0015 ** No Commercial Usage 0016 ** This file contains pre-release code and may not be distributed. 0017 ** You may use this file in accordance with the terms and conditions 0018 ** contained in the Technology Preview License Agreement accompanying 0019 ** this package. 0020 ** 0021 ** GNU Lesser General Public License Usage 0022 ** Alternatively, this file may be used under the terms of the GNU Lesser 0023 ** General Public License version 2.1 as published by the Free Software 0024 ** Foundation and appearing in the file LICENSE.LGPL included in the 0025 ** packaging of this file. Please review the following information to 0026 ** ensure the GNU Lesser General Public License version 2.1 requirements 0027 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 0028 ** 0029 ** In addition, as a special exception, Nokia gives you certain additional 0030 ** rights. These rights are described in the Nokia Qt LGPL Exception 0031 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 0032 ** 0033 ** If you have questions regarding the use of this file, please contact 0034 ** Nokia at qt-info@nokia.com. 0035 ** 0036 ** 0037 ** 0038 ** 0039 ** 0040 ** 0041 ** 0042 ** 0043 ** $QT_END_LICENSE$ 0044 ** 0045 ****************************************************************************/ 0046 #include "kundo2model.h" 0047 #include <klocalizedstring.h> 0048 KUndo2Model::KUndo2Model(QObject *parent) 0049 : QAbstractItemModel(parent) 0050 { 0051 m_stack = 0; 0052 m_sel_model = new QItemSelectionModel(this, this); 0053 connect(m_sel_model, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(setStackCurrentIndex(QModelIndex))); 0054 m_emty_label = i18n("<empty>"); 0055 } 0056 0057 QItemSelectionModel *KUndo2Model::selectionModel() const 0058 { 0059 return m_sel_model; 0060 } 0061 0062 KUndo2QStack *KUndo2Model::stack() const 0063 { 0064 return m_stack; 0065 } 0066 0067 void KUndo2Model::setStack(KUndo2QStack *stack) 0068 { 0069 if (m_stack == stack) 0070 return; 0071 0072 if (m_stack != 0) { 0073 disconnect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged())); 0074 disconnect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged())); 0075 disconnect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*))); 0076 disconnect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(addImage(int))); 0077 } 0078 m_stack = stack; 0079 if (m_stack != 0) { 0080 connect(m_stack, SIGNAL(cleanChanged(bool)), this, SLOT(stackChanged())); 0081 connect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(stackChanged())); 0082 connect(m_stack, SIGNAL(destroyed(QObject*)), this, SLOT(stackDestroyed(QObject*))); 0083 connect(m_stack, SIGNAL(indexChanged(int)), this, SLOT(addImage(int))); 0084 } 0085 0086 stackChanged(); 0087 } 0088 0089 void KUndo2Model::stackDestroyed(QObject *obj) 0090 { 0091 if (obj != m_stack) 0092 return; 0093 m_stack = 0; 0094 0095 stackChanged(); 0096 } 0097 0098 void KUndo2Model::stackChanged() 0099 { 0100 beginResetModel(); 0101 endResetModel(); 0102 m_sel_model->setCurrentIndex(selectedIndex(), QItemSelectionModel::ClearAndSelect); 0103 } 0104 0105 void KUndo2Model::setStackCurrentIndex(const QModelIndex &index) 0106 { 0107 if (m_stack == 0) 0108 return; 0109 0110 if (index == selectedIndex()) 0111 return; 0112 0113 if (index.column() != 0) 0114 return; 0115 0116 m_stack->setIndex(index.row()); 0117 } 0118 0119 QModelIndex KUndo2Model::selectedIndex() const 0120 { 0121 return m_stack == 0 ? QModelIndex() : createIndex(m_stack->index(), 0); 0122 } 0123 0124 QModelIndex KUndo2Model::index(int row, int column, const QModelIndex &parent) const 0125 { 0126 if (m_stack == 0) 0127 return QModelIndex(); 0128 0129 if (parent.isValid()) 0130 return QModelIndex(); 0131 0132 if (column != 0) 0133 return QModelIndex(); 0134 0135 if (row < 0 || row > m_stack->count()) 0136 return QModelIndex(); 0137 0138 return createIndex(row, column); 0139 } 0140 0141 QModelIndex KUndo2Model::parent(const QModelIndex&) const 0142 { 0143 return QModelIndex(); 0144 } 0145 0146 int KUndo2Model::rowCount(const QModelIndex &parent) const 0147 { 0148 if (m_stack == 0) 0149 return 0; 0150 0151 if (parent.isValid()) 0152 return 0; 0153 0154 return m_stack->count() + 1; 0155 } 0156 0157 int KUndo2Model::columnCount(const QModelIndex&) const 0158 { 0159 return 1; 0160 } 0161 0162 QVariant KUndo2Model::data(const QModelIndex &index, int role) const 0163 { 0164 if (m_stack == 0) 0165 return QVariant(); 0166 0167 if (index.column() != 0) 0168 return QVariant(); 0169 0170 if (index.row() < 0 || index.row() > m_stack->count()) 0171 return QVariant(); 0172 0173 if (role == Qt::DisplayRole) { 0174 if (index.row() == 0) 0175 return m_emty_label; 0176 return m_stack->text(index.row() - 1); 0177 } else if (role == Qt::DecorationRole) { 0178 if (index.row() == m_stack->cleanIndex() && !m_clean_icon.isNull()) 0179 return m_clean_icon; 0180 } 0181 0182 return QVariant(); 0183 } 0184 0185 QString KUndo2Model::emptyLabel() const 0186 { 0187 return m_emty_label; 0188 } 0189 0190 void KUndo2Model::setEmptyLabel(const QString &label) 0191 { 0192 m_emty_label = label; 0193 stackChanged(); 0194 } 0195 0196 void KUndo2Model::setCleanIcon(const QIcon &icon) 0197 { 0198 m_clean_icon = icon; 0199 stackChanged(); 0200 } 0201 0202 QIcon KUndo2Model::cleanIcon() const 0203 { 0204 return m_clean_icon; 0205 } 0206