summaryrefslogtreecommitdiffstats
path: root/src/gui/dialogs/LyricEditDialog.cpp
blob: c73467d95634695e0b6dd25c44d7d44ad7575bd3 (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
/* -*- 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 "LyricEditDialog.h"

#include "base/Event.h"
#include "base/BaseProperties.h"
#include <tdelocale.h>
#include "misc/Strings.h"
#include "misc/Debug.h"
#include "base/Composition.h"
#include "base/NotationTypes.h"
#include "base/Segment.h"
#include <kdialogbase.h>
#include <tqgroupbox.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <tqtextedit.h>
#include <tqvbox.h>
#include <tqwidget.h>
#include <kcombobox.h>
#include <tqlabel.h>
#include <tqpushbutton.h>


namespace Rosegarden
{

LyricEditDialog::LyricEditDialog(TQWidget *parent,
                                 Segment *segment) :
    KDialogBase(parent, 0, true, i18n("Edit Lyrics"), Ok | Cancel | Help),
    m_segment(segment),
    m_verseCount(0)
{
    setHelp("nv-text-lyrics");

    TQVBox *vbox = makeVBoxMainWidget();

    TQGroupBox *groupBox = new TQGroupBox
                          (1, Qt::Horizontal, i18n("Lyrics for this segment"), vbox);

    TQHBox *hbox = new TQHBox(groupBox);
    hbox->setSpacing(5);
//    new TQLabel(i18n("Verse:"), hbox);
    m_verseNumber = new KComboBox(hbox);
    m_verseNumber->setEditable(false);
    connect(m_verseNumber, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotVerseNumberChanged(int)));
    m_verseAddButton = new TQPushButton(i18n("Add Verse"), hbox);
    connect(m_verseAddButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAddVerse()));
    TQFrame *f = new TQFrame(hbox);
    hbox->setStretchFactor(f, 10);

    m_textEdit = new TQTextEdit(groupBox);
    m_textEdit->setTextFormat(TQt::PlainText);

    m_textEdit->setMinimumWidth(300);
    m_textEdit->setMinimumHeight(200);

    unparse();
    
    for (int i = 0; i < m_verseCount; ++i) {
        m_verseNumber->insertItem(i18n("Verse %1").arg(i + 1));
    }
    m_currentVerse = 0;
    if (m_verseCount == 12) m_verseAddButton->setEnabled(false);
}

void
LyricEditDialog::slotVerseNumberChanged(int verse)
{
    NOTATION_DEBUG << "LyricEditDialog::slotVerseNumberChanged(" << verse << ")" << endl;
    TQString text = m_textEdit->text();
    m_texts[m_currentVerse] = text;
    m_textEdit->setText(m_texts[verse]);
    m_currentVerse = verse;
}

void
LyricEditDialog::slotAddVerse()
{
    NOTATION_DEBUG << "LyricEditDialog::slotAddVerse" << endl;
    m_verseCount++;
    m_texts.push_back(m_skeleton);
    m_verseNumber->insertItem(i18n("Verse %1").arg(m_verseCount));
    m_verseNumber->setCurrentItem(m_verseCount - 1);
    slotVerseNumberChanged(m_verseCount - 1);
    if (m_verseCount == 12) m_verseAddButton->setEnabled(false);
}

void
LyricEditDialog::countVerses()
{
    m_verseCount = 1;

    for (Segment::iterator i = m_segment->begin();
         m_segment->isBeforeEndMarker(i); ++i) {

        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_verseCount) m_verseCount = verse + 1;
            }
        }
    }
}

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

    countVerses();

    Composition *comp = m_segment->getComposition();

    bool firstNote = true;
    timeT lastTime = m_segment->getStartTime();
    int lastBarNo = comp->getBarNumber(lastTime);
    std::map<int, bool> haveLyric;

    TQString fragment = TQString("[%1] ").arg(lastBarNo + 1);

    m_skeleton = fragment;
    m_texts.clear();
    for (size_t v = 0; v < m_verseCount; ++v) {
        m_texts.push_back(fragment);
        haveLyric[v] = false;
    }

    for (Segment::iterator i = m_segment->begin();
         m_segment->isBeforeEndMarker(i); ++i) {

        bool isNote = (*i)->isa(Note::EventType);
        bool isLyric = false;

        if (!isNote) {
            if ((*i)->isa(Text::EventType)) {
                std::string textType;
                if ((*i)->get<String>(Text::TextTypePropertyName, textType) &&
                    textType == Text::Lyric) {
                    isLyric = true;
                }
            }
        } else {
            if ((*i)->has(BaseProperties::TIED_BACKWARD) &&
                (*i)->get<Bool>(BaseProperties::TIED_BACKWARD)) {
                continue;
            }
        }

        if (!isNote && !isLyric) continue;

        timeT myTime = (*i)->getNotationAbsoluteTime();
        int myBarNo = comp->getBarNumber(myTime);

        if (myBarNo > lastBarNo) {

            fragment = "";

            while (myBarNo > lastBarNo) {
                fragment += " /";
                ++lastBarNo;
            }

            fragment += TQString("\n[%1] ").arg(myBarNo + 1);

            m_skeleton += fragment;
            for (size_t v = 0; v < m_verseCount; ++v) m_texts[v] += fragment;
        }

        if (isNote) {
            if ((myTime > lastTime) || firstNote) {
                m_skeleton += " .";
                for (size_t v = 0; v < m_verseCount; ++v) {
                    if (!haveLyric[v]) m_texts[v] += " .";
                    haveLyric[v] = false;
                }
                lastTime = myTime;
                firstNote = false;
            }
        }

        if (isLyric) {

            std::string ssyllable;
            (*i)->get<String>(Text::TextPropertyName, ssyllable);

            long verse = 0;
            (*i)->get<Int>(Text::LyricVersePropertyName, verse);

            TQString syllable(strtoqstr(ssyllable));
            syllable.replace(TQRegExp("\\s+"), "~");

            m_texts[verse] += " " + syllable;
            haveLyric[verse] = true;
        }
    }

    if (!m_texts.empty()) {
        m_textEdit->setText(m_texts[0]);
    } else {
        m_texts.push_back(m_skeleton);
    }
}

int
LyricEditDialog::getVerseCount() const
{
    return m_verseCount;
}

TQString
LyricEditDialog::getLyricData(int verse) const
{
    if (verse == m_verseNumber->currentItem()) {
        return m_textEdit->text();
    } else {
        return m_texts[verse];
    }
}

}
#include "LyricEditDialog.moc"