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-2010 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 #include "SegmentQuickLinkCommand.h"
19 
20 #include "misc/AppendLabel.h"
21 #include "misc/Strings.h"
22 #include "base/Composition.h"
23 #include "base/SegmentLinker.h"
24 
25 namespace Rosegarden
26 {
27 
28 SegmentQuickLinkCommand::SegmentQuickLinkCommand(Segment *segment) :
29         NamedCommand(getGlobalName()),
30         m_composition(segment->getComposition()),
SegmentQuickCopyCommand(Segment * segment)31         m_originalSegment(segment),
32         m_newLinkedSegment(nullptr),
33         m_detached(false),
34         m_originalSegmentLinked(false)
35 {
36 
37 }
38 
~SegmentQuickCopyCommand()39 SegmentQuickLinkCommand::~SegmentQuickLinkCommand()
40 {
41     if (m_detached) {
42         delete m_newLinkedSegment;
43     }
44 }
45 
46 void
execute()47 SegmentQuickLinkCommand::execute()
48 {
49     if (!m_newLinkedSegment) {
50         m_originalSegmentLinked = m_originalSegment->isLinked();
51         m_newLinkedSegment = SegmentLinker::createLinkedSegment(m_originalSegment);
52 
53         std::string label = m_originalSegment->getLabel();
54         m_newLinkedSegment->setLabel(appendLabel(label, qstrtostr(tr("(linked)"))));
55     } else {
56         if (!m_originalSegmentLinked) {
57             m_newLinkedSegment->getLinker()->addLinkedSegment(m_originalSegment);
58         }
59     }
60 
61     m_composition->addSegment(m_newLinkedSegment);
62     m_detached = false;
63 }
64 
65 void
66 SegmentQuickLinkCommand::unexecute()
67 {
68     if (!m_originalSegmentLinked) {
69         SegmentLinker::unlinkSegment(m_originalSegment);
70     }
71 
72     m_composition->detachSegment(m_newLinkedSegment);
73     m_detached = true;
74 }
75 
76 }