1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "idevicefactory.h"
27 
28 #include <utils/algorithm.h>
29 #include <utils/icon.h>
30 
31 namespace ProjectExplorer {
32 
33 /*!
34     \class ProjectExplorer::IDeviceFactory
35 
36     \brief The IDeviceFactory class implements an interface for classes that
37     provide services related to a certain type of device.
38 
39     The factory objects have to be added to the global object pool via
40     \c ExtensionSystem::PluginManager::addObject().
41 
42     \sa ExtensionSystem::PluginManager::addObject()
43 */
44 
45 /*!
46     \fn virtual QString displayName() const = 0
47 
48     Returns a short, one-line description of the device type this factory
49     can create.
50 */
51 
52 /*!
53     \fn virtual IDevice::Ptr create() const
54     Creates a new device. This may or may not open a wizard.
55 */
56 
57 /*!
58     \fn virtual bool canRestore(const QVariantMap &map) const = 0
59 
60     Checks whether this factory can restore a device from the serialized state
61     specified by \a map.
62 */
63 
64 /*!
65     \fn virtual IDevice::Ptr restore(const QVariantMap &map) const = 0
66 
67     Loads a device from a serialized state. Only called if \c canRestore()
68     returns true for \a map.
69 */
70 
71 /*!
72     Checks whether this factory can create new devices. This function is used
73     to hide auto-detect-only factories from the listing of possible devices
74     to create.
75 */
76 
canCreate() const77 bool IDeviceFactory::canCreate() const
78 {
79     return m_canCreate;
80 }
81 
construct() const82 IDevice::Ptr IDeviceFactory::construct() const
83 {
84     return m_constructor ? m_constructor() : IDevice::Ptr();
85 }
86 
87 static QList<IDeviceFactory *> g_deviceFactories;
88 
find(Utils::Id type)89 IDeviceFactory *IDeviceFactory::find(Utils::Id type)
90 {
91     return Utils::findOrDefault(g_deviceFactories,
92         [&type](IDeviceFactory *factory) {
93             return factory->deviceType() == type;
94         });
95 }
96 
IDeviceFactory(Utils::Id deviceType)97 IDeviceFactory::IDeviceFactory(Utils::Id deviceType)
98     : m_deviceType(deviceType)
99 {
100     g_deviceFactories.append(this);
101 }
102 
setIcon(const QIcon & icon)103 void IDeviceFactory::setIcon(const QIcon &icon)
104 {
105     m_icon = icon;
106 }
107 
setCombinedIcon(const QString & small,const QString & large)108 void IDeviceFactory::setCombinedIcon(const QString &small, const QString &large)
109 {
110     using namespace Utils;
111     m_icon = Icon::combinedIcon({Icon({{small, Theme::PanelTextColorDark}}, Icon::Tint),
112                                  Icon({{large, Theme::IconsBaseColor}})});
113 }
114 
setCanCreate(bool canCreate)115 void IDeviceFactory::setCanCreate(bool canCreate)
116 {
117     m_canCreate = canCreate;
118 }
119 
setConstructionFunction(const std::function<IDevice::Ptr ()> & constructor)120 void IDeviceFactory::setConstructionFunction(const std::function<IDevice::Ptr ()> &constructor)
121 {
122     m_constructor = constructor;
123 }
124 
setDisplayName(const QString & displayName)125 void IDeviceFactory::setDisplayName(const QString &displayName)
126 {
127     m_displayName = displayName;
128 }
129 
~IDeviceFactory()130 IDeviceFactory::~IDeviceFactory()
131 {
132     g_deviceFactories.removeOne(this);
133 }
134 
allDeviceFactories()135 const QList<IDeviceFactory *> IDeviceFactory::allDeviceFactories()
136 {
137     return g_deviceFactories;
138 }
139 
140 } // namespace ProjectExplorer
141