summaryrefslogtreecommitdiffstats
path: root/src/commands/edit/PasteEventsCommand.cpp
blob: 2e2e45833fdc7f308f28cbad141885c6af9a502b (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    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.  See the file
    COPYING included with this distribution for more information.
*/


#include "PasteEventsCommand.h"

#include "misc/Debug.h"
#include "base/Clipboard.h"
#include "base/Event.h"
#include "base/NotationTypes.h"
#include "base/Segment.h"
#include "base/SegmentNotationHelper.h"
#include "document/BasicCommand.h"
#include <tqstring.h>
#include "base/BaseProperties.h"


namespace Rosegarden
{

using namespace BaseProperties;

PasteEventsCommand::PasteEventsCommand(Segment &segment,
                                       Clipboard *clipboard,
                                       timeT pasteTime,
                                       PasteType pasteType) :
    BasicCommand(getGlobalName(), segment, pasteTime,
                 getEffectiveEndTime(segment, clipboard, pasteTime)),
    m_relayoutEndTime(getEndTime()),
    m_clipboard(new Clipboard(*clipboard)),
    m_pasteType(pasteType),
    m_pastedEvents(segment)
{
    if (pasteType != OpenAndPaste) {

        // paste clef or key -> relayout to end

        if (clipboard->isSingleSegment()) {

            Segment *s(clipboard->getSingleSegment());
            for (Segment::iterator i = s->begin(); i != s->end(); ++i) {
                if ((*i)->isa(Clef::EventType) ||
                    (*i)->isa(Key::EventType)) {
                    m_relayoutEndTime = s->getEndTime();
                    break;
                }
            }
        }
    }
}

PasteEventsCommand::PasteEventsCommand(Segment &segment,
                                       Clipboard *clipboard,
                                       timeT pasteTime,
                                       timeT pasteEndTime,
                                       PasteType pasteType) :
    BasicCommand(getGlobalName(), segment, pasteTime, pasteEndTime),
    m_relayoutEndTime(getEndTime()),
    m_clipboard(new Clipboard(*clipboard)),
    m_pasteType(pasteType),
    m_pastedEvents(segment)
{}

PasteEventsCommand::~PasteEventsCommand()
{
    delete m_clipboard;
}

PasteEventsCommand::PasteTypeMap

PasteEventsCommand::getPasteTypes()
{
    static PasteTypeMap types;
    static bool haveTypes = false;
    if (!haveTypes) {
        types[Restricted] =
            i18n("Paste into an existing gap [\"restricted\"]");
        types[Simple] =
            i18n("Erase existing events to make room [\"simple\"]");
        types[OpenAndPaste] =
            i18n("Move existing events out of the way [\"open-n-paste\"]");
        types[NoteOverlay] =
            i18n("Overlay notes, tying against present notes [\"note-overlay\"]");
        types[MatrixOverlay] =
            i18n("Overlay notes, ignoring present notes [\"matrix-overlay\"]");
    }
    return types;
}

timeT
PasteEventsCommand::getEffectiveEndTime(Segment &segment,
                                        Clipboard *clipboard,
                                        timeT pasteTime)
{
    if (!clipboard->isSingleSegment()) {
        RG_DEBUG << "PasteEventsCommand::getEffectiveEndTime: not single segment" << endl;
        return pasteTime;
    }

    RG_DEBUG << "PasteEventsCommand::getEffectiveEndTime: clipboard "
    << clipboard->getSingleSegment()->getStartTime()
    << " -> "
    << clipboard->getSingleSegment()->getEndTime() << endl;

    timeT d = clipboard->getSingleSegment()->getEndTime() -
              clipboard->getSingleSegment()->getStartTime();

    if (m_pasteType == OpenAndPaste) {
        return segment.getEndTime() + d;
    } else {
        Segment::iterator i = segment.findTime(pasteTime + d);
        if (i == segment.end())
            return segment.getEndTime();
        else
            return (*i)->getAbsoluteTime();
    }
}

timeT
PasteEventsCommand::getRelayoutEndTime()
{
    return m_relayoutEndTime;
}

bool
PasteEventsCommand::isPossible()
{
    if (m_clipboard->isEmpty() || !m_clipboard->isSingleSegment()) {
        return false;
    }

    if (m_pasteType != Restricted) {
        return true;
    }

    Segment *source = m_clipboard->getSingleSegment();

    timeT pasteTime = getStartTime();
    timeT origin = source->getStartTime();
    timeT duration = source->getEndTime() - origin;

    RG_DEBUG << "PasteEventsCommand::isPossible: paste time is " << pasteTime << ", origin is " << origin << ", duration is " << duration << endl;

    SegmentNotationHelper helper(getSegment());
    return helper.removeRests(pasteTime, duration, true);
}

void
PasteEventsCommand::modifySegment()
{
    RG_DEBUG << "PasteEventsCommand::modifySegment" << endl;

    if (!m_clipboard->isSingleSegment())
        return ;

    Segment *source = m_clipboard->getSingleSegment();

    timeT pasteTime = getStartTime();
    timeT origin = source->getStartTime();
    timeT duration = source->getEndTime() - origin;

    Segment *destination(&getSegment());
    SegmentNotationHelper helper(*destination);

    RG_DEBUG << "PasteEventsCommand::modifySegment() : paste type = "
    << m_pasteType << " - pasteTime = "
    << pasteTime << " - origin = " << origin << endl;

    // First check for group IDs, which we want to make unique in the
    // copies in the destination segment

    std::map<long, long> groupIdMap;
    for (Segment::iterator i = source->begin(); i != source->end(); ++i) {
        long groupId = -1;
        if ((*i)->get
                <Int>(BEAMED_GROUP_ID, groupId)) {
            if (groupIdMap.find(groupId) == groupIdMap.end()) {
                groupIdMap[groupId] = destination->getNextId();
            }
        }
    }

    switch (m_pasteType) {

        // Do some preliminary work to make space or whatever;
        // we do the actual paste after this switch statement
        // (except where individual cases do the work and return)

    case Restricted:
        if (!helper.removeRests(pasteTime, duration))
            return ;
        break;

    case Simple:
        destination->erase(destination->findTime(pasteTime),
                           destination->findTime(pasteTime + duration));
        break;

    case OpenAndPaste: {
            std::vector<Event *> copies;
            for (Segment::iterator i = destination->findTime(pasteTime);
                    i != destination->end(); ++i) {
                Event *e = (*i)->copyMoving(duration);
                if (e->has(BEAMED_GROUP_ID)) {
                    e->set
                    <Int>(BEAMED_GROUP_ID, groupIdMap[e->get
                                                      <Int>(BEAMED_GROUP_ID)]);
                }
                copies.push_back(e);
            }

            destination->erase(destination->findTime(pasteTime),
                               destination->end());

            for (unsigned int i = 0; i < copies.size(); ++i) {
                destination->insert(copies[i]);
                m_pastedEvents.addEvent(copies[i]);
            }

            break;
        }

    case NoteOverlay:
        for (Segment::iterator i = source->begin(); i != source->end(); ++i) {
            if ((*i)->isa(Note::EventRestType))
                continue;
            Event *e = (*i)->copyMoving(pasteTime - origin);
            if (e->has(BEAMED_GROUP_ID)) {
                e->set<Int>(BEAMED_GROUP_ID,
                            groupIdMap[e->get<Int>(BEAMED_GROUP_ID)]);
            }
            if ((*i)->isa(Note::EventType)) {
                // e is model event: we retain ownership of it
                Segment::iterator i = helper.insertNote(e);
                delete e;
                if (i != destination->end()) m_pastedEvents.addEvent(*i);
            } else {
                destination->insert(e);
                m_pastedEvents.addEvent(e);
            }
        }

        return ;

    case MatrixOverlay:

        for (Segment::iterator i = source->begin(); i != source->end(); ++i) {

            if ((*i)->isa(Note::EventRestType))
                continue;

            Event *e = (*i)->copyMoving(pasteTime - origin);

            if (e->has(BEAMED_GROUP_TYPE) &&
                    e->get
                    <String>(BEAMED_GROUP_TYPE) == GROUP_TYPE_BEAMED) {
                e->unset(BEAMED_GROUP_ID);
                e->unset(BEAMED_GROUP_TYPE);
            }

            if (e->has(BEAMED_GROUP_ID)) {
                e->set
                <Int>(BEAMED_GROUP_ID, groupIdMap[e->get
                                                  <Int>(BEAMED_GROUP_ID)]);
            }

            destination->insert(e);
            m_pastedEvents.addEvent(e);
        }

        destination->normalizeRests
        (source->getStartTime(), source->getEndTime());

        return ;
    }

    RG_DEBUG << "PasteEventsCommand::modifySegment() - inserting\n";

    for (Segment::iterator i = source->begin(); i != source->end(); ++i) {
        Event *e = (*i)->copyMoving(pasteTime - origin);
        if (e->has(BEAMED_GROUP_ID)) {
            e->set
            <Int>(BEAMED_GROUP_ID, groupIdMap[e->get
                                              <Int>(BEAMED_GROUP_ID)]);
        }
        destination->insert(e);
        m_pastedEvents.addEvent(e);
    }

    destination->normalizeRests
    (source->getStartTime(), source->getEndTime());
}

EventSelection
PasteEventsCommand::getPastedEvents()
{
    return m_pastedEvents;
}

}