Warning, file /office/calligra/libs/kundo2/kundo2model.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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