Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qabstracttextdocumentlayout.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <qtextformat.h>
6#include "qtextdocument_p.h"
7#include "qtextengine_p.h"
8#include "qtextlist.h"
9
11
13
17
21
367
377
384
395{
397
398 QTextObjectInterface *iface = qobject_cast<QTextObjectInterface *>(component);
399 if (!iface)
400 return; // ### print error message on terminal?
401
404
406 h.iface = iface;
407 h.component = component;
408 d->handlers.insert(objectType, h);
409}
410
418{
420
421 const auto it = d->handlers.constFind(objectType);
422 if (it != d->handlers.cend() && (!component || component == it->component)) {
423 if (component)
426 d->handlers.erase(it);
427 }
428}
429
434{
436
437 QTextObjectHandler handler = d->handlers.value(objectType);
438 if (!handler.component)
439 return nullptr;
440
441 return handler.iface;
442}
443
456{
458
460 Q_ASSERT(f.isValid());
461 QTextObjectHandler handler = d->handlers.value(f.objectType());
462 if (!handler.component)
463 return;
464
465 QSizeF s = handler.iface->intrinsicSize(document(), posInDocument, format);
466 item.setWidth(s.width());
467 item.setAscent(s.height());
468 item.setDescent(0);
469}
470
483{
484 Q_UNUSED(item);
485 Q_UNUSED(posInDocument);
487}
488
505 int posInDocument, const QTextFormat &format)
506{
507 Q_UNUSED(item);
509
511 Q_ASSERT(f.isValid());
512 QTextObjectHandler handler = d->handlers.value(f.objectType());
513 if (!handler.component)
514 return;
515
516 handler.iface->drawObject(p, rect, document(), posInDocument, format);
517}
518
520{
522 while (it != handlers.end())
523 if ((*it).component == obj)
524 it = handlers.erase(it);
525 else
526 ++it;
527}
528
535{
536 QTextDocumentPrivate *pieceTable = QTextDocumentPrivate::get(qobject_cast<QTextDocument *>(parent()));
537 return pieceTable->find(pos).value()->format;
538}
539
546{
547 QTextDocumentPrivate *pieceTable = QTextDocumentPrivate::get(qobject_cast<QTextDocument *>(parent()));
548 int idx = pieceTable->find(pos).value()->format;
549 return pieceTable->formatCollection()->charFormat(idx);
550}
551
552
553
558{
560 return d->document;
561}
562
574
586
593{
594 int cursorPos = hitTest(pos, Qt::ExactHit);
595 if (cursorPos == -1)
596 return QTextFormat();
597
598 // compensate for preedit in the hit text block
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);
608 break;
609 }
610 block = block.next();
611 }
612
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);
616}
617
625{
626 QTextBlock block = document()->firstBlock();
627 while (block.isValid()) {
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)) {
637 //qDebug() << "hit block" << block.text() << blockBr << adjustedBr << "marker" << block.blockFormat().marker()
638 // << "font" << block.charFormat().font() << "adj" << lineHeight << totalIndent;
640 return block;
641 }
642 }
643 block = block.next();
644 }
645 return QTextBlock();
646}
647
671
678{
680 return d->paintDevice;
681}
682
684
685#include "moc_qabstracttextdocumentlayout.cpp"
IOBluetoothDevice * device
virtual void resizeInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format)
Sets the size of the inline object item corresponding to the text format.
QTextFormat formatAt(const QPointF &pos) const
virtual void drawInlineObject(QPainter *painter, const QRectF &rect, QTextInlineObject object, int posInDocument, const QTextFormat &format)
This function is called to draw the inline object, object, with the given painter within the rectangl...
virtual void positionInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format)
Lays out the inline object item using the given text format.
void unregisterHandler(int objectType, QObject *component=nullptr)
QTextDocument * document() const
Returns the text document that this layout is operating on.
QTextCharFormat format(int pos)
Returns the character format that is applicable at the given position.
void registerHandler(int objectType, QObject *component)
Registers the given component as a handler for items of the given objectType.
virtual QRectF blockBoundingRect(const QTextBlock &block) const =0
Returns the bounding rectangle of block.
QTextBlock blockWithMarkerAt(const QPointF &pos) const
QAbstractTextDocumentLayout(QTextDocument *doc)
\variable QAbstractTextDocumentLayout::Selection::format
void setPaintDevice(QPaintDevice *device)
Sets the paint device used for rendering the document's layout to the given device.
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const =0
Returns the cursor position for the given point with the specified accuracy.
QString anchorAt(const QPointF &pos) const
Returns the reference of the anchor the given position, or an empty string if no anchor exists at tha...
QPaintDevice * paintDevice() const
Returns the paint device used to render the document's layout.
QString imageAt(const QPointF &pos) const
QTextObjectInterface * handlerForObject(int objectType) const
Returns a handler for objects of the given objectType.
\reentrant \inmodule QtGui
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1212
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition qhash.h:1288
iterator erase(const_iterator it)
Definition qhash.h:1233
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:299
static bool disconnect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot)
Definition qobject_p.h:328
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition qrect.h:813
const_iterator cend() const noexcept
Definition qset.h:142
const_iterator constFind(const T &value) const
Definition qset.h:161
\inmodule QtCore
Definition qsize.h:208
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
MarkerType marker() const
\reentrant
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
bool isValid() const
Returns true if this text block is valid; otherwise returns false.
QTextBlock next() const
Returns the text block in the document after this block, or an empty text block if this is the last o...
QTextLayout * layout() const
Returns the QTextLayout that is used to lay out and display the block's contents.
int position() const
Returns the index of the block's first character within the document.
QTextCharFormat charFormat() const
Returns the QTextCharFormat that describes the block's character format.
QTextList * textList() const
If the block represents a list item, returns the list that the item belongs to; otherwise returns \nu...
QString anchorHref() const
Returns the text format's hypertext link, or an empty string if none has been set.
QFont font() const
Returns the font for this character format.
QTextFormatCollection * formatCollection()
static const QTextDocumentPrivate * get(const QTextDocument *document)
FragmentMap::ConstIterator FragmentIterator
FragmentIterator find(int pos) const
\reentrant \inmodule QtGui
QTextBlock firstBlock() const
QTextFormat format(int idx) const
QTextCharFormat charFormat(int index) const
\reentrant
Definition qtextformat.h:90
QTextCharFormat toCharFormat() const
Returns this format as a character format.
QTextImageFormat toImageFormat() const
Returns this format as an image format.
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false.
QString name() const
Returns the name of the image.
\reentrant
Definition qtextlayout.h:70
int indent() const
Returns the list format's indentation.
QTextListFormat format() const
Returns the list's format.
Definition qtextlist.h:37
The QTextObjectInterface class allows drawing of custom text objects in \l{QTextDocument}s.
virtual void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)=0
Draws this text object using the specified painter.
virtual QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)=0
The intrinsicSize() function returns the size of the text object represented by format in the given d...
virtual ~QTextObjectInterface()
Destroys this QTextObjectInterface.
QSet< QString >::iterator it
rect
[4]
Combined button and popup list for selecting options.
@ ExactHit
Definition qnamespace.h:203
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLfloat GLfloat f
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
GLhandleARB obj
[2]
GLdouble s
[6]
Definition qopenglext.h:235
GLfloat GLfloat p
[1]
static qreal component(const QPointF &point, unsigned int i)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QVideoFrameFormat::PixelFormat fmt
QVBoxLayout * layout
QGraphicsItem * item