1// Copyright (C) 2016 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 "qstylefactory.h"
5#include "qstyleplugin.h"
6#include "private/qfactoryloader_p.h"
7#include "qmutex.h"
8
9#include "qapplication.h"
10#include "qwindowsstyle_p.h"
11#if QT_CONFIG(style_fusion)
12#include "qfusionstyle_p.h"
13#endif
14
15QT_BEGIN_NAMESPACE
16
17using namespace Qt::StringLiterals;
18
19Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
20 (QStyleFactoryInterface_iid, "/styles"_L1, Qt::CaseInsensitive))
21
22/*!
23 \class QStyleFactory
24 \brief The QStyleFactory class creates QStyle objects.
25
26 \ingroup appearance
27 \inmodule QtWidgets
28
29 The QStyle class is an abstract base class that encapsulates the
30 look and feel of a GUI. QStyleFactory creates a QStyle object
31 using the create() function and a key identifying the style. The
32 styles are either built-in or dynamically loaded from a style
33 plugin (see QStylePlugin).
34
35 The valid keys can be retrieved using the keys()
36 function. Typically they include "windows" and "fusion".
37 Depending on the platform, "windowsvista"
38 and "macos" may be available.
39 Note that keys are case insensitive.
40
41 \sa QStyle
42*/
43
44/*!
45 Creates and returns a QStyle object that matches the given \a key, or
46 returns \nullptr if no matching style is found.
47
48 Both built-in styles and styles from style plugins are queried for a
49 matching style.
50
51 \note The keys used are case insensitive.
52
53 \sa keys()
54*/
55QStyle *QStyleFactory::create(const QString& key)
56{
57 QStyle *ret = nullptr;
58 QString style = key.toLower();
59#if QT_CONFIG(style_windows)
60 if (style == "windows"_L1)
61 ret = new QWindowsStyle;
62 else
63#endif
64#if QT_CONFIG(style_fusion)
65 if (style == "fusion"_L1)
66 ret = new QFusionStyle;
67 else
68#endif
69#if defined(Q_OS_MACOS) && QT_DEPRECATED_SINCE(6, 0)
70 if (style == "macintosh"_L1) {
71 qWarning() << "The style key 'macintosh' is deprecated. Please use 'macos' instead.";
72 style = QStringLiteral("macos");
73 } else
74#endif
75 { } // Keep these here - they make the #ifdefery above work
76 if (!ret)
77 ret = qLoadPlugin<QStyle, QStylePlugin>(loader: loader(), key: style);
78 if (ret) {
79 ret->setObjectName(style);
80 ret->setName(style);
81 }
82 return ret;
83}
84
85/*!
86 Returns the list of valid keys, i.e. the keys this factory can
87 create styles for.
88
89 \sa create()
90*/
91QStringList QStyleFactory::keys()
92{
93 QStringList list;
94 typedef QMultiMap<int, QString> PluginKeyMap;
95
96 const PluginKeyMap keyMap = loader()->keyMap();
97 const PluginKeyMap::const_iterator cend = keyMap.constEnd();
98 for (PluginKeyMap::const_iterator it = keyMap.constBegin(); it != cend; ++it)
99 list.append(t: it.value());
100#if QT_CONFIG(style_windows)
101 if (!list.contains(str: "Windows"_L1))
102 list << "Windows"_L1;
103#endif
104#if QT_CONFIG(style_fusion)
105 if (!list.contains(str: "Fusion"_L1))
106 list << "Fusion"_L1;
107#endif
108 return list;
109}
110
111QT_END_NAMESPACE
112

source code of qtbase/src/widgets/styles/qstylefactory.cpp