1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qstylefactory.h"
41#include "qstyleplugin.h"
42#include "private/qfactoryloader_p.h"
43#include "qmutex.h"
44
45#include "qapplication.h"
46#include "qwindowsstyle_p.h"
47#if QT_CONFIG(style_fusion)
48#include "qfusionstyle_p.h"
49#endif
50
51QT_BEGIN_NAMESPACE
52
53Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
54 (QStyleFactoryInterface_iid, QLatin1String("/styles"), Qt::CaseInsensitive))
55
56/*!
57 \class QStyleFactory
58 \brief The QStyleFactory class creates QStyle objects.
59
60 \ingroup appearance
61 \inmodule QtWidgets
62
63 The QStyle class is an abstract base class that encapsulates the
64 look and feel of a GUI. QStyleFactory creates a QStyle object
65 using the create() function and a key identifying the style. The
66 styles are either built-in or dynamically loaded from a style
67 plugin (see QStylePlugin).
68
69 The valid keys can be retrieved using the keys()
70 function. Typically they include "windows" and "fusion".
71 Depending on the platform, "windowsvista"
72 and "macos" may be available.
73 Note that keys are case insensitive.
74
75 \sa QStyle
76*/
77
78/*!
79 Creates and returns a QStyle object that matches the given \a key, or
80 returns \nullptr if no matching style is found.
81
82 Both built-in styles and styles from style plugins are queried for a
83 matching style.
84
85 \note The keys used are case insensitive.
86
87 \sa keys()
88*/
89QStyle *QStyleFactory::create(const QString& key)
90{
91 QStyle *ret = nullptr;
92 QString style = key.toLower();
93#if QT_CONFIG(style_windows)
94 if (style == QLatin1String("windows"))
95 ret = new QWindowsStyle;
96 else
97#endif
98#if QT_CONFIG(style_fusion)
99 if (style == QLatin1String("fusion"))
100 ret = new QFusionStyle;
101 else
102#endif
103#if defined(Q_OS_MACOS) && QT_DEPRECATED_SINCE(6, 0)
104 if (style == QLatin1String("macintosh")) {
105 qWarning() << "The style key 'macintosh' is deprecated. Please use 'macos' instead.";
106 style = QStringLiteral("macos");
107 } else
108#endif
109 { } // Keep these here - they make the #ifdefery above work
110 if (!ret)
111 ret = qLoadPlugin<QStyle, QStylePlugin>(loader(), style);
112 if(ret)
113 ret->setObjectName(style);
114 return ret;
115}
116
117/*!
118 Returns the list of valid keys, i.e. the keys this factory can
119 create styles for.
120
121 \sa create()
122*/
123QStringList QStyleFactory::keys()
124{
125 QStringList list;
126 typedef QMultiMap<int, QString> PluginKeyMap;
127
128 const PluginKeyMap keyMap = loader()->keyMap();
129 const PluginKeyMap::const_iterator cend = keyMap.constEnd();
130 for (PluginKeyMap::const_iterator it = keyMap.constBegin(); it != cend; ++it)
131 list.append(it.value());
132#if QT_CONFIG(style_windows)
133 if (!list.contains(QLatin1String("Windows")))
134 list << QLatin1String("Windows");
135#endif
136#if QT_CONFIG(style_fusion)
137 if (!list.contains(QLatin1String("Fusion")))
138 list << QLatin1String("Fusion");
139#endif
140 return list;
141}
142
143QT_END_NAMESPACE
144