File: TransmitTextEdit.cpp

package info (click to toggle)
js8call 2.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,416 kB
  • sloc: cpp: 563,285; f90: 9,265; ansic: 937; python: 132; sh: 93; makefile: 6
file content (520 lines) | stat: -rw-r--r-- 14,968 bytes parent folder | download | duplicates (3)
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
#include "TransmitTextEdit.h"

#include "commons.h"
#include "varicode.h"

#include <iterator>
#include <QDebug>


void setTextEditFont(QTextEdit *edit, QFont font){
  // all uppercase
  font.setCapitalization(QFont::AllUppercase);

  edit->setFont(font);
  edit->setFontFamily(font.family());
  edit->setFontItalic(font.italic());
  edit->setFontPointSize(font.pointSize());
  edit->setFontUnderline(font.underline());
  edit->setFontWeight(font.weight());

  auto d = edit->document();
  d->setDefaultFont(font);
  edit->setDocument(d);

  auto c = edit->textCursor();
  c.select(QTextCursor::Document);
  auto cf = c.blockCharFormat();
  cf.setFont(font);
  cf.setFontCapitalization(QFont::AllUppercase);
  c.mergeBlockCharFormat(cf);

  edit->updateGeometry();
}

void setTextEditStyle(QTextEdit *edit, QColor fg, QColor bg, QFont font){
  edit->setStyleSheet(QString("QTextEdit { color:%1; background: %2; %3; }").arg(fg.name()).arg(bg.name()).arg(font_as_stylesheet(font)));

  //QTimer::singleShot(10, nullptr, [edit, fg, bg](){
        QPalette p = edit->palette();
        p.setColor(QPalette::Base, bg);
        p.setColor(QPalette::Active, QPalette::Base, bg);
        p.setColor(QPalette::Disabled, QPalette::Base, bg);
        p.setColor(QPalette::Inactive, QPalette::Base, bg);

        p.setColor(QPalette::Text, fg);
        p.setColor(QPalette::Active, QPalette::Text, fg);
        p.setColor(QPalette::Disabled, QPalette::Text, fg);
        p.setColor(QPalette::Inactive, QPalette::Text, fg);

        edit->setBackgroundRole(QPalette::Base);
        edit->setForegroundRole(QPalette::Text);
        edit->setPalette(p);

        edit->updateGeometry();
        edit->update();
    //});
}

void highlightBlock(QTextBlock block, QFont font, QColor foreground, QColor background){
  QTextCursor cursor(block);

  // Set background color
  QTextBlockFormat blockFormat = cursor.blockFormat();
  blockFormat.setBackground(background);
  cursor.setBlockFormat(blockFormat);

  // Set font
  /*
  for (QTextBlock::iterator it = cursor.block().begin(); !(it.atEnd()); ++it) {
    QTextCharFormat charFormat = it.fragment().charFormat();
    charFormat.setFont(font);
    charFormat.setFontCapitalization(QFont::AllUppercase);
    charFormat.setForeground(QBrush(foreground));

    QTextCursor tempCursor = cursor;
    tempCursor.setPosition(it.fragment().position());
    tempCursor.setPosition(it.fragment().position() + it.fragment().length(), QTextCursor::KeepAnchor);
    tempCursor.setCharFormat(charFormat);
  }
  */
  cursor.select(QTextCursor::BlockUnderCursor);

  auto charFormat = cursor.charFormat();
  charFormat.setFont(font);
  charFormat.setFontCapitalization(QFont::AllUppercase);
  charFormat.setForeground(QBrush(foreground));
  cursor.setCharFormat(charFormat);
}


TransmitTextEdit::TransmitTextEdit(QWidget *parent):
    QTextEdit(parent),
    m_sent { 0 },
    m_protected { false }
{
    connect(this, &QTextEdit::selectionChanged, this, &TransmitTextEdit::on_selectionChanged);
    connect(this, &QTextEdit::cursorPositionChanged, this, &TransmitTextEdit::on_selectionChanged);
    connect(this->document(), &QTextDocument::contentsChange, this, &TransmitTextEdit::on_textContentsChanged);
    installEventFilter(this);
}

void TransmitTextEdit::setCharsSent(int n){
    // never can send more than the document length
    n = qMin(n, document()->characterCount());

    // update sent display
    auto c = textCursor();
    c.movePosition(QTextCursor::Start);
    c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, n);

    // keep track of sent text
    m_textSent = c.selectedText().toUpper();
    m_sent = n;

    // highlight the sent text
    highlight();
}

// override
QString TransmitTextEdit::toPlainText() const {
    return QTextEdit::toPlainText().toUpper();
}

// override
void TransmitTextEdit::setPlainText(const QString &text){
    QTextEdit::setPlainText(text);
    m_textSent.clear();
    m_sent = 0;
}

//
void TransmitTextEdit::replaceUnsentText(const QString &text, bool keepCursor){
    auto rel = relativeTextCursorPosition(textCursor());
    auto c = textCursor();
    c.movePosition(QTextCursor::Start);
    c.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_sent);
    c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    c.removeSelectedText();
    c.insertText(text);

    // keep cursor
    if(keepCursor){
        c.movePosition(QTextCursor::End);
        c.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, rel.first);
        c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, rel.second - rel.first);
        setTextCursor(c);
    }
}

//
void TransmitTextEdit::replacePlainText(const QString &text, bool keepCursor){
    auto rel = relativeTextCursorPosition(textCursor());
    auto c = textCursor();
    c.movePosition(QTextCursor::Start);
    c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    c.removeSelectedText();
    c.insertText(text);

    // keep cursor
    if(keepCursor){
        c.movePosition(QTextCursor::End);
        c.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, rel.first);
        c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, rel.second - rel.first);
        setTextCursor(c);
    }
}

//
void TransmitTextEdit::setFont(QFont f){
    m_font = f;

    // then rehighlight
    highlight();
}

//
void TransmitTextEdit::setFont(QFont f, QColor fg, QColor bg){
    m_font = f;
    m_fg = fg;
    m_bg = bg;

    // then rehighlight
    highlight();
}

// override
void TransmitTextEdit::clear(){
    QTextEdit::clear();
    m_textSent.clear();
    m_sent = 0;
}

void TransmitTextEdit::setProtected(bool protect){
    m_protected = protect;
}

bool TransmitTextEdit::cursorShouldBeProtected(QTextCursor c){
    int start = c.selectionStart();
    int end = c.selectionEnd();
    if(end < start){
        int x = end;
        end = start;
        start = x;
    }

    //qDebug() << "selection" << start << end << m_sent;

    if(m_sent && start <= m_sent){
        qDebug() << "cursor in protected zone" << start << "<=" << m_sent;
        return true;
    } else {
        return false;
    }
}

// slot
void TransmitTextEdit::on_selectionChanged(){
    auto c = textCursor();

    auto shouldProtect = cursorShouldBeProtected(c);

    if(shouldProtect){
        blockSignals(true);
        {
            int end = c.selectionEnd();
            c.movePosition(QTextCursor::Start);
            c.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_sent);
            c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, qMax(0, end-m_sent));
            setTextCursor(c);
        }
        blockSignals(false);
    }

    setProtected(shouldProtect);
}

// slot
void TransmitTextEdit::on_textContentsChanged(int /*pos*/, int rem, int add){
    if(rem == 0 && add == 0){
        return;
    }

    QString text = toPlainText();
    if(text == m_lastText){
        return;
    }

#if JS8_ALLOW_EXTENDED
    QString normalized = text;
#else
    QString normalized = text.normalized(QString::NormalizationForm_KD);
#endif

    QString result;
    std::copy_if(normalized.begin(), normalized.end(), std::back_inserter(result), [](QChar& c) {
#if JS8_ALLOW_UNICODE
        return (c == 10 || c == 0x1A || (c > 31 && c < 128)) || c.isPrint());
#elif JS8_ALLOW_EXTENDED
        return c.toLatin1() != 0 && (c == 10 || c == 0x1A || (c > 31 && c < 128) || Varicode::extendedChars().contains(c.toUpper()));
#else
        return c.toLatin1() != 0 && (c == 10 || c == 0x1A || (c > 31 && c < 128));
#endif
    });

    if(result != text){
        bool blocked = signalsBlocked();
        blockSignals(true);
        {
            replacePlainText(result, true);
            text = result;
        }
        blockSignals(blocked);
    }

    highlight();

    m_dirty = true;
    m_lastText = text;
}

void TransmitTextEdit::highlightBase(){
    auto d = document();
    if(!d){
        return;
    }

    auto block = d->firstBlock();
    while(block.isValid()){
        highlightBlock(block, m_font, m_fg, m_bg);
        block = block.next();
    }
}

void TransmitTextEdit::highlightCharsSent(){
    if(!m_sent){
        return;
    }

    auto d = document();
    if(!d){
        return;
    }

    // highlight sent text
    auto c = textCursor();
    if(c.isNull()){
        return;
    }
    c.movePosition(QTextCursor::Start);
    c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, m_sent);

    auto ch = c.charFormat();
    ch.setFontStrikeOut(true);
    c.mergeCharFormat(ch);
}

void TransmitTextEdit::highlight(){
    highlightBase();
    highlightCharsSent();
}

QTextCursor::MoveOperation deleteKeyEventToMoveOperation(QKeyEvent *e){
    QTextCursor::MoveOperation op = QTextCursor::NoMove;

#if 0
    if (e == QKeySequence::Delete) {
            op = QTextCursor::PreviousCharacter;
    }
    else
#endif
    if (e == QKeySequence::DeleteStartOfWord) {
            op = QTextCursor::StartOfWord;
    }
    else if (e == QKeySequence::DeleteEndOfWord) {
            op = QTextCursor::EndOfWord;
    }
    else if (e == QKeySequence::DeleteCompleteLine) {
            op = QTextCursor::StartOfLine;
    }
    else if (e == QKeySequence::DeleteEndOfLine) {
            op = QTextCursor::EndOfLine;
    }

    return op;
}

bool isDeleteKeyEvent(QKeyEvent *e){
    return deleteKeyEventToMoveOperation(e) != QTextCursor::NoMove;
}

QTextCursor::MoveOperation movementKeyEventToMoveOperation(QKeyEvent *e){
    QTextCursor::MoveOperation op = QTextCursor::NoMove;

    if (e == QKeySequence::Delete) {
            op = QTextCursor::PreviousCharacter;
    }
    else if (e == QKeySequence::DeleteStartOfWord) {
            op = QTextCursor::StartOfWord;
    }
    else if (e == QKeySequence::DeleteEndOfWord) {
            op = QTextCursor::EndOfWord;
    }
    else if (e == QKeySequence::DeleteCompleteLine) {
            op = QTextCursor::StartOfLine;
    }
    else if (e == QKeySequence::DeleteEndOfLine) {
            op = QTextCursor::EndOfLine;
    }
    else if (e == QKeySequence::MoveToNextChar) {
            op = QTextCursor::Right;
    }
    else if (e == QKeySequence::MoveToPreviousChar) {
            op = QTextCursor::Left;
    }
    else if (e == QKeySequence::SelectNextChar) {
            op = QTextCursor::Right;
    }
    else if (e == QKeySequence::SelectPreviousChar) {
            op = QTextCursor::Left;
    }
    else if (e == QKeySequence::SelectNextWord) {
            op = QTextCursor::WordRight;
    }
    else if (e == QKeySequence::SelectPreviousWord) {
            op = QTextCursor::WordLeft;
    }
    else if (e == QKeySequence::SelectStartOfLine) {
            op = QTextCursor::StartOfLine;
    }
    else if (e == QKeySequence::SelectEndOfLine) {
            op = QTextCursor::EndOfLine;
    }
    else if (e == QKeySequence::SelectStartOfBlock) {
            op = QTextCursor::StartOfBlock;
    }
    else if (e == QKeySequence::SelectEndOfBlock) {
            op = QTextCursor::EndOfBlock;
    }
    else if (e == QKeySequence::SelectStartOfDocument) {
            op = QTextCursor::Start;
    }
    else if (e == QKeySequence::SelectEndOfDocument) {
            op = QTextCursor::End;
    }
    else if (e == QKeySequence::SelectPreviousLine) {
            op = QTextCursor::Up;
    }
    else if (e == QKeySequence::SelectNextLine) {
            op = QTextCursor::Down;
    }
    else if (e == QKeySequence::MoveToNextWord) {
            op = QTextCursor::WordRight;
    }
    else if (e == QKeySequence::MoveToPreviousWord) {
            op = QTextCursor::WordLeft;
    }
    else if (e == QKeySequence::MoveToEndOfBlock) {
            op = QTextCursor::EndOfBlock;
    }
    else if (e == QKeySequence::MoveToStartOfBlock) {
            op = QTextCursor::StartOfBlock;
    }
    else if (e == QKeySequence::MoveToNextLine) {
            op = QTextCursor::Down;
    }
    else if (e == QKeySequence::MoveToPreviousLine) {
            op = QTextCursor::Up;
    }
    else if (e == QKeySequence::MoveToPreviousLine) {
            op = QTextCursor::Up;
    }
    else if (e == QKeySequence::MoveToStartOfLine) {
            op = QTextCursor::StartOfLine;
    }
    else if (e == QKeySequence::MoveToEndOfLine) {
            op = QTextCursor::EndOfLine;
    }
    else if (e == QKeySequence::MoveToStartOfDocument) {
            op = QTextCursor::Start;
    }
    else if (e == QKeySequence::MoveToEndOfDocument) {
            op = QTextCursor::End;
    }

    return op;
}

bool isMovementKeyEvent(QKeyEvent * k){
    return movementKeyEventToMoveOperation(k) != QTextCursor::NoMove;
}

bool TransmitTextEdit::eventFilter(QObject */*o*/, QEvent *e){
    if(e->type() != QEvent::KeyPress){
        return false;
    }

    // -1. don't filter the escape key, return key, or enter key here
    QKeyEvent *k = static_cast<QKeyEvent *>(e);
    if(k->key() == Qt::Key_Escape){
        return false;
    }

    if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter){
        return false;
    }

    auto c = textCursor();

    auto c2 = QTextCursor(c);

    auto op = movementKeyEventToMoveOperation(k);
    c2.movePosition(op);

    bool shouldBeProtected = cursorShouldBeProtected(c2);

    // -1. if this is a delete event (like Ctrl+Backspace) and it should be protected, force protection
    if(isDeleteKeyEvent(k)){
        // if we technically shouldn't be protected...check to see if the previous character is a space
        // if it is, let's try the move operation from before the space, since the space can cause
        // word coallescing in the qtextdocument making the operation not actually move the cursor
        if(!shouldBeProtected){
            auto c3 = QTextCursor(c);
            c3.movePosition(QTextCursor::PreviousCharacter);
            if(c.document()->characterAt(qMin(c3.selectionStart(), c3.selectionEnd())).isSpace()){
                c3.movePosition(op);
                shouldBeProtected = cursorShouldBeProtected(c3);
            }
        }

        if(shouldBeProtected){
            k->ignore();
            return true;
        }
    }

    // 0. only filter when in a protected range
    // 0b. but only if we're not moving/deleting _into_ the protected range :/
    if(!isProtected() && !shouldBeProtected){
        return false;
    }

    // 1. do not filter movement sequences
    if(isMovementKeyEvent(k)){
        return false;
    }

    // 2. if on the edge, do not filter if not a backspace
    int start = qMin(c.selectionStart(), c.selectionEnd());
    if(start == m_sent && k->key() != Qt::Key_Backspace){
        return false;
    }

    // 3. if on the edge, do not filter if a backspace and there is text selected
    int end = qMax(c.selectionStart(), c.selectionEnd());
    if(start == m_sent && start != end && k->key() == Qt::Key_Backspace){
        return false;
    }

    return true;
}