kwave  18.07.70
UndoDeleteAction.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  UndoDeleteAction.cpp - UndoAction for deletion of a range of samples
3  -------------------
4  begin : Jun 08 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 <new>
21 
22 #include <KLocalizedString>
23 
25 #include "libkwave/SignalManager.h"
30 
31 //***************************************************************************
33  const QList<unsigned int> &track_list,
34  sample_index_t offset,
35  sample_index_t length)
36  :Kwave::UndoAction(),
37  m_parent_widget(parent_widget),
38  m_stripes(), m_meta_data(),
39  m_track_list(track_list),
40  m_offset(offset), m_length(length),
41  m_undo_size(sizeof(*this))
42 {
43  // undo size needed for samples
44  m_undo_size += m_length * sizeof(sample_t) * m_track_list.count();
45 }
46 
47 //***************************************************************************
49 {
50  m_stripes.clear();
51 }
52 
53 //***************************************************************************
55 {
56  return i18n("Delete");
57 }
58 
59 //***************************************************************************
61 {
62  return m_undo_size;
63 }
64 
65 //***************************************************************************
67 {
68  return sizeof(Kwave::UndoInsertAction);
69 }
70 
71 //***************************************************************************
73 {
74  if (!m_length) return true; // shortcut: this is an empty action
75 
76  // fork off a multi track stripe list for the selected range
77  const sample_index_t left = m_offset;
78  const sample_index_t right = m_offset + m_length - 1;
79  m_stripes = manager.stripes(m_track_list, left, right);
80  if (m_stripes.isEmpty())
81  return false; // retrieving the stripes failed
82 
83  // save the meta data
84  m_meta_data = manager.metaData().copy(m_offset, m_length, m_track_list);
85 
86  return true;
87 }
88 
89 //***************************************************************************
91  bool with_redo)
92 {
93  Kwave::UndoAction *redo_action = Q_NULLPTR;
94 
95  // store data for redo
96  if (with_redo) {
97  redo_action = new(std::nothrow) Kwave::UndoInsertAction(
100  );
101  Q_ASSERT(redo_action);
102  if (!redo_action) return Q_NULLPTR;
103  redo_action->store(manager);
104  }
105 
106  if (!m_length) return redo_action; // shortcut: this is an empty action
107 
108  // insert space for the stripes
109  if (!manager.insertSpace(m_offset, m_length, m_track_list)) {
110  qWarning("UndoDeleteAction::undo() FAILED [insertSpace]");
111  delete redo_action;
112  return Q_NULLPTR;
113  }
114 
115  // merge the stripes back into the signal
116  if (!manager.mergeStripes(m_stripes, m_track_list)) {
117  qWarning("UndoDeleteAction::undo() FAILED [mergeStripes]");
118  delete redo_action;
119  return Q_NULLPTR;
120  }
121 
122  // restore the saved meta data
123  manager.metaData().merge(m_meta_data);
124 
125  return redo_action;
126 }
127 
128 //***************************************************************************
129 void Kwave::UndoDeleteAction::dump(const QString &indent)
130 {
131  qDebug("%sundo delete from [%lu ... %lu] (%lu)", DBG(indent),
132  static_cast<unsigned long int>(m_offset),
133  static_cast<unsigned long int>(m_offset + ((m_length) ?
134  (m_length - 1) : m_length)),
135  static_cast<unsigned long int>(m_length));
136 }
137 
138 //***************************************************************************
139 //***************************************************************************
Kwave::MetaDataList m_meta_data
virtual MetaDataList copy(sample_index_t offset, sample_index_t length, const QList< unsigned int > &tracks) const
virtual qint64 redoSize() Q_DECL_OVERRIDE
virtual QString description() Q_DECL_OVERRIDE
Definition: App.h:33
bool insertSpace(sample_index_t offset, sample_index_t length, const QList< unsigned int > &track_list)
Kwave::MetaDataList & metaData()
virtual ~UndoDeleteAction() Q_DECL_OVERRIDE
quint64 sample_index_t
Definition: Sample.h:28
void merge(const MetaDataList &meta_data)
QList< Kwave::Stripe::List > m_stripes
virtual qint64 undoSize() Q_DECL_OVERRIDE
#define DBG(qs)
Definition: String.h:55
virtual bool store(Kwave::SignalManager &manager) Q_DECL_OVERRIDE
QList< unsigned int > m_track_list
virtual Kwave::UndoAction * undo(Kwave::SignalManager &manager, bool with_redo) Q_DECL_OVERRIDE
virtual void dump(const QString &indent) Q_DECL_OVERRIDE
virtual bool store(Kwave::SignalManager &manager)=0
QList< Kwave::Stripe::List > stripes(const QList< unsigned int > &track_list, sample_index_t left=0, sample_index_t right=SAMPLE_INDEX_MAX)
qint32 sample_t
Definition: Sample.h:37
bool mergeStripes(const QList< Kwave::Stripe::List > &stripes, const QList< unsigned int > &track_list)
UndoDeleteAction(QWidget *parent_widget, const QList< unsigned int > &track_list, sample_index_t offset, sample_index_t length)