📄 q3textedit.cpp
字号:
redo(); break; case Qt::Key_K: doKeyboardAction(ActionKill); break;#if defined(Q_WS_WIN) case Qt::Key_Insert: copy(); break; case Qt::Key_Delete: del(); break;#endif default: unknownKey = false; break; } } else { unknownKey = true; } } } emit cursorPositionChanged(cursor); emit cursorPositionChanged(cursor->paragraph()->paragId(), cursor->index()); if (clearUndoRedoInfo) clearUndoRedo(); changeIntervalTimer->start(100, true); if (unknownKey) e->ignore();}/*! \reimp*/void Q3TextEdit::inputMethodEvent(QInputMethodEvent *e){ if (isReadOnly()) { e->ignore(); return; } if (hasSelectedText()) removeSelectedText(); clearUndoRedo(); undoRedoInfo.type = UndoRedoInfo::IME; bool oldupdate = updatesEnabled(); if (oldupdate) setUpdatesEnabled(false); bool sigs_blocked = signalsBlocked(); blockSignals(true); const int preeditSelectionBase = 31900; for (int i = 0; i < d->numPreeditSelections; ++i) doc->removeSelection(preeditSelectionBase + i); d->numPreeditSelections = 0; if (d->preeditLength > 0 && cursor->paragraph()) { cursor->setIndex(d->preeditStart); cursor->paragraph()->remove(d->preeditStart, d->preeditLength); d->preeditStart = d->preeditLength = -1; } if (!e->commitString().isEmpty() || e->replacementLength()) { int c = cursor->index(); // cursor position after insertion of commit string if (e->replacementStart() <= 0) c += e->commitString().length() + qMin(-e->replacementStart(), e->replacementLength()); cursor->setIndex(cursor->index() + e->replacementStart()); doc->setSelectionStart(Q3TextDocument::Standard, *cursor); cursor->setIndex(cursor->index() + e->replacementLength()); doc->setSelectionEnd(Q3TextDocument::Standard, *cursor); removeSelectedText(); if (undoRedoInfo.type == UndoRedoInfo::IME) undoRedoInfo.type = UndoRedoInfo::Invalid; insert(e->commitString()); undoRedoInfo.type = UndoRedoInfo::IME; cursor->setIndex(c); } if (!e->preeditString().isEmpty()) { d->preeditStart = cursor->index(); d->preeditLength = e->preeditString().length(); insert(e->preeditString()); cursor->setIndex(d->preeditStart); Q3TextCursor c = *cursor; for (int i = 0; i < e->attributes().size(); ++i) { const QInputMethodEvent::Attribute &a = e->attributes().at(i); if (a.type == QInputMethodEvent::Cursor) cursor->setIndex(cursor->index() + a.start); else if (a.type != QInputMethodEvent::TextFormat) continue; QTextCharFormat f = qvariant_cast<QTextFormat>(a.value).toCharFormat(); if (f.isValid()) { Q3TextCursor c2 = c; c2.setIndex(c.index() + a.start); doc->setSelectionStart(preeditSelectionBase + d->numPreeditSelections, c2); c2.setIndex(c.index() + a.start + a.length); doc->setSelectionEnd(preeditSelectionBase + d->numPreeditSelections, c2); QColor c = f.hasProperty(QTextFormat::BackgroundBrush) ? f.background().color() : QColor(); doc->setSelectionColor(preeditSelectionBase + d->numPreeditSelections, c); c = f.hasProperty(QTextFormat::ForegroundBrush) ? f.foreground().color() : QColor(); doc->setSelectionTextColor(preeditSelectionBase + d->numPreeditSelections, c); if (f.fontUnderline()) { Q3TextParagraph *par = cursor->paragraph(); Q3TextFormat f(*par->string()->at(d->preeditStart).format()); f.setUnderline(true); Q3TextFormat *f2 = doc->formatCollection()->format(&f); par->setFormat(d->preeditStart + a.start, a.length, f2); } ++d->numPreeditSelections; } } } else { undoRedoInfo.type = UndoRedoInfo::Invalid; } blockSignals(sigs_blocked); if (oldupdate) setUpdatesEnabled(true); if (!e->commitString().isEmpty()) emit textChanged(); repaintChanged();}static bool qtextedit_ignore_readonly = false;/*! Executes keyboard action \a action. This is normally called by a key event handler.*/void Q3TextEdit::doKeyboardAction(Q3TextEdit::KeyboardAction action){ if (isReadOnly() && !qtextedit_ignore_readonly) return; if (cursor->nestedDepth() != 0) return; lastFormatted = cursor->paragraph(); drawCursor(false); bool doUpdateCurrentFormat = true; switch (action) { case ActionWordDelete: case ActionDelete: if (action == ActionDelete && !cursor->atParagEnd()) { if (undoEnabled) { checkUndoRedoInfo(UndoRedoInfo::Delete); if (!undoRedoInfo.valid()) { undoRedoInfo.id = cursor->paragraph()->paragId(); undoRedoInfo.index = cursor->index(); undoRedoInfo.d->text.clear(); } int idx = cursor->index(); do { undoRedoInfo.d->text.insert(undoRedoInfo.d->text.length(), cursor->paragraph()->at(idx++), true); } while (!cursor->paragraph()->string()->validCursorPosition(idx)); } cursor->remove(); } else { clearUndoRedo(); doc->setSelectionStart(Q3TextDocument::Temp, *cursor); if (action == ActionWordDelete && !cursor->atParagEnd()) { cursor->gotoNextWord(); } else { cursor->gotoNextLetter(); } doc->setSelectionEnd(Q3TextDocument::Temp, *cursor); removeSelectedText(Q3TextDocument::Temp); } break; case ActionWordBackspace: case ActionBackspace: if (textFormat() == Qt::RichText && (cursor->paragraph()->isListItem() || cursor->paragraph()->listDepth()) && cursor->index() == 0) { if (undoEnabled) { clearUndoRedo(); undoRedoInfo.type = UndoRedoInfo::Style; undoRedoInfo.id = cursor->paragraph()->paragId(); undoRedoInfo.eid = undoRedoInfo.id; undoRedoInfo.styleInformation = Q3TextStyleCommand::readStyleInformation(doc, undoRedoInfo.id, undoRedoInfo.eid); } int ldepth = cursor->paragraph()->listDepth(); if (cursor->paragraph()->isListItem() && ldepth == 1) { cursor->paragraph()->setListItem(false); } else if (qMax(ldepth, 1) == 1) { cursor->paragraph()->setListItem(false); cursor->paragraph()->setListDepth(0); } else { cursor->paragraph()->setListDepth(ldepth - 1); } clearUndoRedo(); lastFormatted = cursor->paragraph(); repaintChanged(); drawCursor(true); return; } if (action == ActionBackspace && !cursor->atParagStart()) { if (undoEnabled) { checkUndoRedoInfo(UndoRedoInfo::Delete); if (!undoRedoInfo.valid()) { undoRedoInfo.id = cursor->paragraph()->paragId(); undoRedoInfo.index = cursor->index(); undoRedoInfo.d->text.clear(); } undoRedoInfo.d->text.insert(0, cursor->paragraph()->at(cursor->index()-1), true); undoRedoInfo.index = cursor->index()-1; } cursor->removePreviousChar(); lastFormatted = cursor->paragraph(); } else if (cursor->paragraph()->prev() || (action == ActionWordBackspace && !cursor->atParagStart())) { clearUndoRedo(); doc->setSelectionStart(Q3TextDocument::Temp, *cursor); if (action == ActionWordBackspace && !cursor->atParagStart()) { cursor->gotoPreviousWord(); } else { cursor->gotoPreviousLetter(); } doc->setSelectionEnd(Q3TextDocument::Temp, *cursor); removeSelectedText(Q3TextDocument::Temp); } break; case ActionReturn: if (undoEnabled) { checkUndoRedoInfo(UndoRedoInfo::Return); if (!undoRedoInfo.valid()) { undoRedoInfo.id = cursor->paragraph()->paragId(); undoRedoInfo.index = cursor->index(); undoRedoInfo.d->text.clear(); } undoRedoInfo.d->text += QString(QLatin1Char('\n')); } cursor->splitAndInsertEmptyParagraph(); if (cursor->paragraph()->prev()) { lastFormatted = cursor->paragraph()->prev(); lastFormatted->invalidate(0); } doUpdateCurrentFormat = false; break; case ActionKill: clearUndoRedo(); doc->setSelectionStart(Q3TextDocument::Temp, *cursor); if (cursor->atParagEnd()) cursor->gotoNextLetter(); else cursor->setIndex(cursor->paragraph()->length() - 1); doc->setSelectionEnd(Q3TextDocument::Temp, *cursor); removeSelectedText(Q3TextDocument::Temp); break; } formatMore(); repaintChanged(); ensureCursorVisible(); drawCursor(true); if (doUpdateCurrentFormat) updateCurrentFormat(); setModified(); emit textChanged();}void Q3TextEdit::readFormats(Q3TextCursor &c1, Q3TextCursor &c2, Q3TextString &text, bool fillStyles){#ifndef QT_NO_DATASTREAM QDataStream styleStream(&undoRedoInfo.styleInformation, IO_WriteOnly);#endif c2.restoreState(); c1.restoreState(); int lastIndex = text.length(); if (c1.paragraph() == c2.paragraph()) { for (int i = c1.index(); i < c2.index(); ++i) text.insert(lastIndex + i - c1.index(), c1.paragraph()->at(i), true);#ifndef QT_NO_DATASTREAM if (fillStyles) { styleStream << (int) 1; c1.paragraph()->writeStyleInformation(styleStream); }#endif } else { int i; for (i = c1.index(); i < c1.paragraph()->length()-1; ++i) text.insert(lastIndex++, c1.paragraph()->at(i), true); int num = 2; // start and end, being different text += QString(QLatin1Char('\n')); lastIndex++; if (c1.paragraph()->next() != c2.paragraph()) { num += text.appendParagraphs(c1.paragraph()->next(), c2.paragraph()); lastIndex = text.length(); } for (i = 0; i < c2.index(); ++i) text.insert(i + lastIndex, c2.paragraph()->at(i), true);#ifndef QT_NO_DATASTREAM if (fillStyles) { styleStream << num; for (Q3TextParagraph *p = c1.paragraph(); --num >= 0; p = p->next()) p->writeStyleInformation(styleStream); }#endif }}/*! Removes the selection \a selNum (by default 0). This does not remove the selected text. \sa removeSelectedText()*/void Q3TextEdit::removeSelection(int selNum){ doc->removeSelection(selNum); repaintChanged();}/*! Deletes the text of selection \a selNum (by default, the default selection, 0). If there is no selected text nothing happens. \sa selectedText removeSelection()*/void Q3TextEdit::removeSelectedText(int selNum){ Q3TextCursor c1 = doc->selectionStartCursor(selNum); c1.restoreState(); Q3TextCursor c2 = doc->selectionEndCursor(selNum); c2.restoreState(); // ### no support for editing tables yet, plus security for broken selections if (c1.nestedDepth() || c2.nestedDepth()) return; for (int i = 0; i < (int)doc->numSelections(); ++i) { if (i == selNum) continue; doc->removeSelection(i); } drawCursor(false); if (undoEnabled) { checkUndoRedoInfo(UndoRedoInfo::RemoveSelected); if (!undoRedoInfo.valid()) { doc->selectionStart(selNum, undoRedoInfo.id, undoRedoInfo.index); undoRedoInfo.d->text.clear(); } readFormats(c1, c2, undoRedoInfo.d->text, true); } doc->removeSelectedText(selNum, cursor); if (cursor->isValid()) { lastFormatted = 0; // make sync a noop ensureCursorVisible(); lastFormatted = cursor->paragraph(); formatMore(); repaintContents(); ensureCursorVisible(); drawCursor(true); clearUndoRedo();#if defined(Q_WS_WIN)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -