-
Notifications
You must be signed in to change notification settings - Fork 29
/
highlight.cpp
197 lines (174 loc) · 6.89 KB
/
highlight.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
/***************************************************************************
//
// softProjector - an open source media projection software
// Copyright (C) 2017 Vladislav Kobzar
//
// 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 version 3 of the License.
//
// 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 "highlight.hpp"
Highlight::Highlight(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
QStringList patterns;
// Verse formating
verseFormat.setForeground(Qt::red);
verseFormat.setBackground(Qt::yellow);
patterns << "^Verse[^\n]*" << "^&Verse[^\n]*"
<< QString::fromUtf8("^Куплет[^\n]*") << QString::fromUtf8("^&Куплет[^\n]*")
<< QString::fromUtf8("^Strophe[^\n]*") << QString::fromUtf8("^&Strophe[^\n]*")
<< QString::fromUtf8("^Verš[^\n]*") << QString::fromUtf8("^&Verš[^\n]*");
foreach (const QString &pattern, patterns)
{
rule.pattern = QRegExp(pattern);
rule.format = verseFormat;
highlightingRules.append(rule);
}
patterns.clear();
// Chorus formating
chorusFormat.setFontItalic(true);
chorusFormat.setForeground(Qt::darkBlue);
chorusFormat.setBackground(QColor(212,240,28,255));
patterns << "^Chorus[^\n]*" << "^&Chorus[^\n]*"
<< QString::fromUtf8("^Sbor[^\n]*") << QString::fromUtf8("^&Sbor[^\n]*")
<< "^Refrain[^\n]*" << "^&Refrain[^\n]*"
<< QString::fromUtf8("^Припев[^\n]*") << QString::fromUtf8("^&Припев[^\n]*")
<< QString::fromUtf8("^Приспів[^\n]*") << QString::fromUtf8("^&Приспів[^\n]*")
<< QString::fromUtf8("^Refrén[^\n]*") << QString::fromUtf8("^&Refrén[^\n]*");
foreach (const QString &pattern, patterns)
{
rule.pattern = QRegExp(pattern);
rule.format = chorusFormat;
highlightingRules.append(rule);
}
patterns.clear();
// Vsavka formating
vstavkaFormat.setForeground(Qt::darkMagenta);
vstavkaFormat.setBackground(QColor(255,140,0,255));
patterns << "^Slide[^\n]*" << "^Insert[^\n]*"
<< "^Intro[^\n]*" << "^Ending[^\n]*"
<< QString::fromUtf8("^Слайд[^\n]*") << QString::fromUtf8("^Вставка[^\n]*")
<< QString::fromUtf8("^Вступление[^\n]*") << QString::fromUtf8("^Окончание[^\n]*")
<< QString::fromUtf8("^Закінчення[^\n]*")
<< QString::fromUtf8("^Dia[^\n]*") << QString::fromUtf8("^Einfügung[^\n]*")
<< QString::fromUtf8("^Einleitung[^\n]*") << QString::fromUtf8("^Ende[^\n]*")
<< QString::fromUtf8("^Snímek[^\n]*") << QString::fromUtf8("^Vložka[^\n]*")
<< QString::fromUtf8("^Úvod[^\n]*") << QString::fromUtf8("^Závěr[^\n]*");
foreach (const QString &pattern, patterns)
{
rule.pattern = QRegExp(pattern);
rule.format = vstavkaFormat;
highlightingRules.append(rule);
}
}
void Highlight::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules)
{
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0)
{
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}
// *** Announcement Editor Highlighter
HighlightAnnounce::HighlightAnnounce(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
QStringList patterns;
// Verse formating
announceFormat.setForeground(Qt::red);
announceFormat.setBackground(Qt::yellow);
patterns << "^Announce[^\n]*" << "^Slide[^\n]*"
<< QString::fromUtf8("^Объявление[^\n]*") << QString::fromUtf8("^Слайд[^\n]*")
<< QString::fromUtf8("^Оголошення[^\n]*")
<< QString::fromUtf8("^Ankündigung[^\n]*") << QString::fromUtf8("^Dia[^\n]*")
<< QString::fromUtf8("^Oznámení[^\n]*") << QString::fromUtf8("^Snímek[^\n]*");
foreach (const QString &pattern, patterns)
{
rule.pattern = QRegExp(pattern);
rule.format = announceFormat;
highlightingRules.append(rule);
}
}
void HighlightAnnounce::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules)
{
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0)
{
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}
// *** Highligting for search results ***
HighlightSearch::HighlightSearch(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
}
void HighlightSearch::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules)
{
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0)
{
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}
void HighlightSearch::setHighlightText(QString text)
{
HighlightingRule rule;
highlightingRules.clear();
resultFormat.setForeground(Qt::red);
rule.pattern = QRegExp(text,Qt::CaseInsensitive);
rule.format = resultFormat;
highlightingRules.append(rule);
}
/**********************************************/
/**** Class for higlighting search results ****/
/**********************************************/
HighlighterDelegate::HighlighterDelegate(QObject *parent)
: QItemDelegate(parent)
{
textDocument = new QTextDocument(this);
highlighter = new HighlightSearch(textDocument);
}
void HighlighterDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
{
Q_UNUSED(option);
textDocument->setDocumentMargin(0);
textDocument->setPlainText(text);
QPixmap pixmap(rect.size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
textDocument->drawContents(&p);
painter->drawPixmap(rect, pixmap);
}