summaryrefslogtreecommitdiffstats
path: root/src/commands/edit/SetLyricsCommand.cpp
blob: cfee2bf73a4df9c204786488179dacb5d1a9d9cb (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
/* -*- 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   <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 "SetLyricsCommand.h"

#include "base/Event.h"
#include "misc/Strings.h"
#include "misc/Debug.h"
#include "base/Composition.h"
#include "base/NotationTypes.h"
#include "base/Segment.h"
#include "base/BaseProperties.h"
#include <tqregexp.h>
#include <tqstring.h>
#include <tqstringlist.h>


namespace Rosegarden
{

using namespace BaseProperties;

SetLyricsCommand::SetLyricsCommand(Segment *segment, int verse, TQString newLyricData) :
        KNamedCommand(getGlobalName()),
        m_segment(segment),
        m_verse(verse),
        m_newLyricData(newLyricData)
{
    // nothing
}

SetLyricsCommand::~SetLyricsCommand()
{
    for (std::vector<Event *>::iterator i = m_oldLyricEvents.begin();
            i != m_oldLyricEvents.end(); ++i) {
        delete *i;
    }
}

void
SetLyricsCommand::execute()
{
    // This and LyricEditDialog::unparse() are opposites that will
    // need to be kept in sync with any changes to one another.  (They
    // should really both be in a common lyric management class.)

    // first remove old lyric events

    Segment::iterator i = m_segment->begin();

    while (i != m_segment->end()) {

        Segment::iterator j = i;
        ++j;

        if ((*i)->isa(Text::EventType)) {
            std::string textType;
            if ((*i)->get<String>(Text::TextTypePropertyName, textType) &&
                textType == Text::Lyric) {
                long verse = 0;
                (*i)->get<Int>(Text::LyricVersePropertyName, verse);
                if (verse == m_verse) {
                    m_oldLyricEvents.push_back(new Event(**i));
                    m_segment->erase(i);
                }
            }
        }

        i = j;
    }

    // now parse the new string

    TQStringList barStrings =
        TQStringList::split("/", m_newLyricData, true); // empties ok

    Composition *comp = m_segment->getComposition();
    int barNo = comp->getBarNumber(m_segment->getStartTime());

    for (TQStringList::Iterator bsi = barStrings.begin();
            bsi != barStrings.end(); ++bsi) {

        NOTATION_DEBUG << "Parsing lyrics for bar number " << barNo << ": \"" << *bsi << "\"" << endl;

        std::pair<timeT, timeT> barRange = comp->getBarRange(barNo++);
        TQString syllables = *bsi;
        syllables.replace(TQRegExp("\\[\\d+\\] "), " ");
        TQStringList syllableList = TQStringList::split(" ", syllables); // no empties

        i = m_segment->findTime(barRange.first);
        timeT laterThan = barRange.first - 1;

        for (TQStringList::Iterator ssi = syllableList.begin();
                ssi != syllableList.end(); ++ssi) {

            while (m_segment->isBeforeEndMarker(i) &&
                    (*i)->getAbsoluteTime() < barRange.second &&
                    (!(*i)->isa(Note::EventType) ||
                     (*i)->getNotationAbsoluteTime() <= laterThan ||
                     ((*i)->has(TIED_BACKWARD) &&
                      (*i)->get
                      <Bool>(TIED_BACKWARD)))) ++i;

            timeT time = m_segment->getEndMarkerTime();
            timeT notationTime = time;
            if (m_segment->isBeforeEndMarker(i)) {
                time = (*i)->getAbsoluteTime();
                notationTime = (*i)->getNotationAbsoluteTime();
            }

            TQString syllable = *ssi;
            syllable.replace(TQRegExp("~"), " ");
            syllable = syllable.simplifyWhiteSpace();
            if (syllable == "")
                continue;
            laterThan = notationTime + 1;
            if (syllable == ".")
                continue;

            NOTATION_DEBUG << "Syllable \"" << syllable << "\" at time " << time << endl;

            Text text(qstrtostr(syllable), Text::Lyric);
            Event *event = text.getAsEvent(time);
            event->set<Int>(Text::LyricVersePropertyName, m_verse);
            m_segment->insert(event);
        }
    }
}

void
SetLyricsCommand::unexecute()
{
    // Before we inserted the new lyric events (in execute()), we
    // removed all the existing ones.  That means we know any lyric
    // events found now must have been inserted by execute(), so we
    // can safely remove them before restoring the old ones.

    Segment::iterator i = m_segment->begin();

    while (i != m_segment->end()) {

        Segment::iterator j = i;
        ++j;

        if ((*i)->isa(Text::EventType)) {
            std::string textType;
            if ((*i)->get<String>(Text::TextTypePropertyName, textType) &&
                textType == Text::Lyric) {
                long verse = 0;
                (*i)->get<Int>(Text::LyricVersePropertyName, verse);
                if (verse == m_verse) {
                    m_segment->erase(i);
                }
            }
        }

        i = j;
    }

    // Now restore the old ones and clear out the vector.

    for (std::vector<Event *>::iterator i = m_oldLyricEvents.begin();
            i != m_oldLyricEvents.end(); ++i) {
        m_segment->insert(*i);
    }

    m_oldLyricEvents.clear();
}

}