kwave  18.07.70
UndoModifyMetaDataAction.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  UndoModifyMetaDataAction.cpp - Undo action for modifying meta data
3  -------------------
4  begin : Sun Apr 03 2011
5  copyright : (C) 2011 by Thomas Eschenbacher
6  email : Thomas Eschenbacher <Thomas.Eschenbacher@gmx.de>
7 
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "config.h"
20 #include <KLocalizedString>
21 
22 #include "libkwave/SignalManager.h"
23 #include "libkwave/String.h"
25 
26 //***************************************************************************
28  const Kwave::MetaDataList &meta_data)
29  :UndoAction(), m_saved_data(meta_data)
30 {
31 }
32 
33 //***************************************************************************
35 {
36 }
37 
38 //***************************************************************************
40 {
41  // sanity check: list should not be empty
42  Q_ASSERT(!m_saved_data.isEmpty());
43  if (m_saved_data.isEmpty()) return _("");
44 
45  QString name;
46  {
47  const Kwave::MetaData &m = m_saved_data.values().first();
49  name = m[Kwave::MetaData::STDPROP_TYPE].toString();
50  }
51 
52  // if the meta data list contains only one object: try to find
53  // out the object's name
54  if ((m_saved_data.count() == 1) && name.length()) {
55  return i18nc(
56  "name of the undo action for modifying a meta data object",
57  "Modify %1",
58  name
59  );
60  }
61 
62  // check if the list contains only objects of the same type
63  bool all_same_type = true;
64  foreach (const Kwave::MetaData &m, m_saved_data) {
65  QString n = m[Kwave::MetaData::STDPROP_TYPE].toString();
66  if (!n.length() || (n != name)) {
67  all_same_type = false;
68  break;
69  }
70  }
71  if (all_same_type) {
72  return i18nc(
73  "name of the undo action for modifying multiple "
74  "meta data objects of the same type: "
75  "%1=number of elements, %2=name of one element in singular",
76  "Modify %1 %2 objects",
77  m_saved_data.count(), name
78  );
79  }
80 
81  return i18n("Modify Meta Data");
82 }
83 
84 //***************************************************************************
86 {
87  return sizeof(*this) + sizeof(m_saved_data);
88 }
89 
90 //***************************************************************************
92 {
93  return undoSize();
94 }
95 
96 //***************************************************************************
98 {
99  // nothing to do, all data has already
100  // been stored in the constructor
101  return true;
102 }
103 
104 //***************************************************************************
106  Kwave::SignalManager &manager, bool with_redo)
107 {
108  if (m_saved_data.isEmpty()) return Q_NULLPTR;
109 
110  // store data for redo
111  if (with_redo) {
112  Kwave::MetaDataList old_data;
113  const Kwave::MetaDataList &current_data = manager.metaData();
114 
115  foreach (const Kwave::MetaData &meta, m_saved_data) {
116  if (current_data.contains(meta)) {
117  // add a new entry that will replace the old one
118  old_data.add(current_data[meta.id()]);
119  } else {
120  // add an empty entry that will delete the old one when added
121  Kwave::MetaData empty_element(meta);
122  old_data.add(empty_element);
123  old_data[empty_element.id()].clear();
124  }
125  }
126 
127  // restore the saved meta data
128  manager.mergeMetaData(m_saved_data);
129 
130  // take the previous values as new undo data
131  m_saved_data = old_data;
132 
133  return this;
134  } else {
135  // restore the saved meta data
136  manager.mergeMetaData(m_saved_data);
137  return Q_NULLPTR;
138  }
139 }
140 
141 //***************************************************************************
142 void Kwave::UndoModifyMetaDataAction::dump(const QString &indent)
143 {
144  foreach (const Kwave::MetaData &m, m_saved_data) {
145  qDebug("%sundo modify meta data object '%s'",
146  DBG(indent), DBG(m.id()));
147 
148  // dump all properties of the object
149  foreach (const QString &key, m.keys()) {
150  QVariant v = m[key];
151  QString value;
152  if (v.type() == QVariant::List) {
153  foreach (const QVariant &v1, v.toList())
154  value += _("'") + v1.toString() + _("' ");
155  } else {
156  value = v.toString();
157  }
158  qDebug("%s '%s' = '%s",
159  DBG(indent), DBG(key), DBG(value));
160  }
161  }
162 }
163 
164 //***************************************************************************
165 //***************************************************************************
virtual void dump(const QString &indent) Q_DECL_OVERRIDE
bool hasProperty(const QString &p) const
Definition: MetaData.cpp:104
virtual bool contains(const MetaData &metadata) const
void mergeMetaData(const Kwave::MetaDataList &meta_data)
virtual Kwave::UndoAction * undo(Kwave::SignalManager &manager, bool with_redo) Q_DECL_OVERRIDE
QStringList keys() const
Definition: MetaData.cpp:149
virtual qint64 redoSize() Q_DECL_OVERRIDE
Kwave::MetaDataList & metaData()
virtual ~UndoModifyMetaDataAction() Q_DECL_OVERRIDE
virtual QString description() Q_DECL_OVERRIDE
virtual void add(const MetaData &metadata)
UndoModifyMetaDataAction(const Kwave::MetaDataList &meta_data)
const char name[16]
Definition: memcpy.c:510
QString id() const
Definition: MetaData.cpp:75
static const QString STDPROP_TYPE
Definition: MetaData.h:41
virtual bool store(Kwave::SignalManager &manager) Q_DECL_OVERRIDE
virtual qint64 undoSize() Q_DECL_OVERRIDE
#define _(m)
Definition: memcpy.c:66
#define DBG(qs)
Definition: String.h:55