-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmytextedit.cpp
151 lines (134 loc) · 4.74 KB
/
mytextedit.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
#include "mytextedit.h"
#include <QVBoxLayout>
#include <QStringListModel>
#include <QDebug>
#include <QAbstractItemView>
#include <QScrollBar>
Completer::Completer(QWidget *parent): QCompleter(parent)
{
}
MyTextEdit::MyTextEdit(QWidget *parent): QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
edit = new QPlainTextEdit();
edit->setPlaceholderText(QString::fromUtf8("输入@可以向对应的IP发送数据"));
layout->addWidget(edit);
completer = nullptr;
connect(edit, SIGNAL(textChanged()), this, SLOT(complete()));
edit->installEventFilter(this);
}
QString MyTextEdit::textUnderCursor()
{
QTextCursor tc = edit->textCursor();
tc.select(QTextCursor::WordUnderCursor);
return tc.selectedText();
}
void MyTextEdit::complete()
{
if(edit->toPlainText().size() == 0 || completer == nullptr) return;
QChar tail = edit->toPlainText().at(edit->toPlainText().size()-1);
if(tail == '@')
{
completer->setCompletionPrefix(tail);
QAbstractItemView *view = completer->popup();
view->setCurrentIndex(completer->completionModel()->index(0, 0));
QRect cr = edit->cursorRect();
QScrollBar *bar = completer->popup()->verticalScrollBar();
cr.setWidth(completer->popup()->sizeHintForColumn(0) + bar->sizeHint().width());
completer->complete(cr);
}
}
void MyTextEdit::changeCompletion(QString text)
{
QTextCursor tc = edit->textCursor();
int len = text.size() - completer->completionPrefix().size();
tc.movePosition(QTextCursor::EndOfWord);
tc.insertText(text.right(len));
edit->setTextCursor(tc);
completer->popup()->hide();
QString str = edit->toPlainText();
int pos = str.size() - 1;
while(str.at(pos) != '@') pos--;
tc.clearSelection();
tc.setPosition(pos, QTextCursor::MoveAnchor);
tc.setPosition(str.size(), QTextCursor::KeepAnchor);
// tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, str.size() - pos);
QTextCharFormat fmt = tc.charFormat();
QTextCharFormat fmt_back = fmt;
fmt.setForeground(QBrush(Qt::white));
fmt.setBackground(QBrush(QColor(0, 160, 233)));
tc.setCharFormat(fmt);
tc.clearSelection();
tc.setCharFormat(fmt_back);
tc.insertText(" ");
edit->setTextCursor(tc);
ipspan.push_back(QPair<int, int>(pos, str.size()+1));
}
QString MyTextEdit::toPlainText()
{
return edit->toPlainText();
}
void MyTextEdit::setPlainText(QString str)
{
edit->setPlainText(str);
}
void MyTextEdit::setPlaceholderText(QString str)
{
edit->setPlaceholderText(str);
}
void MyTextEdit::setCompleter(QStringList stringlist)
{
if(completer == nullptr)
{
completer = new Completer(this);
completer->setWidget(this);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
connect(completer, SIGNAL(activated(QString)), this, SLOT(changeCompletion(QString)));
}
else
{
delete completer->model();
}
QStringListModel * model = new QStringListModel(stringlist, this);
completer->setModel(model);
}
bool MyTextEdit::eventFilter(QObject *obj, QEvent *event)
{
if(obj == edit)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyevent = static_cast<QKeyEvent *>(event);
QTextCursor tc = edit->textCursor();
int p = tc.position();
int i;
for(i = 0; i < ipspan.size(); i++)
{
if( (keyevent->key() == Qt::Key_Backspace && p > ipspan[i].first && p <= ipspan[i].second ) || (keyevent->key() == Qt::Key_Delete && p >= ipspan[i].first && p < ipspan[i].second) )
{
tc.setPosition(ipspan[i].first, QTextCursor::MoveAnchor);
if(p == ipspan[i].second)
{
tc.setPosition(ipspan[i].second, QTextCursor::KeepAnchor);
}
else
{
tc.setPosition(ipspan[i].second + 1, QTextCursor::KeepAnchor);
}
tc.removeSelectedText();
ipspan.removeAt(i);
return true;
}
else if(p >= ipspan[i].first && p <= ipspan[i].second)
{
QTextCursor tc = edit->textCursor();
tc.setPosition(ipspan[i].second);
edit->setTextCursor(tc);
}
}
}
}
return QWidget::eventFilter(obj, event);
}