kwave  18.07.70
UndoModifyAction.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  UndoModifyAction.h - UndoAction for modifications on samples
3  -------------------
4  begin : 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 #include <KLocalizedString>
20 
21 #include "libkwave/Sample.h"
22 #include "libkwave/SampleArray.h"
23 #include "libkwave/SampleReader.h"
24 #include "libkwave/SignalManager.h"
25 #include "libkwave/Writer.h"
27 
28 //***************************************************************************
30  sample_index_t offset,
31  sample_index_t length)
32  :UndoAction(), m_track(track), m_offset(offset), m_length(length),
33  m_stripes()
34 {
35 }
36 
37 //***************************************************************************
39 {
40 }
41 
42 //***************************************************************************
44 {
45  return i18n("Modify Samples");
46 }
47 
48 //***************************************************************************
50 {
51  return sizeof(*this) + (m_length * sizeof(sample_t));
52 }
53 
54 //***************************************************************************
56 {
57  if (!m_length) return true; // shortcut: this is an empty action
58 
59  // fork off a multi track stripe list for the selected range
60  QList<unsigned int> track_list;
61  track_list.append(m_track);
62  const sample_index_t left = m_offset;
63  const sample_index_t right = m_offset + m_length - 1;
64  m_stripes = manager.stripes(track_list, left, right);
65  if (m_stripes.isEmpty())
66  return false; // retrieving the stripes failed
67 
68  return true;
69 }
70 
71 //***************************************************************************
73  Kwave::SignalManager &manager, bool with_redo)
74 {
75  const sample_index_t left = m_offset;
76  const sample_index_t right = m_offset + m_length - 1;
77  QList<Kwave::Stripe::List> redo_data;
78  QList<unsigned int> track_list;
79  track_list.append(m_track);
80  bool ok = true;
81 
82  if (m_length && with_redo) {
83  // save the current stripes for later redo
84  redo_data = manager.stripes(track_list, left, right);
85  ok &= !redo_data.isEmpty();
86  }
87 
88  // merge the stripes back into the signal
89  if (m_length && ok) {
90  if (!manager.mergeStripes(m_stripes, track_list)) {
91  qWarning("UndoModifyAction::undo() FAILED [mergeStripes]");
92  return Q_NULLPTR;
93  }
94  }
95 
96  if (ok && with_redo) {
97  // now store the redo data in this object
98  m_stripes = redo_data;
99  }
100 
101  return (with_redo && ok) ? this : Q_NULLPTR;
102 }
103 
104 //***************************************************************************
105 //***************************************************************************
virtual bool store(Kwave::SignalManager &manager) Q_DECL_OVERRIDE
UndoModifyAction(unsigned int track, sample_index_t offset, sample_index_t length)
quint64 sample_index_t
Definition: Sample.h:28
virtual qint64 undoSize() Q_DECL_OVERRIDE
virtual QString description() Q_DECL_OVERRIDE
virtual UndoAction * undo(Kwave::SignalManager &manager, bool with_redo) Q_DECL_OVERRIDE
QList< Kwave::Stripe::List > m_stripes
virtual ~UndoModifyAction() Q_DECL_OVERRIDE
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)