FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlitebrowser/src/ExtendedScintilla.cpp at master · PeterDaveHello/sqlitebrowser · GitHub
PeterDaveHello
/
sqlitebrowser
Public
forked from
sqlitebrowser/sqlitebrowser
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlitebrowser
/
src
/
ExtendedScintilla.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
341 lines (276 loc) · 12.5 KB
Breadcrumbs
sqlitebrowser
/
src
/
ExtendedScintilla.cpp
Copy path
File metadata and controls
341 lines (276 loc) · 12.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
ExtendedScintilla.h
"
#
include
"
FindReplaceDialog.h
"
#
include
"
Settings.h
"
#
include
"
Qsci/qscilexer.h
"
#
include
"
Qsci/qsciprinter.h
"
#
ifdef
Q_OS_MACX
#
include
<
Qsci/qscicommandset.h
>
#
include
<
Qsci/qscicommand.h
>
#
endif
#
include
<
QFile
>
#
include
<
QDropEvent
>
#
include
<
QUrl
>
#
include
<
QMimeData
>
#
include
<
QShortcut
>
#
include
<
QAction
>
#
include
<
QMenu
>
#
include
<
QPalette
>
#
include
<
QPrintPreviewDialog
>
#
include
<
cmath
>
ExtendedScintilla::ExtendedScintilla
(QWidget* parent) :
QsciScintilla(parent),
showErrorIndicators(
true
),
findReplaceDialog(
new
FindReplaceDialog(
this
))
{
//
This class does not set any lexer, that must be done in the child classes.
//
Enable UTF8
setUtf8
(
true
);
//
Enable brace matching
setBraceMatching
(QsciScintilla::SloppyBraceMatch);
//
Enable auto indentation
setAutoIndent
(
true
);
//
Enable folding
setFolding
(QsciScintilla::BoxedTreeFoldStyle);
//
Create error indicator
errorIndicatorNumber =
indicatorDefine
(QsciScintilla::SquiggleIndicator);
setIndicatorForegroundColor
(Qt::red, errorIndicatorNumber);
//
Set a sensible scroll width, so the scroll bar is avoided in
//
most cases.
setScrollWidth
(
80
);
//
Scroll width is adjusted to ensure that all of the lines
//
currently displayed can be completely scrolled. This mode never
//
adjusts the scroll width to be narrower.
setScrollWidthTracking
(
true
);
//
Visual flags for when wrap lines is enabled
setWrapVisualFlags
(QsciScintilla::WrapFlagByBorder);
//
Connect signals
connect
(
this
, &ExtendedScintilla::linesChanged,
this
, &ExtendedScintilla::updateLineNumberAreaWidth);
connect
(
this
, &QsciScintillaBase::
SCN_ZOOM
,
this
, &ExtendedScintilla::updateLineNumberAreaWidth);
//
The shortcuts are constrained to the Widget context so they do not conflict with other SqlTextEdit widgets in the Main Window.
QShortcut* shortcutFindReplace =
new
QShortcut
(
QKeySequence
(
tr
(
"
Ctrl+H
"
)),
this
,
nullptr
,
nullptr
, Qt::WidgetShortcut);
connect
(shortcutFindReplace, &QShortcut::activated,
this
, &ExtendedScintilla::openFindReplaceDialog);
shortcutFind =
new
QShortcut
(
QKeySequence
(
tr
(
"
Ctrl+F
"
)),
this
,
nullptr
,
nullptr
, Qt::WidgetShortcut);
connect
(shortcutFind, &QShortcut::activated,
this
, &ExtendedScintilla::openFindDialog);
#
ifdef
Q_OS_MACX
//
Alt+Backspace on Mac is expected to delete one word to the left,
//
instead of undoing (default Scintilla binding).
QsciCommand * command =
standardCommands
()->
find
(QsciCommand::DeleteWordLeft);
command->
setKey
(Qt::AltModifier+Qt::Key_Backspace);
//
And Cmd+Backspace should delete from cursor to the beginning of line
command =
standardCommands
()->
find
(QsciCommand::DeleteLineLeft);
command->
setKey
(Qt::ControlModifier+Qt::Key_Backspace);
#
endif
QShortcut* shortcutPrint =
new
QShortcut
(
QKeySequence
(
tr
(
"
Ctrl+P
"
)),
this
,
nullptr
,
nullptr
, Qt::WidgetShortcut);
connect
(shortcutPrint, &QShortcut::activated,
this
, &ExtendedScintilla::openPrintDialog);
//
Prepare for adding the find/replace option to the QScintilla context menu
setContextMenuPolicy
(Qt::CustomContextMenu);
connect
(
this
, &ExtendedScintilla::customContextMenuRequested,
this
, &ExtendedScintilla::showContextMenu);
}
void
ExtendedScintilla::updateLineNumberAreaWidth
()
{
//
Calculate number of digits of the current number of lines
int
digits =
static_cast
<
int
>(
std::log10
(
lines
())) +
1
;
//
Calculate the width of this number if it was all zeros (this is because a 1 might require less space than a 0 and this could
//
cause some flickering depending on the font) and set the new margin width.
setMarginWidth
(
0
,
QString
(
"
0
"
).
repeated
(digits+
1
));
}
void
ExtendedScintilla::dropEvent
(QDropEvent* e)
{
QList<QUrl> urls = e->
mimeData
()->
urls
();
if
(urls.
isEmpty
())
return
QsciScintilla::dropEvent
(e);
QString file = urls.
first
().
toLocalFile
();
if
(!
QFile::exists
(file))
return
;
QFile
f
(file);
f.
open
(QIODevice::ReadOnly);
setText
(f.
readAll
());
f.
close
();
}
void
ExtendedScintilla::setupSyntaxHighlightingFormat
(QsciLexer* lexer,
const
std::string& settings_name,
int
style)
{
lexer->
setColor
(
QColor
(
Settings::getValue
(
"
syntaxhighlighter
"
, settings_name +
"
_colour
"
).
toString
()), style);
QFont
font
(
Settings::getValue
(
"
editor
"
,
"
font
"
).
toString
());
font.
setPointSize
(
Settings::getValue
(
"
editor
"
,
"
fontsize
"
).
toInt
());
font.
setBold
(
Settings::getValue
(
"
syntaxhighlighter
"
, settings_name +
"
_bold
"
).
toBool
());
font.
setItalic
(
Settings::getValue
(
"
syntaxhighlighter
"
, settings_name +
"
_italic
"
).
toBool
());
font.
setUnderline
(
Settings::getValue
(
"
syntaxhighlighter
"
, settings_name +
"
_underline
"
).
toBool
());
lexer->
setFont
(font, style);
}
void
ExtendedScintilla::setLexer
(QsciLexer *lexer)
{
QsciScintilla::setLexer
(lexer);
reloadCommonSettings
();
}
void
ExtendedScintilla::reloadCommonSettings
()
{
//
Set margins and default text colours according to settings. setLexer seems to reset these colours.
//
Use desktop default colors for margins when following desktop
//
style, or the colors matching the dark style-sheet, otherwise.
switch
(
Settings::getValue
(
"
General
"
,
"
appStyle
"
).
toInt
()) {
case
Settings::FollowDesktopStyle :
setMarginsBackgroundColor
(
QPalette
().
color
(QPalette::Active, QPalette::Window));
setMarginsForegroundColor
(
QPalette
().
color
(QPalette::Active, QPalette::WindowText));
break
;
case
Settings::DarkStyle :
setMarginsBackgroundColor
(
QColor
(
0x32
,
0x41
,
0x4B
));
setMarginsForegroundColor
(
QColor
(
0xEF
,
0xF0
,
0xF1
));
break
;
case
Settings::LightStyle :
setMarginsBackgroundColor
(
QColor
(
0xC9
,
0xCD
,
0xD0
));
setMarginsForegroundColor
(
QColor
(
0x00
,
0x00
,
0x00
));
break
;
}
//
Reuse color of current line background in fold margin.
QColor
currentLineColor
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
currentline_colour
"
).
toString
());
setFoldMarginColors
(currentLineColor, currentLineColor);
setPaper
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
background_colour
"
).
toString
());
setColor
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
foreground_colour
"
).
toString
());
setMatchedBraceBackgroundColor
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
highlight_colour
"
).
toString
());
setSelectionBackgroundColor
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
selected_bg_colour
"
).
toString
());
setSelectionForegroundColor
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
selected_fg_colour
"
).
toString
());
}
void
ExtendedScintilla::reloadKeywords
()
{
//
Set lexer again to reload the updated keywords list
setLexer
(
lexer
());
}
void
ExtendedScintilla::reloadSettings
()
{
reloadLexerSettings
(
lexer
());
reloadKeywords
();
}
void
ExtendedScintilla::reloadLexerSettings
(QsciLexer *lexer)
{
QColor
foreground
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
foreground_colour
"
).
toString
());
QColor
background
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
background_colour
"
).
toString
());
QFont
defaultfont
(
Settings::getValue
(
"
editor
"
,
"
font
"
).
toString
());
defaultfont.
setStyleHint
(QFont::TypeWriter);
defaultfont.
setPointSize
(
Settings::getValue
(
"
editor
"
,
"
fontsize
"
).
toInt
());
//
Set syntax highlighting settings
if
(lexer)
{
lexer->
setFont
(defaultfont);
lexer->
setDefaultPaper
(background);
lexer->
setDefaultColor
(foreground);
//
This sets the base colors for all the styles
lexer->
setPaper
(background);
lexer->
setColor
(foreground);
}
//
Set font
setFont
(defaultfont);
//
Show line numbers
setMarginsFont
(defaultfont);
setMarginLineNumbers
(
0
,
true
);
updateLineNumberAreaWidth
();
//
Highlight current line
setCaretLineVisible
(
true
);
setCaretLineBackgroundColor
(
QColor
(
Settings::getValue
(
"
syntaxhighlighter
"
,
"
currentline_colour
"
).
toString
()));
setCaretForegroundColor
(foreground);
//
Set tab settings
setTabWidth
(
Settings::getValue
(
"
editor
"
,
"
tabsize
"
).
toInt
());
setIndentationsUseTabs
(
Settings::getValue
(
"
editor
"
,
"
indentation_use_tabs
"
).
toBool
());
if
(lexer)
lexer->
refreshProperties
();
//
Check if error indicators are enabled and clear them if they just got disabled
showErrorIndicators =
Settings::getValue
(
"
editor
"
,
"
error_indicators
"
).
toBool
();
if
(!showErrorIndicators)
clearErrorIndicators
();
}
void
ExtendedScintilla::clearErrorIndicators
()
{
//
Clear any error indicators from position (0,0) to the last column of the last line
clearIndicatorRange
(
0
,
0
,
lines
(),
lineLength
(
lines
()), errorIndicatorNumber);
}
void
ExtendedScintilla::setErrorIndicator
(
int
fromRow,
int
fromIndex,
int
toRow,
int
toIndex)
{
//
Set error indicator for the specified range but only if they're enabled
if
(showErrorIndicators)
fillIndicatorRange
(fromRow, fromIndex, toRow, toIndex, errorIndicatorNumber);
}
void
ExtendedScintilla::setErrorIndicator
(
int
position)
{
//
Set error indicator for the position until end of line, but only if they're enabled
if
(showErrorIndicators) {
int
column =
static_cast
<
int
>(
SendScintilla
(QsciScintillaBase::
SCI_GETCOLUMN
, position));
int
line =
static_cast
<
int
>(
SendScintilla
(QsciScintillaBase::
SCI_LINEFROMPOSITION
, position));
fillIndicatorRange
(line, column, line+
1
,
0
, errorIndicatorNumber);
}
}
bool
ExtendedScintilla::findText
(QString text,
bool
regexp,
bool
caseSensitive,
bool
words,
bool
wrap,
bool
forward) {
//
For finding the previous occurrence, we need to skip the current
//
selection, otherwise we'd always found the same occurrence.
if
(!forward &&
hasSelectedText
()) {
int
lineFrom, indexFrom;
int
lineTo, indexTo;
getSelection
(&lineFrom, &indexFrom, &lineTo, &indexTo);
setCursorPosition
(lineFrom, indexFrom);
}
return
findFirst
(text, regexp, caseSensitive, words, wrap, forward,
/*
line
*/
-
1
,
/*
index
*/
-
1
,
/*
show
*/
true
,
/*
posix
*/
true
,
/*
cxx11
*/
true
);
}
void
ExtendedScintilla::setEnabledFindDialog
(
bool
value)
{
shortcutFind->
setEnabled
(value);
}
void
ExtendedScintilla::clearSelection
()
{
setSelection
(-
1
,-
1
,-
1
,-
1
);
}
void
ExtendedScintilla::openFindReplaceDialog
()
{
findReplaceDialog->
setExtendedScintilla
(
this
);
findReplaceDialog->
showFindReplaceDialog
(
true
);
}
void
ExtendedScintilla::openFindDialog
()
{
findReplaceDialog->
setExtendedScintilla
(
this
);
findReplaceDialog->
showFindReplaceDialog
(
false
);
}
void
ExtendedScintilla::showContextMenu
(
const
QPoint &pos)
{
//
This has to be created here, otherwise the set of enabled options would not update accordingly.
QMenu* editContextMenu =
createStandardContextMenu
();
editContextMenu->
addSeparator
();
if
(shortcutFind->
isEnabled
()) {
QAction* findAction =
new
QAction
(
QIcon
(
"
:/icons/find
"
),
tr
(
"
Find...
"
),
this
);
findAction->
setShortcut
(shortcutFind->
key
());
connect
(findAction, &QAction::triggered,
this
, &ExtendedScintilla::openFindDialog);
editContextMenu->
addAction
(findAction);
}
QAction* findReplaceAction =
new
QAction
(
QIcon
(
"
:/icons/text_replace
"
),
tr
(
"
Find and Replace...
"
),
this
);
findReplaceAction->
setShortcut
(
QKeySequence
(
tr
(
"
Ctrl+H
"
)));
connect
(findReplaceAction, &QAction::triggered,
this
, &ExtendedScintilla::openFindReplaceDialog);
QAction* printAction =
new
QAction
(
QIcon
(
"
:/icons/print
"
),
tr
(
"
Print...
"
),
this
);
printAction->
setShortcut
(
QKeySequence
(
tr
(
"
Ctrl+P
"
)));
connect
(printAction, &QAction::triggered,
this
, &ExtendedScintilla::openPrintDialog);
editContextMenu->
addAction
(findReplaceAction);
editContextMenu->
addSeparator
();
editContextMenu->
addAction
(printAction);
editContextMenu->
exec
(
mapToGlobal
(pos));
}
void
ExtendedScintilla::openPrintDialog
()
{
QsciPrinter printer;
QPrintPreviewDialog *dialog =
new
QPrintPreviewDialog
(&printer);
connect
(dialog, &QPrintPreviewDialog::paintRequested,
this
, [&](QPrinter *previewPrinter) {
QsciPrinter* sciPrinter =
static_cast
<QsciPrinter*>(previewPrinter);
sciPrinter->
printRange
(
this
);
});
dialog->
exec
();
delete
dialog;
}
void
ExtendedScintilla::setReadOnly
(
bool
ro)
{
QsciScintilla::setReadOnly
(ro);
//
Disable or enable caret blinking so it is obvious whether the text can be modified or not. Otherwise there isn't any other hint.
SendScintilla
(QsciScintillaBase::
SCI_SETCARETPERIOD
, ro ?
0
:
500
);
}
void
ExtendedScintilla::setText
(
const
QString& text)
{
//
Reset scroll width, so the scroll bar is readjusted to the new text.
//
Otherwise, it grows always bigger.
setScrollWidth
(
80
);
QsciScintilla::setText
(text);
}
Back
|
FazBrowse Home
|
New Git URL