logplus/WellLogUI/src/completertextedit.cpp
2026-01-16 17:18:41 +08:00

124 lines
3.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// completertextedit.cpp
#include "completertextedit.h"
#include <QCoreApplication>
#include <QAbstractItemView>
#include <QCompleter>
#include <QKeyEvent>
#include <QString>
#include <QTextCursor>
CompleterTextEdit::CompleterTextEdit(QWidget *parent) :
QTextEdit(parent), m_completer(NULL) {
}
QCompleter * CompleterTextEdit::GetCompleter() {
return m_completer;
}
void CompleterTextEdit::setCompleter(QCompleter *completer)
{
m_completer = completer;
if (m_completer) {
m_completer->setWidget(this);
connect(m_completer, SIGNAL(activated(QString)),
this, SLOT(onCompleterActivated(QString)));
}
}
void CompleterTextEdit::keyReleaseEvent(QKeyEvent *e)
{
QTextEdit::keyReleaseEvent(e);
if(e->key()==Qt::Key_Comma) {
// QCoreApplication::postEvent(this, new QKeyEvent(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, 0));
return;
};
}
void CompleterTextEdit::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::MiddleButton) {
QTextCursor cursor = textCursor();
int pos1=cursor.selectionStart();
int pos2=cursor.selectionEnd();
QString text1=toPlainText();
text1=text1.mid(pos1,pos2-pos1);
// cursor.setPosition(0);
cursor.clearSelection();
QMouseEvent ss(e->type(),e->pos(),Qt::LeftButton,e->buttons(),e->modifiers());
QTextEdit::mousePressEvent(&ss);
QTextEdit::mouseReleaseEvent(&ss);
QTextCursor cursor1 = textCursor();
setTextCursor(cursor1);
if(text1.lastIndexOf(0x2029)>=0) {
text1.remove(0x2029);
}
textCursor().insertText(text1);
}
else QTextEdit::mousePressEvent(e);
}
void CompleterTextEdit::keyPressEvent(QKeyEvent *e) {
if (m_completer) {
if (m_completer->popup()->isVisible()) {
switch(e->key()) {
case Qt::Key_Escape:
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Tab:
e->ignore();
return;
default:
break;
}
}
QTextEdit::keyPressEvent(e);
if(e->key()==Qt::Key_Comma) {
this->insertPlainText(" ");
}
if(e->key()!=Qt::Key_Comma&&e->key()!=Qt::Key_Space
&&e->key()!=Qt::Key_Up
&&e->key()!=Qt::Key_Down
&&e->key()!=Qt::Key_Left
&&e->key()!=Qt::Key_Right
) {
QString completerPrefix = this->wordUnderCursor();
m_completer->setCompletionPrefix(completerPrefix); // 通过设置QCompleter的前缀来让Completer寻找关键词
m_completer->complete();
}
}
else {
QTextEdit::keyPressEvent(e);
}
}
void CompleterTextEdit::onCompleterActivated(const QString &completion) {
QString completionPrefix = wordUnderCursor(),
shouldInertText = completion;
QTextCursor cursor = this->textCursor();
if (!completion.contains(completionPrefix)) {// delete the previously typed.
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, completionPrefix.size());
cursor.clearSelection();
} else { // 补全相应的字符
shouldInertText = shouldInertText.replace(
shouldInertText.indexOf(completionPrefix), completionPrefix.size(), "");
}
cursor.insertText(shouldInertText);
}
QString CompleterTextEdit::wordUnderCursor() { //不断向左移动cursor并选中字符并查看选中的单词中是否含有空格——空格作为单词的分隔符
QTextCursor cursor = this->textCursor();
int pos1=cursor.position();
while (cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor)) {
int pos2=cursor.position();
if(pos2-pos1>1) {
break;
}
pos1=pos2;
if (cursor.selectedText().contains(" ")||cursor.selectedText().contains("\n")||cursor.selectedText().contains(",")||cursor.selectedText().contains("\r")) {
break;
}
}
QString cs=cursor.selectedText();
cs=cs.remove(" ");
cs=cs.remove(",");
cs=cs.remove("\r");
cs=cs.remove("\n");
return cs;
}