Qt 4.8
qdeclarativefontloader.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 QtDeclarative 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 #include "private/qdeclarativefontloader_p.h"
43 
44 #include <qdeclarativecontext.h>
45 #include <qdeclarativeengine.h>
46 
47 #include <QStringList>
48 #include <QUrl>
49 #include <QDebug>
50 #include <QNetworkRequest>
51 #include <QNetworkReply>
52 #include <QFontDatabase>
53 
54 #include <private/qobject_p.h>
55 #include <private/qdeclarativeengine_p.h>
56 #include <qdeclarativeinfo.h>
57 
59 
60 #define FONTLOADER_MAXIMUM_REDIRECT_RECURSION 16
61 
63 {
65 
66 public:
67  QDeclarativeFontObject(int _id = -1);
68 
69  void download(const QUrl &url, QNetworkAccessManager *manager);
70 
71 Q_SIGNALS:
73 
74 private Q_SLOTS:
75  void replyFinished();
76 
77 public:
78  int id;
79 
80 private:
83 
85 };
86 
88  : QObject(0), id(_id), reply(0), redirectCount(0) {}
89 
90 
92 {
93  QNetworkRequest req(url);
95  reply = manager->get(req);
96  QObject::connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
97 }
98 
100 {
101  if (reply) {
102  redirectCount++;
105  if (redirect.isValid()) {
106  QUrl url = reply->url().resolved(redirect.toUrl());
107  QNetworkAccessManager *manager = reply->manager();
108  reply->deleteLater();
109  reply = 0;
110  download(url, manager);
111  return;
112  }
113  }
114  redirectCount = 0;
115 
116  if (!reply->error()) {
118  if (id != -1)
120  else
122  } else {
124  }
125  reply->deleteLater();
126  reply = 0;
127  }
128 }
129 
130 
132 {
134 
135 public:
137 
142 };
143 
145 
176  : QObject(*(new QDeclarativeFontLoaderPrivate), parent)
177 {
178 }
179 
181 {
182 }
183 
192 {
194  return d->url;
195 }
196 
198 {
200  if (url == d->url)
201  return;
202  d->url = qmlContext(this)->resolvedUrl(url);
204 
205 #ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
207  if (!localFile.isEmpty()) {
208  if (!d->fonts.contains(d->url)) {
209  int id = QFontDatabase::addApplicationFont(localFile);
210  if (id != -1) {
213  d->fonts[d->url] = fo;
214  } else {
216  }
217  } else {
219  }
220  } else
221 #endif
222  {
223  if (!d->fonts.contains(d->url)) {
225  d->fonts[d->url] = fo;
226  fo->download(d->url, qmlEngine(this)->networkAccessManager());
227  d->status = Loading;
231  } else {
232  QDeclarativeFontObject *fo = d->fonts[d->url];
233  if (fo->id == -1) {
234  d->status = Loading;
238  }
239  else
241  }
242  }
243 }
244 
246 {
248 
249  if (name != d->name) {
250  d->name = name;
251  emit nameChanged();
252  }
253  if (status != d->status) {
254  if (status == Error)
255  qmlInfo(this) << "Cannot load font: \"" << d->url.toString() << "\"";
256  d->status = status;
258  }
259 }
260 
289 {
291  return d->name;
292 }
293 
295 {
297  if (d->name == name)
298  return;
299  d->name = name;
300  emit nameChanged();
301  d->status = Ready;
303 }
304 
343 {
345  return d->status;
346 }
347 
349 
350 #include <qdeclarativefontloader.moc>
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static QString urlToLocalFileOrQrc(const QUrl &url)
double d
Definition: qnumeric_p.h:62
NetworkError error() const
Returns the error that was found during the processing of this request.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static int addApplicationFont(const QString &fileName)
Loads the font from the file specified by fileName and makes it available to the application.
QVariant attribute(QNetworkRequest::Attribute code) const
Returns the attribute associated with the code code.
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Definition: qnetworkreply.h:65
#define at(className, varName)
#define SLOT(a)
Definition: qobjectdefs.h:226
QUrl resolvedUrl(const QUrl &)
Resolves the URL src relative to the URL of the containing component.
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_SLOTS
Definition: qobjectdefs.h:71
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
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
void setName(const QString &name)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
Q_DECLARATIVE_EXPORT QDeclarativeContext * qmlContext(const QObject *)
#define Q_D(Class)
Definition: qglobal.h:2482
#define Q_SIGNALS
Definition: qobjectdefs.h:72
void setAttribute(Attribute code, const QVariant &value)
Sets the attribute associated with code code to be value value.
QDeclarativeFontLoader::Status status
void updateFontInfo(const QString &, QDeclarativeFontLoader::Status)
void download(const QUrl &url, QNetworkAccessManager *manager)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
Status status() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
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
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QDeclarativeFontLoader(QObject *parent=0)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
QString name() const
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
The QNetworkAccessManager class allows the application to send network requests and receive replies...
#define Q_OBJECT
Definition: qobjectdefs.h:157
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
Q_DECLARATIVE_EXPORT QDeclarativeEngine * qmlEngine(const QObject *)
static int addApplicationFontFromData(const QByteArray &fontData)
Loads the font from binary data specified by fontData and makes it available to the application...
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
static QStringList applicationFontFamilies(int id)
Returns a list of font families for the given application font identified by id.
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
#define FONTLOADER_MAXIMUM_REDIRECT_RECURSION
void fontDownloaded(const QString &, QDeclarativeFontLoader::Status)
QUrl url() const
Returns the URL of the content downloaded or uploaded.
QNetworkAccessManager * manager() const
Returns the QNetworkAccessManager that was used to create this QNetworkReply object.
QDeclarativeInfo qmlInfo(const QObject *me)
void setSource(const QUrl &url)
static QHash< QUrl, QDeclarativeFontObject * > fonts
QUrl resolved(const QUrl &relative) const
Returns the result of the merge of this URL with relative.
Definition: qurl.cpp:5819
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145