diff options
author | Liang Qi <liang.qi@theqtcompany.com> | 2015-09-02 10:32:47 +0200 |
---|---|---|
committer | Liang Qi <liang.qi@theqtcompany.com> | 2015-09-02 12:12:23 +0000 |
commit | 0addfd5d586c7850c1e470f24c6364e6305dfa08 (patch) | |
tree | a1765680b89890e7b2de873aab7d1b47b072a621 /src/controls | |
parent | e3c86bd3185e288ce23a0ff07084cd0def54e898 (diff) |
QQuickControl: add font property
When the font of a QQuickControl subclass is set, it will
propagate it to all children that are also derived from
QQuickControl, as well as TextField and TextArea.
Change-Id: I7c851f84b89609094d9a81d239ade0f0ac212985
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
Diffstat (limited to 'src/controls')
-rw-r--r-- | src/controls/controls.pri | 2 | ||||
-rw-r--r-- | src/controls/qquickcontrol.cpp | 112 | ||||
-rw-r--r-- | src/controls/qquickcontrol_p.h | 8 | ||||
-rw-r--r-- | src/controls/qquickcontrol_p_p.h | 11 | ||||
-rw-r--r-- | src/controls/qquicktextarea.cpp | 61 | ||||
-rw-r--r-- | src/controls/qquicktextarea_p.h | 5 | ||||
-rw-r--r-- | src/controls/qquicktextarea_p_p.h | 85 | ||||
-rw-r--r-- | src/controls/qquicktextfield.cpp | 63 | ||||
-rw-r--r-- | src/controls/qquicktextfield_p.h | 5 | ||||
-rw-r--r-- | src/controls/qquicktextfield_p_p.h | 88 |
10 files changed, 405 insertions, 35 deletions
diff --git a/src/controls/controls.pri b/src/controls/controls.pri index af65c841..e7e8f65d 100644 --- a/src/controls/controls.pri +++ b/src/controls/controls.pri @@ -31,7 +31,9 @@ HEADERS += \ $$PWD/qquicktabbar_p.h \ $$PWD/qquicktabbutton_p.h \ $$PWD/qquicktextarea_p.h \ + $$PWD/qquicktextarea_p_p.h \ $$PWD/qquicktextfield_p.h \ + $$PWD/qquicktextfield_p_p.h \ $$PWD/qquicktogglebutton_p.h \ $$PWD/qquicktoolbar_p.h \ $$PWD/qquicktoolbutton_p.h diff --git a/src/controls/qquickcontrol.cpp b/src/controls/qquickcontrol.cpp index 1b41c352..e91149e3 100644 --- a/src/controls/qquickcontrol.cpp +++ b/src/controls/qquickcontrol.cpp @@ -37,6 +37,12 @@ #include "qquickcontrol_p.h" #include "qquickcontrol_p_p.h" +#include <QtGui/qguiapplication.h> +#include "qquicktextarea_p.h" +#include "qquicktextarea_p_p.h" +#include "qquicktextfield_p.h" +#include "qquicktextfield_p_p.h" + QT_BEGIN_NAMESPACE /*! @@ -143,6 +149,78 @@ void QQuickControlPrivate::resizeContent() } } +/*! + \internal + + Returns the font that the control w inherits from its ancestors and + QGuiApplication::font. +*/ +QFont QQuickControlPrivate::naturalControlFont(const QQuickItem *q) +{ + QFont naturalFont = QGuiApplication::font(); + QQuickItem *p = q->parentItem(); + while (p) { + if (QQuickControl *qc = qobject_cast<QQuickControl *>(p)) { + naturalFont = qc->font(); + break; + } + + p = p->parentItem(); + } + + naturalFont.resolve(0); + return naturalFont; +} + +/*! + \internal + + Determine which font is implicitly imposed on this control by its ancestors + and QGuiApplication::font, resolve this against its own font (attributes from + the implicit font are copied over). Then propagate this font to this + control's children. +*/ +void QQuickControlPrivate::resolveFont() +{ + Q_Q(const QQuickControl); + QFont naturalFont = QQuickControlPrivate::naturalControlFont(q); + QFont resolvedFont = font.resolve(naturalFont); + setFont_helper(resolvedFont); +} + +/*! + \internal + + Assign \a font to this control, and propagate it to all children. +*/ +void QQuickControlPrivate::updateFont(const QFont &f) +{ + Q_Q(QQuickControl); + font = f; + + QQuickControlPrivate::updateFontRecur(q, f); + + emit q->fontChanged(); +} + +void QQuickControlPrivate::updateFontRecur(QQuickItem *i, const QFont &f) +{ + foreach (QQuickItem *child, i->childItems()) { + if (QQuickControl *qc = qobject_cast<QQuickControl *>(child)) { + QQuickControlPrivate *qcp = qc->d_func(); + qcp->resolveFont(); + } else if (QQuickTextArea *qta = qobject_cast<QQuickTextArea *>(child)) { + QQuickTextAreaPrivate *qtap = QQuickTextAreaPrivate::get(qta); + qtap->resolveFont(); + } else if (QQuickTextField *qtf = qobject_cast<QQuickTextField *>(child)) { + QQuickTextFieldPrivate *qtfp = QQuickTextFieldPrivate::get(qtf); + qtfp->resolveFont(); + } else { + QQuickControlPrivate::updateFontRecur(child, f); + } + } +} + QQuickControl::QQuickControl(QQuickItem *parent) : QQuickItem(*(new QQuickControlPrivate), parent) { @@ -153,6 +231,40 @@ QQuickControl::QQuickControl(QQuickControlPrivate &dd, QQuickItem *parent) : { } +void QQuickControl::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) +{ + Q_D(QQuickControl); + QQuickItem::itemChange(change, value); + if (change == ItemParentHasChanged) + d->resolveFont(); +} + +QFont QQuickControl::font() const +{ + Q_D(const QQuickControl); + return d->font; +} + +void QQuickControl::setFont(const QFont &f) +{ + Q_D(QQuickControl); + if (d->font == f) + return; + + // Determine which font is inherited from this control's ancestors and + // QGuiApplication::font, resolve this against \a font (attributes from the + // inherited font are copied over). Then propagate this font to this + // control's children. + QFont naturalFont = QQuickControlPrivate::naturalControlFont(this); + QFont resolvedFont = f.resolve(naturalFont); + d->setFont_helper(resolvedFont); +} + +void QQuickControl::resetFont() +{ + setFont(QFont()); +} + /*! \qmlproperty real QtQuickControls2::Control::availableWidth diff --git a/src/controls/qquickcontrol_p.h b/src/controls/qquickcontrol_p.h index 9da4235e..9b2ede50 100644 --- a/src/controls/qquickcontrol_p.h +++ b/src/controls/qquickcontrol_p.h @@ -58,6 +58,7 @@ class QQuickControlPrivate; class Q_QUICKCONTROLS_EXPORT QQuickControl : public QQuickItem { Q_OBJECT + Q_PROPERTY(QFont font READ font WRITE setFont RESET resetFont NOTIFY fontChanged) Q_PROPERTY(qreal availableWidth READ availableWidth NOTIFY availableWidthChanged FINAL) Q_PROPERTY(qreal availableHeight READ availableHeight NOTIFY availableHeightChanged FINAL) Q_PROPERTY(qreal padding READ padding WRITE setPadding RESET resetPadding NOTIFY paddingChanged FINAL) @@ -75,6 +76,10 @@ class Q_QUICKCONTROLS_EXPORT QQuickControl : public QQuickItem public: explicit QQuickControl(QQuickItem *parent = Q_NULLPTR); + QFont font() const; + void setFont(const QFont &); + void resetFont(); + qreal availableWidth() const; qreal availableHeight() const; @@ -115,6 +120,7 @@ public: void setContentItem(QQuickItem *item); Q_SIGNALS: + void fontChanged(); void availableWidthChanged(); void availableHeightChanged(); void paddingChanged(); @@ -132,6 +138,8 @@ Q_SIGNALS: protected: QQuickControl(QQuickControlPrivate &dd, QQuickItem *parent); + void itemChange(ItemChange change, const ItemChangeData &value) Q_DECL_OVERRIDE; + void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; diff --git a/src/controls/qquickcontrol_p_p.h b/src/controls/qquickcontrol_p_p.h index 273ca6c9..2861c067 100644 --- a/src/controls/qquickcontrol_p_p.h +++ b/src/controls/qquickcontrol_p_p.h @@ -69,6 +69,17 @@ public: void resizeBackground(); void resizeContent(); + void updateFont(const QFont &); + static void updateFontRecur(QQuickItem *item, const QFont &); + inline void setFont_helper(const QFont &f) { + if (font.resolve() == f.resolve() && font == f) + return; + updateFont(f); + } + void resolveFont(); + static QFont naturalControlFont(const QQuickItem *); + + QFont font; bool hasTopPadding; bool hasLeftPadding; bool hasRightPadding; diff --git a/src/controls/qquicktextarea.cpp b/src/controls/qquicktextarea.cpp index a670f893..b9e4e4f8 100644 --- a/src/controls/qquicktextarea.cpp +++ b/src/controls/qquicktextarea.cpp @@ -35,12 +35,14 @@ ****************************************************************************/ #include "qquicktextarea_p.h" -#include "qquickpressandholdhelper_p.h" +#include "qquicktextarea_p_p.h" +#include "qquickcontrol_p.h" +#include "qquickcontrol_p_p.h" +#include <QtGui/qguiapplication.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuick/private/qquicktext_p.h> #include <QtQuick/private/qquickclipnode_p.h> -#include <QtQuick/private/qquicktextedit_p_p.h> QT_BEGIN_NAMESPACE @@ -64,20 +66,6 @@ QT_BEGIN_NAMESPACE \sa TextField, {Customizing TextArea} */ -class QQuickTextAreaPrivate : public QQuickTextEditPrivate -{ - Q_DECLARE_PUBLIC(QQuickTextArea) - -public: - QQuickTextAreaPrivate() : background(Q_NULLPTR), placeholder(Q_NULLPTR) { } - - void resizeBackground(); - - QQuickItem *background; - QQuickText *placeholder; - QQuickPressAndHoldHelper pressAndHoldHelper; -}; - void QQuickTextAreaPrivate::resizeBackground() { Q_Q(QQuickTextArea); @@ -106,6 +94,47 @@ QQuickTextArea::~QQuickTextArea() } /*! + \internal + + Determine which font is implicitly imposed on this control by its ancestors + and QGuiApplication::font, resolve this against its own font (attributes from + the implicit font are copied over). Then propagate this font to this + control's children. +*/ +void QQuickTextAreaPrivate::resolveFont() +{ + Q_Q(const QQuickTextArea); + QFont naturalFont = QQuickControlPrivate::naturalControlFont(q); + QFont resolvedFont = sourceFont.resolve(naturalFont); + setFont_helper(resolvedFont); +} + +QFont QQuickTextArea::font() const +{ + Q_D(const QQuickTextArea); + return d->sourceFont; +} + +void QQuickTextArea::setFont(const QFont &font) +{ + Q_D(QQuickTextArea); + if (d->sourceFont == font) + return; + + // Determine which font is inherited from this control's ancestors and + // QGuiApplication::font, resolve this against \a font (attributes from the + // inherited font are copied over). Then propagate this font to this + // control's children. + QFont naturalFont = QQuickControlPrivate::naturalControlFont(this); + QFont resolvedFont = font.resolve(naturalFont); + d->setFont_helper(resolvedFont); + + QQuickTextEdit::setFont(font); + + emit fontChanged(); +} + +/*! \qmlproperty Item QtQuickControls2::TextArea::background This property holds the background item. diff --git a/src/controls/qquicktextarea_p.h b/src/controls/qquicktextarea_p.h index f952f304..239fa559 100644 --- a/src/controls/qquicktextarea_p.h +++ b/src/controls/qquicktextarea_p.h @@ -60,6 +60,7 @@ class QQuickMouseEvent; class Q_QUICKCONTROLS_EXPORT QQuickTextArea : public QQuickTextEdit { Q_OBJECT + Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) // override Q_PROPERTY(QQuickItem *background READ background WRITE setBackground NOTIFY backgroundChanged FINAL) Q_PROPERTY(QQuickText *placeholder READ placeholder WRITE setPlaceholder NOTIFY placeholderChanged FINAL) @@ -67,6 +68,9 @@ public: explicit QQuickTextArea(QQuickItem *parent = Q_NULLPTR); ~QQuickTextArea(); + QFont font() const; + void setFont(const QFont &font); + QQuickItem *background() const; void setBackground(QQuickItem *background); @@ -74,6 +78,7 @@ public: void setPlaceholder(QQuickText *placeholder); Q_SIGNALS: + void fontChanged(); void backgroundChanged(); void placeholderChanged(); void pressAndHold(QQuickMouseEvent *event); diff --git a/src/controls/qquicktextarea_p_p.h b/src/controls/qquicktextarea_p_p.h new file mode 100644 index 00000000..83194ef1 --- /dev/null +++ b/src/controls/qquicktextarea_p_p.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKTEXTAREA_P_P_H +#define QQUICKTEXTAREA_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtQuick/private/qquicktextedit_p_p.h> +#include <QtQuickControls/private/qquickpressandholdhelper_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickTextAreaPrivate : public QQuickTextEditPrivate +{ + Q_DECLARE_PUBLIC(QQuickTextArea) + +public: + QQuickTextAreaPrivate() : background(Q_NULLPTR), placeholder(Q_NULLPTR) { } + + static QQuickTextAreaPrivate *get(QQuickTextArea *item) { + return static_cast<QQuickTextAreaPrivate *>(QObjectPrivate::get(item)); } + + void resizeBackground(); + + inline void setFont_helper(const QFont &f) { + // In QQuickTextEditPrivate, sourceFont was used, instead of font... + if (sourceFont.resolve() == f.resolve() && sourceFont == f) + return; + sourceFont = f; + } + void resolveFont(); + + QQuickItem *background; + QQuickText *placeholder; + QQuickPressAndHoldHelper pressAndHoldHelper; +}; + +Q_DECLARE_TYPEINFO(QQuickTextAreaPrivate, Q_COMPLEX_TYPE); + +QT_END_NAMESPACE + +#endif // QQUICKTEXTAREA_P_P_H diff --git a/src/controls/qquicktextfield.cpp b/src/controls/qquicktextfield.cpp index e8b4086c..01b26cf7 100644 --- a/src/controls/qquicktextfield.cpp +++ b/src/controls/qquicktextfield.cpp @@ -35,13 +35,14 @@ ****************************************************************************/ #include "qquicktextfield_p.h" -#include "qquickpressandholdhelper_p.h" +#include "qquicktextfield_p_p.h" +#include "qquickcontrol_p.h" +#include "qquickcontrol_p_p.h" #include <QtCore/qbasictimer.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuick/private/qquicktext_p.h> #include <QtQuick/private/qquickclipnode_p.h> -#include <QtQuick/private/qquicktextinput_p_p.h> QT_BEGIN_NAMESPACE @@ -82,23 +83,6 @@ QT_BEGIN_NAMESPACE position of the press, and which button is pressed. */ -class QQuickTextFieldPrivate : public QQuickTextInputPrivate -{ - Q_DECLARE_PUBLIC(QQuickTextField) - -public: - QQuickTextFieldPrivate() - : background(Q_NULLPTR) - , placeholder(Q_NULLPTR) - { } - - void resizeBackground(); - - QQuickItem *background; - QQuickText *placeholder; - QQuickPressAndHoldHelper pressAndHoldHelper; -}; - void QQuickTextFieldPrivate::resizeBackground() { Q_Q(QQuickTextField); @@ -127,6 +111,47 @@ QQuickTextField::~QQuickTextField() } /*! + \internal + + Determine which font is implicitly imposed on this control by its ancestors + and QGuiApplication::font, resolve this against its own font (attributes from + the implicit font are copied over). Then propagate this font to this + control's children. +*/ +void QQuickTextFieldPrivate::resolveFont() +{ + Q_Q(QQuickTextField); + QFont naturalFont = QQuickControlPrivate::naturalControlFont(q); + QFont resolvedFont = sourceFont.resolve(naturalFont); + setFont_helper(resolvedFont); +} + +QFont QQuickTextField::font() const +{ + Q_D(const QQuickTextField); + return d->sourceFont; +} + +void QQuickTextField::setFont(const QFont &font) +{ + Q_D(QQuickTextField); + if (d->sourceFont == font) + return; + + // Determine which font is inherited from this control's ancestors and + // QGuiApplication::font, resolve this against \a font (attributes from the + // inherited font are copied over). Then propagate this font to this + // control's children. + QFont naturalFont = QQuickControlPrivate::naturalControlFont(this); + QFont resolvedFont = font.resolve(naturalFont); + d->setFont_helper(resolvedFont); + + QQuickTextInput::setFont(font); + + emit fontChanged(); +} + +/*! \qmlproperty Item QtQuickControls2::TextField::background This property holds the background item. diff --git a/src/controls/qquicktextfield_p.h b/src/controls/qquicktextfield_p.h index a3b0fa8d..6f22cdd5 100644 --- a/src/controls/qquicktextfield_p.h +++ b/src/controls/qquicktextfield_p.h @@ -60,6 +60,7 @@ class QQuickMouseEvent; class Q_QUICKCONTROLS_EXPORT QQuickTextField : public QQuickTextInput { Q_OBJECT + Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) // override Q_PROPERTY(QQuickItem *background READ background WRITE setBackground NOTIFY backgroundChanged FINAL) Q_PROPERTY(QQuickText *placeholder READ placeholder WRITE setPlaceholder NOTIFY placeholderChanged FINAL) @@ -67,6 +68,9 @@ public: explicit QQuickTextField(QQuickItem *parent = Q_NULLPTR); ~QQuickTextField(); + QFont font() const; + void setFont(const QFont &font); + QQuickItem *background() const; void setBackground(QQuickItem *background); @@ -74,6 +78,7 @@ public: void setPlaceholder(QQuickText *placeholder); Q_SIGNALS: + void fontChanged(); void backgroundChanged(); void placeholderChanged(); void pressAndHold(QQuickMouseEvent *mouse); diff --git a/src/controls/qquicktextfield_p_p.h b/src/controls/qquicktextfield_p_p.h new file mode 100644 index 00000000..ceefb91e --- /dev/null +++ b/src/controls/qquicktextfield_p_p.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKTEXTFIELD_P_P_H +#define QQUICKTEXTFIELD_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtQuick/private/qquicktextinput_p_p.h> +#include <QtQuickControls/private/qquickpressandholdhelper_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickTextFieldPrivate : public QQuickTextInputPrivate +{ + Q_DECLARE_PUBLIC(QQuickTextField) + +public: + QQuickTextFieldPrivate() + : background(Q_NULLPTR) + , placeholder(Q_NULLPTR) + { } + + static QQuickTextFieldPrivate *get(QQuickTextField *item) { + return static_cast<QQuickTextFieldPrivate *>(QObjectPrivate::get(item)); } + + void resizeBackground(); + + inline void setFont_helper(const QFont &f) { + // In QQuickTextInputPrivate, sourceFont was used, instead of font... + if (sourceFont.resolve() == f.resolve() && sourceFont == f) + return; + sourceFont = f; + } + void resolveFont(); + + QQuickItem *background; + QQuickText *placeholder; + QQuickPressAndHoldHelper pressAndHoldHelper; +}; + +Q_DECLARE_TYPEINFO(QQuickTextFieldPrivate, Q_COMPLEX_TYPE); + +QT_END_NAMESPACE + +#endif // QQUICKTEXTFIELD_P_P_H |