1 /***************************************************************************
2     UndoTransaction.cpp  -  groups multiple UndoAction objects together
3 			     -------------------
4     begin                : Fri May 25 2001
5     copyright            : (C) 2001 by Thomas Eschenbacher
6     email                : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QListIterator>
21 
22 #include "libkwave/String.h"
23 #include "libkwave/undo/UndoAction.h"
24 #include "libkwave/undo/UndoTransaction.h"
25 
26 //***************************************************************************
UndoTransaction(const QString & name)27 Kwave::UndoTransaction::UndoTransaction(const QString &name)
28     :QList<UndoAction *>(), m_description(name), m_aborted(false)
29 {
30 }
31 
32 //***************************************************************************
~UndoTransaction()33 Kwave::UndoTransaction::~UndoTransaction()
34 {
35     while (!isEmpty()) {
36 	UndoAction *action = takeLast();
37 	if (action) delete action;
38     }
39 }
40 
41 //***************************************************************************
undoSize()42 qint64 Kwave::UndoTransaction::undoSize()
43 {
44     qint64 s = 0;
45     QListIterator<UndoAction *> it(*this);
46     while (it.hasNext()) {
47 	UndoAction *undo = it.next();
48 	if (undo) s += undo->undoSize();
49     }
50     return s;
51 }
52 
53 //***************************************************************************
redoSize()54 qint64 Kwave::UndoTransaction::redoSize()
55 {
56     qint64 s = 0;
57     QListIterator<UndoAction *> it(*this);
58     while (it.hasNext()) {
59 	UndoAction *undo = it.next();
60 	if (undo) s += undo->redoSize();
61     }
62     return s;
63 }
64 
65 //***************************************************************************
description()66 QString Kwave::UndoTransaction::description()
67 {
68     // if description exists, return it
69     if (m_description.length()) return m_description;
70 
71     QString str;
72     QListIterator<UndoAction *> it(*this);
73     while (it.hasNext()) {
74 	UndoAction *undo = it.next();
75 	if (!undo) continue;
76 	QString d = undo->description();
77 
78 	// skip duplicates
79 	if (str.contains(_(", ") + d) || (str == d)) continue;
80 
81 	// append others
82 	if (str.length()) str += _(", ");
83 	str += d;
84     }
85     return str;
86 }
87 
88 //***************************************************************************
containsModification() const89 bool Kwave::UndoTransaction::containsModification() const
90 {
91     if (isEmpty()) return false;
92     QListIterator<UndoAction *> it(*this);
93     while (it.hasNext()) {
94 	UndoAction *action = it.next();
95 	if (!action) continue;
96 	if (action->containsModification()) return true;
97     }
98     return false;
99 }
100 
101 //***************************************************************************
abort()102 void Kwave::UndoTransaction::abort()
103 {
104     m_aborted = true;
105 }
106 
107 //***************************************************************************
dump(const QString & indent)108 void Kwave::UndoTransaction::dump(const QString &indent)
109 {
110     qDebug("%s [%s]", DBG(indent), DBG(description()));
111     if (isEmpty()) return;
112 
113     QListIterator<UndoAction *> it(*this);
114     it.toBack();
115     while (it.hasPrevious()) {
116 	UndoAction *action = it.previous();
117 	Q_ASSERT(action);
118 	if (!action) continue;
119 	action->dump(_("    "));
120     }
121 }
122 
123 //***************************************************************************
124 //***************************************************************************
125