-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathLrdScrollEdit.cpp
657 lines (622 loc) · 22.8 KB
/
LrdScrollEdit.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
/******************************************************************************
** Copyright (C) 2015-2023 Laird Connectivity
**
** Project: UwTerminalX
**
** Module: LrdEdit.cpp
**
** Notes:
**
** License: 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.
**
** 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/
**
** SPDX-License-Identifier: GPL-3.0
**
*******************************************************************************/
/******************************************************************************/
// Include Files
/******************************************************************************/
#include "LrdScrollEdit.h"
/******************************************************************************/
// Local Functions or Private Members
/******************************************************************************/
LrdScrollEdit::LrdScrollEdit(QWidget *parent) : QPlainTextEdit(parent)
{
//Enable an event filter
installEventFilter(this);
installEventFilter(this->verticalScrollBar());
mchItems = 0; //Number of items is 0
mchPosition = 0; //Current position is 0
mbLineMode = true; //Line mode is on by default
mbSerialOpen = false; //Serial port is not open by default
mbLocalEcho = true; //Local echo mode on by default
mstrDatIn = ""; //Data in is an empty string
mstrDatOut = ""; //Data out is empty string
mintCurPos = 0; //Current cursor position is 0
mbContextMenuOpen = false; //Context menu not currently open
mstrItemArray = NULL;
nItemArraySize = 0;
mbSliderShown = false;
}
//=============================================================================
//=============================================================================
LrdScrollEdit::~LrdScrollEdit()
{
//Destructor
delete[] mstrItemArray;
nItemArraySize = 0;
}
//=============================================================================
//=============================================================================
bool
LrdScrollEdit::SetupScrollback(
quint16 nLines
)
{
//Sets up the scrollback array
mstrItemArray = new QString[nLines+1];
nItemArraySize = nLines;
return (mstrItemArray != NULL);
}
//=============================================================================
//=============================================================================
bool
LrdScrollEdit::eventFilter(
QObject *target,
QEvent *event
)
{
if (target == this->verticalScrollBar())
{
//Vertical scroll bar event
if (event->type() == QEvent::MouseButtonRelease)
{
//Button was released, update display buffer
this->UpdateDisplay();
}
else if (event->type() == QEvent::UpdateLater && mbSliderShown == false)
{
//Slider has been shown, scroll down to the bottom of the text edit if cursor position is at the end
if (this->textCursor().atEnd() == true)
{
this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
}
mbSliderShown = true;
}
else if (event->type() == QEvent::Hide)
{
//Slider has been hidden, clear flag
mbSliderShown = false;
}
}
else if (event->type() == QEvent::KeyPress)
{
//Key has been pressed...
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if ((keyEvent->modifiers() & Qt::ControlModifier) == Qt::ControlModifier)
{
//Check if this is a shortcut for cut
if (QKeySequence(keyEvent->key() | Qt::ControlModifier) == QKeySequence::Cut)
{
//We can either disallow cut or treat it as a copy - we will treat it as copy
QApplication::clipboard()->setText(this->textCursor().selection().toPlainText());
return true;
}
}
if (mbLineMode == true)
{
//Line mode
if (keyEvent->key() == Qt::Key_Up && !(keyEvent->modifiers() & Qt::ShiftModifier))
{
//Up pressed without holding shift
if (mchPosition > 0)
{
mchPosition = mchPosition-1;
}
mstrDatOut = mstrItemArray[mchPosition];
mintCurPos = mstrDatOut.length();
this->UpdateDisplay();
return true;
}
else if (keyEvent->key() == Qt::Key_Down && !(keyEvent->modifiers() & Qt::ShiftModifier))
{
//Down pressed without holding shift
if (mchPosition < mchItems)
{
mchPosition = mchPosition+1;
}
mstrDatOut = mstrItemArray[mchPosition];
mintCurPos = mstrDatOut.length();
this->UpdateDisplay();
return true;
}
else if ((keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) && !(keyEvent->modifiers() & Qt::ControlModifier) && (!(keyEvent->modifiers() & Qt::ShiftModifier) || mbLineSeparator == false))
{
//Enter pressed
if (mbSerialOpen == true)
{
if (mstrDatOut != mstrItemArray[mchItems])
{
//Previous entry is not the same as this entry
if (mchItems > (nItemArraySize-1))
{
//Shift out last array item
unsigned char i = 1;
while (i < nItemArraySize)
{
mstrItemArray[(i-1)] = mstrItemArray[i];
++i;
}
mchItems--;
}
//mstrItemArray[mchItems] = this->toPlainText();
mstrItemArray[mchItems] = mstrDatOut;
mchItems++;
mchPosition = mchItems;
}
//Send message to main window
emit EnterPressed();
}
this->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
mintCurPos = 0;
return true;
}
else if (keyEvent->key() == Qt::Key_Backspace)
{
if ((keyEvent->modifiers() & Qt::ControlModifier))
{
//Delete word
quint32 intSpacePos = mintCurPos;
while (intSpacePos > 0)
{
--intSpacePos;
if (mstrDatOut.at(intSpacePos) == ' ')
{
//Found a space
break;
}
}
//Previous word found, remove up to the previous word
mstrDatOut.remove(intSpacePos, mintCurPos-intSpacePos);
mintCurPos -= mintCurPos-intSpacePos;
}
else if (mintCurPos > 0)
{
//Delete character
mstrDatOut.remove(mintCurPos-1, 1);
--mintCurPos;
}
this->UpdateDisplay();
this->UpdateCursor();
return true;
}
else if (keyEvent->key() == Qt::Key_Left)
{
//Left key pressed
if (keyEvent->modifiers() & Qt::ControlModifier)
{
//Move to previous word
while (mintCurPos > 0)
{
--mintCurPos;
if (mstrDatOut.at(mintCurPos) == ' ' || mstrDatOut.at(mintCurPos) == '\r' || mstrDatOut.at(mintCurPos) == '\n')
{
//Found a space or newline character
break;
}
}
this->UpdateCursor();
}
else if (mintCurPos > 0)
{
//Move left
--mintCurPos;
this->UpdateCursor();
}
return true;
}
else if (keyEvent->key() == Qt::Key_Right)
{
//Right key pressed
if (keyEvent->modifiers() & Qt::ControlModifier)
{
//Move to next word
while (mintCurPos < mstrDatOut.length())
{
++mintCurPos;
if (mintCurPos < mstrDatOut.length() && (mstrDatOut.at(mintCurPos) == ' ' || mstrDatOut.at(mintCurPos) == '\r' || mstrDatOut.at(mintCurPos) == '\n'))
{
//Found a space or newline character
break;
}
}
this->UpdateCursor();
}
else if (mintCurPos < mstrDatOut.length())
{
//Move right
++mintCurPos;
this->UpdateCursor();
}
return true;
}
else if (keyEvent->key() == Qt::Key_Home)
{
//Home key pressed
if (!(keyEvent->modifiers() & Qt::ControlModifier))
{
//Move to beginning of line
mintCurPos = 0;
this->UpdateCursor();
}
return true;
}
else if (keyEvent->key() == Qt::Key_End)
{
//End key pressed
if (!(keyEvent->modifiers() & Qt::ControlModifier))
{
//Move to end of line
mintCurPos = mstrDatOut.length();
this->UpdateCursor();
}
return true;
}
else if (keyEvent->key() == Qt::Key_Delete)
{
//Delete key pressed
if ((keyEvent->modifiers() & Qt::ControlModifier))
{
//Delete word
qint32 intSpacePos = mintCurPos;
while (intSpacePos < mstrDatOut.length())
{
++intSpacePos;
if (mstrDatOut.at(intSpacePos) == ' ')
{
//Found a space
break;
}
}
if (intSpacePos == mstrDatOut.length())
{
//Next word not found, remove text from current position until end
mstrDatOut.remove(mintCurPos, mstrDatOut.length()-mintCurPos);
}
else
{
//Next word found, remove up to next word
mstrDatOut.remove(mintCurPos, intSpacePos-mintCurPos);
}
}
else if (mstrDatOut.length() > 0)
{
//Delete character
mstrDatOut.remove(mintCurPos, 1);
}
this->UpdateDisplay();
this->UpdateCursor();
return true;
}
else if (keyEvent->key() != Qt::Key_Escape && keyEvent->key() != Qt::Key_Backtab && keyEvent->key() != Qt::Key_Backspace && keyEvent->key() != Qt::Key_Insert && keyEvent->key() != Qt::Key_Pause && keyEvent->key() != Qt::Key_Print && keyEvent->key() != Qt::Key_SysReq && keyEvent->key() != Qt::Key_Clear && keyEvent->key() != Qt::Key_Home && keyEvent->key() != Qt::Key_End && keyEvent->key() != Qt::Key_Shift && keyEvent->key() != Qt::Key_Control && keyEvent->key() != Qt::Key_Meta && keyEvent->key() != Qt::Key_Alt && keyEvent->key() != Qt::Key_AltGr && keyEvent->key() != Qt::Key_CapsLock && keyEvent->key() != Qt::Key_NumLock && keyEvent->key() != Qt::Key_ScrollLock && !(keyEvent->modifiers() & Qt::ControlModifier))
{
//Move cursor to correct position to prevent inserting at wrong location if e.g. text is selected
this->UpdateCursor();
//Add character
mstrDatOut.insert(mintCurPos, keyEvent->text());
mintCurPos += keyEvent->text().length();
}
}
else
{
//Character mode
if (mbSerialOpen == true)
{
if (!(keyEvent->modifiers() & Qt::ControlModifier))
{
//Control key not held down
this->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
if (keyEvent->key() != Qt::Key_Escape && keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab && /*keyEvent->key() != Qt::Key_Backspace &&*/ keyEvent->key() != Qt::Key_Insert && keyEvent->key() != Qt::Key_Delete && keyEvent->key() != Qt::Key_Pause && keyEvent->key() != Qt::Key_Print && keyEvent->key() != Qt::Key_SysReq && keyEvent->key() != Qt::Key_Clear && keyEvent->key() != Qt::Key_Home && keyEvent->key() != Qt::Key_End && keyEvent->key() != Qt::Key_Shift && keyEvent->key() != Qt::Key_Control && keyEvent->key() != Qt::Key_Meta && keyEvent->key() != Qt::Key_Alt && keyEvent->key() != Qt::Key_AltGr && keyEvent->key() != Qt::Key_CapsLock && keyEvent->key() != Qt::Key_NumLock && keyEvent->key() != Qt::Key_ScrollLock)
{
//Not a special character
emit KeyPressed(keyEvent->key(), *keyEvent->text().unicode());
this->UpdateDisplay();
}
return true;
}
}
else
{
return true;
}
}
if (mbLocalEcho == false)
{
//Return true now if local echo is off
return true;
}
}
return QObject::eventFilter(target, event);
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::SetLineMode(
bool bNewLineMode
)
{
//Enables or disables line mode
mbLineMode = bNewLineMode;
this->UpdateDisplay();
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::AddDatInText(
QByteArray *baDat
)
{
//Adds data to the DatIn buffer
bool bIsEmpty = mstrDatIn.isEmpty();
mstrDatIn += QString(baDat->replace("\r\n", "\n").replace("\r", "\n"));
if (bIsEmpty == true && (baDat[0] == "\r" || baDat[0] == "\n"))
{
//Remove first newline
mstrDatIn.remove(0, 1);
}
this->UpdateDisplay();
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::AddDatOutText(
const QString strDat
)
{
//Adds data to the DatOut buffer
if (mbLineMode == true)
{
//Line mode
mstrDatOut += strDat;
mintCurPos += strDat.toUtf8().length();
this->UpdateDisplay();
}
else
{
//Character mode
QChar qcTmpQC;
foreach (qcTmpQC, strDat)
{
emit KeyPressed(0, qcTmpQC);
}
}
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::ClearDatIn(
)
{
//Clears the DatIn buffer
mstrDatIn.clear();
this->moveCursor(QTextCursor::End);
this->UpdateDisplay();
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::ClearDatOut(
)
{
//Clears the DatOut buffer
mstrDatOut.clear();
mintCurPos = 0;
this->UpdateDisplay();
}
//=============================================================================
//=============================================================================
QString *
LrdScrollEdit::GetDatOut(
)
{
//Returns the DatOut buffer
return &mstrDatOut;
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::insertFromMimeData(
const QMimeData *mdSrc
)
{
if (mdSrc->hasUrls() == true)
{
//A file has been dropped
QList<QUrl> urls = mdSrc->urls();
if (urls.isEmpty())
{
//No files
return;
}
else if (urls.length() > 1)
{
//More than 1 file - ignore
return;
}
if (urls.first().toLocalFile().isEmpty())
{
//Invalid filename
return;
}
//Send back to main application
emit FileDropped(urls.first().toLocalFile());
}
else if (mdSrc->hasText() == true)
{
//Text has been pasted
if (mbLineMode == true)
{
//Line mode
mstrDatOut.insert(mintCurPos, mdSrc->text());
mintCurPos += mdSrc->text().length();
this->UpdateDisplay();
}
else
{
//Character mode
QString strTmpStr = mdSrc->text();
QChar qcTmpQC;
foreach (qcTmpQC, strTmpStr)
{
emit KeyPressed(0, qcTmpQC);
}
}
}
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::UpdateDisplay(
)
{
//Updates the receive text buffer, faster
if (this->verticalScrollBar()->isSliderDown() != true && mbContextMenuOpen == false)
{
//Variables for text selection storage
unsigned int uiAnchor = 0;
unsigned int uiPosition = 0;
QTextCursor tcTmpCur;
bool bShiftStart = false;
bool bShiftEnd = false;
unsigned int uiCurrentSize = 0;
if (this->textCursor().anchor() != this->textCursor().position())
{
//Text is selected
uiAnchor = this->textCursor().anchor();
uiPosition = this->textCursor().position();
tcTmpCur = this->textCursor();
if (uiAnchor > mintPrevTextSize)
{
//Start of selected text is in the output buffer
bShiftStart = true;
uiCurrentSize = this->toPlainText().size();
}
if (uiPosition > mintPrevTextSize)
{
//End of selected text is in the output buffer
bShiftEnd = true;
uiCurrentSize = this->toPlainText().size();
}
}
//Slider not held down, update
unsigned int Pos;
if (this->verticalScrollBar()->sliderPosition() == this->verticalScrollBar()->maximum())
{
//Scroll to bottom
Pos = 65535;
}
else
{
//Stay here
Pos = this->verticalScrollBar()->sliderPosition();
}
this->setUpdatesEnabled(false);
this->setPlainText(QString(mstrDatIn).append((mbLocalEcho == true && mbLineMode == true ? mstrDatOut : "")));
this->setUpdatesEnabled(true);
//Update previous text size variable
mintPrevTextSize = mstrDatIn.size();
//Update the cursor position
this->UpdateCursor();
if (uiAnchor != 0 || uiPosition != 0)
{
//Reselect previous text
if (bShiftStart == true)
{
//Adjust start position
uiAnchor = uiAnchor + (this->toPlainText().size() - uiCurrentSize);
}
if (bShiftEnd == true)
{
//Adjust end position
uiPosition = uiPosition + (this->toPlainText().size() - uiCurrentSize);
}
tcTmpCur.setPosition(uiAnchor);
tcTmpCur.setPosition(uiPosition, QTextCursor::KeepAnchor);
this->setTextCursor(tcTmpCur);
}
//Go back to previous position
if (Pos == 65535)
{
//Bottom
this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
if (uiAnchor == 0 && uiPosition == 0)
{
this->moveCursor(QTextCursor::End);
}
}
else
{
//Maintain
this->verticalScrollBar()->setValue(Pos);
}
}
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::UpdateCursor(
)
{
//Updates the text control's cursor position
if (mbLocalEcho == true && mbLineMode == true)
{
//Local echo mode and line mode are enabled so move the cursor
QTextCursor tcTmpCur = this->textCursor();
if (mstrDatIn.length() > 0)
{
tcTmpCur.setPosition(mstrDatIn.length()+mintCurPos);
}
else
{
tcTmpCur.setPosition(mintCurPos);
}
this->setTextCursor(tcTmpCur);
}
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::SetSerialOpen(
bool SerialOpen
)
{
//Updates the serial open variable
mbSerialOpen = SerialOpen;
}
//=============================================================================
//=============================================================================
void
LrdScrollEdit::TrimDatIn(
qint32 intThreshold,
quint32 intSize
)
{
//Trim the display buffer
if (mstrDatIn.length() > intThreshold)
{
//Threshold exceeded, trim to desired size
mstrDatIn.remove(0, mstrDatIn.length() - intSize);
}
}
/******************************************************************************/
// END OF FILE
/******************************************************************************/