summaryrefslogtreecommitdiffstats
path: root/src/commands/edit/PasteSegmentsCommand.cpp
blob: 7db92889b31a72150671d6c17f045557ab9b93ef (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
/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    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 "PasteSegmentsCommand.h"

#include "base/Clipboard.h"
#include "base/Composition.h"
#include "base/Segment.h"
#include "base/Track.h"
#include <tqstring.h>


namespace Rosegarden
{

PasteSegmentsCommand::PasteSegmentsCommand(Composition *composition,
        Clipboard *clipboard,
        timeT pasteTime,
        TrackId baseTrack,
        bool useExactTracks) :
        KNamedCommand(getGlobalName()),
        m_composition(composition),
        m_clipboard(new Clipboard(*clipboard)),
        m_pasteTime(pasteTime),
        m_baseTrack(baseTrack),
        m_exactTracks(useExactTracks),
        m_detached(false)
{
    // nothing else
}

PasteSegmentsCommand::~PasteSegmentsCommand()
{
    if (m_detached) {
        for (unsigned int i = 0; i < m_addedSegments.size(); ++i) {
            delete m_addedSegments[i];
        }
    }

    delete m_clipboard;
}

void
PasteSegmentsCommand::execute()
{
    if (m_addedSegments.size() > 0) {
        // been here before
        for (unsigned int i = 0; i < m_addedSegments.size(); ++i) {
            m_composition->addSegment(m_addedSegments[i]);
        }
        return ;
    }

    if (m_clipboard->isEmpty())
        return ;

    // We want to paste such that the earliest Segment starts at
    // m_pasteTime and the others start at the same times relative to
    // that as they did before.  Likewise for track, unless
    // m_exactTracks is set.

    timeT earliestStartTime = m_clipboard->getBaseTime();
    timeT latestEndTime = 0;
    int lowestTrackPos = -1;

    for (Clipboard::iterator i = m_clipboard->begin();
            i != m_clipboard->end(); ++i) {

        int trackPos = m_composition->getTrackPositionById((*i)->getTrack());
        if (trackPos >= 0 &&
                (lowestTrackPos < 0 || trackPos < lowestTrackPos)) {
            lowestTrackPos = trackPos;
        }

        if ((*i)->getEndMarkerTime() > latestEndTime) {
            latestEndTime = (*i)->getEndMarkerTime();
        }
    }

    if (m_exactTracks)
        lowestTrackPos = 0;
    if (lowestTrackPos < 0)
        lowestTrackPos = 0;
    timeT offset = m_pasteTime - earliestStartTime;
    int baseTrackPos = m_composition->getTrackPositionById(m_baseTrack);
    int trackOffset = baseTrackPos - lowestTrackPos;

    for (Clipboard::iterator i = m_clipboard->begin();
            i != m_clipboard->end(); ++i) {

        int newTrackPos = trackOffset +
                          m_composition->getTrackPositionById((*i)->getTrack());

        Track *track = m_composition->getTrackByPosition(newTrackPos);

        if (!track) {
            newTrackPos = 0;
            track = m_composition->getTrackByPosition(newTrackPos);
        }

        TrackId newTrackId = track->getId();

        Segment *segment = new Segment(**i);
        segment->setStartTime(segment->getStartTime() + offset);
        segment->setTrack(newTrackId);
        m_composition->addSegment(segment);
        if (m_clipboard->isPartial()) {
            segment->normalizeRests(segment->getStartTime(),
                                    segment->getEndMarkerTime());
        }
        m_addedSegments.push_back(segment);
    }

    // User preference? Update song pointer position on paste
    m_composition->setPosition(latestEndTime
                               + m_pasteTime
                               - earliestStartTime);

    m_detached = false;
}

void
PasteSegmentsCommand::unexecute()
{
    for (unsigned int i = 0; i < m_addedSegments.size(); ++i) {
        m_composition->detachSegment(m_addedSegments[i]);
    }
    m_detached = true;
}

}