From 29b184cf0bdebafe6d667994543f7af6e44b709b Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Wed, 5 Aug 2009 09:12:57 +1000 Subject: Renaming: FontFamily -> FontLoader --- examples/declarative/fonts/fonts.qml | 8 +- src/declarative/extra/extra.pri | 4 +- src/declarative/extra/qmlfontfamily.cpp | 206 -------------------------------- src/declarative/extra/qmlfontfamily.h | 94 --------------- src/declarative/extra/qmlfontloader.cpp | 206 ++++++++++++++++++++++++++++++++ src/declarative/extra/qmlfontloader.h | 94 +++++++++++++++ 6 files changed, 306 insertions(+), 306 deletions(-) delete mode 100644 src/declarative/extra/qmlfontfamily.cpp delete mode 100644 src/declarative/extra/qmlfontfamily.h create mode 100644 src/declarative/extra/qmlfontloader.cpp create mode 100644 src/declarative/extra/qmlfontloader.h diff --git a/examples/declarative/fonts/fonts.qml b/examples/declarative/fonts/fonts.qml index eb8cfef..c981e51 100644 --- a/examples/declarative/fonts/fonts.qml +++ b/examples/declarative/fonts/fonts.qml @@ -8,13 +8,13 @@ Rect { Palette { id: Palette; colorGroup: "Active" } - FontFamily { id: FixedFont; name: "Courier" } + FontLoader { id: FixedFont; name: "Courier" } - FontFamily { id: LocalFont; source: "fonts/Fontin-Bold.ttf" } + FontLoader { id: LocalFont; source: "fonts/Fontin-Bold.ttf" } /* A font by Jos Buivenga (exljbris) -> www.exljbris.nl */ - FontFamily { id: WebFont; source: "http://www.princexml.com/fonts/steffmann/Starburst.ttf" } - FontFamily { id: WebFont2; source: "http://wrong.address.org" } + FontLoader { id: WebFont; source: "http://www.princexml.com/fonts/steffmann/Starburst.ttf" } + FontLoader { id: WebFont2; source: "http://wrong.address.org" } VerticalLayout { anchors.fill: parent diff --git a/src/declarative/extra/extra.pri b/src/declarative/extra/extra.pri index c731afa..1f84406 100644 --- a/src/declarative/extra/extra.pri +++ b/src/declarative/extra/extra.pri @@ -9,7 +9,7 @@ SOURCES += \ extra/qfxparticles.cpp \ extra/qmlbehaviour.cpp \ extra/qbindablemap.cpp \ - extra/qmlfontfamily.cpp + extra/qmlfontloader.cpp HEADERS += \ extra/qnumberformat.h \ @@ -23,7 +23,7 @@ HEADERS += \ extra/qfxparticles.h \ extra/qmlbehaviour.h \ extra/qbindablemap.h \ - extra/qmlfontfamily.h + extra/qmlfontloader.h contains(QT_CONFIG, xmlpatterns) { QT+=xmlpatterns diff --git a/src/declarative/extra/qmlfontfamily.cpp b/src/declarative/extra/qmlfontfamily.cpp deleted file mode 100644 index 73688c1..0000000 --- a/src/declarative/extra/qmlfontfamily.cpp +++ /dev/null @@ -1,206 +0,0 @@ -/**************************************************************************** -** -** 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 -#include -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QmlFontFamilyPrivate : public QObjectPrivate -{ - Q_DECLARE_PUBLIC(QmlFontFamily); - -public: - QmlFontFamilyPrivate() : reply(0), status(QmlFontFamily::Null) {} - - void addFontToDatabase(const QByteArray &); - - QUrl url; - QString name; - QNetworkReply *reply; - QmlFontFamily::Status status; -}; - -QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,FontFamily,QmlFontFamily) - -/*! - \qmlclass FontFamily QmlFontFamily - \ingroup group_utility - \brief This item allows using fonts by name or url. - - Example: - \code - FontFamily { id: FixedFont; name: "Courier" } - FontFamily { id: WebFont; source: "http://www.mysite.com/myfont.ttf" } - - Text { text: "Fixed-size font"; font.family: FixedFont.name } - Text { text: "Fancy font"; font.family: WebFont.name } - \endcode -*/ -QmlFontFamily::QmlFontFamily(QObject *parent) - : QObject(*(new QmlFontFamilyPrivate), parent) -{ -} - -QmlFontFamily::~QmlFontFamily() -{ -} - -/*! - \qmlproperty url FontFamily::source - The url of the font to load. -*/ -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); - - d->status = Loading; - emit statusChanged(); -#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())); - } -} - -/*! - \qmlproperty string FontFamily::name - - This property holds the name of the font family. - It is set automatically when a font is loaded using the \c url property. - - Use this to set the \c font.family property of a \c Text item. - - Example: - \qml - FontFamily { id: WebFont; source: "http://www.mysite.com/myfont.ttf" } - Text { text: "Fancy font"; font.family: WebFont.name } - \endqml -*/ -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(); -} - -/*! - \qmlproperty enum FontFamily::status - - This property holds the status of font loading. It can be one of: - \list - \o Null - no font has been set - \o Ready - the font has been loaded - \o Loading - the font is currently being loaded - \o Error - an error occurred while loading the font - \endlist -*/ -QmlFontFamily::Status QmlFontFamily::status() const -{ - Q_D(const QmlFontFamily); - return d->status; -} - -void QmlFontFamily::replyFinished() -{ - Q_D(QmlFontFamily); - if (!d->reply->error()) { - QByteArray ba = d->reply->readAll(); - d->addFontToDatabase(ba); - } else { - d->status = Error; - emit statusChanged(); - } - 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(); - status = QmlFontFamily::Ready; - } else { - status = QmlFontFamily::Error; - qWarning() << "Cannot load font: " << name << url; - } - emit q->statusChanged(); -} - -QT_END_NAMESPACE diff --git a/src/declarative/extra/qmlfontfamily.h b/src/declarative/extra/qmlfontfamily.h deleted file mode 100644 index 17b6635..0000000 --- a/src/declarative/extra/qmlfontfamily.h +++ /dev/null @@ -1,94 +0,0 @@ -/**************************************************************************** -** -** 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 -#include - -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_ENUMS(Status) - - Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) - Q_PROPERTY(Status status READ status NOTIFY statusChanged) - -public: - enum Status { Null = 0, Ready, Loading, Error }; - - QmlFontFamily(QObject *parent = 0); - ~QmlFontFamily(); - - QUrl source() const; - void setSource(const QUrl &url); - - QString name() const; - void setName(const QString &name); - - Status status() const; - -private Q_SLOTS: - void replyFinished(); - -Q_SIGNALS: - void nameChanged(); - void statusChanged(); -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QmlFontFamily) - -QT_END_HEADER - -#endif // QMLFONTFAMILY_H - diff --git a/src/declarative/extra/qmlfontloader.cpp b/src/declarative/extra/qmlfontloader.cpp new file mode 100644 index 0000000..2f54f24 --- /dev/null +++ b/src/declarative/extra/qmlfontloader.cpp @@ -0,0 +1,206 @@ +/**************************************************************************** +** +** 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 "qmlfontloader.h" +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QmlFontLoaderPrivate : public QObjectPrivate +{ + Q_DECLARE_PUBLIC(QmlFontLoader); + +public: + QmlFontLoaderPrivate() : reply(0), status(QmlFontLoader::Null) {} + + void addFontToDatabase(const QByteArray &); + + QUrl url; + QString name; + QNetworkReply *reply; + QmlFontLoader::Status status; +}; + +QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,FontLoader,QmlFontLoader) + +/*! + \qmlclass FontLoader QmlFontLoader + \ingroup group_utility + \brief This item allows using fonts by name or url. + + Example: + \code + FontLoader { id: FixedFont; name: "Courier" } + FontLoader { id: WebFont; source: "http://www.mysite.com/myfont.ttf" } + + Text { text: "Fixed-size font"; font.family: FixedFont.name } + Text { text: "Fancy font"; font.family: WebFont.name } + \endcode +*/ +QmlFontLoader::QmlFontLoader(QObject *parent) + : QObject(*(new QmlFontLoaderPrivate), parent) +{ +} + +QmlFontLoader::~QmlFontLoader() +{ +} + +/*! + \qmlproperty url FontLoader::source + The url of the font to load. +*/ +QUrl QmlFontLoader::source() const +{ + Q_D(const QmlFontLoader); + return d->url; +} + +void QmlFontLoader::setSource(const QUrl &url) +{ + Q_D(QmlFontLoader); + if (url == d->url) + return; + d->url = qmlContext(this)->resolvedUrl(url); + + d->status = Loading; + emit statusChanged(); +#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())); + } +} + +/*! + \qmlproperty string FontLoader::name + + This property holds the name of the font family. + It is set automatically when a font is loaded using the \c url property. + + Use this to set the \c font.family property of a \c Text item. + + Example: + \qml + FontLoader { id: WebFont; source: "http://www.mysite.com/myfont.ttf" } + Text { text: "Fancy font"; font.family: WebFont.name } + \endqml +*/ +QString QmlFontLoader::name() const +{ + Q_D(const QmlFontLoader); + return d->name; +} + +void QmlFontLoader::setName(const QString &name) +{ + Q_D(QmlFontLoader); + if (d->name == name ) + return; + d->name = name; + emit nameChanged(); +} + +/*! + \qmlproperty enum FontLoader::status + + This property holds the status of font loading. It can be one of: + \list + \o Null - no font has been set + \o Ready - the font has been loaded + \o Loading - the font is currently being loaded + \o Error - an error occurred while loading the font + \endlist +*/ +QmlFontLoader::Status QmlFontLoader::status() const +{ + Q_D(const QmlFontLoader); + return d->status; +} + +void QmlFontLoader::replyFinished() +{ + Q_D(QmlFontLoader); + if (!d->reply->error()) { + QByteArray ba = d->reply->readAll(); + d->addFontToDatabase(ba); + } else { + d->status = Error; + emit statusChanged(); + } + d->reply->deleteLater(); + d->reply = 0; +} + +void QmlFontLoaderPrivate::addFontToDatabase(const QByteArray &ba) +{ + Q_Q(QmlFontLoader); + + int id = QFontDatabase::addApplicationFontFromData(ba); + if (id != -1) { + name = QFontDatabase::applicationFontFamilies(id).at(0); + emit q->nameChanged(); + status = QmlFontLoader::Ready; + } else { + status = QmlFontLoader::Error; + qWarning() << "Cannot load font: " << name << url; + } + emit q->statusChanged(); +} + +QT_END_NAMESPACE diff --git a/src/declarative/extra/qmlfontloader.h b/src/declarative/extra/qmlfontloader.h new file mode 100644 index 0000000..c2c7a16 --- /dev/null +++ b/src/declarative/extra/qmlfontloader.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** 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 QMLFONTLOADER_H +#define QMLFONTLOADER_H + +#include +#include + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlFontLoaderPrivate; +class Q_DECLARATIVE_EXPORT QmlFontLoader : public QObject +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QmlFontLoader) + Q_ENUMS(Status) + + Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(Status status READ status NOTIFY statusChanged) + +public: + enum Status { Null = 0, Ready, Loading, Error }; + + QmlFontLoader(QObject *parent = 0); + ~QmlFontLoader(); + + QUrl source() const; + void setSource(const QUrl &url); + + QString name() const; + void setName(const QString &name); + + Status status() const; + +private Q_SLOTS: + void replyFinished(); + +Q_SIGNALS: + void nameChanged(); + void statusChanged(); +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlFontLoader) + +QT_END_HEADER + +#endif // QMLFONTLOADER_H + -- cgit v0.12