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

121 lines
3.6 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)
{
QTextCursor cursor = textCursor();
QString text1=cursor.selectedText();
cursor.clearSelection();
if (e->button() == Qt::MiddleButton) {
// cursor.setPosition(e->pos());
QMouseEvent ss(e->type(),e->pos(),Qt::LeftButton,e->buttons(),e->modifiers());
QTextEdit::mousePressEvent(&ss);
QTextEdit::mouseReleaseEvent(&ss);
setTextCursor(textCursor());
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); // ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QCompleter<65><72>ǰ׺<C7B0><D7BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CompleterѰ<72>ҹؼ<D2B9><D8BC><EFBFBD>
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 { // <20><>ȫ<EFBFBD><C8AB>Ӧ<EFBFBD><D3A6><EFBFBD>ַ<EFBFBD>
shouldInertText = shouldInertText.replace(
shouldInertText.indexOf(completionPrefix), completionPrefix.size(), "");
}
cursor.insertText(shouldInertText);
}
QString CompleterTextEdit::wordUnderCursor() { //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>cursor<6F><72><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<E9BFB4>еĵ<D0B5><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>пո񡪡<D5B8><F1A1AAA1>ո<EFBFBD><D5B8><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ʵķָ<C4B7><D6B8><EFBFBD>
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;
}