/
qscintillawidget.cpp
768 lines (542 loc) · 22.6 KB
/
qscintillawidget.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR 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.
OpenCOR 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/>.
*******************************************************************************/
//==============================================================================
// QScintilla widget
//==============================================================================
#include "coreguiutils.h"
#include "filemanager.h"
#include "qscintillawidget.h"
//==============================================================================
#include <QDragEnterEvent>
#include <QLabel>
#include <QMenu>
#include <QMimeData>
//==============================================================================
#include "Qsci/qscilexer.h"
//==============================================================================
namespace OpenCOR {
namespace QScintillaSupport {
//==============================================================================
QScintillaWidget::QScintillaWidget(QsciLexer *pLexer, QWidget *pParent) :
QsciScintilla(pParent),
mCanSelectAll(false),
mInsertMode(true)
{
// Customise ourselves
setAutoIndent(true);
setBraceMatching(QsciScintilla::SloppyBraceMatch);
setCaretLineVisible(true);
setFolding(QsciScintilla::BoxedTreeFoldStyle);
setFrameShape(QFrame::NoFrame);
setIndentationsUseTabs(false);
setMarginLineNumbers(SC_MARGIN_NUMBER, true);
setMatchedBraceBackgroundColor(Qt::white);
setMatchedBraceForegroundColor(Qt::red);
setTabWidth(4);
setUtf8(true);
// Set our font
#if defined(Q_OS_WIN)
mFont = QFont("Lucida Console", 11);
#elif defined(Q_OS_LINUX)
mFont = QFont("Monospace", 9);
#elif defined(Q_OS_MAC)
mFont = QFont("Monaco", 12);
#else
#error Unsupported platform
#endif
setFont(mFont);
setMarginsFont(mFont);
// Use the given lexer
setLexer(pLexer);
// Force the use of UNIX EOL mode
// Note: by default QScintilla will use EolWindows on Windows and EolUnix on
// Linux and macOS. However, the fact that it uses EolWindows on
// Windows can cause problems on that platform (with files not using a
// Windows EOL mode), so to be safe we use EolUnix on all platforms...
setEolMode(EolUnix);
// Initialise our colours by 'updating' them
updateColors();
// Clear some key mappings inherited from QsciScintilla
// Note #1: indeed, QsciScintilla handles some shortcuts (e.g. Ctrl+L),
// which we don't want to see handled (e.g. Ctrl+L is used by
// QsciScintilla to delete the current line while we want OpenCOR
// to be able to use it to (un)lock the current file)...
// Note #2: even though we are clearing those key mappings, we must also
// bypass QsciScintilla's handling of event() (see below). Indeed,
// not to do so would mean that if, for example, the user was to
// press Ctrl+L, then nothing would happen while we would have
// expected the current file to be (un)locked...
SendScintilla(SCI_CLEARCMDKEY, (SCMOD_CTRL << 16)+'/');
SendScintilla(SCI_CLEARCMDKEY, (SCMOD_CTRL << 16)+'D');
SendScintilla(SCI_CLEARCMDKEY, (SCMOD_CTRL << 16)+'L');
SendScintilla(SCI_CLEARCMDKEY, (SCMOD_CTRL << 16)+(SCMOD_SHIFT << 16)+'L');
SendScintilla(SCI_CLEARCMDKEY, (SCMOD_CTRL << 16)+'R');
SendScintilla(SCI_CLEARCMDKEY, (SCMOD_CTRL << 16)+'T');
// Add support for Specials (Unicode block)
// Note #1: see issue #709 for more information...
// Note #2: make sure that specials() supports the below representations...
SendScintilla(SCI_SETREPRESENTATION, "\xef\xbf\xb9", "IAA");
SendScintilla(SCI_SETREPRESENTATION, "\xef\xbf\xba", "IAS");
SendScintilla(SCI_SETREPRESENTATION, "\xef\xbf\xbb", "IAT");
SendScintilla(SCI_SETREPRESENTATION, "\xef\xbf\xbc", "OBJ");
// Empty context menu by default
mContextMenu = new QMenu(this);
// Create our two labels to show our cursor position and editing mode
mCursorPositionWidget = new QLabel(this);
mEditingModeWidget = new QLabel(this);
// Keep track of the change to the UI
connect(this, SIGNAL(SCN_UPDATEUI(int)),
this, SLOT(updateUi()));
// Keep track of changes to our editor and resize the margin line numbers
// accordingly
connect(this, SIGNAL(textChanged()),
this, SLOT(updateMarginLineNumbersWidth()));
// Keep track of changes to our editor that may affect our ability to select
// all of its text
connect(this, SIGNAL(selectionChanged()),
this, SLOT(checkCanSelectAll()));
connect(this, SIGNAL(textChanged()),
this, SLOT(checkCanSelectAll()));
// Keep track of the change in the cursor position
connect(this, SIGNAL(cursorPositionChanged(int, int)),
this, SLOT(cursorPositionChanged(const int &, const int &)));
}
//==============================================================================
QMenu * QScintillaWidget::contextMenu() const
{
// Return our context menu
return mContextMenu;
}
//==============================================================================
void QScintillaWidget::setContextMenu(const QList<QAction *> &pContextMenuActions)
{
// Set our context menu
mContextMenu->clear();
foreach (QAction *action, pContextMenuActions)
mContextMenu->addAction(action);
}
//==============================================================================
void QScintillaWidget::cursorPosition(int &pLine, int &pColumn)
{
// Retrieve our cursor position
QsciScintilla::getCursorPosition(&pLine, &pColumn);
}
//==============================================================================
void QScintillaWidget::setCursorPosition(int pLine, int pColumn)
{
// Make sure that the line and column numbers make sense
pLine = qMin(qMax(0, pLine), lines()-1);
pColumn = qMin(qMax(0, pColumn), text(pLine).size()-1);
// Make sure that the line is not within a folded block
ensureLineVisible(pLine);
// Center ourselves around the given line
SendScintilla(SCI_LINESCROLL, 0, pLine-firstVisibleLine()-0.5*SendScintilla(SCI_LINESONSCREEN));
// Set our cursor position
QsciScintilla::setCursorPosition(pLine, pColumn);
}
//==============================================================================
void QScintillaWidget::setLexer(QsciLexer *pLexer)
{
// Set our font for the given lexer, if any
if (pLexer)
pLexer->setFont(mFont);
QsciScintilla::setLexer(pLexer);
}
//==============================================================================
int QScintillaWidget::currentPosition() const
{
// Return our current position
return SendScintilla(SCI_GETCURRENTPOS);
}
//==============================================================================
void QScintillaWidget::setCurrentPosition(const int &pCurrentPosition)
{
// Set our current position
SendScintilla(SCI_SETCURRENTPOS, pCurrentPosition);
}
//==============================================================================
QString QScintillaWidget::contents() const
{
// Return our contents
return text();
}
//==============================================================================
void QScintillaWidget::setContents(const QString &pContents,
const bool &pKeepHistory)
{
// Set our contents and keep our history, if required
if (pKeepHistory) {
bool readOnly = isReadOnly();
if (readOnly)
setReadOnly(false);
SendScintilla(SCI_SETTEXT, ScintillaBytesConstData(textAsBytes(pContents)));
setReadOnly(readOnly);
} else {
setText(pContents);
}
}
//==============================================================================
int QScintillaWidget::contentsSize() const
{
// Return the size of our contents
return SendScintilla(SCI_GETLENGTH);
}
//==============================================================================
QString QScintillaWidget::textInRange(const int &pStartRange,
const int &pEndRange) const
{
// Retrieve and return the text in the given range, after making sure that
// the given range makes sense
int maxRange = contentsSize();
if ( (pStartRange < 0) || (pStartRange >= maxRange)
|| (pEndRange < 0) || (pEndRange >= maxRange)
|| (pStartRange >= pEndRange)) {
return QString();
}
char *text = new char[pEndRange-pStartRange+1];
SendScintilla(SCI_GETTEXTRANGE, pStartRange, pEndRange, text);
QString res = QString(text);
delete[] text;
return res;
}
//==============================================================================
int QScintillaWidget::findTextInRange(const int &pStartRange,
const int &pEndRange,
const QString &pText,
const bool &pRegularExpression,
const bool &pCaseSensitive,
const bool &pWholeWordsOnly) const
{
// Keep track of the start and end of the current target
int crtTargetStart = SendScintilla(SCI_GETTARGETSTART);
int crtTargetEnd = SendScintilla(SCI_GETTARGETEND);
// Find and return the position, if any, of the given text within the given
// range
SendScintilla(SCI_SETSEARCHFLAGS,
(pRegularExpression?SCFIND_REGEXP:0)
+(pCaseSensitive?SCFIND_MATCHCASE:0)
+(pWholeWordsOnly?SCFIND_WHOLEWORD:0));
SendScintilla(SCI_SETTARGETSTART, pStartRange);
SendScintilla(SCI_SETTARGETEND, pEndRange);
QByteArray byteArray = pText.toUtf8();
int res = SendScintilla(SCI_SEARCHINTARGET, byteArray.length(), byteArray.constData());
// Retrieve the start and end of the current target
SendScintilla(SCI_SETTARGETSTART, crtTargetStart);
SendScintilla(SCI_SETTARGETEND, crtTargetEnd);
return res;
}
//==============================================================================
QString QScintillaWidget::wordAt(const int &pLine, const int &pColumn) const
{
// Return the current word, if any
return wordAtLineIndex(pLine, pColumn);
}
//==============================================================================
void QScintillaWidget::selectWordAt(const int &pLine, const int &pColumn)
{
// Return the current word, if any
int position = positionFromLineIndex(pLine, pColumn);
int startPosition = SendScintilla(SCI_WORDSTARTPOSITION, position, true);
int endPosition = SendScintilla(SCI_WORDENDPOSITION, position, true);
if (endPosition-startPosition > 0) {
setSelection(SendScintilla(SCI_LINEFROMPOSITION, startPosition),
SendScintilla(SCI_GETCOLUMN, startPosition),
SendScintilla(SCI_LINEFROMPOSITION, endPosition),
SendScintilla(SCI_GETCOLUMN, endPosition));
}
}
//==============================================================================
bool QScintillaWidget::isSelectAllAvailable() const
{
// Return whether we can select all the text
return mCanSelectAll;
}
//==============================================================================
void QScintillaWidget::del()
{
// Delete the selected text, if any
SendScintilla(SCI_CLEAR);
}
//==============================================================================
void QScintillaWidget::resetUndoHistory()
{
// Reset our undo history
SendScintilla(SCI_EMPTYUNDOBUFFER);
}
//==============================================================================
QLabel * QScintillaWidget::cursorPositionWidget() const
{
// Return our cursort position widget
return mCursorPositionWidget;
}
//==============================================================================
QLabel * QScintillaWidget::editingModeWidget() const
{
// Return our editing mode widget
return mEditingModeWidget;
}
//==============================================================================
QString QScintillaWidget::eolString() const
{
// Return the end of line we use
switch (eolMode()) {
case EolWindows:
return "\r\n";
case EolUnix:
return "\n";
case EolMac:
return "\r";
}
return QString();
// Note: we can't reach this point, but without it we may, at compilation
// time, be told that not all control paths return a value...
}
//==============================================================================
QColor QScintillaWidget::backgroundColor(const int &pStyle)
{
// Return the background color for the given style
return SendScintilla(SCI_STYLEGETBACK, pStyle);
}
//==============================================================================
void QScintillaWidget::setBackgroundColor(const int &pStyle,
const QColor &pBackgroundColor)
{
// Set the background color for the given style
SendScintilla(SCI_STYLESETBACK, pStyle, pBackgroundColor);
}
//==============================================================================
QColor QScintillaWidget::foregroundColor(const int &pStyle)
{
// Return the foreground color for the given style
return SendScintilla(SCI_STYLEGETFORE, pStyle);
}
//==============================================================================
void QScintillaWidget::setForegroundColor(const int &pStyle,
const QColor &pForegroundColor)
{
// Set the foreground color for the given style
SendScintilla(SCI_STYLESETFORE, pStyle, pForegroundColor);
}
//==============================================================================
int QScintillaWidget::zoomLevel() const
{
// Return our zoom level
return SendScintilla(SCI_GETZOOM);
}
//==============================================================================
QString QScintillaWidget::specials(const QString &pString)
{
// Check whether the given string is one of the Specials (Unicode block)
// Note: see the customised representation of the Specials (Unicode block)
// in the constructor...
if (!pString.compare("\xef\xbf\xb9"))
return "IAA";
else if (!pString.compare("\xef\xbf\xba"))
return "IAS";
else if (!pString.compare("\xef\xbf\xbb"))
return "IAT";
else if (!pString.compare("\xef\xbf\xbc"))
return "OBJ";
else
return pString;
}
//==============================================================================
void QScintillaWidget::changeEvent(QEvent *pEvent)
{
// Default handling of the event
QsciScintilla::changeEvent(pEvent);
// Check whether the palette has changed and if so then update our colors
if (pEvent->type() == QEvent::PaletteChange)
updateColors();
}
//==============================================================================
void QScintillaWidget::contextMenuEvent(QContextMenuEvent *pEvent)
{
// Show our context menu or QsciScintilla's one, if we don't have one
if (mContextMenu->isEmpty())
QsciScintilla::contextMenuEvent(pEvent);
else
mContextMenu->exec(pEvent->globalPos());
}
//==============================================================================
void QScintillaWidget::dragEnterEvent(QDragEnterEvent *pEvent)
{
// Accept the proposed action for the event, but only if we are not dropping
// URIs
// Note: this is not (currently?) needed on Windows and macOS, but if we
// don't have that check on Linux, then to drop some files on our
// Scintilla editor will result in the text/plain version of the data
// (e.g. file:///home/me/myFile) to be inserted in the text...
if (!pEvent->mimeData()->hasFormat(Core::FileSystemMimeType))
pEvent->acceptProposedAction();
else
pEvent->ignore();
}
//==============================================================================
bool QScintillaWidget::event(QEvent *pEvent)
{
// Bypass QsciScintilla's handling of event()
// Note: see the note on the clearing of some key mappings in the contructor
// above...
return QsciScintillaBase::event(pEvent);
}
//==============================================================================
void QScintillaWidget::focusInEvent(QFocusEvent *pEvent)
{
// We are getting the focus, so make sure that our caret is visible
setCaretWidth(1);
// Default handling of the event
QsciScintilla::focusInEvent(pEvent);
}
//==============================================================================
void QScintillaWidget::focusOutEvent(QFocusEvent *pEvent)
{
// We are losing the focus, so hide our caret
setCaretWidth(0);
// Default handling of the event
QsciScintilla::focusOutEvent(pEvent);
}
//==============================================================================
void QScintillaWidget::keyPressEvent(QKeyEvent *pEvent)
{
// Let people know that a key has been pressed
bool handled = false;
emit keyPressed(pEvent, handled);
// Carry on as normal, if the event wasn't handled
if (handled) {
pEvent->accept();
} else {
// Reset the font size, if needed
if ( !(pEvent->modifiers() & Qt::ShiftModifier)
&& (pEvent->modifiers() & Qt::ControlModifier)
&& !(pEvent->modifiers() & Qt::AltModifier)
&& !(pEvent->modifiers() & Qt::MetaModifier)) {
if (pEvent->key() == Qt::Key_0) {
zoomTo(0);
pEvent->accept();
} else if ( (pEvent->key() == Qt::Key_Plus)
|| (pEvent->key() == Qt::Key_Equal)) {
zoomIn();
pEvent->accept();
} else if (pEvent->key() == Qt::Key_Minus) {
zoomOut();
pEvent->accept();
} else {
// Default handling of the event
QsciScintilla::keyPressEvent(pEvent);
}
} else {
// Default handling of the event
QsciScintilla::keyPressEvent(pEvent);
}
}
}
//==============================================================================
void QScintillaWidget::wheelEvent(QWheelEvent *pEvent)
{
// Increasing/decrease the font size, if needed
if (pEvent->modifiers() == Qt::ControlModifier) {
int delta = pEvent->delta();
if (delta > 0)
zoomIn();
else if (delta < 0)
zoomOut();
pEvent->accept();
} else {
QsciScintilla::wheelEvent(pEvent);
}
}
//==============================================================================
void QScintillaWidget::zoomIn()
{
// Zoom in the default way and then update our margin line numbers width
QsciScintilla::zoomIn();
updateMarginLineNumbersWidth();
}
//==============================================================================
void QScintillaWidget::zoomOut()
{
// Zoom out the default way and then update our margin line numbers width
QsciScintilla::zoomOut();
updateMarginLineNumbersWidth();
}
//==============================================================================
void QScintillaWidget::zoomTo(int pSize)
{
// Zoom to the default way and then update our margin line numbers width
QsciScintilla::zoomTo(pSize);
updateMarginLineNumbersWidth();
}
//==============================================================================
void QScintillaWidget::updateUi()
{
// Update our editing mode, if needed
bool newInsertMode = !overwriteMode();
if ( (newInsertMode != mInsertMode)
|| mEditingModeWidget->text().isEmpty()) {
mInsertMode = newInsertMode;
mEditingModeWidget->setText(mInsertMode?"INS":"OVR");
}
}
//==============================================================================
void QScintillaWidget::updateMarginLineNumbersWidth()
{
// Resize the margin line numbers width
// Note: the +6 is to ensure that the margin line numbers width is indeed
// wide enough (there is clearly a 'problem' with the width computed
// by Scintilla)...
setMarginWidth(SC_MARGIN_NUMBER,
SendScintilla(SCI_TEXTWIDTH, STYLE_LINENUMBER,
ScintillaBytesConstData(textAsBytes(QString::number(lines()))))+6);
}
//==============================================================================
void QScintillaWidget::checkCanSelectAll()
{
// Check whether we can select all the text
bool newCanSelectAll = !text().isEmpty() && selectedText().compare(text());
if (newCanSelectAll != mCanSelectAll) {
mCanSelectAll = newCanSelectAll;
emit canSelectAll(mCanSelectAll);
}
}
//==============================================================================
void QScintillaWidget::updateColors()
{
// Compute and set the background colour of our caret line
static const qreal Threshold = 0.875;
QColor caretLineBackgroundColor = Core::highlightColor();
qreal red = caretLineBackgroundColor.redF();
qreal green = caretLineBackgroundColor.greenF();
qreal blue = caretLineBackgroundColor.blueF();
while ((red < Threshold) || (green < Threshold) || (blue < Threshold)) {
red = 0.5*(red+1.0);
green = 0.5*(green+1.0);
blue = 0.5*(blue+1.0);
}
setCaretLineBackgroundColor(qRgba(red*255, green*255, blue*255, caretLineBackgroundColor.alpha()));
}
//==============================================================================
void QScintillaWidget::cursorPositionChanged(const int &pLine,
const int &pColumn)
{
// Update our cursor position
mCursorPositionWidget->setText(QString("Line: %1, Col: %2").arg(QString::number(pLine+1),
QString::number(pColumn+1)));
}
//==============================================================================
} // namespace QScintillaSupport
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================