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 
51 QT_BEGIN_NAMESPACE
52 
53 Q_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 "macintosh" 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 */
create(const QString & key)89 QStyle *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     { } // Keep these here - they make the #ifdefery above work
104     if (!ret)
105         ret = qLoadPlugin<QStyle, QStylePlugin>(loader(), style);
106     if(ret)
107         ret->setObjectName(style);
108     return ret;
109 }
110 
111 /*!
112     Returns the list of valid keys, i.e. the keys this factory can
113     create styles for.
114 
115     \sa create()
116 */
keys()117 QStringList QStyleFactory::keys()
118 {
119     QStringList list;
120     typedef QMultiMap<int, QString> PluginKeyMap;
121 
122     const PluginKeyMap keyMap = loader()->keyMap();
123     const PluginKeyMap::const_iterator cend = keyMap.constEnd();
124     for (PluginKeyMap::const_iterator it = keyMap.constBegin(); it != cend; ++it)
125         list.append(it.value());
126 #if QT_CONFIG(style_windows)
127     if (!list.contains(QLatin1String("Windows")))
128         list << QLatin1String("Windows");
129 #endif
130 #if QT_CONFIG(style_fusion)
131     if (!list.contains(QLatin1String("Fusion")))
132         list << QLatin1String("Fusion");
133 #endif
134     return list;
135 }
136 
137 QT_END_NAMESPACE
138