kwave  18.07.70
UndoAddMetaDataAction.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  UndoAddMetaDataAction.cpp - Undo action for insertion of labels
3  -------------------
4  begin : Wed Aug 16 2006
5  copyright : (C) 2006 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 
21 #include <algorithm>
22 #include <new>
23 
24 #include <QVariant>
25 
26 #include <KLocalizedString>
27 
28 #include "libkwave/MetaData.h"
29 #include "libkwave/SignalManager.h"
32 
33 //***************************************************************************
35  const Kwave::MetaDataList &meta_data)
36  :Kwave::UndoAction(),
37  m_description(),
38  m_offset(0),
39  m_length(SAMPLE_INDEX_MAX),
40  m_tracks()
41 {
43  sample_index_t last = 0;
44 
45  // sanity check: list should not be empty
46  Q_ASSERT(!meta_data.isEmpty());
47  if (meta_data.isEmpty()) return;
48 
49  /*
50  * loop over the list to find out the first/last sample offset
51  * and the list of affected tracks
52  */
53  foreach (const Kwave::MetaData &m, meta_data)
54  {
55  // search over a list of known properties which contain range/position
56  QStringList properties = Kwave::MetaData::positionBoundPropertyNames();
57  foreach (const QString &tag, properties) {
58  // check for a "start" property
59  QVariant v = m[tag];
60  bool ok = false;
61  sample_index_t pos = static_cast<sample_index_t>(v.toULongLong(&ok));
62  if (ok && (pos < first)) first = pos;
63  if (ok && (pos > last)) last = pos;
64  }
65 
66  // convert the track list into a usable list of unsigned int
67  const QList<QVariant> v_track_list =
69  QList<unsigned int> bound_tracks;
70  foreach (const QVariant &v, v_track_list) {
71  bool ok = false;
72  unsigned int t = v.toUInt(&ok);
73  if (ok) bound_tracks += t;
74  }
75 
76  foreach (unsigned int t, bound_tracks)
77  if (!m_tracks.contains(t)) m_tracks.append(t);;
78  }
79 
80  // fix first/last in case that nothing was found, select everything
81  if (first > last) {
83  m_length = 0;
84  } else {
85  m_offset = first;
86  m_length = (last - first) + 1;
87  }
88  if (!m_tracks.isEmpty())
89  std::sort(m_tracks.begin(),
90  m_tracks.end(), std::greater<unsigned int>());
91 
92  /*
93  * determine the description of the action
94  */
95 
96  for (;;) {
97  QString name;
98  QList<Kwave::MetaData> values = meta_data.values();
99  Q_ASSERT(!values.isEmpty());
100  if (!values.isEmpty()) {
101  const Kwave::MetaData &m = values.first();
103  name = m[Kwave::MetaData::STDPROP_TYPE].toString();
104  }
105 
106  // if the meta data list contains only one object: try to find
107  // out the object's name
108  if ((meta_data.count() == 1) && name.length()) {
109  m_description = i18nc(
110  "name of the undo action for inserting a meta data object",
111  "Insert %1",
112  name
113  );
114  break;
115  }
116 
117  // check if the list contains only objects of the same type
118  bool all_same_type = true;
119  foreach (const Kwave::MetaData &m, meta_data) {
120  QString n = m[Kwave::MetaData::STDPROP_TYPE].toString();
121  if (!n.length() || (n != name)) {
122  all_same_type = false;
123  break;
124  }
125  }
126  if (all_same_type) {
127  m_description = i18nc(
128  "name of the undo action for inserting multiple "
129  "meta data objects of the same type: "
130  "%1=number of elements, %2=name of one element in singular",
131  "Insert %1 %2 objects", meta_data.count(), name
132  );
133  break;
134  }
135 
136  m_description = i18n("Insert Meta Data");
137  break;
138  }
139 }
140 
141 //***************************************************************************
143 {
144 }
145 
146 //***************************************************************************
148 {
149  return m_description;
150 }
151 
152 //***************************************************************************
154 {
155  return sizeof(*this);
156 }
157 
158 //***************************************************************************
160 {
161  return sizeof(UndoDeleteMetaDataAction);
162 }
163 
164 //***************************************************************************
166 {
167  // nothing to do, all data has already
168  // been stored in the constructor
169  return true;
170 }
171 
172 //***************************************************************************
174  Kwave::SignalManager &manager, bool with_redo)
175 {
176  Kwave::UndoAction *redo = Q_NULLPTR;
177 
178  Kwave::MetaDataList meta_data =
179  manager.metaData().copy(m_offset, m_length, m_tracks);
180 
181  // store data for redo
182  if (with_redo && !meta_data.isEmpty()) {
183  redo = new(std::nothrow) Kwave::UndoDeleteMetaDataAction(meta_data);
184  Q_ASSERT(redo);
185  if (redo) redo->store(manager);
186  }
187 
188  // remove the meta data from the signal manager
190 
191  return redo;
192 }
193 
194 //***************************************************************************
195 //***************************************************************************
virtual MetaDataList copy(sample_index_t offset, sample_index_t length, const QList< unsigned int > &tracks) const
static QStringList positionBoundPropertyNames()
Definition: MetaData.cpp:155
bool hasProperty(const QString &p) const
Definition: MetaData.cpp:104
UndoAddMetaDataAction(const Kwave::MetaDataList &meta_data)
Definition: App.h:33
QString description() Q_DECL_OVERRIDE
Kwave::MetaDataList & metaData()
static const QString STDPROP_TRACKS
Definition: MetaData.h:44
virtual ~UndoAddMetaDataAction() Q_DECL_OVERRIDE
quint64 sample_index_t
Definition: Sample.h:28
const char name[16]
Definition: memcpy.c:510
static const QString STDPROP_TYPE
Definition: MetaData.h:41
virtual bool store(SignalManager &manager) Q_DECL_OVERRIDE
virtual qint64 undoSize() Q_DECL_OVERRIDE
virtual void deleteRange(sample_index_t offset, sample_index_t length, const QList< unsigned int > &tracks)
virtual Kwave::UndoAction * undo(Kwave::SignalManager &manager, bool with_redo) Q_DECL_OVERRIDE
#define SAMPLE_INDEX_MAX
Definition: Sample.h:31
virtual qint64 redoSize() Q_DECL_OVERRIDE
virtual bool store(Kwave::SignalManager &manager)=0