diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/declarative/util/qmlfontfamily.cpp | 152 | ||||
-rw-r--r-- | src/declarative/util/qmlfontfamily.h | 86 | ||||
-rw-r--r-- | src/declarative/util/util.pri | 2 |
3 files changed, 240 insertions, 0 deletions
diff --git a/src/declarative/util/qmlfontfamily.cpp b/src/declarative/util/qmlfontfamily.cpp new file mode 100644 index 0000000..91d1cde --- /dev/null +++ b/src/declarative/util/qmlfontfamily.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "private/qobject_p.h" +#include "qmlfontfamily.h" +#include <QUrl> +#include <QDebug> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QFile> +#include <QmlContext> +#include <QtDeclarative/qmlengine.h> +#include <QFontDatabase> + +QT_BEGIN_NAMESPACE + +class QmlFontFamilyPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QmlFontFamily); + +public: + QmlFontFamilyPrivate() : reply(0) {} + + void addFontToDatabase(const QByteArray &); + + QUrl url; + QString name; + QNetworkReply *reply; +}; + +QML_DEFINE_TYPE(QmlFontFamily,FontFamily) + +/*! + \qmlclass FontFamily QmlFontFamily + \ingroup group_utility +*/ +QmlFontFamily::QmlFontFamily(QObject *parent) + : QObject(*(new QmlFontFamilyPrivate), parent) +{ +} + +QmlFontFamily::~QmlFontFamily() +{ +} + +QUrl QmlFontFamily::source() const +{ + Q_D(const QmlFontFamily); + return d->url; +} + +void QmlFontFamily::setSource(const QUrl &url) +{ + Q_D(QmlFontFamily); + if (url == d->url) + return; + d->url = qmlContext(this)->resolvedUrl(url); + +#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML + if (d->url.scheme() == QLatin1String("file")) { + QFile file(d->url.toLocalFile()); + file.open(QIODevice::ReadOnly); + QByteArray ba = file.readAll(); + d->addFontToDatabase(ba); + } else +#endif + { + QNetworkRequest req(d->url); + req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); + d->reply = qmlEngine(this)->networkAccessManager()->get(req); + QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(replyFinished())); + } +} + +QString QmlFontFamily::name() const +{ + Q_D(const QmlFontFamily); + return d->name; +} + +void QmlFontFamily::setName(const QString &name) +{ + Q_D(QmlFontFamily); + if (d->name == name ) + return; + d->name = name; + emit nameChanged(); +} + +void QmlFontFamily::replyFinished() +{ + Q_D(QmlFontFamily); + if (!d->reply->error()) { + QByteArray ba = d->reply->readAll(); + d->addFontToDatabase(ba); + } + d->reply->deleteLater(); + d->reply = 0; +} + +void QmlFontFamilyPrivate::addFontToDatabase(const QByteArray &ba) +{ + Q_Q(QmlFontFamily); + + int id = QFontDatabase::addApplicationFontFromData(ba); + if (id != -1) { + name = QFontDatabase::applicationFontFamilies(id).at(0); + emit q->nameChanged(); + } else { + qWarning() << "Cannot load font: " << name << url; + } +} + +QT_END_NAMESPACE diff --git a/src/declarative/util/qmlfontfamily.h b/src/declarative/util/qmlfontfamily.h new file mode 100644 index 0000000..739a553 --- /dev/null +++ b/src/declarative/util/qmlfontfamily.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLFONTFAMILY_H +#define QMLFONTFAMILY_H + +#include <QtCore/qobject.h> +#include <QtDeclarative/qml.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlFontFamilyPrivate; +class Q_DECLARATIVE_EXPORT QmlFontFamily : public QObject +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QmlFontFamily) + + Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + +public: + QmlFontFamily(QObject *parent = 0); + ~QmlFontFamily(); + + QUrl source() const; + void setSource(const QUrl &url); + + QString name() const; + void setName(const QString &name); + +private Q_SLOTS: + void replyFinished(); + +Q_SIGNALS: + void nameChanged(); +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlFontFamily) + +QT_END_HEADER + +#endif // QMLFONTFAMILY_H diff --git a/src/declarative/util/util.pri b/src/declarative/util/util.pri index 59e3695..dfb79ac 100644 --- a/src/declarative/util/util.pri +++ b/src/declarative/util/util.pri @@ -7,6 +7,7 @@ SOURCES += \ util/qmlscript.cpp \ util/qmlanimation.cpp \ util/qmlfont.cpp \ + util/qmlfontfamily.cpp \ util/qmlpalette.cpp \ util/qmlfollow.cpp \ util/qmlstate.cpp\ @@ -33,6 +34,7 @@ HEADERS += \ util/qmlanimation.h \ util/qmlanimation_p.h \ util/qmlfont.h \ + util/qmlfontfamily.h \ util/qmlpalette.h \ util/qmlfollow.h \ util/qmlstate.h\ |