-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbcPlainTextEdit.h
136 lines (111 loc) · 3.51 KB
/
AbcPlainTextEdit.h
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
// This source code is part of QAbc, a minimal ABC music notation editor.
// QAbc is Copyright © 2021 Benoît Rouits <[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.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef ABCPLAINTEXTEDIT_H
#define ABCPLAINTEXTEDIT_H
#include <QPlainTextEdit>
#include <QSyntaxHighlighter>
#include <QRegularExpression>
#include <QCompleter>
class AbcHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
AbcHighlighter(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text) override;
private:
struct AbcHighlightingRule
{
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<AbcHighlightingRule> highlightingRules;
#if 0
QRegularExpression headerStartExpression;
QRegularExpression headerEndExpression;
#endif
QTextCharFormat headerFormat;
QTextCharFormat extraInstructionFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat noteFormat;
QTextCharFormat decorFormat;
QTextCharFormat gchordFormat;
QTextCharFormat barFormat;
QTextCharFormat lyricFormat;
#if 0
QTextCharFormat multiLineHeaderFormat;
#endif
};
class AbcPlainTextEdit: public QPlainTextEdit
{
Q_OBJECT
public:
AbcPlainTextEdit(QWidget* parent = nullptr);
~AbcPlainTextEdit();
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
void setCompleter(QCompleter *c);
QCompleter *completer() const;
bool isSaved();
void setSaved();
void findX(int x);
protected:
void resizeEvent(QResizeEvent *event) override;
void keyPressEvent(QKeyEvent *e) override;
void focusInEvent(QFocusEvent *e) override;
void flagModified(bool enable);
virtual void contextMenuEvent(QContextMenuEvent *e) override;
virtual void mouseDoubleClickEvent(QMouseEvent *e) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void checkDictionnary();
void updateLineNumberArea(const QRect &rect, int dy);
void insertCompletion(const QString &completion);
void onFindActivated();
void onFindForwardActivated();
void onFindBackwardActivated();
private:
QAbstractItemModel *modelFromFile(const QString &fileName);
QAbstractItemModel *dictModel; /* normal dictionnary */
QAbstractItemModel *psModel; /* Rendering dictionnary */
QAbstractItemModel *gmModel; /* General MIDI dictionary */
static const QString delimiter;
QWidget *lineNumberArea;
AbcHighlighter *highlighter;
QString textUnderCursor() const;
QString lineUnderCursor() const;
QString charBeforeCursor(QTextCursor tc) const;
QString wordBeforeCursor(QTextCursor tc) const;
QString m_find;
QAction* findaction;
QAction* findnextaction;
QAction* findprevaction;
QCompleter *c = nullptr;
bool saved;
};
class LineNumberArea : public QWidget
{
public:
LineNumberArea(AbcPlainTextEdit *editor) : QWidget(editor), abcplaintextedit(editor)
{}
QSize sizeHint() const override
{
return QSize(abcplaintextedit->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) override
{
abcplaintextedit->lineNumberAreaPaintEvent(event);
}
private:
AbcPlainTextEdit *abcplaintextedit;
};
#endif