Qt 4.8
qabstracttextdocumentlayout.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 #include <qtextformat.h>
44 #include "qtextdocument_p.h"
45 #include "qtextengine_p.h"
46 
48 
50 
444 {
446  d->setDocument(document);
447 }
448 
453  :QObject(p, document)
454 {
456  d->setDocument(document);
457 }
458 
463 {
464 }
465 
476 {
478 
480  if (!iface)
481  return; // ### print error message on terminal?
482 
483  connect(component, SIGNAL(destroyed(QObject*)), this, SLOT(_q_handlerDestroyed(QObject*)));
484 
486  h.iface = iface;
487  h.component = component;
488  d->handlers.insert(formatType, h);
489 }
490 
495 {
497 
498  QTextObjectHandler handler = d->handlers.value(objectType);
499  if (!handler.component)
500  return 0;
501 
502  return handler.iface;
503 }
504 
517 {
519 
520  QTextCharFormat f = format.toCharFormat();
521  Q_ASSERT(f.isValid());
522  QTextObjectHandler handler = d->handlers.value(f.objectType());
523  if (!handler.component)
524  return;
525 
526  QSizeF s = handler.iface->intrinsicSize(document(), posInDocument, format);
527  item.setWidth(s.width());
528  item.setAscent(s.height() - 1);
529  item.setDescent(0);
530 }
531 
544 {
545  Q_UNUSED(item);
546  Q_UNUSED(posInDocument);
547  Q_UNUSED(format);
548 }
549 
566  int posInDocument, const QTextFormat &format)
567 {
568  Q_UNUSED(item);
570 
571  QTextCharFormat f = format.toCharFormat();
572  Q_ASSERT(f.isValid());
573  QTextObjectHandler handler = d->handlers.value(f.objectType());
574  if (!handler.component)
575  return;
576 
577  handler.iface->drawObject(p, rect, document(), posInDocument, format);
578 }
579 
581 {
582  HandlerHash::Iterator it = handlers.begin();
583  while (it != handlers.end())
584  if ((*it).component == obj)
585  it = handlers.erase(it);
586  else
587  ++it;
588 }
589 
599 {
600  QTextDocumentPrivate *pieceTable = qobject_cast<QTextDocument *>(parent())->docHandle();
601  return pieceTable->find(pos).value()->format;
602 }
603 
613 {
614  QTextDocumentPrivate *pieceTable = qobject_cast<QTextDocument *>(parent())->docHandle();
615  int idx = pieceTable->find(pos).value()->format;
616  return pieceTable->formatCollection()->charFormat(idx);
617 }
618 
619 
620 
625 {
627  return d->document;
628 }
629 
640 {
641  int cursorPos = hitTest(pos, Qt::ExactHit);
642  if (cursorPos == -1)
643  return QString();
644 
645  QTextDocumentPrivate *pieceTable = qobject_cast<const QTextDocument *>(parent())->docHandle();
646  QTextDocumentPrivate::FragmentIterator it = pieceTable->find(cursorPos);
647  QTextCharFormat fmt = pieceTable->formatCollection()->charFormat(it->format);
648  return fmt.anchorHref();
649 }
650 
673 {
675  d->paintDevice = device;
676 }
677 
684 {
686  return d->paintDevice;
687 }
688 
690 
691 #include "moc_qabstracttextdocumentlayout.cpp"
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
double d
Definition: qnumeric_p.h:62
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
void registerHandler(int objectType, QObject *component)
Registers the given component as a handler for items of the given objectType.
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QTextCharFormat format(int pos)
Returns the character format that is applicable at the given position.
#define it(className, varName)
QTextObjectInterface * handlerForObject(int objectType) const
Returns a handler for objects of the given objectType.
virtual void resizeInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format)
Sets the size of the inline object item corresponding to the text format.
qreal width() const
Returns the width.
Definition: qsize.h:284
QTextObjectInterface * iface
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...
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
qreal height() const
Returns the height.
Definition: qsize.h:287
QTextDocument * document() const
Returns the text document that this layout is operating on.
QString anchorHref() const
Returns the text format&#39;s hypertext link, or an empty string if none has been set.
Definition: qtextformat.h:506
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
bool isValid() const
Returns true if this character format is valid; otherwise returns false.
Definition: qtextformat.h:397
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...
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
FragmentMap::ConstIterator FragmentIterator
void setDescent(qreal d)
Sets the inline object&#39;s decent to d.
QAbstractTextDocumentLayout(QTextDocument *doc)
Creates a new text document layout for the given document.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
FragmentIterator find(int pos) const
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
int objectType() const
Returns the text format&#39;s object type.
Definition: qtextformat.h:315
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
The QTextFormat class provides formatting information for a QTextDocument.
Definition: qtextformat.h:129
QTextFormatCollection * formatCollection()
static qreal component(const QPointF &point, unsigned int i)
The QTextObjectInterface class allows drawing of custom text objects in QTextDocument.
void setWidth(qreal w)
Sets the inline object&#39;s width to w.
int formatIndex(int pos)
Returns the index of the format at position pos.
The QTextInlineObject class represents an inline object in a QTextLayout.
Definition: qtextlayout.h:69
void setAscent(qreal a)
Sets the inline object&#39;s ascent to a.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QPaintDevice * paintDevice() const
Returns the paint device used to render the document&#39;s layout.
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
void setPaintDevice(QPaintDevice *device)
Sets the paint device used for rendering the document&#39;s layout to the given device.
QTextCharFormat toCharFormat() const
Returns this format as a character format.
virtual void positionInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format)
Lays out the inline object item using the given text format.
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const =0
Returns the cursor postion for the given point with the specified accuracy.
QImageIOHandler * handler
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473
The QAbstractTextDocumentLayout class is an abstract base class used to implement custom layouts for ...