blob: 53d91d445d7f85778ef9818f051713f4d4934a36 (
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
|
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "arrayoptionlineedit.h"
namespace MesonProjectManager {
namespace Internal {
ArrayOptionLineEdit::ArrayOptionLineEdit(QWidget *parent)
: QPlainTextEdit(parent)
{
m_highLighter = new RegexHighlighter(this);
m_highLighter->setDocument(this->document());
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setLineWrapMode(QPlainTextEdit::NoWrap);
QFontMetrics metrics(this->font());
int lineHeight = metrics.lineSpacing();
this->setFixedHeight(lineHeight * 1.5);
}
QStringList ArrayOptionLineEdit::options()
{
return m_highLighter->options(this->toPlainText());
}
void ArrayOptionLineEdit::keyPressEvent(QKeyEvent *e)
{
if (e->key() != Qt::Key_Return)
return QPlainTextEdit::keyPressEvent(e);
e->accept();
}
RegexHighlighter::RegexHighlighter(QWidget *parent)
: QSyntaxHighlighter(parent)
{
m_format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
m_format.setUnderlineColor(QColor(180, 180, 180));
m_format.setBackground(QBrush(QColor(180, 180, 230, 80)));
}
QStringList RegexHighlighter::options(const QString &text)
{
QRegularExpressionMatchIterator i = m_regex.globalMatch(text);
QStringList op;
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
for (int j = 1; j <= match.lastCapturedIndex(); j++) {
auto str = match.captured(j);
if (!str.isEmpty())
op.push_back(str);
}
}
return op;
}
void RegexHighlighter::highlightBlock(const QString &text)
{
QRegularExpressionMatchIterator i = m_regex.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
for (int j = 1; j <= match.lastCapturedIndex(); j++) {
setFormat(match.capturedStart(j), match.capturedLength(j), m_format);
}
}
}
} // namespace Internal
} // namespace MesonProjectManager
|