-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMelodyEditorTextArea.cpp
More file actions
304 lines (238 loc) · 8.1 KB
/
MelodyEditorTextArea.cpp
File metadata and controls
304 lines (238 loc) · 8.1 KB
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
/**
* MelodyEditorTextArea.cpp
*
* Copyright (c) 2026 Bimal Poudel <anytizer@users.noreply.github.com>
* Copyright (c) 2026 Alex <allejok96@users.noreply.github.com>
*/
#include <QMimeData>
#include <QTextStream>
#include <QString>
#include <QFont>
#include <QFileInfo>
#include <QFileDialog>
#include <QTextCursor>
#include <QPlainTextEdit>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QEvent>
#include <QPainter>
#include <QTextBlock>
#include <QDebug>
#include "MelodyEditorTextArea.h"
#include "src/includes/Utilities.h"
using namespace lmms::melodyeditor;
namespace lmms::gui::melodyeditor
{
MelodyEditorTextArea::MelodyEditorTextArea()
{
lineNumberArea = new LineNumberArea(this);
connect(this, &MelodyEditorTextArea::blockCountChanged, this, &MelodyEditorTextArea::updateLineNumberAreaWidth);
connect(this, &MelodyEditorTextArea::updateRequest, this, &MelodyEditorTextArea::updateLineNumberArea);
connect(this, &MelodyEditorTextArea::cursorPositionChanged, this, &MelodyEditorTextArea::highlightCurrentLine);
updateLineNumberAreaWidth(0);
highlightCurrentLine();
this->setAcceptDrops(true);
// Try to be OS neutral.
// @todo embed a font for uniform experience across all platforms.
QFont font("Consolas", 14); // Consolas | sans-serif @ 14 points
this->setFont(font);
this->setStyleSheet(
"font-family: Consolas, 'Courier New', Menlo, 'Roboto Mono', 'DejaVu Sans Mono', monospace;"
//"selection-background-color: #c02b2b;"
"selection-color: #d908f0;"
);
//this->setPlainText("# Double-Click to load a file.");
this->setPlaceholderText(
"# 1. Type or paste melody notations here.\n"
"# 2. Double-Click to load a file.\n"
"# 3. Press Ctrl + / to toggle comments.\n"
);
// Disable right click menus.
// But still, keep the shortcuts enabled eg. ctrl+C, ctrl+v
// this->setContextMenuPolicy(Qt::NoContextMenu);
// Enforce
this->setLineWrapMode(QPlainTextEdit::NoWrap);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
this->setCursor(Qt::IBeamCursor);
this->ensureCursorVisible();
}
MelodyEditorTextArea::~MelodyEditorTextArea()
{
}
void MelodyEditorTextArea::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
emit doubleClicked();
event->accept();
} else {
QPlainTextEdit::mouseDoubleClickEvent(event);
}
}
/**
* Custom zoom logic on ctrl+wheel
*/
void MelodyEditorTextArea::wheelEvent(QWheelEvent *event)
{
if (event->modifiers() & Qt::ControlModifier)
{
QFont font = this->font();
int pointsize = font.pointSize() + (event->angleDelta().y() / ZOOM_FACTOR);
font.setPointSize(std::clamp(pointsize, MIN_FONTSIZE, MAX_FONTSIZE));
this->setFont(font);
}
// @see https://doc.qt.io/qt-6/qml-qtquick-controls-scrollview.html
event->accept();
QPlainTextEdit::wheelEvent(event); // make scrollable with wheel
}
void MelodyEditorTextArea::dragEnterEvent(QDragEnterEvent *event)
{
if (!pathFromMimeData(event->mimeData()).isEmpty())
{
event->acceptProposedAction();
}
}
void MelodyEditorTextArea::dropEvent(QDropEvent *event)
{
auto path = pathFromMimeData(event->mimeData());
if (!path.isEmpty())
{
emit fileDropped(path);
event->acceptProposedAction();
}
}
int MelodyEditorTextArea::lineNumberAreaWidth() {
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
digits++;
}
// minimum size: 2 digits
digits = qMax(2, digits);
int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
return space;
}
void MelodyEditorTextArea::updateLineNumberAreaWidth(int newBlockCount) {
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void MelodyEditorTextArea::updateLineNumberArea(const QRect &rect, int dy) {
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
void MelodyEditorTextArea::resizeEvent(QResizeEvent *e) {
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void MelodyEditorTextArea::lineNumberAreaPaintEvent(QPaintEvent *event) {
QPainter painter(lineNumberArea);
//painter.fillRect(event->rect(), Qt::lightGray);
painter.fillRect(event->rect(), QColor("#000000"));
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
int bottom = top + qRound(blockBoundingRect(block).height());
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(QColor("#fafa06"));
painter.drawText(0, top, lineNumberArea->width() - 2, fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + qRound(blockBoundingRect(block).height());
++blockNumber;
}
}
void MelodyEditorTextArea::highlightCurrentLine() {
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
//QColor lineColor = QColor(Qt::yellow).lighter(160);
QColor lineColor = QColor("#8e8810");
lineColor.setAlpha(40);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void MelodyEditorTextArea::keyPressEvent(QKeyEvent *e) {
QString pressed = e->text();
int key = e->key();
QString completion = "";
if(pressed == "[") completion = "[]";
if(pressed == "{") completion = "{}";
if(pressed == "(") completion = "()";
// Single quotes are part of notations itself, as in ALDA
// Double quotes are often chords in ABC
if(pressed == "\"") completion = "\"\"";
if(completion!="")
{
this->insertPlainText(completion);
QTextCursor c = this->textCursor();
c.movePosition(QTextCursor::Left);
this->setTextCursor(c);
return;
}
if (e->modifiers() == Qt::ControlModifier && key == Qt::Key_Slash) {
toggleComments();
return;
}
// Others to implement
// F9: Play at the cursor line
// CTRL+UP: Play
// CTRL+DOWN: Pause
// CTRL+LEFT: jump backwards
// CTRL+RIGHT: Jump forward
QPlainTextEdit::keyPressEvent(e);
}
/**
* Press Ctrl+/ to toggle comments
*/
void MelodyEditorTextArea::toggleComments() {
QTextCursor cursor = textCursor();
// 1. Capture the selection range
int start = cursor.selectionStart();
int end = cursor.selectionEnd();
// 2. Identify the start and end block numbers
QTextBlock startBlock = document()->findBlock(start);
QTextBlock endBlock = document()->findBlock(end);
// If the cursor is at the very beginning of a new line at the end of a selection,
// skip that last empty line (common behavior in IDEs)
if (end > start && end == endBlock.position()) {
endBlock = endBlock.previous();
}
int startIdx = startBlock.blockNumber();
int endIdx = endBlock.blockNumber();
// 3. Start Undo Group
cursor.beginEditBlock();
for (int i = startIdx; i <= endIdx; ++i) {
QTextBlock block = document()->findBlockByNumber(i);
if (!block.isValid()) continue;
QString text = block.text();
QTextCursor lineCursor(block);
// Check if line is already commented (ignoring leading whitespace)
int commentPos = text.indexOf("#");
bool isCommented = (commentPos != -1 && text.left(commentPos).trimmed().isEmpty());
if (isCommented) {
// Remove "#"
lineCursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, commentPos);
lineCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1);
lineCursor.removeSelectedText();
} else {
// Insert "#" at the start of the block
lineCursor.movePosition(QTextCursor::StartOfBlock);
lineCursor.insertText("#");
}
}
cursor.endEditBlock();
}
} // lmms::gui