Qt 4.8
qtextimagehandler.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 
42 
43 #include "qtextimagehandler_p.h"
44 
45 #include <qapplication.h>
46 #include <qtextformat.h>
47 #include <qpainter.h>
48 #include <qdebug.h>
49 #include <private/qtextengine_p.h>
50 #include <qpalette.h>
51 #include <qtextbrowser.h>
52 #include <qthread.h>
53 
55 
56 // set by the mime source factory in Qt3Compat
58 
60 {
61  QPixmap pm;
62 
63  QString name = format.name();
64  if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
65  name.prepend(QLatin1String("qrc"));
66  QUrl url = QUrl::fromEncoded(name.toUtf8());
68  if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) {
69  pm = qvariant_cast<QPixmap>(data);
70  } else if (data.type() == QVariant::ByteArray) {
71  pm.loadFromData(data.toByteArray());
72  }
73 
74  if (pm.isNull()) {
75  QString context;
76 #ifndef QT_NO_TEXTBROWSER
77  QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());
78  if (browser)
79  context = browser->source().toString();
80 #endif
81  QImage img;
83  img = QTextImageHandler::externalLoader(name, context);
84 
85  if (img.isNull()) { // try direct loading
86  name = format.name(); // remove qrc:/ prefix again
87  if (name.isEmpty() || !img.load(name))
88  return QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/file-16.png"));
89  }
90  pm = QPixmap::fromImage(img);
92  }
93 
94  return pm;
95 }
96 
98 {
99  QPixmap pm;
100 
101  const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
102  const int width = qRound(format.width());
103  const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
104  const int height = qRound(format.height());
105 
106  QSize size(width, height);
107  if (!hasWidth || !hasHeight) {
108  pm = getPixmap(doc, format);
109  if (!hasWidth) {
110  if (!hasHeight)
111  size.setWidth(pm.width());
112  else
113  size.setWidth(qRound(height * (pm.width() / (qreal) pm.height())));
114  }
115  if (!hasHeight) {
116  if (!hasWidth)
117  size.setHeight(pm.height());
118  else
119  size.setHeight(qRound(width * (pm.height() / (qreal) pm.width())));
120  }
121  }
122 
123  qreal scale = 1.0;
124  QPaintDevice *pdev = doc->documentLayout()->paintDevice();
125  if (pdev) {
126  if (pm.isNull())
127  pm = getPixmap(doc, format);
128  if (!pm.isNull())
129  scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
130  }
131  size *= scale;
132 
133  return size;
134 }
135 
137 {
138  QImage image;
139 
140  QString name = format.name();
141  if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
142  name.prepend(QLatin1String("qrc"));
143  QUrl url = QUrl::fromEncoded(name.toUtf8());
145  if (data.type() == QVariant::Image) {
146  image = qvariant_cast<QImage>(data);
147  } else if (data.type() == QVariant::ByteArray) {
148  image.loadFromData(data.toByteArray());
149  }
150 
151  if (image.isNull()) {
152  QString context;
153 #ifndef QT_NO_TEXTBROWSER
154  QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());
155  if (browser)
156  context = browser->source().toString();
157 #endif
159  image = QTextImageHandler::externalLoader(name, context);
160 
161  if (image.isNull()) { // try direct loading
162  name = format.name(); // remove qrc:/ prefix again
163  if (name.isEmpty() || !image.load(name))
164  return QImage(QLatin1String(":/trolltech/styles/commonstyle/images/file-16.png"));
165  }
166  doc->addResource(QTextDocument::ImageResource, url, image);
167  }
168 
169  return image;
170 }
171 
173 {
174  QImage image;
175 
176  const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
177  const int width = qRound(format.width());
178  const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
179  const int height = qRound(format.height());
180 
181  QSize size(width, height);
182  if (!hasWidth || !hasHeight) {
183  image = getImage(doc, format);
184  if (!hasWidth)
185  size.setWidth(image.width());
186  if (!hasHeight)
187  size.setHeight(image.height());
188  }
189 
190  qreal scale = 1.0;
191  QPaintDevice *pdev = doc->documentLayout()->paintDevice();
192  if (pdev) {
193  if (image.isNull())
194  image = getImage(doc, format);
195  if (!image.isNull())
196  scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
197  }
198  size *= scale;
199 
200  return size;
201 }
202 
204  : QObject(parent)
205 {
206 }
207 
209 {
210  Q_UNUSED(posInDocument)
211  const QTextImageFormat imageFormat = format.toImageFormat();
212 
213  if (qApp->thread() != QThread::currentThread())
214  return getImageSize(doc, imageFormat);
215  return getPixmapSize(doc, imageFormat);
216 }
217 
218 void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
219 {
220  Q_UNUSED(posInDocument)
221  const QTextImageFormat imageFormat = format.toImageFormat();
222 
223  if (qApp->thread() != QThread::currentThread()) {
224  const QImage image = getImage(doc, imageFormat);
225  p->drawImage(rect, image, image.rect());
226  } else {
227  const QPixmap pixmap = getPixmap(doc, imageFormat);
228  p->drawPixmap(rect, pixmap, pixmap.rect());
229  }
230 }
231 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QTextImageHandler(QObject *parent=0)
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format)
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition: qpixmap.cpp:2197
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
virtual QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
The intrinsicSize() function returns the size of the text object represented by format in the given d...
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
int logicalDpiY() const
Definition: qpaintdevice.h:96
static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format)
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format)
QString & prepend(QChar c)
Definition: qstring.h:261
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static Q_GUI_EXPORT ExternalImageLoaderFunction externalLoader
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
qreal height() const
Returns the height of the rectangle occupied by the image.
Definition: qtextformat.h:710
QRect rect() const
Returns the enclosing rectangle (0, 0, width(), height()) of the image.
Definition: qimage.cpp:1603
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
QUrl source
the name of the displayed document.
Definition: qtextbrowser.h:62
bool load(QIODevice *device, const char *format)
This function reads a QImage from the given device.
Definition: qimage.cpp:5251
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
Definition: qthread.cpp:419
The QTextImageFormat class provides formatting information for images in a QTextDocument.
Definition: qtextformat.h:694
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
static QImage getImage(QTextDocument *doc, const QTextImageFormat &format)
The QTextFormat class provides formatting information for a QTextDocument.
Definition: qtextformat.h:129
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
#define qApp
const char * name
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
bool loadFromData(const uchar *buf, uint len, const char *format=0, Qt::ImageConversionFlags flags=Qt::AutoColor)
Loads a pixmap from the len first bytes of the given binary data.
Definition: qpixmap.cpp:979
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
QTextImageFormat toImageFormat() const
Returns this format as an image format.
Q_GUI_EXPORT int qt_defaultDpi()
Definition: qfont.cpp:240
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QAbstractTextDocumentLayout * documentLayout() const
Returns the document layout for this document.
The QTextBrowser class provides a rich text browser with hypertext navigation.
Definition: qtextbrowser.h:58
QPaintDevice * paintDevice() const
Returns the paint device used to render the document&#39;s layout.
void addResource(int type, const QUrl &name, const QVariant &resource)
Adds the resource resource to the resource cache, using type and name as identifiers.
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
QRect rect() const
Returns the pixmap&#39;s enclosing rectangle.
Definition: qpixmap.cpp:676
void drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition: qpainter.cpp:5936
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QVariant resource(int type, const QUrl &name) const
Returns data of the specified type from the resource with the given name.
static QUrl fromEncoded(const QByteArray &url)
Parses input and returns the corresponding QUrl.
Definition: qurl.cpp:5964
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
#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
QImage(* ExternalImageLoaderFunction)(const QString &name, const QString &context)
virtual void drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
Draws this text object using the specified painter.
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QString name() const
Returns the name of the image.
Definition: qtextformat.h:702
bool loadFromData(const uchar *buf, int len, const char *format=0)
Loads an image from the first len bytes of the given binary data.
Definition: qimage.cpp:5275
qreal width() const
Returns the width of the rectangle occupied by the image.
Definition: qtextformat.h:706
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571