Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickstyleplugin.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickstyle.h"
5#include "qquickstyle_p.h"
7
8#include <QtCore/private/qfileselector_p.h>
9#include <QtCore/qloggingcategory.h>
10#include <QtGui/qstylehints.h>
11#include <QtGui/qguiapplication.h>
12#include <QtQml/qqmlengine.h>
13#include <QtQml/qqmlfile.h>
14#include <QtQml/private/qqmlmetatype_p.h>
15#include <QtQuickTemplates2/private/qquicktheme_p_p.h>
16
18
19Q_LOGGING_CATEGORY(lcStylePlugin, "qt.quick.controls.styleplugin")
20
25
29
31{
32 qCDebug(lcStylePlugin).nospace() << "registerTypes called with uri " << uri << "; plugin name is " << name();
33
34 const QTypeRevision latestControlsRevision = QQmlMetaType::latestModuleVersion(QLatin1String("QtQuick.Controls"));
35 // Use the private function because we don't want to cause resolve() to be called,
36 // as the logic that assigns a default style if one wasn't set would interfere with compile-time style selection.
38 if (!latestControlsRevision.isValid() && styleName.isEmpty()) {
39 // The user hasn't imported QtQuick.Controls, nor set a style via the runtime methods.
40 qCDebug(lcStylePlugin).nospace() << uri << " imported before QtQuick.Controls; using compile-time style selection";
42 styleName = name();
43 }
44
45 // Even if this style plugin isn't for the style set by the user,
46 // we still want to create the theme object, because that function
47 // is also responsible for reading values from qtquickcontrols2.conf.
48 // So, even if a style doesn't have a QQuickTheme, it can still have
49 // values set for (e.g. fonts and palettes) in qtquickcontrols2.conf.
50 const QString effectiveCurrentStyleName = QQuickStylePrivate::effectiveStyleName(styleName);
51 auto theme = QQuickTheme::instance();
52 if (!theme) {
53 qCDebug(lcStylePlugin) << "creating theme";
54 theme = createTheme(effectiveCurrentStyleName);
55 }
56
57 // The primary fallback is the style set by the user. We need to check for that here
58 // so that we can ensure that fallback styles' themes are initialized (QTBUG-117403)
59 // without also allowing the Basic style to be initialized, as it is a secondary fallback
60 // for every built-in style (and only built-in styles can be fallbacks).
61 const bool thisPluginBelongsToCurrentStyle = name() == effectiveCurrentStyleName;
62 const bool isPrimaryFallback = name() == QQuickStylePrivate::fallbackStyle();
63 if (!thisPluginBelongsToCurrentStyle && !isPrimaryFallback) {
64 qCDebug(lcStylePlugin).nospace() << "this style plugin does not belong to the current ("
65 << effectiveCurrentStyleName << ") or fallback (" << QQuickStylePrivate::fallbackStyle()
66 << ") style; not calling initializeTheme()";
67 return;
68 }
69
70 if (thisPluginBelongsToCurrentStyle) {
71 qCDebug(lcStylePlugin).nospace() << "this style plugin belongs to the current style "
72 << effectiveCurrentStyleName << "; calling initializeTheme()";
73 } else {
74 qCDebug(lcStylePlugin).nospace() << "this style plugin belongs to the fallback style "
75 << QQuickStylePrivate::fallbackStyle() << "; calling initializeTheme()";
76 }
77 initializeTheme(theme);
80
81 if (!isPrimaryFallback && !styleName.isEmpty())
83}
84
86{
87 qCDebug(lcStylePlugin) << "unregisterTypes called; plugin name is" << name();
89 return;
90
93
94 // Not every style has a plugin - some styles are QML-only. So, we clean this
95 // stuff up when the first style plugin is unregistered rather than when the
96 // plugin for the current style is unregistered.
99}
100
113QQuickTheme *QQuickStylePlugin::createTheme(const QString &name)
114{
115 qCDebug(lcStylePlugin) << "creating QQuickTheme instance to be initialized by style-specific theme of" << name;
116
117 QQuickTheme *theme = new QQuickTheme;
118#if QT_CONFIG(settings)
120 QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(name);
121 if (settings) {
122 p->defaultFont.reset(QQuickStylePrivate::readFont(settings));
123 // Set the default font as the System scope, because that's what
124 // QQuickControlPrivate::parentFont() uses as its fallback if no
125 // parent item has a font explicitly set. QQuickControlPrivate::parentFont()
126 // is used as the starting point for font inheritance/resolution.
127 // The same goes for palettes below.
128 theme->setFont(QQuickTheme::System, *p->defaultFont);
129
130 p->defaultPalette.reset(QQuickStylePrivate::readPalette(settings));
131 theme->setPalette(QQuickTheme::System, *p->defaultPalette);
132 }
133#endif
134 QQuickThemePrivate::instance.reset(theme);
135 return theme;
136}
137
139
140#include "moc_qquickstyleplugin_p.cpp"
static void addStatics(const QStringList &)
static QStyleHints * styleHints()
Returns the application's style hints.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QQmlExtensionPlugin class provides an abstract base for custom QML extension plugins with custom ...
static QTypeRevision latestModuleVersion(const QString &uri)
void registerTypes(const char *uri) override
Registers the QML types in the given uri.
virtual void updateTheme()
void unregisterTypes() override
virtual QString name() const =0
virtual void initializeTheme(QQuickTheme *theme)=0
static QString effectiveStyleName(const QString &styleName)
static void reset()
static const QFont * readFont(const QSharedPointer< QSettings > &settings)
static QSharedPointer< QSettings > settings(const QString &group=QString())
static QString style()
static const QPalette * readPalette(const QSharedPointer< QSettings > &settings)
static QString fallbackStyle()
static void setStyle(const QString &style)
Sets the application style to style.
static QQuickThemePrivate * get(QQuickTheme *theme)
static std::unique_ptr< QQuickTheme > instance
static QQuickTheme * instance()
void setFont(Scope scope, const QFont &font)
void setPalette(Scope scope, const QPalette &palette)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void colorSchemeChanged(Qt::ColorScheme colorScheme)
\inmodule QtCore
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
GLuint name
GLfloat GLfloat p
[1]
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
QSettings settings("MySoft", "Star Runner")
[0]
myObject disconnect()
[26]