File: TransmitTextEdit.h

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 (93 lines) | stat: -rw-r--r-- 2,243 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
#ifndef TRANSMITTEXTEDIT_H
#define TRANSMITTEXTEDIT_H

#include "qt_helpers.hpp"

#include <QTextEdit>
#include <QTextBlock>
#include <QTextCursor>
#include <QBrush>
#include <QColor>
#include <QFont>

void setTextEditFont(QTextEdit *edit, QFont font);
void setTextEditStyle(QTextEdit *edit, QColor fg, QColor bg, QFont font);
void highlightBlock(QTextBlock block, QFont font, QColor foreground, QColor background);

class TransmitTextEdit : public QTextEdit
{
public:
    TransmitTextEdit(QWidget *parent);

    static QPair<int,int> relativeTextCursorPosition(QTextCursor cursor){
        auto c = QTextCursor(cursor);
        c.movePosition(QTextCursor::End);
        int last = c.position();

        auto cc = QTextCursor(cursor);
        int relstart = last - qMin(cc.selectionStart(), cc.selectionEnd());
        int relend = last - qMax(cc.selectionStart(), cc.selectionEnd());

        return {relstart, relend};
    }

    int charsSent() const {
        return m_sent;
    }
    void setCharsSent(int n);

    QString sentText() const {
        return m_textSent;
    }

    QString unsentText() const {
        return toPlainText().mid(charsSent());
    }

    QString toPlainText() const;
    void setPlainText(const QString &text);
    void replaceUnsentText(const QString &text, bool keepCursor);
    void replacePlainText(const QString &text, bool keepCursor);

    void setFont(QFont f);
    void setFont(QFont f, QColor fg, QColor bg);
    void clear();

    bool isProtected() const {
        return m_protected;
    }
    void setProtected(bool protect);
    bool cursorShouldBeProtected(QTextCursor c);

    bool isEmpty() const {
        return toPlainText().isEmpty();
    }
    bool isDirty() const {
        return m_dirty;
    }
    void setClean(){
        m_dirty = false;
    }

    void highlightBase();
    void highlightCharsSent();
    void highlight();

    bool eventFilter(QObject */*o*/, QEvent *e);

public slots:
    void on_selectionChanged();
    void on_textContentsChanged(int pos, int rem, int add);

private:
    QString m_lastText;
    int m_sent;
    QString m_textSent;
    bool m_protected;
    bool m_dirty;
    QFont m_font;
    QColor m_fg;
    QColor m_bg;
};

#endif // TRANSMITTEXTEDIT_H