File indexing completed on 2024-05-12 04:35:05
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2015 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "UndoGroup.h" 0021 #include "UndoItem.h" 0022 #include "UndoManager.h" 0023 0024 #include <QDebug> 0025 0026 namespace tikz { 0027 namespace core { 0028 0029 class UndoGroupPrivate 0030 { 0031 public: 0032 /** 0033 * Pointer to the undo manager. 0034 */ 0035 UndoManager * manager = nullptr; 0036 0037 /** 0038 * Name of the undo group. 0039 */ 0040 QString text; 0041 0042 /** 0043 * list of items contained 0044 */ 0045 QList<UndoItem *> items; 0046 }; 0047 0048 UndoGroup::UndoGroup(const QString & text, UndoManager * manager) 0049 : d(new UndoGroupPrivate()) 0050 { 0051 d->manager = manager; 0052 d->text = text; 0053 } 0054 0055 UndoGroup::~UndoGroup() 0056 { 0057 qDeleteAll(d->items); 0058 0059 delete d; 0060 } 0061 0062 Document * UndoGroup::document() const 0063 { 0064 return d->manager->document(); 0065 } 0066 0067 QString UndoGroup::text() const 0068 { 0069 return d->text; 0070 } 0071 0072 bool UndoGroup::isEmpty() const 0073 { 0074 return d->items.isEmpty(); 0075 } 0076 0077 void UndoGroup::undo() 0078 { 0079 for (int i = d->items.size() - 1; i >= 0; --i) { 0080 d->items[i]->undo(); 0081 } 0082 } 0083 0084 void UndoGroup::redo() 0085 { 0086 for (int i = 0; i < d->items.size(); ++i) { 0087 d->items[i]->redo(); 0088 } 0089 } 0090 0091 void UndoGroup::addItem(UndoItem *item) 0092 { 0093 // only try merge, if undo item id's match 0094 const int lastUndoId = d->items.isEmpty() ? -1 : d->items.last()->id(); 0095 const int newUndoId = item->id(); 0096 if (lastUndoId >= 0 && lastUndoId == newUndoId && d->items.last()->mergeWith(item)) { 0097 delete item; 0098 } else { 0099 // add to this undo group 0100 d->items.append(item); 0101 0102 // associate the UndoItem's group with this UndoGroup 0103 item->setGroup(this); 0104 } 0105 } 0106 0107 QList<UndoItem *> UndoGroup::undoItems() const 0108 { 0109 return d->items; 0110 } 0111 0112 int UndoGroup::count() const 0113 { 0114 return d->items.count(); 0115 } 0116 0117 void UndoGroup::printTree() 0118 { 0119 QString str = "group: " + text(); 0120 for (auto item : d->items) { 0121 str += " -->" + item->text(); 0122 } 0123 qDebug() << str; 0124 } 0125 0126 } 0127 } 0128 0129 // kate: indent-width 4; replace-tabs on;