refactoring qad widgets part 2
c++ cast, nullptr, forward declaration, agregate ui, connect to member functions, order and clear includes
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
#include "qcodeedit.h"
|
||||
#include "ui_qcodeedit.h"
|
||||
#include "qcodeedit_completer_p.h"
|
||||
#include "ecombobox.h"
|
||||
#include "qad_types.h"
|
||||
#include "iconedlabel.h"
|
||||
#include <QLayout>
|
||||
#include <QBoxLayout>
|
||||
#include <QScrollBar>
|
||||
@@ -13,12 +17,10 @@
|
||||
#include <QApplication>
|
||||
#include <QDesktopServices>
|
||||
#include <QPainter>
|
||||
#include <QPlainTextEdit>
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
||||
# include <QWindow>
|
||||
#endif
|
||||
#include "ecombobox.h"
|
||||
#include "qad_types.h"
|
||||
#include "ui_qcodeedit.h"
|
||||
|
||||
Q_DECLARE_METATYPE(QTextCursor)
|
||||
Q_DECLARE_METATYPE(QCodeEdit::ACEntry)
|
||||
@@ -29,10 +31,9 @@ public:
|
||||
_QCE_Viewport(QWidget * p = nullptr): QWidget(p) {
|
||||
setObjectName("__qcodeedit_viewport__");
|
||||
setMouseTracking(true);
|
||||
//setCursor(Qt::IBeamCursor);
|
||||
}
|
||||
|
||||
void paintEvent(QPaintEvent * e) override {
|
||||
void paintEvent(QPaintEvent *) override {
|
||||
if (!isEnabled()) return;
|
||||
ce->drawCursor();
|
||||
}
|
||||
@@ -42,8 +43,8 @@ public:
|
||||
};
|
||||
|
||||
|
||||
QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
overlay = 0;
|
||||
QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent), ui(new Ui::QCodeEdit) {
|
||||
overlay = nullptr;
|
||||
prev_lc = auto_comp_pl = cur_search_ind = pos_press = pos_el_press = -1;
|
||||
timer_parse = 0;
|
||||
_ignore_focus_out = _destructor = _replacing = cursor_state = block_sel_state = false;
|
||||
@@ -51,7 +52,6 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
comment_text = "//";
|
||||
qRegisterMetaType<QTextCursor>();
|
||||
qRegisterMetaType<QCodeEdit::ACEntry>();
|
||||
ui = new Ui::QCodeEdit();
|
||||
ui->setupUi(this);
|
||||
overlay = new _QCE_Viewport(ui->textCode->viewport());
|
||||
overlay->ce = this;
|
||||
@@ -127,9 +127,6 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
QTextOption to = ui->textLines->document()->defaultTextOption();
|
||||
to.setAlignment(Qt::AlignTop | Qt::AlignRight);
|
||||
ui->textLines->document()->setDefaultTextOption(to);
|
||||
/*to = ui->textCode->document()->defaultTextOption();
|
||||
to.setFlags(QTextOption::SuppressColors);
|
||||
ui->textCode->document()->setDefaultTextOption(to);*/
|
||||
setShowSpaces(true);
|
||||
|
||||
a = new QAction(this);
|
||||
@@ -138,12 +135,6 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
connect(a, SIGNAL(triggered(bool)), this, SLOT(search_triggered()));
|
||||
addAction(a);
|
||||
|
||||
/*a = new QAction(this);
|
||||
a->setShortcut(QKeySequence("Esc"));
|
||||
a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
|
||||
connect(a, SIGNAL(triggered(bool)), this, SLOT(hideSearch()));
|
||||
addAction(a);*/
|
||||
|
||||
connect(completer, SIGNAL(itemDoubleClicked(QTreeWidgetItem * ,int)), this, SLOT(commitCompletition()));
|
||||
connect(completer, SIGNAL(commit()), this, SLOT(commitCompletition()));
|
||||
connect(completer, SIGNAL(gotoHRef(QCodeEdit::ACEntry)), this, SLOT(gotoHelpHRef(QCodeEdit::ACEntry)));
|
||||
@@ -159,7 +150,7 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
connect(ui->comboSearch->lineEdit(), SIGNAL(returnPressed()), this, SLOT(searchNext()));
|
||||
connect(ui->comboReplace->lineEdit(), SIGNAL(returnPressed()), this, SLOT(on_buttonReplaceSearch_clicked()));
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
||||
connect(qApp, &QGuiApplication::focusWindowChanged, this, [this](QWindow*w){if (w == 0) {hideHelp(); completer->hide();}});
|
||||
connect(qApp, &QGuiApplication::focusWindowChanged, this, [this](QWindow*w){if (w == nullptr) {hideHelp(); completer->hide();}});
|
||||
#endif
|
||||
updateLines();
|
||||
timer_blink = startTimer(QApplication::cursorFlashTime() / 2);
|
||||
@@ -171,10 +162,8 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
QCodeEdit::~QCodeEdit() {
|
||||
_destructor = true;
|
||||
delete completer;
|
||||
//for (int i = 0; i < 2; ++i)
|
||||
// delete lbl_help[i];
|
||||
delete widget_help;
|
||||
//delete ui;
|
||||
delete ui;
|
||||
}
|
||||
|
||||
|
||||
@@ -198,8 +187,8 @@ void QCodeEdit::setDocument(QTextDocument * doc) {
|
||||
ui->textLines->setEnabled(doc);
|
||||
documentUnset();
|
||||
if (!doc) {
|
||||
ui->textCode->setDocument(0);
|
||||
documentChanged(0);
|
||||
ui->textCode->setDocument(nullptr);
|
||||
documentChanged(nullptr);
|
||||
return;
|
||||
}
|
||||
if (!qobject_cast<QPlainTextDocumentLayout*>(doc->documentLayout()))
|
||||
@@ -207,7 +196,6 @@ void QCodeEdit::setDocument(QTextDocument * doc) {
|
||||
ui->textCode->setDocument(doc);
|
||||
cursor_width = qMax<int>(qRound(fontHeight(this) / 10.), 1);
|
||||
ui->textCode->setCursorWidth(0);
|
||||
//ui->textCode->setCursorWidth(qMax<int>(qRound(fontHeight() / 10.), 1));
|
||||
setShowSpaces(spaces_);
|
||||
if (doc->property("_cursor").isValid()) {
|
||||
setTextCursor(doc->property("_cursor").value<QTextCursor>());
|
||||
@@ -388,45 +376,40 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
|
||||
return QWidget::eventFilter(o, e);
|
||||
}
|
||||
if (ui->textLines) {
|
||||
if (o == ui->textLines->viewport()) {/*
|
||||
if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonRelease ||
|
||||
e->type() == QEvent::MouseMove || e->type() == QEvent::MouseButtonDblClick) {
|
||||
#if (QT_VERSION < 0x050000)
|
||||
const_cast<QPoint&>(((QMouseEvent*)e)->pos()) = QPoint(0, ((QMouseEvent*)e)->pos().y());
|
||||
#else
|
||||
const_cast<QPointF&>(((QMouseEvent*)e)->localPos()) = QPointF(0, ((QMouseEvent*)e)->localPos().y());
|
||||
#endif
|
||||
QApplication::sendEvent(ui->textCode->viewport(), e);
|
||||
return true;
|
||||
}*/
|
||||
if (o == ui->textLines->viewport()) {
|
||||
QTextCursor tc;
|
||||
int tcpos = 0;
|
||||
switch (e->type()) {
|
||||
case QEvent::MouseButtonPress:
|
||||
if (!isEnabled()) break;
|
||||
tc = ui->textCode->cursorForPosition(((QMouseEvent*)e)->pos());
|
||||
if (!isEnabled()) {
|
||||
break;
|
||||
}
|
||||
tc = ui->textCode->cursorForPosition(static_cast<QMouseEvent*>(e)->pos());
|
||||
tc.movePosition(QTextCursor::EndOfLine);
|
||||
pos_el_press = tc.anchor();
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
pos_press = tc.anchor();
|
||||
if (!tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor))
|
||||
if (!tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor)) {
|
||||
tc.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
||||
}
|
||||
ui->textCode->setFocus();
|
||||
ui->textCode->setTextCursor(tc);
|
||||
return true;
|
||||
case QEvent::MouseMove:
|
||||
if (!isEnabled()) break;
|
||||
tc = ui->textCode->cursorForPosition(((QMouseEvent*)e)->pos());
|
||||
tc = ui->textCode->cursorForPosition(static_cast<QMouseEvent*>(e)->pos());
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
if (pos_press == tc.anchor()) {
|
||||
if (!tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor))
|
||||
if (!tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor)) {
|
||||
tc.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
||||
}
|
||||
ui->textCode->setTextCursor(tc);
|
||||
return true;
|
||||
}
|
||||
if (pos_press < tc.anchor()) {
|
||||
if (!tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor))
|
||||
if (!tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor)) {
|
||||
tc.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
||||
}
|
||||
}
|
||||
tcpos = tc.position();
|
||||
tc.setPosition(pos_press < tc.anchor() ? pos_press : pos_el_press);
|
||||
@@ -451,7 +434,7 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
|
||||
if (o == ui->comboSearch || o == ui->comboReplace) {
|
||||
//qDebug() << o << e;
|
||||
if (e->type() == QEvent::KeyPress) {
|
||||
if (((QKeyEvent * )e)->key() == Qt::Key_Escape) {
|
||||
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
|
||||
hideHelp();
|
||||
if (completer->isVisible())
|
||||
completer->hide();
|
||||
@@ -469,14 +452,14 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
|
||||
cancelBlockSelection();
|
||||
completer->hide();
|
||||
hideHelp();
|
||||
QMouseEvent * me = (QMouseEvent*)e;
|
||||
auto me = static_cast<QMouseEvent*>(e);
|
||||
if (me->modifiers().testFlag(Qt::ControlModifier) && (me->button() == Qt::LeftButton))
|
||||
if (!hasBlockSelection() && !ui->textCode->textCursor().hasSelection())
|
||||
gotoLink();
|
||||
} break;
|
||||
case QEvent::MouseMove: {
|
||||
if (!completer->isHidden()) break;
|
||||
QMouseEvent * me = (QMouseEvent*)e;
|
||||
auto me = static_cast<QMouseEvent*>(e);
|
||||
if (me->buttons() != 0)
|
||||
switchBlockSelection();
|
||||
if (me->modifiers().testFlag(Qt::ControlModifier))
|
||||
@@ -490,9 +473,9 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
|
||||
if (!isEnabled()) break;
|
||||
drag_cursor = ui->textCode->cursorForPosition(
|
||||
#if QT_VERSION_MAJOR <= 5
|
||||
((QDragMoveEvent*)e)->pos()
|
||||
static_cast<QDragMoveEvent*>(e)->pos()
|
||||
#else
|
||||
((QDragMoveEvent*)e)->position().toPoint()
|
||||
static_cast<QDragMoveEvent*>(e)->position().toPoint()
|
||||
#endif
|
||||
);
|
||||
repaintCursor();
|
||||
@@ -512,14 +495,14 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
|
||||
switch (e->type()) {
|
||||
case QEvent::ToolTip:
|
||||
if (completer->isHidden()) {
|
||||
QTextCursor tc = ui->textCode->cursorForPosition(((QHelpEvent*)e)->pos());
|
||||
QTextCursor tc = ui->textCode->cursorForPosition(static_cast<QHelpEvent*>(e)->pos());
|
||||
tc.select(QTextCursor::WordUnderCursor);
|
||||
raiseHelp(tc);
|
||||
}
|
||||
break;
|
||||
case QEvent::KeyPress:
|
||||
//qDebug() << "key" << ke;
|
||||
if (codeKeyEvent((QKeyEvent * )e))
|
||||
if (codeKeyEvent(static_cast<QKeyEvent*>(e)))
|
||||
return true;
|
||||
break;
|
||||
case QEvent::KeyRelease:
|
||||
@@ -608,10 +591,11 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
break;
|
||||
case Qt::Key_Escape:
|
||||
hideHelp();
|
||||
if (completer->isVisible())
|
||||
if (completer->isVisible()) {
|
||||
completer->hide();
|
||||
else
|
||||
} else {
|
||||
hideSearch();
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Up:
|
||||
switchBlockSelection(ke);
|
||||
@@ -622,10 +606,11 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
completer->hide();
|
||||
hideHelp();
|
||||
if (ke->modifiers().testFlag(Qt::AltModifier)) {
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier))
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
return false;
|
||||
else
|
||||
} else {
|
||||
copyLineUp();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier) && ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
@@ -642,10 +627,11 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
completer->hide();
|
||||
hideHelp();
|
||||
if (ke->modifiers().testFlag(Qt::AltModifier)) {
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier))
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
return false;
|
||||
else
|
||||
} else {
|
||||
copyLineDown();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier) && ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
@@ -666,13 +652,17 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
case Qt::Key_Left:
|
||||
case Qt::Key_Right:
|
||||
switchBlockSelection(ke);
|
||||
if (hasBlockSelection()) break;
|
||||
if (hasBlockSelection()) {
|
||||
break;
|
||||
}
|
||||
case Qt::Key_Backspace:
|
||||
case Qt::Key_Delete:
|
||||
if (completer->isVisible())
|
||||
if (completer->isVisible()) {
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
if (removeBlockSelection(ke->key() == Qt::Key_Delete))
|
||||
}
|
||||
if (removeBlockSelection(ke->key() == Qt::Key_Delete)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Return:
|
||||
if (hasBlockSelection()) {
|
||||
@@ -684,15 +674,17 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
completer->hide();
|
||||
return true;
|
||||
}
|
||||
if (ui->textCode->textCursor().selectedText().isEmpty())
|
||||
if (ui->textCode->textCursor().selectedText().isEmpty()) {
|
||||
QMetaObject::invokeMethod(this, "autoIndent", Qt::QueuedConnection);
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Tab:
|
||||
if (!ui->textCode->textCursor().selectedText().isEmpty()) {
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier))
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
deindent();
|
||||
else
|
||||
} else {
|
||||
indent();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
@@ -703,12 +695,14 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Control:
|
||||
if (!hasBlockSelection() && !ui->textCode->textCursor().hasSelection())
|
||||
if (!hasBlockSelection() && !ui->textCode->textCursor().hasSelection()) {
|
||||
showLink();
|
||||
}
|
||||
break;
|
||||
case Qt::Key_F1:
|
||||
if (widget_help->isVisible())
|
||||
if (widget_help->isVisible()) {
|
||||
gotoHelpHRef(help_entry);
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
@@ -723,8 +717,9 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
completer->hide();
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
} else {
|
||||
if ((kc.isLetterOrNumber() || kc.toLatin1() == '_') && completer->isVisible())
|
||||
if ((kc.isLetterOrNumber() || kc.toLatin1() == '_') && completer->isVisible()) {
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -733,14 +728,21 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) {
|
||||
void QCodeEdit::toggleComment() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
tc.beginEditBlock();
|
||||
int ss = tc.selectionStart(), ss_ = ss, se = tc.selectionEnd(), se_ = se;
|
||||
int ss = tc.selectionStart();
|
||||
int ss_ = ss;
|
||||
int se = tc.selectionEnd();
|
||||
QString st_ = tc.selection().toPlainText();
|
||||
if (st_.endsWith("\n")) {
|
||||
st_.chop(1);
|
||||
se--; se_--;
|
||||
se--;
|
||||
}
|
||||
tc.setPosition(ss); tc.movePosition(QTextCursor::StartOfLine); ss = tc.position();
|
||||
tc.setPosition(se); tc.movePosition(QTextCursor::EndOfLine ); se = tc.position();
|
||||
int se_ = se;
|
||||
tc.setPosition(ss);
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
ss = tc.position();
|
||||
tc.setPosition(se);
|
||||
tc.movePosition(QTextCursor::EndOfLine );
|
||||
se = tc.position();
|
||||
tc.setPosition(ss);
|
||||
bool need_comment = false;
|
||||
QMap<int, bool> comms;
|
||||
@@ -754,11 +756,13 @@ void QCodeEdit::toggleComment() {
|
||||
need_comment = true;
|
||||
comms[line] = false;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
comms[line] = false;
|
||||
}
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
if (!tc.movePosition(QTextCursor::Down))
|
||||
if (!tc.movePosition(QTextCursor::Down)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tc.setPosition(ss);
|
||||
bool first = true;
|
||||
@@ -780,8 +784,9 @@ void QCodeEdit::toggleComment() {
|
||||
}
|
||||
first = false;
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
if (!tc.movePosition(QTextCursor::Down))
|
||||
if (!tc.movePosition(QTextCursor::Down)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tc.setPosition(ss_);
|
||||
tc.setPosition(se_, QTextCursor::KeepAnchor);
|
||||
@@ -822,7 +827,8 @@ void QCodeEdit::highlightBrackets() {
|
||||
es.cursor = tc;
|
||||
es_brackets << es;
|
||||
QTextCursor ftc = tc;
|
||||
int bcnt = 1; char fch = antiBracket(ch);
|
||||
int bcnt = 1;
|
||||
char fch = antiBracket(ch);
|
||||
while (bcnt > 0) {
|
||||
ftc.setPosition(ftc.position());
|
||||
if (!ftc.movePosition(mop[d], QTextCursor::KeepAnchor)) break;
|
||||
@@ -836,8 +842,9 @@ void QCodeEdit::highlightBrackets() {
|
||||
es_brackets << es;
|
||||
es.format = es_range.format;
|
||||
es.cursor.setPosition(tc.position(), QTextCursor::KeepAnchor);
|
||||
if (!es.cursor.selection().isEmpty())
|
||||
if (!es.cursor.selection().isEmpty()) {
|
||||
es_brackets << es;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -859,10 +866,18 @@ void QCodeEdit::clearSearch() {
|
||||
|
||||
|
||||
void QCodeEdit::moveToSearch() {
|
||||
if (es_search_list.isEmpty()) return;
|
||||
if (cur_search_ind < 0) cur_search_ind += es_search_list.size();
|
||||
if (cur_search_ind >= es_search_list.size()) cur_search_ind = 0;
|
||||
if (cur_search_ind < 0 || cur_search_ind >= es_search_list.size()) return;
|
||||
if (es_search_list.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (cur_search_ind < 0) {
|
||||
cur_search_ind += es_search_list.size();
|
||||
}
|
||||
if (cur_search_ind >= es_search_list.size()) {
|
||||
cur_search_ind = 0;
|
||||
}
|
||||
if (cur_search_ind < 0 || cur_search_ind >= es_search_list.size()) {
|
||||
return;
|
||||
}
|
||||
ui->textCode->setTextCursor(es_search_list[cur_search_ind].cursor);
|
||||
}
|
||||
|
||||
@@ -870,9 +885,11 @@ void QCodeEdit::moveToSearch() {
|
||||
int QCodeEdit::searchIndFromCursor() {
|
||||
if (es_search_list.isEmpty()) return -1;
|
||||
int ci = ui->textCode->textCursor().anchor();
|
||||
for (int i = 0; i < es_search_list.size(); ++i)
|
||||
if (es_search_list[i].cursor.anchor() > ci)
|
||||
for (int i = 0; i < es_search_list.size(); ++i) {
|
||||
if (es_search_list[i].cursor.anchor() > ci) {
|
||||
return i - 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -891,11 +908,8 @@ QRect QCodeEdit::cursorRect() {
|
||||
|
||||
QRect QCodeEdit::blockSelectionRect() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
QPoint ps(block_start_cursor.positionInBlock(), block_start_cursor.blockNumber()),
|
||||
pe(tc.positionInBlock(), tc.blockNumber());
|
||||
//QRect bsr(QPoint(qMin(ps.x(), pe.x()), qMin(ps.y(), pe.y())),
|
||||
// QSize(qAbs(ps.x() - pe.x()), qAbs(ps.y() - pe.y()) + 1));
|
||||
//return bsr;
|
||||
QPoint ps(block_start_cursor.positionInBlock(), block_start_cursor.blockNumber());
|
||||
QPoint pe(tc.positionInBlock(), tc.blockNumber());
|
||||
return (ui->textCode->cursorRect(tc) | ui->textCode->cursorRect(block_start_cursor))
|
||||
.translated(ui->textCode->horizontalScrollBar()->value(), 0);
|
||||
}
|
||||
@@ -966,12 +980,16 @@ void QCodeEdit::cancelBlockSelection() {
|
||||
|
||||
void QCodeEdit::switchBlockSelection(QKeyEvent * ke) {
|
||||
bool alt = QApplication::keyboardModifiers().testFlag(Qt::AltModifier);
|
||||
if (ke) alt = ke->modifiers().testFlag(Qt::AltModifier) && ke->modifiers().testFlag(Qt::ShiftModifier);
|
||||
if (ke) {
|
||||
alt = ke->modifiers().testFlag(Qt::AltModifier) && ke->modifiers().testFlag(Qt::ShiftModifier);
|
||||
}
|
||||
if (alt) {
|
||||
startBlockSelection();
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
QTextCursor::MoveOperation op = QTextCursor::NoMove;
|
||||
if (!ke) return;
|
||||
if (!ke) {
|
||||
return;
|
||||
}
|
||||
switch (ke->key()) {
|
||||
case Qt::Key_Left : op = QTextCursor::Left ; break;
|
||||
case Qt::Key_Right: op = QTextCursor::Right; break;
|
||||
@@ -984,18 +1002,25 @@ void QCodeEdit::switchBlockSelection(QKeyEvent * ke) {
|
||||
ui->textCode->setTextCursor(tc);
|
||||
}
|
||||
repaintCursor();
|
||||
} else
|
||||
} else {
|
||||
cancelBlockSelection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool QCodeEdit::removeBlockSelection(bool is_del) {
|
||||
if (!hasBlockSelection()) return false;
|
||||
if (!hasBlockSelection()) {
|
||||
return false;
|
||||
}
|
||||
QRect bsr = blockSelectionRect();
|
||||
bool del = false, back = false;
|
||||
bool del = false;
|
||||
bool back = false;
|
||||
if (bsr.width() <= 1) {
|
||||
if (is_del) del = true;
|
||||
else back = true;
|
||||
if (is_del) {
|
||||
del = true;
|
||||
} else {
|
||||
back = true;
|
||||
}
|
||||
}
|
||||
QVector<QTextCursor> clist = blockSelectionCursors(bsr);
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
@@ -1008,19 +1033,24 @@ bool QCodeEdit::removeBlockSelection(bool is_del) {
|
||||
width
|
||||
#endif
|
||||
(" ");
|
||||
int min_dist = nullw * 0.5;
|
||||
int min_dist = nullw / 2;
|
||||
int new_pos = -1;
|
||||
foreach (QTextCursor c, clist) {
|
||||
for (QTextCursor & c : clist) {
|
||||
if (del || back) {
|
||||
if (qAbs(bspx - ui->textCode->cursorRect(c).center().x()) > min_dist) continue;
|
||||
if (qAbs(bspx - ui->textCode->cursorRect(c).center().x()) > min_dist) {
|
||||
continue;
|
||||
}
|
||||
int line = c.blockNumber();
|
||||
c.movePosition(del ? QTextCursor::Right : QTextCursor::Left, QTextCursor::KeepAnchor);
|
||||
if (line != c.blockNumber()) continue;
|
||||
if (line != c.blockNumber()) {
|
||||
continue;
|
||||
}
|
||||
//qDebug() << qAbs(bspx - ui->textCode->cursorRect(c).center().x()) << min_dist;
|
||||
}
|
||||
c.removeSelectedText();
|
||||
if (c.blockNumber() == tc.blockNumber())
|
||||
if (c.blockNumber() == tc.blockNumber()) {
|
||||
new_pos = c.position();
|
||||
}
|
||||
}
|
||||
tc.setPosition(new_pos);
|
||||
ui->textCode->setTextCursor(tc);
|
||||
@@ -1030,7 +1060,9 @@ bool QCodeEdit::removeBlockSelection(bool is_del) {
|
||||
|
||||
|
||||
void QCodeEdit::insertBlockSelection(QString text) {
|
||||
if (!hasBlockSelection()) return;
|
||||
if (!hasBlockSelection()) {
|
||||
return;
|
||||
}
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
QRect bsr = blockSelectionRect();
|
||||
//qDebug() << "___" << bsr;
|
||||
@@ -1041,7 +1073,6 @@ void QCodeEdit::insertBlockSelection(QString text) {
|
||||
//qDebug() << "del" << bsr;
|
||||
}
|
||||
tc.beginEditBlock();
|
||||
//tc.joinPreviousEditBlock();
|
||||
int nullw = ui->textCode->fontMetrics().
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
|
||||
horizontalAdvance
|
||||
@@ -1050,7 +1081,7 @@ void QCodeEdit::insertBlockSelection(QString text) {
|
||||
#endif
|
||||
(" ");
|
||||
QVector<QTextCursor> clist = blockSelectionCursors(bsr);
|
||||
foreach (QTextCursor c, clist) {
|
||||
for (QTextCursor & c : clist) {
|
||||
c.removeSelectedText();
|
||||
int spcnt = (bsr.left() - ui->textCode->cursorRect(c).center().x() - scrl) / nullw;
|
||||
if (spcnt > 0) {
|
||||
@@ -1058,20 +1089,20 @@ void QCodeEdit::insertBlockSelection(QString text) {
|
||||
}
|
||||
c.insertText(text);
|
||||
}
|
||||
//tc.setPosition(tc.block().position() + block_start_cursor.positionInBlock());
|
||||
//ui->textCode->setTextCursor(tc);
|
||||
tc.endEditBlock();
|
||||
}
|
||||
|
||||
|
||||
void QCodeEdit::createBlockSelection() {
|
||||
if (!hasBlockSelection()) return;
|
||||
if (!hasBlockSelection()) {
|
||||
return;
|
||||
}
|
||||
es_blockselection.clear();
|
||||
QTextEdit::ExtraSelection es;
|
||||
es.format.setForeground(palette().brush(QPalette::HighlightedText));
|
||||
es.format.setBackground(palette().brush(QPalette::Highlight));
|
||||
QVector<QTextCursor> clist = blockSelectionCursors(blockSelectionRect());
|
||||
foreach (QTextCursor c, clist) {
|
||||
for (QTextCursor & c : clist) {
|
||||
es.cursor = c;
|
||||
es_blockselection << es;
|
||||
}
|
||||
@@ -1096,15 +1127,18 @@ void QCodeEdit::searchAll() {
|
||||
QTextEdit::ExtraSelection es = es_search;
|
||||
while (true) {
|
||||
tc = ui->textCode->document()->find(st, tc, ff);
|
||||
if (tc.isNull()) break;
|
||||
if (tc.isNull()) {
|
||||
break;
|
||||
}
|
||||
es.cursor = tc;
|
||||
es_search_list << es;
|
||||
}
|
||||
}
|
||||
applyExtraSelection();
|
||||
QString ss;
|
||||
if (es_search_list.isEmpty())
|
||||
if (es_search_list.isEmpty()) {
|
||||
ss = "color: rgb(180, 0, 0);";
|
||||
}
|
||||
ui->comboSearch->lineEdit()->setStyleSheet(ss);
|
||||
}
|
||||
|
||||
@@ -1117,16 +1151,15 @@ void QCodeEdit::search_triggered() {
|
||||
st = tc.selectedText();
|
||||
}
|
||||
search(st);
|
||||
//QMetaObject::invokeMethod(ui->comboSearch->lineEdit(), "returnPressed");
|
||||
if (ui->comboSearch->findText(st) < 0)
|
||||
if (ui->comboSearch->findText(st) < 0) {
|
||||
ui->comboSearch->insertItem(0, st);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void QCodeEdit::syncScrolls() {
|
||||
ui->textLines->verticalScrollBar()->setValue(ui->textCode->verticalScrollBar()->value());
|
||||
ui->textLines->setHorizontalScrollBarPolicy(ui->textCode->horizontalScrollBar()->isVisible() ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAlwaysOff);
|
||||
//qDebug() << "!!!";
|
||||
}
|
||||
|
||||
|
||||
@@ -1163,23 +1196,35 @@ void QCodeEdit::deleteLine() {
|
||||
void QCodeEdit::copyLineUp() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
tc.beginEditBlock();
|
||||
int ss = tc.selectionStart(), ss_ = ss, se = tc.selectionEnd(), se_ = se;
|
||||
int ss = tc.selectionStart();
|
||||
int ss_ = ss;
|
||||
int se = tc.selectionEnd();
|
||||
QString st_ = tc.selection().toPlainText();
|
||||
if (st_.endsWith("\n")) {
|
||||
st_.chop(1);
|
||||
se--; se_--;
|
||||
se--;
|
||||
}
|
||||
tc.setPosition(ss); tc.movePosition(QTextCursor::StartOfLine); ss = tc.position();
|
||||
tc.setPosition(se); tc.movePosition(QTextCursor::EndOfLine); se = tc.position();
|
||||
tc.setPosition(ss); tc.setPosition(se, QTextCursor::KeepAnchor);
|
||||
int se_ = se;
|
||||
tc.setPosition(ss);
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
ss = tc.position();
|
||||
tc.setPosition(se);
|
||||
tc.movePosition(QTextCursor::EndOfLine);
|
||||
se = tc.position();
|
||||
tc.setPosition(ss);
|
||||
|
||||
tc.setPosition(se, QTextCursor::KeepAnchor);
|
||||
bool ins_nl = false;
|
||||
if (!tc.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor))
|
||||
if (!tc.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor)) {
|
||||
ins_nl = true;
|
||||
}
|
||||
QString l = tc.selectedText();
|
||||
tc.beginEditBlock();
|
||||
tc.setPosition(ss);
|
||||
if (ins_nl)
|
||||
if (ins_nl) {
|
||||
l.append("\n");
|
||||
}
|
||||
|
||||
tc.insertText(l);
|
||||
tc.setPosition(ss_);
|
||||
tc.setPosition(se_, QTextCursor::KeepAnchor);
|
||||
@@ -1192,26 +1237,39 @@ void QCodeEdit::copyLineUp() {
|
||||
void QCodeEdit::copyLineDown() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
tc.beginEditBlock();
|
||||
int ss = tc.selectionStart(), ss_ = ss, se = tc.selectionEnd(), se_ = se;
|
||||
int ss = tc.selectionStart();
|
||||
int ss_ = ss;
|
||||
int se = tc.selectionEnd();
|
||||
QString st_ = tc.selection().toPlainText();
|
||||
if (st_.endsWith("\n")) {
|
||||
st_.chop(1);
|
||||
se--; se_--;
|
||||
se--;
|
||||
}
|
||||
tc.setPosition(ss); tc.movePosition(QTextCursor::StartOfLine); ss = tc.position();
|
||||
tc.setPosition(se); tc.movePosition(QTextCursor::EndOfLine); se = tc.position();
|
||||
tc.setPosition(ss); tc.setPosition(se, QTextCursor::KeepAnchor);
|
||||
int se_ = se;
|
||||
tc.setPosition(ss);
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
ss = tc.position();
|
||||
tc.setPosition(se);
|
||||
tc.movePosition(QTextCursor::EndOfLine);
|
||||
se = tc.position();
|
||||
tc.setPosition(ss);
|
||||
|
||||
tc.setPosition(se, QTextCursor::KeepAnchor);
|
||||
bool ins_nl = false;
|
||||
if (!tc.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor))
|
||||
if (!tc.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor)) {
|
||||
ins_nl = true;
|
||||
}
|
||||
QString l = tc.selectedText();
|
||||
tc.beginEditBlock();
|
||||
tc.setPosition(ss);
|
||||
ss_ += l.size(); se_ += l.size();
|
||||
ss_ += l.size();
|
||||
se_ += l.size();
|
||||
if (ins_nl) {
|
||||
l.append("\n");
|
||||
ss_++; se_++;
|
||||
ss_++;
|
||||
se_++;
|
||||
}
|
||||
|
||||
tc.insertText(l);
|
||||
tc.setPosition(ss_);
|
||||
tc.setPosition(se_, QTextCursor::KeepAnchor);
|
||||
@@ -1223,22 +1281,33 @@ void QCodeEdit::copyLineDown() {
|
||||
|
||||
void QCodeEdit::moveLineUp() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
int ss = tc.selectionStart(), ss_ = ss, se = tc.selectionEnd(), se_ = se;
|
||||
int ss = tc.selectionStart();
|
||||
int ss_ = ss;
|
||||
int se = tc.selectionEnd();
|
||||
QString st_ = tc.selection().toPlainText();
|
||||
if (st_.endsWith("\n")) {
|
||||
st_.chop(1);
|
||||
se--; se_--;
|
||||
se--;
|
||||
}
|
||||
tc.setPosition(ss); tc.movePosition(QTextCursor::StartOfLine); ss = tc.position();
|
||||
tc.setPosition(se); tc.movePosition(QTextCursor::EndOfLine); se = tc.position();
|
||||
int se_ = se;
|
||||
tc.setPosition(ss);
|
||||
if (!tc.movePosition(QTextCursor::Up))
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
ss = tc.position();
|
||||
tc.setPosition(se);
|
||||
tc.movePosition(QTextCursor::EndOfLine);
|
||||
se = tc.position();
|
||||
tc.setPosition(ss);
|
||||
|
||||
if (!tc.movePosition(QTextCursor::Up)) {
|
||||
return;
|
||||
}
|
||||
tc.beginEditBlock();
|
||||
tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);
|
||||
QString l = tc.selectedText();
|
||||
ss -= l.size(); se -= l.size();
|
||||
ss_ -= l.size(); se_ -= l.size();
|
||||
ss -= l.size();
|
||||
se -= l.size();
|
||||
ss_ -= l.size();
|
||||
se_ -= l.size();
|
||||
tc.beginEditBlock();
|
||||
tc.removeSelectedText();
|
||||
tc.setPosition(se);
|
||||
@@ -1247,6 +1316,7 @@ void QCodeEdit::moveLineUp() {
|
||||
l.prepend("\n");
|
||||
de = true;
|
||||
}
|
||||
|
||||
tc.insertText(l);
|
||||
if (de) {
|
||||
tc.movePosition(QTextCursor::End);
|
||||
@@ -1263,17 +1333,26 @@ void QCodeEdit::moveLineUp() {
|
||||
|
||||
void QCodeEdit::moveLineDown() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
int ss = tc.selectionStart(), ss_ = ss, se = tc.selectionEnd(), se_ = se;
|
||||
int ss = tc.selectionStart();
|
||||
int ss_ = ss;
|
||||
int se = tc.selectionEnd();
|
||||
QString st_ = tc.selection().toPlainText();
|
||||
if (st_.endsWith("\n")) {
|
||||
st_.chop(1);
|
||||
se--; se_--;
|
||||
se--;
|
||||
}
|
||||
tc.setPosition(ss); tc.movePosition(QTextCursor::StartOfLine); ss = tc.position();
|
||||
tc.setPosition(se); tc.movePosition(QTextCursor::EndOfLine); se = tc.position();
|
||||
int se_ = se;
|
||||
tc.setPosition(ss);
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
ss = tc.position();
|
||||
tc.setPosition(se);
|
||||
if (!tc.movePosition(QTextCursor::Right))
|
||||
tc.movePosition(QTextCursor::EndOfLine);
|
||||
se = tc.position();
|
||||
tc.setPosition(se);
|
||||
|
||||
if (!tc.movePosition(QTextCursor::Right)) {
|
||||
return;
|
||||
}
|
||||
tc.beginEditBlock();
|
||||
bool de = false;
|
||||
if (!tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor)) {
|
||||
@@ -1284,15 +1363,19 @@ void QCodeEdit::moveLineDown() {
|
||||
tc.beginEditBlock();
|
||||
tc.removeSelectedText();
|
||||
tc.setPosition(ss);
|
||||
if (de) l += "\n";
|
||||
if (de) {
|
||||
l += "\n";
|
||||
}
|
||||
tc.insertText(l);
|
||||
if (de) {
|
||||
tc.movePosition(QTextCursor::End);
|
||||
tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
|
||||
tc.removeSelectedText();
|
||||
}
|
||||
ss += l.size(); se += l.size();
|
||||
ss_ += l.size(); se_ += l.size();
|
||||
ss += l.size();
|
||||
se += l.size();
|
||||
ss_ += l.size();
|
||||
se_ += l.size();
|
||||
tc.setPosition(ss_);
|
||||
tc.setPosition(se_, QTextCursor::KeepAnchor);
|
||||
tc.endEditBlock();
|
||||
@@ -1304,21 +1387,29 @@ void QCodeEdit::moveLineDown() {
|
||||
void QCodeEdit::indent() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
tc.beginEditBlock();
|
||||
int ss = tc.selectionStart(), ss_ = ss, se = tc.selectionEnd(), se_ = se;
|
||||
int ss = tc.selectionStart();
|
||||
int ss_ = ss;
|
||||
int se = tc.selectionEnd();
|
||||
QString st_ = tc.selection().toPlainText();
|
||||
if (st_.endsWith("\n")) {
|
||||
st_.chop(1);
|
||||
se--; se_--;
|
||||
se--;
|
||||
}
|
||||
tc.setPosition(ss); tc.movePosition(QTextCursor::StartOfLine); ss = tc.position();
|
||||
tc.setPosition(se); tc.movePosition(QTextCursor::EndOfLine); se = tc.position();
|
||||
int se_ = se;
|
||||
tc.setPosition(ss);
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
ss = tc.position();
|
||||
tc.setPosition(se);
|
||||
tc.movePosition(QTextCursor::EndOfLine);
|
||||
se = tc.position();
|
||||
tc.setPosition(ss);
|
||||
while (tc.position() < se_) {
|
||||
tc.insertText("\t");
|
||||
se_++;
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
if (!tc.movePosition(QTextCursor::Down))
|
||||
if (!tc.movePosition(QTextCursor::Down)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tc.setPosition(ss_ + 1);
|
||||
tc.setPosition(se_, QTextCursor::KeepAnchor);
|
||||
@@ -1330,14 +1421,21 @@ void QCodeEdit::indent() {
|
||||
void QCodeEdit::deindent() {
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
tc.beginEditBlock();
|
||||
int ss = tc.selectionStart(), ss_ = ss, se = tc.selectionEnd(), se_ = se;
|
||||
int ss = tc.selectionStart();
|
||||
int ss_ = ss;
|
||||
int se = tc.selectionEnd();
|
||||
QString st_ = tc.selection().toPlainText();
|
||||
if (st_.endsWith("\n")) {
|
||||
st_.chop(1);
|
||||
se--; se_--;
|
||||
se--;
|
||||
}
|
||||
tc.setPosition(ss); tc.movePosition(QTextCursor::StartOfLine); ss = tc.position();
|
||||
tc.setPosition(se); tc.movePosition(QTextCursor::EndOfLine); se = tc.position();
|
||||
int se_ = se;
|
||||
tc.setPosition(ss);
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
ss = tc.position();
|
||||
tc.setPosition(se);
|
||||
tc.movePosition(QTextCursor::EndOfLine);
|
||||
se = tc.position();
|
||||
tc.setPosition(ss);
|
||||
bool first = true;
|
||||
while (tc.position() < se_) {
|
||||
@@ -1374,22 +1472,29 @@ void QCodeEdit::deindent() {
|
||||
|
||||
|
||||
void QCodeEdit::autoIndent() {
|
||||
QTextCursor tc = ui->textCode->textCursor(), stc = tc;
|
||||
QTextCursor tc = ui->textCode->textCursor();
|
||||
QTextCursor stc = tc;
|
||||
tc.movePosition(QTextCursor::StartOfLine);
|
||||
if (!tc.movePosition(QTextCursor::Up)) return;
|
||||
if (!tc.movePosition(QTextCursor::Up)) {
|
||||
return;
|
||||
}
|
||||
tc.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);
|
||||
QString line = tc.selectedText(), tabs;
|
||||
QString line = tc.selectedText();
|
||||
QString tabs;
|
||||
int i = 0;
|
||||
for (; i < line.size(); ++i)
|
||||
if (!line[i].isSpace()) {
|
||||
tabs = line.left(i);
|
||||
break;
|
||||
}
|
||||
if (i >= line.size())
|
||||
if (i >= line.size()) {
|
||||
tabs = line.left(line.size() - 1);
|
||||
}
|
||||
int nt = qMax<int>(0, line.count(QChar('{')) - line.count(QChar('}')));
|
||||
tabs.append(QString("\t").repeated(nt));
|
||||
if (tabs.isEmpty()) return;
|
||||
if (tabs.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
stc.beginEditBlock();
|
||||
stc.insertText(tabs);
|
||||
ui->textCode->setTextCursor(stc);
|
||||
@@ -1470,7 +1575,9 @@ void QCodeEdit::setText(const QString & t) {
|
||||
|
||||
|
||||
void QCodeEdit::updateLines() {
|
||||
if (timer_parse > 0) killTimer(timer_parse);
|
||||
if (timer_parse > 0) {
|
||||
killTimer(timer_parse);
|
||||
}
|
||||
timer_parse = startTimer(500);
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
||||
# if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
|
||||
@@ -1482,7 +1589,9 @@ void QCodeEdit::updateLines() {
|
||||
ui->textCode->setTabStopWidth(ui->textCode->fontMetrics().width(" "));
|
||||
#endif
|
||||
int lc = ui->textCode->document()->lineCount();
|
||||
if (prev_lc == lc) return;
|
||||
if (prev_lc == lc) {
|
||||
return;
|
||||
}
|
||||
prev_lc = lc;
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
|
||||
ui->textLines->setFixedWidth(ui->textLines->fontMetrics().horizontalAdvance(QString(" %1").arg(lc)));
|
||||
@@ -1490,25 +1599,38 @@ void QCodeEdit::updateLines() {
|
||||
ui->textLines->setFixedWidth(ui->textLines->fontMetrics().width(QString(" %1").arg(lc)));
|
||||
#endif
|
||||
ui->textLines->clear();
|
||||
for (int i = 1; i <= lc; ++i)
|
||||
for (int i = 1; i <= lc; ++i) {
|
||||
ui->textLines->appendPlainText(QString("%1").arg(i));
|
||||
}
|
||||
ui->textLines->verticalScrollBar()->setValue(ui->textCode->verticalScrollBar()->value());
|
||||
}
|
||||
|
||||
|
||||
QString QCodeEdit::selectArg(QString s, int arg) {
|
||||
if (!s.contains('(') || arg < 0) return s;
|
||||
if (!s.contains('(') || arg < 0) {
|
||||
return s;
|
||||
}
|
||||
QString ss = s.left(s.indexOf('('));
|
||||
s.remove(0, ss.size());
|
||||
if (s.startsWith('(')) s.remove(0, 1);
|
||||
if (s.endsWith(')')) s.chop(1);
|
||||
if (s.startsWith('(')) {
|
||||
s.remove(0, 1);
|
||||
}
|
||||
if (s.endsWith(')')) {
|
||||
s.chop(1);
|
||||
}
|
||||
QStringList al = s.split(",");
|
||||
QString ret = ss + "(";
|
||||
for (int i = 0; i < al.size(); ++i) {
|
||||
if (i > 0) ret += ", ";
|
||||
if (i == arg) ret += "<span style=\"font-weight:600;\">";
|
||||
if (i > 0) {
|
||||
ret += ", ";
|
||||
}
|
||||
if (i == arg) {
|
||||
ret += "<span style=\"font-weight:600;\">";
|
||||
}
|
||||
ret += al[i].trimmed();
|
||||
if (i == arg) ret += "</span>";
|
||||
if (i == arg) {
|
||||
ret += "</span>";
|
||||
}
|
||||
}
|
||||
ret += ")";
|
||||
return ret;
|
||||
@@ -1518,16 +1640,20 @@ QString QCodeEdit::selectArg(QString s, int arg) {
|
||||
QCodeEdit::ACEntry QCodeEdit::findEntryOnCursor(QTextCursor tc, int arg, ACClass * acc, QPair<QStringList, QString> * scope) {
|
||||
bool ok = false;
|
||||
QPair<QStringList, QString> sc = getScope(tc, &ok);
|
||||
if (scope) *scope = sc;
|
||||
if (scope) {
|
||||
*scope = sc;
|
||||
}
|
||||
QString st = tc.selectedText();
|
||||
if (arg >= 0) st = sc.second;
|
||||
if (arg >= 0) {
|
||||
st = sc.second;
|
||||
}
|
||||
if (!ok || st.isEmpty()) {
|
||||
return ACEntry();
|
||||
}
|
||||
ok = false;
|
||||
ACList acl(autoCompletitionList(sc.first, sc.second));
|
||||
foreach (const ACSection & i, acl) {
|
||||
foreach (const ACEntry & s, i.second) {
|
||||
for (const ACSection & i : acl) {
|
||||
for (const ACEntry & s : i.second) {
|
||||
QString ts = s.name;
|
||||
//qDebug() << ts << st;
|
||||
if (ts != st) {
|
||||
@@ -1535,14 +1661,18 @@ QCodeEdit::ACEntry QCodeEdit::findEntryOnCursor(QTextCursor tc, int arg, ACClass
|
||||
ts.remove(0, st.size());
|
||||
ts = ts.trimmed();
|
||||
if (!ts.isEmpty()) {
|
||||
if (ts[0] != '(')
|
||||
if (ts[0] != '(') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//qDebug() << s.second << st;
|
||||
if (acc) *acc = ac_classes.value(i.first);
|
||||
if (acc) {
|
||||
*acc = ac_classes.value(i.first);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -1561,8 +1691,9 @@ void QCodeEdit::raiseHelp(QTextCursor tc, int arg) {
|
||||
lbl_help[lhMain]->setText(QString("<html><body>[%1] %2 %3</html></body>").arg(acc.name, e.type, selectArg(e.name, arg)));
|
||||
lbl_help[lhMain]->setIcon(acc.icon);
|
||||
QString hint;
|
||||
if (!e.hint.isEmpty())
|
||||
if (!e.hint.isEmpty()) {
|
||||
hint = QString("<span style=\"font-style:italic;\">%1</span>").arg(e.hint);
|
||||
}
|
||||
lbl_help[lhHint]->setText(hint);
|
||||
lbl_help[lhHint]->setHidden(hint.isEmpty());
|
||||
//qDebug() << "help found" << tc.selectionStart() << tc.selectionEnd();
|
||||
@@ -1596,14 +1727,17 @@ void QCodeEdit::raiseHelp(QTextCursor tc, int arg) {
|
||||
|
||||
|
||||
void QCodeEdit::gotoHelpHRef(QCodeEdit::ACEntry e) {
|
||||
if (e.help_href.isEmpty()) return;
|
||||
if (e.help_href.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
QDesktopServices::openUrl(e.help_href);
|
||||
}
|
||||
|
||||
|
||||
void QCodeEdit::resizeOverlay() {
|
||||
if (overlay)
|
||||
if (overlay) {
|
||||
overlay->setGeometry(ui->textCode->viewport()->geometry());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1622,16 +1756,22 @@ QTextCursor QCodeEdit::functionStart(QTextCursor tc, int * arg) {
|
||||
for (i = tc.position() - 1; i >= 0; --i) {
|
||||
if (doc[i] == ')') bcnt++;
|
||||
if (doc[i] == '(') {
|
||||
if (bcnt == 0)
|
||||
if (bcnt == 0) {
|
||||
break;
|
||||
else
|
||||
} else {
|
||||
bcnt--;
|
||||
}
|
||||
}
|
||||
if (doc[i] == ',' && bcnt == 0) {
|
||||
a++;
|
||||
}
|
||||
//if (doc[i] == '(') bcnt--;
|
||||
if (doc[i] == ',' && bcnt == 0) a++;
|
||||
}
|
||||
if (i < 0) return QTextCursor();
|
||||
if (arg) *arg = a;
|
||||
if (i < 0) {
|
||||
return QTextCursor();
|
||||
}
|
||||
if (arg) {
|
||||
*arg = a;
|
||||
}
|
||||
QTextCursor ret(ui->textCode->document());
|
||||
ret.setPosition(i);
|
||||
//qDebug() << "found" << i << a;
|
||||
@@ -1642,23 +1782,28 @@ QTextCursor QCodeEdit::functionStart(QTextCursor tc, int * arg) {
|
||||
QCodeEdit::ACList QCodeEdit::wordsCompletitionList(const QString & written) const {
|
||||
QCodeEdit::ACList ret;
|
||||
if (!written.isEmpty()) {
|
||||
QTextCursor tc = QTextCursor(ui->textCode->document()->begin()), stc;
|
||||
QTextCursor tc = QTextCursor(ui->textCode->document()->begin());
|
||||
QTextCursor stc;
|
||||
QStringList acwl;
|
||||
tc = QTextCursor(ui->textCode->document()->begin());
|
||||
while (true) {
|
||||
tc = ui->textCode->document()->find(written, tc);
|
||||
if (tc.isNull()) break;
|
||||
if (tc.isNull()) {
|
||||
break;
|
||||
}
|
||||
stc = tc;
|
||||
stc.movePosition(QTextCursor::Left);
|
||||
stc.select(QTextCursor::WordUnderCursor);
|
||||
if (!stc.selectedText().isEmpty() && stc.selectedText().trimmed() != written)
|
||||
if (!stc.selectedText().isEmpty() && stc.selectedText().trimmed() != written) {
|
||||
acwl << stc.selectedText();
|
||||
}
|
||||
}
|
||||
acwl.removeDuplicates();
|
||||
ACSection acl;
|
||||
acl.first = -1;
|
||||
foreach (const QString & s, acwl)
|
||||
for (const QString & s : acwl) {
|
||||
acl.second << ACEntry("", s);
|
||||
}
|
||||
ret << acl;
|
||||
}
|
||||
return ret;
|
||||
@@ -1670,7 +1815,9 @@ QPair<QStringList, QString> QCodeEdit::getScope(QTextCursor tc, bool * ok) {
|
||||
QTextCursor stc = tc;
|
||||
if (tc.isNull()) {
|
||||
completer->hide();
|
||||
if (ok) *ok = false;
|
||||
if (ok) {
|
||||
*ok = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int line = tc.block().firstLineNumber();
|
||||
@@ -1678,7 +1825,9 @@ QPair<QStringList, QString> QCodeEdit::getScope(QTextCursor tc, bool * ok) {
|
||||
if (auto_comp_pl != line) {
|
||||
completer->hide();
|
||||
auto_comp_pl = line;
|
||||
if (ok) *ok = false;
|
||||
if (ok) {
|
||||
*ok = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -1694,8 +1843,9 @@ QPair<QStringList, QString> QCodeEdit::getScope(QTextCursor tc, bool * ok) {
|
||||
written.clear();
|
||||
} else {
|
||||
cpos = skipCWord(doc, spos);
|
||||
if (cpos >= 0)
|
||||
if (cpos >= 0) {
|
||||
written = doc.mid(cpos, spos - cpos).trimmed();
|
||||
}
|
||||
}
|
||||
while (cpos >= 0) {
|
||||
cpos--;
|
||||
@@ -1737,11 +1887,15 @@ void QCodeEdit::invokeAutoCompletition(bool force) {
|
||||
hideHelp();
|
||||
return;
|
||||
}
|
||||
if (word_complete) acl << wordsCompletitionList(scope.second);
|
||||
if (word_complete) {
|
||||
acl << wordsCompletitionList(scope.second);
|
||||
}
|
||||
QFont bf(font());
|
||||
bf.setBold(true);
|
||||
foreach (const ACSection & ac, acl) {
|
||||
if (ac.second.isEmpty()) continue;
|
||||
if (ac.second.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
ACClass acc = ac_classes.value(ac.first);
|
||||
completer->addItems(bf, acc, ac);
|
||||
}
|
||||
@@ -1750,14 +1904,13 @@ void QCodeEdit::invokeAutoCompletition(bool force) {
|
||||
|
||||
|
||||
void QCodeEdit::commitCompletition() {
|
||||
if (completer->currentItem() == 0) return;
|
||||
if (completer->currentItem() == nullptr) return;
|
||||
if (!completer->currentItem()->flags().testFlag(Qt::ItemIsSelectable)) return;
|
||||
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;
|
||||
if (!tc.selectedText().isEmpty()) {
|
||||
// if (tc.selectedText()[0].isSpace()) {
|
||||
if (!tc.selectedText()[0].isLetterOrNumber() && !tc.selectedText()[0].isSpace() && !(tc.selectedText()[0] == '_')) {
|
||||
stc.movePosition(QTextCursor::Left);
|
||||
shifted = true;
|
||||
@@ -1766,20 +1919,25 @@ void QCodeEdit::commitCompletition() {
|
||||
tc.movePosition(QTextCursor::EndOfWord);
|
||||
tc.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
|
||||
}
|
||||
if (!tc.selectedText().isEmpty())
|
||||
if (tc.selectedText()[0].toLatin1() == '(')
|
||||
if (!tc.selectedText().isEmpty()) {
|
||||
if (tc.selectedText()[0].toLatin1() == '(') {
|
||||
ins_br = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ins.contains("("))
|
||||
if (ins.contains("(")) {
|
||||
ins = ins.left(ins.indexOf("(")) + "()";
|
||||
if (!ins_br && ins.endsWith("()"))
|
||||
}
|
||||
if (!ins_br && ins.endsWith("()")) {
|
||||
ins.chop(2);
|
||||
}
|
||||
tc = stc;
|
||||
tc.select(QTextCursor::WordUnderCursor);
|
||||
if (!tc.selectedText().leftJustified(1)[0].isLetterOrNumber() && !(tc.selectedText().leftJustified(1)[0] == '_')) {
|
||||
tc = stc;
|
||||
if (shifted)
|
||||
if (shifted) {
|
||||
tc.movePosition(QTextCursor::Right);
|
||||
}
|
||||
}
|
||||
ui->textCode->setTextCursor(tc);
|
||||
ui->textCode->textCursor().insertText(ins);
|
||||
@@ -1817,7 +1975,9 @@ void QCodeEdit::textEdit_cursorPositionChanged() {
|
||||
es_line.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
|
||||
highlightBrackets();
|
||||
applyExtraSelection();
|
||||
if (timer_blink) killTimer(timer_blink);
|
||||
if (timer_blink) {
|
||||
killTimer(timer_blink);
|
||||
}
|
||||
timer_blink = startTimer(QApplication::cursorFlashTime() / 2);
|
||||
cursor_state = true;
|
||||
createBlockSelection();
|
||||
@@ -1825,7 +1985,9 @@ void QCodeEdit::textEdit_cursorPositionChanged() {
|
||||
|
||||
|
||||
void QCodeEdit::textEdit_textChanged() {
|
||||
if (_replacing) return;
|
||||
if (_replacing) {
|
||||
return;
|
||||
}
|
||||
searchAll();
|
||||
updateLines();
|
||||
}
|
||||
@@ -1863,7 +2025,9 @@ void QCodeEdit::textEdit_selectionChanged() {
|
||||
|
||||
|
||||
void QCodeEdit::textEdit_redoAvailable(bool available) {
|
||||
if (available) cancelBlockSelection();
|
||||
if (available) {
|
||||
cancelBlockSelection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1871,8 +2035,11 @@ void QCodeEdit::setShowSpaces(bool yes) {
|
||||
spaces_ = yes;
|
||||
QTextOption to = ui->textCode->document()->defaultTextOption();
|
||||
QTextOption::Flags tof = to.flags();
|
||||
if (yes) tof |= QTextOption::ShowTabsAndSpaces;
|
||||
else tof &= ~QTextOption::ShowTabsAndSpaces;
|
||||
if (yes) {
|
||||
tof |= QTextOption::ShowTabsAndSpaces;
|
||||
} else {
|
||||
tof &= ~QTextOption::ShowTabsAndSpaces;
|
||||
}
|
||||
to.setFlags(tof);
|
||||
ui->textCode->document()->setDefaultTextOption(to);
|
||||
}
|
||||
@@ -1888,22 +2055,23 @@ void QCodeEdit::search(const QString & t) {
|
||||
ui->comboSearch->setEditText(QString());
|
||||
ui->comboSearch->setEditText(t);
|
||||
ui->comboSearch->setFocus();
|
||||
//searchAll();
|
||||
searchNext(false);
|
||||
}
|
||||
|
||||
|
||||
void QCodeEdit::searchNext(bool next) {
|
||||
if (es_search_list.isEmpty())
|
||||
if (es_search_list.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
cur_search_ind = searchIndFromCursor() + (next ? 1 : 0);
|
||||
moveToSearch();
|
||||
}
|
||||
|
||||
|
||||
void QCodeEdit::searchPrevious() {
|
||||
if (es_search_list.isEmpty())
|
||||
if (es_search_list.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
cur_search_ind = searchIndFromCursor() - 1;
|
||||
moveToSearch();
|
||||
}
|
||||
@@ -1922,9 +2090,15 @@ void QCodeEdit::on_comboSearch_currentTextChanged(const QString & t) {
|
||||
|
||||
|
||||
void QCodeEdit::on_buttonReplace_clicked() {
|
||||
if (es_search_list.isEmpty() || cur_search_ind < 0 || cur_search_ind >= es_search_list.size()) return;
|
||||
if (ui->textCode->textCursor() != es_search_list[cur_search_ind].cursor) return;
|
||||
if (ui->textCode->textCursor().selectedText().size() != es_search_list[cur_search_ind].cursor.selectedText().size()) return;
|
||||
if (es_search_list.isEmpty() || cur_search_ind < 0 || cur_search_ind >= es_search_list.size()) {
|
||||
return;
|
||||
}
|
||||
if (ui->textCode->textCursor() != es_search_list[cur_search_ind].cursor) {
|
||||
return;
|
||||
}
|
||||
if (ui->textCode->textCursor().selectedText().size() != es_search_list[cur_search_ind].cursor.selectedText().size()) {
|
||||
return;
|
||||
}
|
||||
ui->textCode->textCursor().insertText(ui->comboReplace->currentText());
|
||||
}
|
||||
|
||||
@@ -1939,8 +2113,9 @@ void QCodeEdit::on_buttonReplaceAll_clicked() {
|
||||
_replacing = true;
|
||||
QString rt = ui->comboReplace->currentText();
|
||||
textCursor().beginEditBlock();
|
||||
for (int i = es_search_list.size() - 1; i >= 0; --i)
|
||||
for (int i = es_search_list.size() - 1; i >= 0; --i) {
|
||||
es_search_list[i].cursor.insertText(rt);
|
||||
}
|
||||
_replacing = false;
|
||||
textCursor().endEditBlock();
|
||||
textEdit_textChanged();
|
||||
@@ -1950,6 +2125,8 @@ void QCodeEdit::on_buttonReplaceAll_clicked() {
|
||||
|
||||
|
||||
QString QCodeEdit::ACEntry::declaration() const {
|
||||
if (declaration_pos < 0 || declaration_file.isEmpty()) return QString();
|
||||
if (declaration_pos < 0 || declaration_file.isEmpty()) {
|
||||
return QString();
|
||||
}
|
||||
return (declaration_file + ": %1").arg(declaration_pos);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user