Warning, file /office/calligra/libs/kundo2/kundo2view.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 "kundo2stack.h"
0060 #include "kundo2view.h"
0061 #include "kundo2model.h"
0062 #include "kundo2group.h"
0063 
0064 #ifndef QT_NO_UNDOVIEW
0065 
0066 #include <QAbstractItemModel>
0067 #include <QPointer>
0068 #include <QIcon>
0069 
0070 
0071 /*!
0072     \class KUndo2View
0073     \brief The KUndo2View class displays the contents of a KUndo2QStack.
0074     \since 4.2
0075 
0076     \ingroup advanced
0077 
0078     KUndo2View is a QListView which displays the list of commands pushed on an undo stack.
0079     The most recently executed command is always selected. Selecting a different command
0080     results in a call to KUndo2QStack::setIndex(), rolling the state of the document
0081     backwards or forward to the new command.
0082 
0083     The stack can be set explicitly with setStack(). Alternatively, a QUndoGroup object can
0084     be set with setGroup(). The view will then update itself automatically whenever the
0085     active stack of the group changes.
0086 
0087     \image KUndo2View.png
0088 */
0089 
0090 class KUndo2ViewPrivate
0091 {
0092 public:
0093     KUndo2ViewPrivate() :
0094 #ifndef QT_NO_UNDOGROUP
0095         group(0),
0096 #endif
0097         model(0) {}
0098 
0099 #ifndef QT_NO_UNDOGROUP
0100     QPointer<KUndo2Group> group;
0101 #endif
0102     KUndo2Model *model;
0103     KUndo2View* q;
0104 
0105     void init(KUndo2View* view);
0106 };
0107 
0108 void KUndo2ViewPrivate::init(KUndo2View* view)
0109 {
0110     q = view;
0111     model = new KUndo2Model(q);
0112     q->setModel(model);
0113     q->setSelectionModel(model->selectionModel());
0114 }
0115 
0116 /*!
0117     Constructs a new view with parent \a parent.
0118 */
0119 
0120 KUndo2View::KUndo2View(QWidget *parent) : QListView(parent), d(new KUndo2ViewPrivate)
0121 {
0122     d->init(this);
0123 }
0124 
0125 /*!
0126     Constructs a new view with parent \a parent and sets the observed stack to \a stack.
0127 */
0128 
0129 KUndo2View::KUndo2View(KUndo2QStack *stack, QWidget *parent) : QListView(parent), d(new KUndo2ViewPrivate)
0130 {
0131     d->init(this);
0132     setStack(stack);
0133 }
0134 
0135 #ifndef QT_NO_UNDOGROUP
0136 
0137 /*!
0138     Constructs a new view with parent \a parent and sets the observed group to \a group.
0139 
0140     The view will update itself autmiatically whenever the active stack of the group changes.
0141 */
0142 
0143 KUndo2View::KUndo2View(KUndo2Group *group, QWidget *parent) : QListView(parent), d(new KUndo2ViewPrivate)
0144 {
0145     d->init(this);
0146     setGroup(group);
0147 }
0148 
0149 #endif // QT_NO_UNDOGROUP
0150 
0151 /*!
0152     Destroys this view.
0153 */
0154 
0155 KUndo2View::~KUndo2View()
0156 {
0157     delete d;
0158 }
0159 
0160 /*!
0161     Returns the stack currently displayed by this view. If the view is looking at a
0162     QUndoGroup, this the group's active stack.
0163 
0164     \sa setStack() setGroup()
0165 */
0166 
0167 KUndo2QStack *KUndo2View::stack() const
0168 {
0169 
0170     return d->model->stack();
0171 }
0172 
0173 /*!
0174     Sets the stack displayed by this view to \a stack. If \a stack is 0, the view
0175     will be empty.
0176 
0177     If the view was previously looking at a QUndoGroup, the group is set to 0.
0178 
0179     \sa stack() setGroup()
0180 */
0181 
0182 void KUndo2View::setStack(KUndo2QStack *stack)
0183 {
0184 
0185 #ifndef QT_NO_UNDOGROUP
0186     setGroup(0);
0187 #endif
0188     d->model->setStack(stack);
0189 }
0190 
0191 #ifndef QT_NO_UNDOGROUP
0192 
0193 /*!
0194     Sets the group displayed by this view to \a group. If \a group is 0, the view will
0195     be empty.
0196 
0197     The view will update itself autmiatically whenever the active stack of the group changes.
0198 
0199     \sa group() setStack()
0200 */
0201 
0202 void KUndo2View::setGroup(KUndo2Group *group)
0203 {
0204 
0205 
0206     if (d->group == group)
0207         return;
0208 
0209     if (d->group != 0) {
0210         disconnect(d->group, SIGNAL(activeStackChanged(KUndo2QStack*)),
0211                    d->model, SLOT(setStack(KUndo2QStack*)));
0212     }
0213 
0214     d->group = group;
0215 
0216     if (d->group != 0) {
0217         connect(d->group, SIGNAL(activeStackChanged(KUndo2QStack*)),
0218                 d->model, SLOT(setStack(KUndo2QStack*)));
0219         d->model->setStack((KUndo2QStack *)d->group->activeStack());
0220     } else {
0221         d->model->setStack(0);
0222     }
0223 }
0224 
0225 /*!
0226     Returns the group displayed by this view.
0227 
0228     If the view is not looking at group, this function returns 0.
0229 
0230     \sa setGroup() setStack()
0231 */
0232 
0233 KUndo2Group *KUndo2View::group() const
0234 {
0235 
0236     return d->group;
0237 }
0238 
0239 #endif // QT_NO_UNDOGROUP
0240 
0241 /*!
0242     \property KUndo2View::emptyLabel
0243     \brief the label used for the empty state.
0244 
0245     The empty label is the topmost element in the list of commands, which represents
0246     the state of the document before any commands were pushed on the stack. The default
0247     is the string "<empty>".
0248 */
0249 
0250 void KUndo2View::setEmptyLabel(const QString &label)
0251 {
0252 
0253     d->model->setEmptyLabel(label);
0254 }
0255 
0256 QString KUndo2View::emptyLabel() const
0257 {
0258 
0259     return d->model->emptyLabel();
0260 }
0261 
0262 /*!
0263     \property KUndo2View::cleanIcon
0264     \brief the icon used to represent the clean state.
0265 
0266     A stack may have a clean state set with KUndo2QStack::setClean(). This is usually
0267     the state of the document at the point it was saved. KUndo2View can display an
0268     icon in the list of commands to show the clean state. If this property is
0269     a null icon, no icon is shown. The default value is the null icon.
0270 */
0271 
0272 void KUndo2View::setCleanIcon(const QIcon &icon)
0273 {
0274 
0275     d->model->setCleanIcon(icon);
0276 
0277 }
0278 
0279 QIcon KUndo2View::cleanIcon() const
0280 {
0281 
0282     return d->model->cleanIcon();
0283 }
0284 
0285 #endif // QT_NO_UNDOVIEW