361QAbstractTextDocumentLayout::QAbstractTextDocumentLayout(QTextDocument *document)
362 : QObject(*
new QAbstractTextDocumentLayoutPrivate, document)
364 Q_D(QAbstractTextDocumentLayout);
365 d->setDocument(document);
394void QAbstractTextDocumentLayout::registerHandler(
int objectType, QObject *component)
396 Q_D(QAbstractTextDocumentLayout);
398 QTextObjectInterface *iface = qobject_cast<QTextObjectInterface *>(component);
402 QObjectPrivate::connect(component, &QObject::destroyed, d,
403 &QAbstractTextDocumentLayoutPrivate::_q_handlerDestroyed);
405 QTextObjectHandler h;
407 h.component = component;
408 d->handlers.insert(objectType, h);
417void QAbstractTextDocumentLayout::unregisterHandler(
int objectType, QObject *component)
419 Q_D(QAbstractTextDocumentLayout);
421 const auto it = d->handlers.constFind(objectType);
422 if (it != d->handlers.cend() && (!component || component == it->component)) {
424 QObjectPrivate::disconnect(component, &QObject::destroyed, d,
425 &QAbstractTextDocumentLayoutPrivate::_q_handlerDestroyed);
426 d->handlers.erase(it);
455void QAbstractTextDocumentLayout::resizeInlineObject(QTextInlineObject item,
int posInDocument,
const QTextFormat &format)
457 Q_D(QAbstractTextDocumentLayout);
459 QTextCharFormat f = format.toCharFormat();
460 Q_ASSERT(f.isValid());
461 QTextObjectHandler handler = d->handlers.value(f.objectType());
462 if (!handler.component)
465 QSizeF s = handler.iface->intrinsicSize(document(), posInDocument, format);
466 item.setWidth(s.width());
467 item.setAscent(s.height());
504void QAbstractTextDocumentLayout::drawInlineObject(QPainter *p,
const QRectF &rect, QTextInlineObject item,
505 int posInDocument,
const QTextFormat &format)
508 Q_D(QAbstractTextDocumentLayout);
510 QTextCharFormat f = format.toCharFormat();
511 Q_ASSERT(f.isValid());
512 QTextObjectHandler handler = d->handlers.value(f.objectType());
513 if (!handler.component)
516 handler.iface->drawObject(p, rect, document(), posInDocument, format);
545QTextCharFormat QAbstractTextDocumentLayout::format(
int pos)
547 QTextDocumentPrivate *pieceTable = QTextDocumentPrivate::get(qobject_cast<QTextDocument *>(parent()));
548 int idx = pieceTable->find(pos).value()->format;
549 return pieceTable->formatCollection()->charFormat(idx);
592QTextFormat QAbstractTextDocumentLayout::formatAt(
const QPointF &pos)
const
594 int cursorPos = hitTest(pos, Qt::ExactHit);
596 return QTextFormat();
599 QTextBlock block = document()->firstBlock();
600 while (block.isValid()) {
601 QRectF blockBr = blockBoundingRect(block);
602 if (blockBr.contains(pos)) {
603 QTextLayout *layout = block.layout();
604 int relativeCursorPos = cursorPos - block.position();
605 const int preeditLength = layout ? layout->preeditAreaText().size() : 0;
606 if (preeditLength > 0 && relativeCursorPos > layout->preeditAreaPosition())
607 cursorPos -= qMin(cursorPos - layout->preeditAreaPosition(), preeditLength);
610 block = block.next();
613 const QTextDocumentPrivate *pieceTable = QTextDocumentPrivate::get(qobject_cast<
const QTextDocument *>(parent()));
614 QTextDocumentPrivate::FragmentIterator it = pieceTable->find(cursorPos);
615 return pieceTable->formatCollection()->format(it->format);
624QTextBlock QAbstractTextDocumentLayout::blockWithMarkerAt(
const QPointF &pos)
const
626 QTextBlock block = document()->firstBlock();
627 while (block.isValid()) {
628 if (block.blockFormat().marker() != QTextBlockFormat::MarkerType::NoMarker) {
629 QRectF blockBr = blockBoundingRect(block);
630 QTextBlockFormat blockFmt = block.blockFormat();
631 QFontMetrics fm(block.charFormat().font());
632 qreal totalIndent = blockFmt.indent() + blockFmt.leftMargin() + blockFmt.textIndent();
633 if (block.textList())
634 totalIndent += block.textList()->format().indent() * 40;
635 QRectF adjustedBr = blockBr.adjusted(totalIndent - fm.height(), 0, totalIndent - blockBr.width(), fm.height() - blockBr.height());
636 if (adjustedBr.contains(pos)) {
639 if (block.blockFormat().hasProperty(QTextFormat::BlockMarker))
643 block = block.next();