File: text-edit.cpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (83 lines) | stat: -rw-r--r-- 2,387 bytes parent folder | download | duplicates (2)
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
#if defined(Hiro_TextEdit)

namespace hiro {

auto pTextEdit::construct() -> void {
  qtWidget = qtTextEdit = new QtTextEdit(*this);
  qtTextEdit->connect(qtTextEdit, SIGNAL(textChanged()), SLOT(onChange()));

  pWidget::construct();
  _setState();
}

auto pTextEdit::destruct() -> void {
  delete qtTextEdit;
  qtWidget = qtTextEdit = nullptr;
}

auto pTextEdit::setBackgroundColor(Color color) -> void {
  _setState();
}

auto pTextEdit::setCursor(Cursor cursor) -> void {
  _setState();
}

auto pTextEdit::setEditable(bool editable) -> void {
  _setState();
}

auto pTextEdit::setForegroundColor(Color color) -> void {
  _setState();
}

auto pTextEdit::setText(const string& text) -> void {
  qtTextEdit->setPlainText(QString::fromUtf8(text));
}

auto pTextEdit::setWordWrap(bool wordWrap) -> void {
  _setState();
}

auto pTextEdit::text() const -> string {
  return qtTextEdit->toPlainText().toUtf8().constData();
}

auto pTextEdit::_setState() -> void {
  if(auto color = state().backgroundColor) {
    QPalette palette = qtTextEdit->palette();
    palette.setColor(QPalette::Base, QColor(color.red(), color.green(), color.blue()));
    qtTextEdit->setPalette(palette);
    qtTextEdit->setAutoFillBackground(true);
  } else {
    //todo
  }
  QTextCursor cursor = qtTextEdit->textCursor();
  signed lastCharacter = strlen(qtTextEdit->toPlainText().toUtf8().constData());
  cursor.setPosition(max(0, min(lastCharacter, state().cursor.offset())));
  cursor.setPosition(max(0, min(lastCharacter, state().cursor.offset() + state().cursor.length())), QTextCursor::KeepAnchor);
  qtTextEdit->setTextCursor(cursor);
  qtTextEdit->setTextInteractionFlags(state().editable
    ? Qt::TextEditorInteraction
    : Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse
  );
  if(auto color = state().foregroundColor) {
    QPalette palette = qtTextEdit->palette();
    palette.setColor(QPalette::Text, QColor(color.red(), color.green(), color.blue()));
    qtTextEdit->setPalette(palette);
  } else {
    //todo
  }
  qtTextEdit->setWordWrapMode(state().wordWrap ? QTextOption::WordWrap : QTextOption::NoWrap);
  qtTextEdit->setHorizontalScrollBarPolicy(state().wordWrap ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAlwaysOn);
  qtTextEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}

auto QtTextEdit::onChange() -> void {
//p.state().text = text();
  p.self().doChange();
}

}

#endif