QtBase  v6.3.1
qabstracttextdocumentlayout.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
41 #include <qtextformat.h>
42 #include "qtextdocument_p.h"
43 #include "qtextengine_p.h"
44 #include "qtextlist.h"
45 
47 
49 
51 {
52 }
53 
55 {
56 }
57 
399 {
401  d->setDocument(document);
402 }
403 
408  :QObject(p, document)
409 {
411  d->setDocument(document);
412 }
413 
418 {
419 }
420 
431 {
433 
434  QTextObjectInterface *iface = qobject_cast<QTextObjectInterface *>(component);
435  if (!iface)
436  return; // ### print error message on terminal?
437 
440 
442  h.iface = iface;
443  h.component = component;
444  d->handlers.insert(objectType, h);
445 }
446 
454 {
456 
457  const auto it = d->handlers.constFind(objectType);
458  if (it != d->handlers.cend() && (!component || component == it->component)) {
459  if (component)
462  d->handlers.erase(it);
463  }
464 }
465 
470 {
471  Q_D(const QAbstractTextDocumentLayout);
472 
473  QTextObjectHandler handler = d->handlers.value(objectType);
474  if (!handler.component)
475  return nullptr;
476 
477  return handler.iface;
478 }
479 
492 {
494 
496  Q_ASSERT(f.isValid());
497  QTextObjectHandler handler = d->handlers.value(f.objectType());
498  if (!handler.component)
499  return;
500 
501  QSizeF s = handler.iface->intrinsicSize(document(), posInDocument, format);
502  item.setWidth(s.width());
503  item.setAscent(s.height());
504  item.setDescent(0);
505 }
506 
519 {
520  Q_UNUSED(item);
521  Q_UNUSED(posInDocument);
522  Q_UNUSED(format);
523 }
524 
541  int posInDocument, const QTextFormat &format)
542 {
543  Q_UNUSED(item);
545 
547  Q_ASSERT(f.isValid());
548  QTextObjectHandler handler = d->handlers.value(f.objectType());
549  if (!handler.component)
550  return;
551 
552  handler.iface->drawObject(p, rect, document(), posInDocument, format);
553 }
554 
556 {
558  while (it != handlers.end())
559  if ((*it).component == obj)
560  it = handlers.erase(it);
561  else
562  ++it;
563 }
564 
571 {
572  QTextDocumentPrivate *pieceTable = QTextDocumentPrivate::get(qobject_cast<QTextDocument *>(parent()));
573  return pieceTable->find(pos).value()->format;
574 }
575 
582 {
583  QTextDocumentPrivate *pieceTable = QTextDocumentPrivate::get(qobject_cast<QTextDocument *>(parent()));
584  int idx = pieceTable->find(pos).value()->format;
585  return pieceTable->formatCollection()->charFormat(idx);
586 }
587 
588 
589 
594 {
595  Q_D(const QAbstractTextDocumentLayout);
596  return d->document;
597 }
598 
606 {
608  return fmt.anchorHref();
609 }
610 
618 {
620  return fmt.name();
621 }
622 
629 {
630  int cursorPos = hitTest(pos, Qt::ExactHit);
631  if (cursorPos == -1)
632  return QTextFormat();
633 
634  // compensate for preedit in the hit text block
636  while (block.isValid()) {
637  QRectF blockBr = blockBoundingRect(block);
638  if (blockBr.contains(pos)) {
639  QTextLayout *layout = block.layout();
640  int relativeCursorPos = cursorPos - block.position();
641  const int preeditLength = layout ? layout->preeditAreaText().length() : 0;
642  if (preeditLength > 0 && relativeCursorPos > layout->preeditAreaPosition())
643  cursorPos -= qMin(cursorPos - layout->preeditAreaPosition(), preeditLength);
644  break;
645  }
646  block = block.next();
647  }
648 
649  const QTextDocumentPrivate *pieceTable = QTextDocumentPrivate::get(qobject_cast<const QTextDocument *>(parent()));
650  QTextDocumentPrivate::FragmentIterator it = pieceTable->find(cursorPos);
651  return pieceTable->formatCollection()->format(it->format);
652 }
653 
661 {
663  while (block.isValid()) {
664  if (block.blockFormat().marker() != QTextBlockFormat::MarkerType::NoMarker) {
665  QRectF blockBr = blockBoundingRect(block);
666  QTextBlockFormat blockFmt = block.blockFormat();
667  QFontMetrics fm(block.charFormat().font());
668  qreal totalIndent = blockFmt.indent() + blockFmt.leftMargin() + blockFmt.textIndent();
669  if (block.textList())
670  totalIndent += block.textList()->format().indent() * 40;
671  QRectF adjustedBr = blockBr.adjusted(totalIndent - fm.height(), 0, totalIndent - blockBr.width(), fm.height() - blockBr.height());
672  if (adjustedBr.contains(pos)) {
673  //qDebug() << "hit block" << block.text() << blockBr << adjustedBr << "marker" << block.blockFormat().marker()
674  // << "font" << block.charFormat().font() << "adj" << lineHeight << totalIndent;
675  if (block.blockFormat().hasProperty(QTextFormat::BlockMarker))
676  return block;
677  }
678  }
679  block = block.next();
680  }
681  return QTextBlock();
682 }
683 
703 {
705  d->paintDevice = device;
706 }
707 
714 {
715  Q_D(const QAbstractTextDocumentLayout);
716  return d->paintDevice;
717 }
718 
720 
721 #include "moc_qabstracttextdocumentlayout.cpp"
FT_UInt idx
Definition: cffcmap.c:135
The QAbstractTextDocumentLayout class is an abstract base class used to implement custom layouts for ...
virtual void resizeInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format)
QTextFormat formatAt(const QPointF &pos) const
virtual void drawInlineObject(QPainter *painter, const QRectF &rect, QTextInlineObject object, int posInDocument, const QTextFormat &format)
virtual void positionInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format)
void unregisterHandler(int objectType, QObject *component=nullptr)
void registerHandler(int objectType, QObject *component)
virtual QRectF blockBoundingRect(const QTextBlock &block) const =0
QTextBlock blockWithMarkerAt(const QPointF &pos) const
QAbstractTextDocumentLayout(QTextDocument *doc)
the format of the selection
void setPaintDevice(QPaintDevice *device)
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const =0
QString anchorAt(const QPointF &pos) const
QString imageAt(const QPointF &pos) const
QTextObjectInterface * handlerForObject(int objectType) const
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:56
int height() const
iterator begin()
Definition: qhash.h:1155
iterator erase(const_iterator it)
Definition: qhash.h:1172
iterator end() noexcept
Definition: qhash.h:1159
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
QObject * parent() const
Definition: qobject.h:409
void destroyed(QObject *=nullptr)
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:533
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:562
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
constexpr qreal height() const noexcept
Definition: qrect.h:741
constexpr qreal width() const noexcept
Definition: qrect.h:738
bool contains(const QRectF &r) const noexcept
Definition: qrect.cpp:2006
constexpr QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
Definition: qrect.h:822
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument....
Definition: qtextformat.h:640
int indent() const
Definition: qtextformat.h:690
qreal leftMargin() const
Definition: qtextformat.h:676
qreal textIndent() const
Definition: qtextformat.h:686
The QTextBlock class provides a container for text fragments in a QTextDocument. \inmodule QtGui.
Definition: qtextobject.h:193
The QTextCharFormat class provides formatting information for characters in a QTextDocument....
Definition: qtextformat.h:416
QString anchorHref() const
Definition: qtextformat.h:600
The QTextDocument class holds formatted text.
Definition: qtextdocument.h:93
QTextBlock firstBlock() const
QTextFormatCollection * formatCollection()
FragmentMap::ConstIterator FragmentIterator
static const QTextDocumentPrivate * get(const QTextDocument *document)
FragmentIterator find(int pos) const
QTextFormat format(int idx) const
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:84
The QTextFormat class provides formatting information for a QTextDocument. \inmodule QtGui.
Definition: qtextformat.h:126
QTextCharFormat toCharFormat() const
QTextImageFormat toImageFormat() const
The QTextImageFormat class provides formatting information for images in a QTextDocument....
Definition: qtextformat.h:811
QString name() const
Definition: qtextformat.h:818
The QTextInlineObject class represents an inline object in a QAbstractTextDocumentLayout and its impl...
Definition: qtextlayout.h:70
The QTextLayout class is used to lay out and render text. \inmodule QtGui.
Definition: qtextlayout.h:106
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
virtual QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)=0
QDomDocument document
[0]
rect
[4]
JOCTET JCOEFPTR block
Definition: jsimd.h:109
@ ExactHit
Definition: qnamespace.h:228
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
GLfloat GLfloat f
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
GLhandleARB obj
[2]
Definition: qopenglext.h:4164
GLdouble s
[6]
Definition: qopenglext.h:235
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
Q_UNUSED(salary)
[21]
QVBoxLayout * layout
QGraphicsItem * item
QStringList::Iterator it
QTextObjectInterface * iface