Debian Codegraph @

hiro/qt/widget/text-edit.cpp

raw
1 #if defined(Hiro_TextEdit)
2
3 namespace hiro {
4
5 auto pTextEdit::construct() -> void {
6 qtWidget = qtTextEdit = new QtTextEdit(*this);
7 qtTextEdit->connect(qtTextEdit, SIGNAL(textChanged()), SLOT(onChange()));
8
9 pWidget::construct();
10 setBackgroundColor(state().backgroundColor);
11 setForegroundColor(state().foregroundColor);
12 _setState();
13 }
14
15 auto pTextEdit::destruct() -> void {
16 if(Application::state().quit) return; //TODO: hack
17 delete qtTextEdit;
18 qtWidget = qtTextEdit = nullptr;
19 }
20
21 auto pTextEdit::setBackgroundColor(Color color) -> void {
22 static auto defaultColor = qtTextEdit->palette().color(QPalette::Base);
23
24 auto palette = qtTextEdit->palette();
25 palette.setColor(QPalette::Base, CreateColor(color, defaultColor));
26 qtTextEdit->setPalette(palette);
27 qtTextEdit->setAutoFillBackground((bool)color);
28 }
29
30 auto pTextEdit::setEditable(bool editable) -> void {
31 _setState();
32 }
33
34 auto pTextEdit::setForegroundColor(Color color) -> void {
35 static auto defaultColor = qtTextEdit->palette().color(QPalette::Text);
36
37 auto palette = qtTextEdit->palette();
38 palette.setColor(QPalette::Text, CreateColor(color, defaultColor));
39 qtTextEdit->setPalette(palette);
40 }
41
42 auto pTextEdit::setText(const string& text) -> void {
43 qtTextEdit->setPlainText(QString::fromUtf8(text));
44 }
45
46 auto pTextEdit::setTextCursor(TextCursor cursor) -> void {
47 _setState();
48 }
49
50 auto pTextEdit::setWordWrap(bool wordWrap) -> void {
51 _setState();
52 }
53
54 auto pTextEdit::text() const -> string {
55 return qtTextEdit->toPlainText().toUtf8().constData();
56 }
57
58 auto pTextEdit::textCursor() const -> TextCursor {
59 //TODO
60 return state().textCursor;
61 }
62
63 auto pTextEdit::_setState() -> void {
64 QTextCursor cursor = qtTextEdit->textCursor();
65 s32 lastCharacter = strlen(qtTextEdit->toPlainText().toUtf8().constData());
66 cursor.setPosition(max(0, min(lastCharacter, state().textCursor.offset())));
67 cursor.setPosition(max(0, min(lastCharacter, state().textCursor.offset() + state().textCursor.length())), QTextCursor::KeepAnchor);
68 qtTextEdit->setTextCursor(cursor);
69 qtTextEdit->setTextInteractionFlags(state().editable
70 ? Qt::TextEditorInteraction
71 : Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse
72 );
73 qtTextEdit->setWordWrapMode(state().wordWrap ? QTextOption::WordWrap : QTextOption::NoWrap);
74 qtTextEdit->setHorizontalScrollBarPolicy(state().wordWrap ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAlwaysOn);
75 qtTextEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
76 }
77
78 auto QtTextEdit::onChange() -> void {
79 //p.state().text = text();
80 p.self().doChange();
81 }
82
83 }
84
85 #endif