save/restore collapsed state
git-svn-id: svn://db.shs.com.ru/libs@16 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
|
||||
QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
prev_lc = auto_comp_pl = -1;
|
||||
textCode = textLines = 0;
|
||||
timer = 0;
|
||||
_ignore_focus_out = false;
|
||||
es_line.format.setBackground(QColor(240, 245, 240));
|
||||
es_line.format.setProperty(QTextFormat::FullWidthSelection, true);
|
||||
@@ -52,6 +54,7 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
|
||||
textLines->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
textLines->viewport()->setAutoFillBackground(false);
|
||||
textLines->viewport()->setCursor(Qt::ArrowCursor);
|
||||
textLines->viewport()->installEventFilter(this);
|
||||
textLines->setFixedWidth(textLines->fontMetrics().width(" "));
|
||||
setLayout(new QBoxLayout(QBoxLayout::BottomToTop));
|
||||
layout()->setContentsMargins(0, 0, 0, 0);
|
||||
@@ -164,6 +167,20 @@ QChar QCodeEdit::pairChar(QChar c) {
|
||||
|
||||
|
||||
bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
|
||||
if (textLines) {
|
||||
if (o == textLines->viewport()) {
|
||||
if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonRelease ||
|
||||
e->type() == QEvent::MouseMove || e->type() == QEvent::MouseButtonDblClick) {
|
||||
const_cast<QPoint&>(((QMouseEvent*)e)->pos()) = QPoint(0, ((QMouseEvent*)e)->pos().y());
|
||||
QApplication::sendEvent(textCode->viewport(), e);
|
||||
return true;
|
||||
}
|
||||
if (e->type() == QEvent::Wheel) {
|
||||
QApplication::sendEvent(textCode->viewport(), e);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (o == &completer) {
|
||||
//qDebug() << o << e;
|
||||
if (e->type() == QEvent::WindowActivate)
|
||||
@@ -171,127 +188,137 @@ bool QCodeEdit::eventFilter(QObject * o, QEvent * e) {
|
||||
//qDebug() << e;
|
||||
return QWidget::eventFilter(o, e);
|
||||
}
|
||||
if (o == textCode->viewport()) {
|
||||
if (e->type() == QEvent::MouseButtonPress)
|
||||
completer.hide();
|
||||
return QWidget::eventFilter(o, e);
|
||||
}
|
||||
if (o == textCode) {
|
||||
//qDebug() << o << e;
|
||||
QKeyEvent * ke;
|
||||
QChar kc(0);
|
||||
switch (e->type()) {
|
||||
case QEvent::KeyPress:
|
||||
ke = (QKeyEvent * )e;
|
||||
switch (ke->key()) {
|
||||
case Qt::Key_Space:
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier)) {
|
||||
invokeAutoCompletition(true);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Escape:
|
||||
if (textCode) {
|
||||
if (o == textCode->viewport()) {
|
||||
if (e->type() == QEvent::MouseButtonPress)
|
||||
completer.hide();
|
||||
break;
|
||||
case Qt::Key_Up:
|
||||
if (completer.isVisible()) {
|
||||
previousCompletition();
|
||||
return true;
|
||||
return QWidget::eventFilter(o, e);
|
||||
}
|
||||
if (o == textCode) {
|
||||
//qDebug() << o << e;
|
||||
QKeyEvent * ke;
|
||||
QChar kc(0);
|
||||
switch (e->type()) {
|
||||
case QEvent::KeyPress:
|
||||
ke = (QKeyEvent * )e;
|
||||
switch (ke->key()) {
|
||||
case Qt::Key_Space:
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier)) {
|
||||
invokeAutoCompletition(true);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Escape:
|
||||
completer.hide();
|
||||
break;
|
||||
case Qt::Key_Up:
|
||||
if (completer.isVisible()) {
|
||||
previousCompletition();
|
||||
return true;
|
||||
}
|
||||
completer.hide();
|
||||
if (ke->modifiers().testFlag(Qt::AltModifier)) {
|
||||
copyLineUp();
|
||||
return true;
|
||||
}
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier) && ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
moveLineUp();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Down:
|
||||
if (completer.isVisible()) {
|
||||
nextCompletition();
|
||||
return true;
|
||||
}
|
||||
completer.hide();
|
||||
if (ke->modifiers().testFlag(Qt::AltModifier)) {
|
||||
copyLineDown();
|
||||
return true;
|
||||
}
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier) && ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
moveLineDown();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Home:
|
||||
case Qt::Key_End:
|
||||
case Qt::Key_PageUp:
|
||||
case Qt::Key_PageDown:
|
||||
if (completer.isVisible()) {
|
||||
qApp->sendEvent(&completer, new QKeyEvent(e->type(), ke->key(), ke->modifiers()));
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Left:
|
||||
case Qt::Key_Right:
|
||||
case Qt::Key_Backspace:
|
||||
case Qt::Key_Delete:
|
||||
if (completer.isVisible())
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
break;
|
||||
case Qt::Key_Return:
|
||||
if (completer.isVisible()) {
|
||||
commitCompletition();
|
||||
completer.hide();
|
||||
return true;
|
||||
}
|
||||
if (textCode->textCursor().selectedText().isEmpty())
|
||||
QMetaObject::invokeMethod(this, "autoIndent", Qt::QueuedConnection);
|
||||
break;
|
||||
case Qt::Key_Tab:
|
||||
if (!textCode->textCursor().selectedText().isEmpty()) {
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier))
|
||||
deindent();
|
||||
else
|
||||
indent();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_D:
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier)) {
|
||||
completer.hide();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
completer.hide();
|
||||
if (ke->modifiers().testFlag(Qt::AltModifier)) {
|
||||
copyLineUp();
|
||||
return true;
|
||||
}
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier) && ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
moveLineUp();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Down:
|
||||
if (completer.isVisible()) {
|
||||
nextCompletition();
|
||||
return true;
|
||||
}
|
||||
completer.hide();
|
||||
if (ke->modifiers().testFlag(Qt::AltModifier)) {
|
||||
copyLineDown();
|
||||
return true;
|
||||
}
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier) && ke->modifiers().testFlag(Qt::ShiftModifier)) {
|
||||
moveLineDown();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Home:
|
||||
case Qt::Key_End:
|
||||
case Qt::Key_PageUp:
|
||||
case Qt::Key_PageDown:
|
||||
if (completer.isVisible()) {
|
||||
qApp->sendEvent(&completer, new QKeyEvent(e->type(), ke->key(), ke->modifiers()));
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Qt::Key_Left:
|
||||
case Qt::Key_Right:
|
||||
case Qt::Key_Backspace:
|
||||
case Qt::Key_Delete:
|
||||
if (completer.isVisible())
|
||||
if (!ke->text().isEmpty())
|
||||
kc = ke->text()[0];
|
||||
if (kc == '.') {
|
||||
completer.hide();
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
break;
|
||||
case Qt::Key_Return:
|
||||
if (completer.isVisible()) {
|
||||
commitCompletition();
|
||||
completer.hide();
|
||||
return true;
|
||||
}
|
||||
if (textCode->textCursor().selectedText().isEmpty())
|
||||
QMetaObject::invokeMethod(this, "autoIndent", Qt::QueuedConnection);
|
||||
break;
|
||||
case Qt::Key_Tab:
|
||||
if (!textCode->textCursor().selectedText().isEmpty()) {
|
||||
if (ke->modifiers().testFlag(Qt::ShiftModifier))
|
||||
deindent();
|
||||
else
|
||||
indent();
|
||||
return true;
|
||||
} else {
|
||||
if ((kc.isLetterOrNumber() || kc.toLatin1() == '_') && completer.isVisible())
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
}
|
||||
break;
|
||||
case Qt::Key_D:
|
||||
if (ke->modifiers().testFlag(Qt::ControlModifier)) {
|
||||
completer.hide();
|
||||
return true;
|
||||
case QEvent::FocusOut:
|
||||
if (_ignore_focus_out) {
|
||||
_ignore_focus_out = false;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case QEvent::Hide:
|
||||
case QEvent::HideToParent:
|
||||
case QEvent::MouseButtonPress:
|
||||
//qDebug() << e;
|
||||
completer.hide();
|
||||
default: break;
|
||||
}
|
||||
if (!ke->text().isEmpty())
|
||||
kc = ke->text()[0];
|
||||
if (kc == '.') {
|
||||
completer.hide();
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
} else {
|
||||
if ((kc.isLetterOrNumber() || kc.toLatin1() == '_') && completer.isVisible())
|
||||
QMetaObject::invokeMethod(this, "invokeAutoCompletition", Qt::QueuedConnection, Q_ARG(bool, false));
|
||||
}
|
||||
break;
|
||||
case QEvent::FocusOut:
|
||||
if (_ignore_focus_out) {
|
||||
_ignore_focus_out = false;
|
||||
break;
|
||||
}
|
||||
case QEvent::Hide:
|
||||
case QEvent::HideToParent:
|
||||
case QEvent::MouseButtonPress:
|
||||
//qDebug() << e;
|
||||
completer.hide();
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return QWidget::eventFilter(o, e);
|
||||
}
|
||||
|
||||
|
||||
void QCodeEdit::timerEvent(QTimerEvent * ) {
|
||||
parse();
|
||||
emit parseRequest();
|
||||
killTimer(timer);
|
||||
timer = 0;
|
||||
}
|
||||
|
||||
|
||||
void QCodeEdit::applyExtraSelection() {
|
||||
textCode->setExtraSelections(QList<QTextEdit::ExtraSelection>() << es_line << es_selected << es_custom);
|
||||
}
|
||||
@@ -563,6 +590,8 @@ void QCodeEdit::autoIndent() {
|
||||
|
||||
|
||||
void QCodeEdit::updateLines() {
|
||||
if (timer > 0) killTimer(timer);
|
||||
timer = startTimer(500);
|
||||
textCode->setTabStopWidth(textCode->fontMetrics().width(" "));
|
||||
int lc = textCode->document()->lineCount();
|
||||
if (prev_lc == lc) return;
|
||||
|
||||
Reference in New Issue
Block a user