-
Notifications
You must be signed in to change notification settings - Fork 6
/
lyrics.cpp
486 lines (456 loc) · 16.8 KB
/
lyrics.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
Drumstick MIDI File Player Multiplatform Program
Copyright (C) 2006-2024, Pedro Lopez-Cabanillas <[email protected]>
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 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QApplication>
#include <QWidget>
#include <QCloseEvent>
#include <QAction>
#include <QLabel>
#include <QMenu>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QComboBox>
#include <QToolButton>
#include <QTextEdit>
#include <QFrame>
#include <QFontDialog>
#include <QFileDialog>
#include <QFileInfo>
#include <QTextCodec>
#include <QScrollBar>
#include <QToolBar>
#include <QClipboard>
#include <QSaveFile>
#include <QPrintDialog>
#include <QPrinter>
#include <QRegularExpression>
#include <QTextStream>
#if QT_VERSION < QT_VERSION_CHECK(5,14,0)
#include <QDesktopWidget>
#else
#include <QScreen>
#endif
#include <drumstick/settingsfactory.h>
#include "settings.h"
#include "iconutils.h"
#include "lyrics.h"
#include "sequence.h"
Lyrics::Lyrics(QWidget *parent) : FramelessWindow(parent),
m_track(0),
m_mib(0),
m_type(0),
m_song(nullptr),
m_codec(nullptr)
{
setObjectName(QString::fromUtf8("Lyrics"));
setContextMenuPolicy(Qt::CustomContextMenu); // prevent default ctx
QToolBar* tbar = new QToolBar(this);
tbar->setObjectName("toolbar");
tbar->setMovable(false);
tbar->setFloatable(false);
tbar->setIconSize(QSize(22,22));
addToolBar(tbar);
setPseudoCaption(tbar);
m_actionCopy = new QAction(this);
m_actionCopy->setObjectName(QString::fromUtf8("actionCopy"));
m_actionSave = new QAction(this);
m_actionSave->setObjectName(QString::fromUtf8("actionSave"));
m_actionPrint = new QAction(this);
m_actionPrint->setObjectName(QString::fromUtf8("actionPrint"));
m_label1 = new QLabel(this);
m_label1->setObjectName(QString::fromUtf8("label1"));
tbar->addWidget(m_label1);
m_comboTrack = new QComboBox(this);
m_comboTrack->setObjectName(QString::fromUtf8("comboTrack"));
QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(m_comboTrack->sizePolicy().hasHeightForWidth());
m_comboTrack->setSizePolicy(sizePolicy);
tbar->addWidget(m_comboTrack);
m_label2 = new QLabel(this);
m_label2->setObjectName(QString::fromUtf8("label2"));
tbar->addWidget(m_label2);
m_comboType = new QComboBox(this);
m_comboType->addItem(QString());
m_comboType->addItem(QString());
m_comboType->addItem(QString());
m_comboType->addItem(QString());
m_comboType->addItem(QString());
m_comboType->addItem(QString());
m_comboType->addItem(QString());
m_comboType->addItem(QString());
m_comboType->setObjectName(QString::fromUtf8("comboType"));
sizePolicy.setHeightForWidth(m_comboType->sizePolicy().hasHeightForWidth());
m_comboType->setSizePolicy(sizePolicy);
tbar->addWidget(m_comboType);
m_label3 = new QLabel(this);
m_label3->setObjectName(QString::fromUtf8("label3"));
tbar->addWidget(m_label3);
m_comboCodec = new QComboBox(this);
m_comboCodec->setObjectName(QString::fromUtf8("comboCodec"));
sizePolicy.setHeightForWidth(m_comboCodec->sizePolicy().hasHeightForWidth());
m_comboCodec->setSizePolicy(sizePolicy);
m_comboCodec->setMaxVisibleItems(10);
m_comboCodec->setStyleSheet("combobox-popup: 0;");
tbar->addWidget(m_comboCodec);
QWidget *spacer = new QWidget(this);
spacer->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
tbar->addWidget(spacer);
m_chmenu = new QMenu(this);
m_chmenu->addAction(m_actionCopy);
m_chmenu->addAction(m_actionSave);
m_chmenu->addAction(m_actionPrint);
m_actionFullScreen = new QAction(this); // Full Screen
m_actionFullScreen->setShortcut(QKeySequence::FullScreen);
m_chmenu->addAction(m_actionFullScreen);
m_actionFont = new QAction(this); // Font dialog
m_chmenu->addAction(m_actionFont);
m_toolButton = new QToolButton(this);
m_toolButton->setObjectName(QString::fromUtf8("toolButton"));
m_toolButton->setMenu(m_chmenu);
m_toolButton->setPopupMode(QToolButton::InstantPopup);
m_toolButton->setIcon(IconUtils::GetIcon("application-menu"));
tbar->addWidget(m_toolButton);
auto closeBtn = new QToolButton(this);
closeBtn->setIcon(IconUtils::GetIcon("window-close"));
connect(closeBtn, &QToolButton::clicked, this, &Lyrics::close);
tbar->addWidget(closeBtn);
QVBoxLayout *vlayout = new QVBoxLayout;
vlayout->setContentsMargins(5,5,5,5);
QWidget *centralWidget = new QWidget(this);
centralWidget->setLayout(vlayout);
m_textViewer = new QTextEdit(this);
m_textViewer->setObjectName(QString::fromUtf8("textViewer"));
m_textViewer->setFont(Settings::instance()->lyricsFont());
m_textViewer->setReadOnly(true);
m_textViewer->setTextInteractionFlags(Qt::NoTextInteraction);
m_normalColor = Settings::instance()->getFutureColor();
m_otherColor = Settings::instance()->getPastColor();
m_highlightColor = Settings::instance()->highlightColor();
vlayout->addWidget(m_textViewer);
this->setCentralWidget(centralWidget);
#ifndef QT_NO_SHORTCUT
m_label1->setBuddy(m_comboTrack);
m_label2->setBuddy(m_comboType);
m_label3->setBuddy(m_comboCodec);
#endif // QT_NO_SHORTCUT
// Populate the codecs combobox
populateCodecsCombo();
// connect combos
connect(m_comboTrack, QOverload<int>::of(&QComboBox::activated), this, &Lyrics::trackChanged);
connect(m_comboType, QOverload<int>::of(&QComboBox::activated), this, &Lyrics::typeChanged);
connect(m_comboCodec, QOverload<int>::of(&QComboBox::activated), this, &Lyrics::codecChanged);
connect(m_actionFullScreen, &QAction::triggered, this, &Lyrics::toggleFullScreen);
connect(m_actionFont, &QAction::triggered, this, &Lyrics::changeFont);
connect(m_actionCopy, &QAction::triggered, this, &Lyrics::slotCopy);
connect(m_actionSave, &QAction::triggered, this, &Lyrics::slotSave);
connect(m_actionPrint, &QAction::triggered, this, &Lyrics::slotPrint);
retranslateUi();
emit sizeAdjustNeeded();
}
void Lyrics::readSettings()
{
const QByteArray geometry = Settings::instance()->lyricsWindowGeometry();
const QByteArray state = Settings::instance()->lyricsWindowState();
if (geometry.isEmpty()) {
const QRect availableGeometry =
#if QT_VERSION < QT_VERSION_CHECK(5,14,0)
QApplication::desktop()->availableGeometry(parentWidget());
#else
parentWidget()->screen()->availableGeometry();
#endif
setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,
size(), availableGeometry));
} else {
restoreGeometry(geometry);
}
if (!state.isEmpty()) {
restoreState(state);
}
}
void Lyrics::writeSettings()
{
Settings::instance()->setLyricsWindowGeometry(saveGeometry());
Settings::instance()->setLyricsWindowState(saveState());
}
void Lyrics::retranslateUi()
{
m_label1->setText(QApplication::translate("Lyrics", "Track:", nullptr));
m_label2->setText(QApplication::translate("Lyrics", "Type:", nullptr));
m_comboType->setItemText(0, QApplication::translate("Lyrics", "All Types", nullptr));
m_comboType->setItemText(1, QApplication::translate("Lyrics", "Texts", nullptr));
m_comboType->setItemText(2, QApplication::translate("Lyrics", "Copyright Notice", nullptr));
m_comboType->setItemText(3, QApplication::translate("Lyrics", "Sequence Name", nullptr));
m_comboType->setItemText(4, QApplication::translate("Lyrics", "Instrument Name", nullptr));
m_comboType->setItemText(5, QApplication::translate("Lyrics", "Lyric Events", nullptr));
m_comboType->setItemText(6, QApplication::translate("Lyrics", "Marker", nullptr));
m_comboType->setItemText(7, QApplication::translate("Lyrics", "Cue Point", nullptr));
m_label3->setText(QApplication::translate("Lyrics", "Encoding:", nullptr));
m_actionFont->setText(QApplication::translate("Lyrics", "Font...", nullptr));
m_actionFullScreen->setText(QApplication::translate("Lyrics", "Full Screen", nullptr));
m_actionCopy->setText(QApplication::translate("Lyrics", "Copy to clipboard", nullptr));
m_actionSave->setText(QApplication::translate("Lyrics", "Save to file...", nullptr));
m_actionPrint->setText(QApplication::translate("Lyrics", "Print...", nullptr));
}
void Lyrics::showEvent(QShowEvent *event)
{
QMainWindow::showEvent(event);
std::call_once(m_firstTime, &Lyrics::readSettings, this);
}
void Lyrics::closeEvent(QCloseEvent *event)
{
writeSettings();
emit closed();
event->accept();
}
void Lyrics::populateCodecsCombo()
{
QMap<QString,int> aux;
foreach(const auto mib, QTextCodec::availableMibs()) {
QTextCodec *c = QTextCodec::codecForMib(mib);
if (c != nullptr && mib != 0) {
aux.insert(c->name(), mib);
}
}
QByteArrayList umibkeys = Sequence::getExtraCodecNames();
foreach(const auto k, umibkeys) {
auto mib = Sequence::getMibForName(k);
if (!aux.contains(k) || aux.key(mib).isEmpty())
{
QTextCodec *c = QTextCodec::codecForMib(mib);
if (c != nullptr)
{
aux.insert(k, mib);
//qDebug() << "adding extra codec:" << k << mib;
}
}
}
QStringList keys = aux.keys();
keys.sort();
keys.removeDuplicates();
m_comboCodec->clear();
m_comboCodec->addItem(tr("Default (latin1)"), 0);
foreach(const auto k, keys) {
m_comboCodec->addItem(k, aux[k]);
}
//qDebug() << Q_FUNC_INFO << m_comboCodec->count();
}
void Lyrics::populateTracksCombo()
{
m_comboTrack->clear();
m_comboTrack->addItem(tr("All Tracks"));
if (m_song != nullptr) {
for(int track = 1; track <= m_song->getNumTracks(); ++track) {
QString name = sanitizeText( m_song->trackName(track) );
if (name.isEmpty()) {
m_comboTrack->addItem(tr("Track %1").arg(track));
} else {
m_comboTrack->addItem(QString::number(track) + ": " + name);
}
}
}
}
void Lyrics::displayText()
{
m_textViewer->clear();
m_textViewer->setTextColor(m_normalColor);
m_textPos.clear();
if (m_song != nullptr) {
QList<QPair<int,QByteArray>> textList = m_song->getRawText(m_track, static_cast<Sequence::TextType>(m_type));
foreach(const auto& p, textList) {
auto cp = m_textViewer->textCursor().position();
auto s = sanitizeText(p.second);
if (!m_textPos.contains(p.first)) {
m_textPos.insert(p.first, cp);
}
m_textViewer->insertPlainText(s);
}
m_textViewer->moveCursor(QTextCursor::Start);
}
}
void Lyrics::toggleFullScreen(bool /*enabled*/)
{
if (isFullScreen()) {
showNormal();
} else {
showFullScreen();
}
}
void Lyrics::slotCopy()
{
QClipboard *clipboard = QGuiApplication::clipboard();
clipboard->setText(m_textViewer->toPlainText());
}
void Lyrics::slotSave()
{
QFileInfo info(m_song->currentFile());
QFileDialog dialog(this);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setDefaultSuffix("txt");
dialog.setNameFilters({"Text files (*.txt)","Any files (*)"});
dialog.selectFile(info.baseName() + ".txt");
if (dialog.exec() == QDialog::Accepted) {
QStringList fileNames = dialog.selectedFiles();
QSaveFile file(fileNames.first());
if (file.open(QFile::WriteOnly | QFile::Text)) {
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
QTextStream out(&file);
out.setCodec(m_codec);
out << m_textViewer->toPlainText();
#else
QByteArray encoded = m_codec->fromUnicode(m_textViewer->toPlainText());
file.write(encoded);
#endif
}
file.commit();
}
}
void Lyrics::slotPrint()
{
QPrinter printer(QPrinter::HighResolution);
#if defined(Q_OS_LINUX)
QFileInfo info(m_song->currentFile());
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(info.baseName() + ".pdf");
#endif
QPrintDialog printDialog(&printer, this);
if (printDialog.exec() == QDialog::Accepted) {
QTextDocument doc(m_textViewer->toPlainText());
doc.print(&printer);
}
}
void Lyrics::initSong( Sequence *song )
{
//qDebug() << Q_FUNC_INFO;
m_song = song;
if (m_song != nullptr) {
populateTracksCombo();
m_mib = m_song->currentMIB();
m_track = m_song->trackMaxPoints();
m_type = m_song->typeMaxPoints();
}
m_textViewer->clear();
// selected Codec:
int idx = m_comboCodec->findData(m_mib, Qt::UserRole, Qt::MatchExactly|Qt::MatchWrap);
m_comboCodec->setCurrentIndex(idx);
m_codec = QTextCodec::codecForMib(m_mib);
if (idx < 0 && m_codec != nullptr) {
idx = m_comboCodec->findText(m_codec->name(), Qt::MatchFixedString|Qt::MatchWrap);
m_comboCodec->setCurrentIndex(idx);
}
//qDebug() << Q_FUNC_INFO << "mib:" << m_mib << "combo index:" << idx << "codec:" << (m_codec == nullptr ? "null" : m_codec->name());
m_comboTrack->setCurrentIndex(m_track);
m_comboType->setCurrentIndex(m_type);
// populate text browser:
displayText();
}
void Lyrics::applySettings()
{
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
m_chmenu->setPalette(qApp->palette());
#endif
m_toolButton->setIcon(IconUtils::GetIcon("application-menu"));
m_textViewer->setFont(Settings::instance()->lyricsFont());
m_normalColor = Settings::instance()->getFutureColor();
m_otherColor = Settings::instance()->getPastColor();
m_highlightColor = Settings::instance()->highlightColor();
QPalette p;
p.setColor(QPalette::Highlight, m_highlightColor);
m_textViewer->setPalette(p);
if (!m_textViewer->document()->isEmpty()) {
displayText();
}
foreach(QComboBox *cb, findChildren<QComboBox*>()) {
cb->setPalette(qApp->palette());
foreach(QWidget *w, cb->findChildren<QWidget*>()) {
w->setPalette(qApp->palette());
}
}
FramelessWindow::applySettings();
}
QString Lyrics::sanitizeText(const QByteArray& data)
{
QString s;
if (m_codec == nullptr) {
s = QString::fromLatin1(data);
} else {
s = m_codec->toUnicode(data);
}
s.replace(QRegularExpression("@[IKLTVW]"), "\n");
s.replace(QRegularExpression("[/\\\\]+"), "\n");
s.replace(QRegularExpression("[\r\n]+"), "\n");
s.replace('\0', QChar::Space);
return s;
}
void Lyrics::slotMidiText(const int track, const int type, int ticks, const QByteArray &data)
{
if ((m_track == 0 || m_track == track) &&
(m_type == 0 || m_type == type)) {
QString stext = sanitizeText(data).trimmed();
if (stext.isEmpty()) {
return;
}
if (m_textPos.contains(ticks)) {
//qDebug() << ticks << m_textPos[ticks] << stext;
QTextCursor cursor = m_textViewer->textCursor();
cursor.setPosition(m_textPos[ticks]);
m_textViewer->setTextCursor(cursor);
}
if (m_textViewer->find(stext)) {
m_textViewer->setTextColor(m_otherColor);
QRect r = m_textViewer->cursorRect();
QScrollBar *s = m_textViewer->verticalScrollBar();
int half = m_textViewer->viewport()->height() / 2;
int newpos = s->value() + r.top() - half;
if ((r.top() > half) && (newpos < s->maximum())) {
s->setValue(newpos);
}
} /*else {
qDebug() << Q_FUNC_INFO << "not found:" << stext;
}*/
}
}
void Lyrics::trackChanged(int index)
{
m_track = index;
//qDebug() << "track:" << m_track;
displayText();
}
void Lyrics::typeChanged(int index)
{
m_type = index;
//qDebug() << "type:" << m_type;
displayText();
}
void Lyrics::codecChanged(int index)
{
m_mib = m_comboCodec->itemData(index).toInt();
//qDebug() << "mib:" << m_mib;
m_codec = QTextCodec::codecForMib(m_mib);
m_song->setCodec(m_codec);
displayText();
}
void Lyrics::changeFont()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, m_textViewer->font(), this);
if (ok) {
m_textViewer->setFont(font);
Settings::instance()->setLyricsFont(font);
displayText();
}
}