1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A MIDI and audio sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7 
8     Other copyrights also apply to some parts of this work.  Please
9     see the AUTHORS file and individual file headers for details.
10 
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.  See the file
15     COPYING included with this distribution for more information.
16 */
17 
18 
19 #include "SegmentQuickCopyCommand.h"
20 
21 #include "misc/AppendLabel.h"
22 #include "misc/Strings.h"
23 #include "base/Composition.h"
24 #include "base/Segment.h"
25 #include <QString>
26 
27 
28 namespace Rosegarden
29 {
30 
31 SegmentQuickCopyCommand::SegmentQuickCopyCommand(Segment *segment):
32         NamedCommand(getGlobalName()),
33         m_composition(segment->getComposition()),
34         m_originalSegment(segment),
35         m_newSegment(nullptr),
36         m_detached(false)
37 {}
38 
39 SegmentQuickCopyCommand::~SegmentQuickCopyCommand()
index_form_tuple(TupleDesc tupleDescriptor,Datum * values,bool * isnull)40 {
41     if (m_detached) {
42         delete m_newSegment;
43     }
44 }
45 
46 void
47 SegmentQuickCopyCommand::execute()
48 {
49     if (!m_newSegment) {
50         m_newSegment = m_originalSegment->clone(false);
51         m_originalLabel = m_originalSegment->getLabel();
52         // We rename the original segment to end in (copied) since it is
53         // the one that is being moved by SegmentTool.  The copy is being
54         // left behind in place of the original.
55         m_originalSegment->setLabel(
56                 appendLabel(m_originalLabel, qstrtostr(tr("(copied)"))));
57     }
58     m_composition->addSegment(m_newSegment);
59     m_detached = false;
60 }
61 
62 void
63 SegmentQuickCopyCommand::unexecute()
64 {
65     // Delete the copy
66     m_composition->detachSegment(m_newSegment);
67     m_detached = true;
68 
69     // Put back the original label.
70     m_originalSegment->setLabel(m_originalLabel);
71 }
72 
73 }
74