kwave  18.07.70
MultiTrackWriter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  MultiTrackWriter.cpp - writer for multi-track signals
3  -------------------
4  begin : Sat Jun 30 2001
5  copyright : (C) 2001 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 <new>
22 
24 #include "libkwave/SampleArray.h"
25 #include "libkwave/SignalManager.h"
29 
30 //***************************************************************************
32  :Kwave::MultiWriter()
33 {
34 }
35 
36 //***************************************************************************
38  const QList<unsigned int> &track_list,
39  Kwave::InsertMode mode,
40  sample_index_t left,
41  sample_index_t right)
42  :Kwave::MultiWriter()
43 {
44  if (!init(signal_manager, track_list, mode, left, right)) {
45  // out of memory or aborted
46  qWarning("MultiTrackWriter::init FAILED");
47  }
48 }
49 
50 //***************************************************************************
52  Kwave::InsertMode mode)
53  :Kwave::MultiWriter()
54 {
55  QList<unsigned int> track_list = signal_manager.selectedTracks();
56  sample_index_t left = 0;
57  sample_index_t right = 0;
58 
59  if (signal_manager.length()) {
60  // default if signal is present: current selection
61  left = signal_manager.selection().first();
62  right = signal_manager.selection().last();
63  if (left == right) {
64  // if no selection: whole signal
65  left = 0;
66  right = signal_manager.length() - 1;
67  }
68  }
69 
70  if (!init(signal_manager, track_list, mode, left, right)) {
71  // out of memory or aborted
72  qWarning("MultiTrackWriter::init FAILED");
73  }
74 }
75 
76 //***************************************************************************
78 {
79  flush();
80  clear();
81 }
82 
83 //***************************************************************************
85  const QList<unsigned int> &track_list,
86  Kwave::InsertMode mode,
87  sample_index_t left,
88  sample_index_t right)
89 {
90  Kwave::UndoTransactionGuard guard(signal_manager, QString());
91 
92  unsigned int index = 0;
93  foreach (unsigned int track, track_list) {
94  // NOTE: this function is *nearly* identical to the one in the
95  // Signal class, except for undo support
96  Kwave::Writer *writer = signal_manager.openWriter(
97  mode, track, left, right);
98  if (writer) {
99  insert(index++, writer);
100 
101  // get the real/effective left and right sample
102  left = writer->first();
103  right = writer->last();
104  } else {
105  // out of memory or aborted
106  clear();
107  return false;
108  }
109  }
110 
111  // skip all that undo handling below if undo is not enabled
112  // or the writer creation has failed
113  if (!signal_manager.undoEnabled()) return true;
114 
115  // enter a new undo transaction and let it close when the writer closes
116  signal_manager.startUndoTransaction();
117  QObject::connect(this, SIGNAL(destroyed()),
118  &signal_manager, SLOT(closeUndoTransaction()),
119  Qt::QueuedConnection);
120 
121  // create an undo action for the modification of the samples
122  UndoAction *undo = Q_NULLPTR;
123  switch (mode) {
124  case Kwave::Append:
125  case Kwave::Insert: {
126  undo = new(std::nothrow) Kwave::UndoInsertAction(
127  signal_manager.parentWidget(),
128  track_list,
129  left,
130  right - left + 1
131  );
132 
133  if (undo) {
134  Kwave::Writer *last_writer = at(tracks() - 1);
136  last_writer,
137  SIGNAL(sigSamplesWritten(sample_index_t)),
138  static_cast<Kwave::UndoInsertAction *>(undo),
139  SLOT(setLength(sample_index_t)));
140  }
141  break;
142  }
143  case Kwave::Overwrite: {
144  foreach (unsigned int track, track_list) {
145  undo = new(std::nothrow) Kwave::UndoModifyAction(
146  track, left, right - left + 1);
147  if (!signal_manager.registerUndoAction(undo)) {
148  // aborted, do not continue without undo
149  clear();
150  return false;
151  }
152  }
153  break;
154  }
155  }
156 
157  if ( (mode != Kwave::Overwrite) &&
158  !signal_manager.registerUndoAction(undo) )
159  {
160  // aborted, do not continue without undo
161  clear();
162  return false;
163  }
164 
165  // Everything was ok, the action now is owned by the current undo
166  // transaction. The transaction is owned by the SignalManager and
167  // will be closed when the writer gets closed.
168  return true;
169 }
170 
171 //***************************************************************************
172 //***************************************************************************
QWidget * parentWidget() const
virtual unsigned int tracks() const Q_DECL_OVERRIDE
sample_index_t first() const
Definition: Selection.h:71
Definition: App.h:33
sample_index_t last() const
Definition: Selection.h:76
Kwave::Selection & selection()
Kwave::Writer * openWriter(Kwave::InsertMode mode, unsigned int track, sample_index_t left=0, sample_index_t right=0)
virtual bool insert(unsigned int track, Kwave::Writer *writer) Q_DECL_OVERRIDE
Definition: MultiWriter.cpp:37
quint64 sample_index_t
Definition: Sample.h:28
bool registerUndoAction(Kwave::UndoAction *action)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
virtual void flush()
sample_index_t length()
void startUndoTransaction(const QString &name=QString())
sample_index_t last() const
Definition: Writer.h:114
bool init(Kwave::SignalManager &signal_manager, const QList< unsigned int > &track_list, Kwave::InsertMode mode, sample_index_t left, sample_index_t right)
bool undoEnabled() const
sample_index_t first() const
Definition: Writer.h:108
const QList< unsigned int > selectedTracks()
InsertMode
Definition: InsertMode.h:26
virtual void clear() Q_DECL_OVERRIDE
Definition: MultiWriter.cpp:97
virtual Kwave::Writer * at(unsigned int track) const