1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/***************************************************************************
MultiTrackWriter.cpp - writer for multi-track signals
-------------------
begin : Sat Jun 30 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <new>
#include "libkwave/MultiTrackWriter.h"
#include "libkwave/SignalManager.h"
#include "libkwave/Utils.h"
#include "libkwave/undo/UndoInsertAction.h"
#include "libkwave/undo/UndoModifyAction.h"
#include "libkwave/undo/UndoTransactionGuard.h"
//***************************************************************************
Kwave::MultiTrackWriter::MultiTrackWriter()
:Kwave::MultiWriter()
{
}
//***************************************************************************
Kwave::MultiTrackWriter::MultiTrackWriter(
Kwave::SignalManager &signal_manager,
const QVector<unsigned int> &track_list,
Kwave::InsertMode mode,
sample_index_t left,
sample_index_t right)
:Kwave::MultiWriter()
{
if (!init(signal_manager, track_list, mode, left, right)) {
// out of memory or aborted
qWarning("MultiTrackWriter::init FAILED");
}
}
//***************************************************************************
Kwave::MultiTrackWriter::MultiTrackWriter(Kwave::SignalManager &signal_manager,
Kwave::InsertMode mode)
:Kwave::MultiWriter()
{
QVector<unsigned int> track_list = signal_manager.selectedTracks();
sample_index_t left = 0;
sample_index_t right = 0;
if (signal_manager.length()) {
// default if signal is present: current selection
left = signal_manager.selection().first();
right = signal_manager.selection().last();
if (left == right) {
// if no selection: whole signal
left = 0;
right = signal_manager.length() - 1;
}
}
if (!init(signal_manager, track_list, mode, left, right)) {
// out of memory or aborted
qWarning("MultiTrackWriter::init FAILED");
}
}
//***************************************************************************
Kwave::MultiTrackWriter::~MultiTrackWriter()
{
flush();
clear();
}
//***************************************************************************
bool Kwave::MultiTrackWriter::init(Kwave::SignalManager &signal_manager,
const QVector<unsigned int> &track_list,
Kwave::InsertMode mode,
sample_index_t left,
sample_index_t right)
{
Kwave::UndoTransactionGuard guard(signal_manager, QString());
unsigned int index = 0;
foreach (unsigned int track, track_list) {
// NOTE: this function is *nearly* identical to the one in the
// Signal class, except for undo support
Kwave::Writer *writer = signal_manager.openWriter(
mode, track, left, right);
if (writer) {
insert(index++, writer);
// get the real/effective left and right sample
left = writer->first();
right = writer->last();
} else {
// out of memory or aborted
clear();
return false;
}
}
// skip all that undo handling below if undo is not enabled
// or the writer creation has failed
if (!signal_manager.undoEnabled()) return true;
// enter a new undo transaction and let it close when the writer closes
signal_manager.startUndoTransaction();
QObject::connect(this, SIGNAL(destroyed()),
&signal_manager, SLOT(closeUndoTransaction()),
Qt::QueuedConnection);
// create an undo action for the modification of the samples
UndoAction *undo = nullptr;
switch (mode) {
case Kwave::Append:
case Kwave::Insert: {
undo = new(std::nothrow) Kwave::UndoInsertAction(
signal_manager.parentWidget(),
track_list,
left,
right - left + 1
);
if (undo) {
Kwave::Writer *last_writer = at(tracks() - 1);
QObject::connect(
last_writer,
SIGNAL(sigSamplesWritten(sample_index_t)),
static_cast<Kwave::UndoInsertAction *>(undo),
SLOT(setLength(sample_index_t)));
}
break;
}
case Kwave::Overwrite: {
foreach (unsigned int track, track_list) {
undo = new(std::nothrow) Kwave::UndoModifyAction(
track, left, right - left + 1);
if (!signal_manager.registerUndoAction(undo)) {
// aborted, do not continue without undo
clear();
return false;
}
}
break;
}
DEFAULT_IMPOSSIBLE;
}
if ( (mode != Kwave::Overwrite) &&
!signal_manager.registerUndoAction(undo) )
{
// aborted, do not continue without undo
clear();
return false;
}
// Everything was ok, the action now is owned by the current undo
// transaction. The transaction is owned by the SignalManager and
// will be closed when the writer gets closed.
return true;
}
//***************************************************************************
//***************************************************************************
#include "moc_MultiTrackWriter.cpp"
|