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