QCodeEdit completer refactor and works fine!

This commit is contained in:
2020-09-10 21:39:24 +03:00
parent d65f09b1c5
commit be2870e116
4 changed files with 174 additions and 82 deletions

View File

@@ -1,4 +1,5 @@
#include "qcodeedit.h"
#include "qcodeedit_completer_p.h"
#include <QLayout>
#include <QBoxLayout>
#include <QScrollBar>
@@ -9,7 +10,6 @@
#include <QTextBlock>
#include <QAction>
#include <QApplication>
#include <QHeaderView>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
# include <QWindow>
#endif
@@ -54,21 +54,7 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
}
lbl_help[1]->setIcon(QIcon(":/icons/f1.png"));
lbl_help[1]->setText(tr("Press F1 for details"));
completer = new QTreeWidget();
completer->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
completer->setFocusPolicy(Qt::NoFocus);
completer->setColumnCount(2);
completer->setRootIsDecorated(false);
completer->setHeaderHidden(true);
completer->header()->setDefaultAlignment(Qt::AlignCenter);
completer->header()->
#if (QT_VERSION >= 0x050000)
setSectionResizeMode
#else
setResizeMode
#endif
(QHeaderView::ResizeToContents);
// completer->header()->setStretchLastSection(true);
completer = new QCodeEditCompleter();
ui->textCode->setCursorWidth(qMax<int>(qRound(fontHeight() / 10.), 1));
ui->textLines->viewport()->setAutoFillBackground(false);
@@ -447,7 +433,7 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
break;
case Qt::Key_Up:
if (completer->isVisible()) {
previousCompletition();
completer->previousCompletition();
return true;
}
completer->hide();
@@ -463,7 +449,7 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
break;
case Qt::Key_Down:
if (completer->isVisible()) {
nextCompletition();
completer->nextCompletition();
return true;
}
completer->hide();
@@ -646,30 +632,6 @@ void QCodeEdit::applyExtraSelection() {
}
void QCodeEdit::nextCompletition() {
int ci = completer->currentIndex().row();
if (ci >= completer->topLevelItemCount() - 1) return;
if (completer->topLevelItem(ci + 1)->flags().testFlag(Qt::ItemIsSelectable))
completer->setCurrentItem(completer->topLevelItem(ci + 1));
else {
if (ci >= completer->topLevelItemCount() - 2) return;
completer->setCurrentItem(completer->topLevelItem(ci + 2));
}
}
void QCodeEdit::previousCompletition() {
int ci = completer->currentIndex().row();
if (ci <= 0) return;
if (completer->topLevelItem(ci - 1)->flags().testFlag(Qt::ItemIsSelectable))
completer->setCurrentItem(completer->topLevelItem(ci - 1));
else {
if (ci <= 1) return;
completer->setCurrentItem(completer->topLevelItem(ci - 2));
}
}
void QCodeEdit::clearSearch() {
es_search_list.clear();
applyExtraSelection();
@@ -1275,47 +1237,16 @@ void QCodeEdit::invokeAutoCompletition(bool force) {
foreach (const ACPair & ac, acl) {
if (ac.second.isEmpty()) continue;
ACClass acc = ac_classes.value(ac.first);
QTreeWidgetItem * gi = new QTreeWidgetItem();
gi->setText(0, acc.name);
gi->setTextAlignment(0, Qt::AlignCenter);
gi->setTextAlignment(1, Qt::AlignCenter);
gi->setFont(0, bf);
gi->setBackground(0, Qt::lightGray);
gi->setFlags(Qt::ItemIsEnabled);
completer->addTopLevelItem(gi);
gi->setFirstColumnSpanned(true);
foreach (const StringsPair & s, ac.second) {
QTreeWidgetItem * ni = new QTreeWidgetItem();
ni->setIcon(0, acc.icon);
ni->setText(0, s.first);
ni->setText(1, s.second);
completer->addTopLevelItem(ni);
}
completer->addItems(bf, acc, ac);
}
if (completer->topLevelItemCount() > 1)
completer->setCurrentItem(completer->topLevelItem(1));
if (completer->isHidden())
completer->move(ui->textCode->mapToGlobal(ui->textCode->cursorRect().bottomRight()));
if (completer->topLevelItemCount() > 0) {
completer->setVisible(true);
//qApp->processEvents();
int sz = completer->verticalScrollBar()->width();
sz += completer->sizeHint().width();
// int sz = ((QAbstractItemView*)completer)->viewport()->width();
// for (int i = 0; i < completer->header()->count(); ++i) {
// completer->resizeColumnToContents(i);
// sz += completer->columnWidth(i);
// }
completer->resize(sz, fontHeight() * 16);
} else
completer->hide();
completer->invoke(ui->textCode->mapToGlobal(ui->textCode->cursorRect().bottomRight()));
}
void QCodeEdit::commitCompletition() {
if (completer->currentItem() == 0) return;
if (!completer->currentItem()->flags().testFlag(Qt::ItemIsSelectable)) return;
QString ins = completer->currentItem()->text(1), ret = completer->currentItem()->text(0);
QString ins = completer->currentValue(), ret = completer->currentReturn();
QTextCursor tc = ui->textCode->textCursor(), stc = tc;
tc.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
bool ins_br = true, shifted = false;
@@ -1355,16 +1286,16 @@ void QCodeEdit::commitCompletition() {
tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 2);
}
}
if (ins.endsWith(")") && !completer->currentItem()->text(1).endsWith("()")) {
if (ins.endsWith(")") && !completer->currentValue().endsWith("()")) {
tc.movePosition(QTextCursor::Left);
ui->textCode->setTextCursor(tc);
}
} else {
if (completer->currentItem()->text(1).endsWith(")")) {
if (completer->currentValue().endsWith(")")) {
tc.movePosition(QTextCursor::Right);
ui->textCode->setTextCursor(tc);
}
if (completer->currentItem()->text(1).endsWith("()")) {
if (completer->currentValue().endsWith("()")) {
tc.movePosition(QTextCursor::Right);
ui->textCode->setTextCursor(tc);
}