fcc13a58ffe7d6f888ddc87625e2295d758c1b90
[profile/ivi/qtbase.git] / src / gui / text / qtextimagehandler.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include "qtextimagehandler_p.h"
44
45 #include <qcoreapplication.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 <qthread.h>
52
53 QT_BEGIN_NAMESPACE
54
55 // set by the mime source factory in Qt3Compat
56 QTextImageHandler::ExternalImageLoaderFunction QTextImageHandler::externalLoader = 0;
57
58 static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format)
59 {
60     QPixmap pm;
61
62     QString name = format.name();
63     if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
64         name.prepend(QLatin1String("qrc"));
65     QUrl url = QUrl::fromEncoded(name.toUtf8());
66     const QVariant data = doc->resource(QTextDocument::ImageResource, url);
67     if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) {
68         pm = qvariant_cast<QPixmap>(data);
69     } else if (data.type() == QVariant::ByteArray) {
70         pm.loadFromData(data.toByteArray());
71     }
72
73     if (pm.isNull()) {
74         QString context;
75 #if 0
76         // ### Qt5
77         QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());
78         if (browser)
79             context = browser->source().toString();
80 #endif
81         QImage img;
82         if (QTextImageHandler::externalLoader)
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);
91         doc->addResource(QTextDocument::ImageResource, url, pm);
92     }
93
94     return pm;
95 }
96
97 static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format)
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
136 static QImage getImage(QTextDocument *doc, const QTextImageFormat &format)
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());
144     const QVariant data = doc->resource(QTextDocument::ImageResource, url);
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
154 #if 0
155         // ### Qt5
156         QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());
157         if (browser)
158             context = browser->source().toString();
159 #endif
160         if (QTextImageHandler::externalLoader)
161             image = QTextImageHandler::externalLoader(name, context);
162
163         if (image.isNull()) { // try direct loading
164             name = format.name(); // remove qrc:/ prefix again
165             if (name.isEmpty() || !image.load(name))
166                 return QImage(QLatin1String(":/trolltech/styles/commonstyle/images/file-16.png"));
167         }
168         doc->addResource(QTextDocument::ImageResource, url, image);
169     }
170
171     return image;
172 }
173
174 static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format)
175 {
176     QImage image;
177
178     const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
179     const int width = qRound(format.width());
180     const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
181     const int height = qRound(format.height());
182
183     QSize size(width, height);
184     if (!hasWidth || !hasHeight) {
185         image = getImage(doc, format);
186         if (!hasWidth)
187             size.setWidth(image.width());
188         if (!hasHeight)
189             size.setHeight(image.height());
190     }
191
192     qreal scale = 1.0;
193     QPaintDevice *pdev = doc->documentLayout()->paintDevice();
194     if (pdev) {
195         if (image.isNull())
196             image = getImage(doc, format);
197         if (!image.isNull())
198             scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
199     }
200     size *= scale;
201
202     return size;
203 }
204
205 QTextImageHandler::QTextImageHandler(QObject *parent)
206     : QObject(parent)
207 {
208 }
209
210 QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
211 {
212     Q_UNUSED(posInDocument)
213     const QTextImageFormat imageFormat = format.toImageFormat();
214
215     if (QCoreApplication::instance()->thread() != QThread::currentThread())
216         return getImageSize(doc, imageFormat);
217     return getPixmapSize(doc, imageFormat);
218 }
219
220 void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
221 {
222     Q_UNUSED(posInDocument)
223         const QTextImageFormat imageFormat = format.toImageFormat();
224
225     if (QCoreApplication::instance()->thread() != QThread::currentThread()) {
226         const QImage image = getImage(doc, imageFormat);
227         p->drawImage(rect, image, image.rect());
228     } else {
229         const QPixmap pixmap = getPixmap(doc, imageFormat);
230         p->drawPixmap(rect, pixmap, pixmap.rect());
231     }
232 }
233
234 QT_END_NAMESPACE