📄 qtextcontrol.cpp
字号:
return textEdit->loadResource(type, name);#endif return QVariant();}QRectF QTextControlPrivate::rectForPosition(int position) const{ const QTextBlock block = doc->findBlock(position); if (!block.isValid()) return QRectF(); const QAbstractTextDocumentLayout *docLayout = doc->documentLayout(); const QTextLayout *layout = block.layout(); const QPointF layoutPos = docLayout->blockBoundingRect(block).topLeft(); int relativePos = position - block.position(); if (preeditCursor != 0) { int preeditPos = layout->preeditAreaPosition(); if (relativePos == preeditPos) relativePos += preeditCursor; else if (relativePos > preeditPos) relativePos += layout->preeditAreaText().length(); } QTextLine line = layout->lineForTextPosition(relativePos); int cursorWidth; { bool ok = false;#ifndef QT_NO_PROPERTIES cursorWidth = docLayout->property("cursorWidth").toInt(&ok);#endif if (!ok) cursorWidth = 1; } QRectF r; if (line.isValid()) r = QRectF(layoutPos.x() + line.cursorToX(relativePos) - 5 - cursorWidth, layoutPos.y() + line.y(), 2 * cursorWidth + 10, line.ascent() + line.descent()+1.); else r = QRectF(layoutPos.x() - 5 - cursorWidth, layoutPos.y(), 2 * cursorWidth + 10, 10); // #### correct height return r;}static inline bool firstFramePosLessThanCursorPos(QTextFrame *frame, int position){ return frame->firstPosition() < position;}static inline bool cursorPosLessThanLastFramePos(int position, QTextFrame *frame){ return position < frame->lastPosition();}static QRectF boundingRectOfFloatsInSelection(const QTextCursor &cursor){ QRectF r; QTextFrame *frame = cursor.currentFrame(); const QList<QTextFrame *> children = frame->childFrames(); const QList<QTextFrame *>::ConstIterator firstFrame = qLowerBound(children.constBegin(), children.constEnd(), cursor.selectionStart(), firstFramePosLessThanCursorPos); const QList<QTextFrame *>::ConstIterator lastFrame = qUpperBound(children.constBegin(), children.constEnd(), cursor.selectionEnd(), cursorPosLessThanLastFramePos); for (QList<QTextFrame *>::ConstIterator it = firstFrame; it != lastFrame; ++it) { if ((*it)->frameFormat().position() != QTextFrameFormat::InFlow) r |= frame->document()->documentLayout()->frameBoundingRect(*it); } return r;}QRectF QTextControl::selectionRect(const QTextCursor &cursor) const{ Q_D(const QTextControl); QRectF r = d->rectForPosition(cursor.selectionStart()); if (cursor.hasComplexSelection() && cursor.currentTable()) { QTextTable *table = cursor.currentTable(); r = d->doc->documentLayout()->frameBoundingRect(table); /* int firstRow, numRows, firstColumn, numColumns; cursor.selectedTableCells(&firstRow, &numRows, &firstColumn, &numColumns); const QTextTableCell firstCell = table->cellAt(firstRow, firstColumn); const QTextTableCell lastCell = table->cellAt(firstRow + numRows - 1, firstColumn + numColumns - 1); const QAbstractTextDocumentLayout * const layout = doc->documentLayout(); QRectF tableSelRect = layout->blockBoundingRect(firstCell.firstCursorPosition().block()); for (int col = firstColumn; col < firstColumn + numColumns; ++col) { const QTextTableCell cell = table->cellAt(firstRow, col); const qreal y = layout->blockBoundingRect(cell.firstCursorPosition().block()).top(); tableSelRect.setTop(qMin(tableSelRect.top(), y)); } for (int row = firstRow; row < firstRow + numRows; ++row) { const QTextTableCell cell = table->cellAt(row, firstColumn); const qreal x = layout->blockBoundingRect(cell.firstCursorPosition().block()).left(); tableSelRect.setLeft(qMin(tableSelRect.left(), x)); } for (int col = firstColumn; col < firstColumn + numColumns; ++col) { const QTextTableCell cell = table->cellAt(firstRow + numRows - 1, col); const qreal y = layout->blockBoundingRect(cell.lastCursorPosition().block()).bottom(); tableSelRect.setBottom(qMax(tableSelRect.bottom(), y)); } for (int row = firstRow; row < firstRow + numRows; ++row) { const QTextTableCell cell = table->cellAt(row, firstColumn + numColumns - 1); const qreal x = layout->blockBoundingRect(cell.lastCursorPosition().block()).right(); tableSelRect.setRight(qMax(tableSelRect.right(), x)); } r = tableSelRect.toRect(); */ } else if (cursor.hasSelection()) { const int position = cursor.selectionStart(); const int anchor = cursor.selectionEnd(); const QTextBlock posBlock = d->doc->findBlock(position); const QTextBlock anchorBlock = d->doc->findBlock(anchor); if (posBlock == anchorBlock && posBlock.isValid() && posBlock.layout()->lineCount()) { const QTextLine posLine = posBlock.layout()->lineForTextPosition(position - posBlock.position()); const QTextLine anchorLine = anchorBlock.layout()->lineForTextPosition(anchor - anchorBlock.position()); const int firstLine = qMin(posLine.lineNumber(), anchorLine.lineNumber()); const int lastLine = qMax(posLine.lineNumber(), anchorLine.lineNumber()); const QTextLayout *layout = posBlock.layout(); r = QRectF(); for (int i = firstLine; i <= lastLine; ++i) { r |= layout->lineAt(i).rect(); } r.translate(d->doc->documentLayout()->blockBoundingRect(posBlock).topLeft()); } else { QRectF anchorRect = d->rectForPosition(cursor.selectionEnd()); r |= anchorRect; r |= boundingRectOfFloatsInSelection(cursor); QRectF frameRect(d->doc->documentLayout()->frameBoundingRect(cursor.currentFrame())); r.setLeft(frameRect.left()); r.setRight(frameRect.right()); } if (r.isValid()) r.adjust(-1, -1, 1, 1); } return r;}QRectF QTextControl::selectionRect() const{ Q_D(const QTextControl); return selectionRect(d->cursor);}void QTextControlPrivate::mousePressEvent(Qt::MouseButton button, const QPointF &pos, Qt::KeyboardModifiers modifiers, Qt::MouseButtons buttons, const QPoint &globalPos){ Q_Q(QTextControl); if (interactionFlags & Qt::LinksAccessibleByMouse) { anchorOnMousePress = q->anchorAt(pos); if (cursorIsFocusIndicator) { cursorIsFocusIndicator = false; repaintSelection(); cursor.clearSelection(); } } if (!(button & Qt::LeftButton)) return; if (!((interactionFlags & Qt::TextSelectableByMouse) || (interactionFlags & Qt::TextEditable))) return; cursorIsFocusIndicator = false; const QTextCursor oldSelection = cursor; const int oldCursorPos = cursor.position(); mousePressed = true;#ifndef QT_NO_DRAGANDDROP mightStartDrag = false;#endif if (trippleClickTimer.isActive() && ((pos - trippleClickPoint).toPoint().manhattanLength() < QApplication::startDragDistance())) { cursor.movePosition(QTextCursor::StartOfBlock); cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); anchorOnMousePress = QString(); trippleClickTimer.stop(); } else { int cursorPos = doc->documentLayout()->hitTest(pos, Qt::FuzzyHit); if (cursorPos == -1) return;#if !defined(QT_NO_IM) QTextLayout *layout = cursor.block().layout(); if (contextWidget && layout && !layout->preeditAreaText().isEmpty()) { QInputContext *ctx = contextWidget->inputContext(); if (!ctx && contextWidget->parentWidget()) ctx = contextWidget->parentWidget()->inputContext(); if (ctx) { QMouseEvent ev(QEvent::MouseButtonPress, contextWidget->mapFromGlobal(globalPos), globalPos, button, buttons, modifiers); ctx->mouseHandler(cursorPos - cursor.position(), &ev); } if (!layout->preeditAreaText().isEmpty()) return; }#endif if (modifiers == Qt::ShiftModifier) { if (selectedWordOnDoubleClick.hasSelection()) extendWordwiseSelection(cursorPos, pos.x()); else if (selectedLineOnDoubleClick.hasSelection()) extendLinewiseSelection(cursorPos); else setCursorPosition(cursorPos, QTextCursor::KeepAnchor); } else { if (cursor.hasSelection() && !cursorIsFocusIndicator && cursorPos >= cursor.selectionStart() && cursorPos <= cursor.selectionEnd() && doc->documentLayout()->hitTest(pos, Qt::ExactHit) != -1) {#ifndef QT_NO_DRAGANDDROP mightStartDrag = true; dragStartPos = pos.toPoint();#endif return; } setCursorPosition(cursorPos); } } if (interactionFlags & Qt::TextEditable) { q->ensureCursorVisible(); if (cursor.position() != oldCursorPos) emit q->cursorPositionChanged(); _q_updateCurrentCharFormatAndSelection(); } else { if (cursor.position() != oldCursorPos) emit q->cursorPositionChanged(); selectionChanged(); } repaintOldAndNewSelection(oldSelection); hadSelectionOnMousePress = cursor.hasSelection();}void QTextControlPrivate::mouseMoveEvent(Qt::MouseButtons buttons, const QPointF &mousePos){ Q_Q(QTextControl); if (interactionFlags & Qt::LinksAccessibleByMouse) { QString anchor = q->anchorAt(mousePos); if (anchor != highlightedAnchor) { highlightedAnchor = anchor; emit q->linkHovered(anchor); } } if (!(buttons & Qt::LeftButton)) return; if (!((interactionFlags & Qt::TextSelectableByMouse) || (interactionFlags & Qt::TextEditable))) return; if (!(mousePressed || selectedWordOnDoubleClick.hasSelection() || selectedLineOnDoubleClick.hasSelection())) return; const QTextCursor oldSelection = cursor; const int oldCursorPos = cursor.position(); if (mightStartDrag) { if ((mousePos.toPoint() - dragStartPos).manhattanLength() > QApplication::startDragDistance()) startDrag(); return; } const qreal mouseX = qreal(mousePos.x());#if !defined(QT_NO_IM) QTextLayout *layout = cursor.block().layout(); if (layout && !layout->preeditAreaText().isEmpty()) return;#endif int newCursorPos = doc->documentLayout()->hitTest(mousePos, Qt::FuzzyHit); if (newCursorPos == -1) return; if (selectedWordOnDoubleClick.hasSelection()) extendWordwiseSelection(newCursorPos, mouseX); else if (selectedLineOnDoubleClick.hasSelection()) extendLinewiseSelection(newCursorPos); else setCursorPosition(newCursorPos, QTextCursor::KeepAnchor); if (interactionFlags & Qt::TextEditable) { q->ensureCursorVisible(); if (cursor.position() != oldCursorPos) emit q->cursorPositionChanged(); _q_updateCurrentCharFormatAndSelection(); } else { emit q->visibilityRequest(QRectF(mousePos, QSizeF(1, 1))); if (cursor.position() != oldCursorPos) emit q->cursorPositionChanged(); selectionChanged(); } repaintOldAndNewSelection(oldSelection);}void QTextControlPrivate::mouseReleaseEvent(Qt::MouseButton button, const QPointF &pos){ Q_Q(QTextControl); const QTextCursor oldSelection = cursor; const int oldCursorPos = cursor.position();#ifndef QT_NO_DRAGANDDROP if (mightStartDrag && (button & Qt::LeftButton)) { mousePressed = false; setCursorPosition(pos); cursor.clearSelection(); selectionChanged(); }#endif if (mousePressed) { mousePressed = false;#ifndef QT_NO_CLIPBOARD if (interactionFlags & Qt::TextSelectableByMouse) { setClipboardSelection(); selectionChanged(true); } } else if (button == Qt::MidButton && (interactionFlags & Qt::TextEditable) && QApplication::clipboard()->supportsSelection()) { setCursorPosition(pos); const QMimeData *md = QApplication::clipboard()->mimeData(QClipboard::Selection); if (md) q->insertFromMimeData(md);#endif } repaintOldAndNewSelection(oldSelection); if (cursor.position() != oldCursorPos) emit q->cursorPositionChanged(); if (interactionFlags & Qt::LinksAccessibleByMouse) { if (!(button & Qt::LeftButton)) return; const QString anchor = q->anchorAt(pos); if (anchor.isEmpty()) return; if (!cursor.hasSelection() || (anchor == anchorOnMousePress && hadSelectionOnMousePress)) { const int anchorPos = doc->documentLayout()->hitTest(pos, Qt::ExactHit); if (anchorPos != -1) { cursor.setPosition(anchorPos); QString anchor = anchorOnMousePress; anchorOnMousePress = QString(); activateLinkUnderCursor(anchor); } } }}void QTextControlPrivate::mouseDoubleClickEvent(QEvent *e, Qt::MouseButton button, const QPointF &pos){ Q_Q(QTextControl); if (button != Qt::LeftButton || !(interactionFlags & Qt::TextSelectableByMouse)) { e->ignore(); return; }#if !defined(QT_NO_IM) QTextLayout *layout = cursor.block().layout(); if (layout && !layout->preeditAreaText().isEmpty()) return;#endif#ifndef QT_NO_DRAGANDDROP mightStartDrag = false;#endif const QTextCursor oldSelection = cursor; setCursorPosition(pos); QTextLine line = currentTextLine(cursor); if (line.isValid() && line.textLength()) { cursor.select(QTextCursor::WordUnderCursor); selectionChanged();#ifndef QT_NO_CLIPBOARD
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -