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 "projectconfiguration.h"
27 
28 #include "kitinformation.h"
29 #include "target.h"
30 
31 #include <utils/algorithm.h>
32 #include <utils/qtcassert.h>
33 
34 #include <QFormLayout>
35 #include <QWidget>
36 
37 using namespace ProjectExplorer;
38 using namespace Utils;
39 
40 const char CONFIGURATION_ID_KEY[] = "ProjectExplorer.ProjectConfiguration.Id";
41 const char DISPLAY_NAME_KEY[] = "ProjectExplorer.ProjectConfiguration.DisplayName";
42 
43 // ProjectConfiguration
44 
ProjectConfiguration(QObject * parent,Utils::Id id)45 ProjectConfiguration::ProjectConfiguration(QObject *parent, Utils::Id id)
46     : QObject(parent)
47     , m_id(id)
48 {
49     m_aspects.setOwnsSubAspects(true);
50 
51     QTC_CHECK(parent);
52     QTC_CHECK(id.isValid());
53     setObjectName(id.toString());
54 
55     for (QObject *obj = this; obj; obj = obj->parent()) {
56         m_target = qobject_cast<Target *>(obj);
57         if (m_target)
58             break;
59     }
60     QTC_CHECK(m_target);
61 }
62 
63 ProjectConfiguration::~ProjectConfiguration() = default;
64 
project() const65 Project *ProjectConfiguration::project() const
66 {
67     return m_target->project();
68 }
69 
kit() const70 Kit *ProjectConfiguration::kit() const
71 {
72     return m_target->kit();
73 }
74 
id() const75 Utils::Id ProjectConfiguration::id() const
76 {
77     return m_id;
78 }
79 
settingsIdKey()80 QString ProjectConfiguration::settingsIdKey()
81 {
82     return QString(CONFIGURATION_ID_KEY);
83 }
84 
setDisplayName(const QString & name)85 void ProjectConfiguration::setDisplayName(const QString &name)
86 {
87     if (m_displayName.setValue(name))
88         emit displayNameChanged();
89 }
90 
setDefaultDisplayName(const QString & name)91 void ProjectConfiguration::setDefaultDisplayName(const QString &name)
92 {
93     if (m_displayName.setDefaultValue(name))
94         emit displayNameChanged();
95 }
96 
setToolTip(const QString & text)97 void ProjectConfiguration::setToolTip(const QString &text)
98 {
99     if (text == m_toolTip)
100         return;
101     m_toolTip = text;
102     emit toolTipChanged();
103 }
104 
toolTip() const105 QString ProjectConfiguration::toolTip() const
106 {
107     return m_toolTip;
108 }
109 
toMap() const110 QVariantMap ProjectConfiguration::toMap() const
111 {
112     QTC_CHECK(m_id.isValid());
113     QVariantMap map;
114     map.insert(QLatin1String(CONFIGURATION_ID_KEY), m_id.toSetting());
115     m_displayName.toMap(map, DISPLAY_NAME_KEY);
116     m_aspects.toMap(map);
117     return map;
118 }
119 
target() const120 Target *ProjectConfiguration::target() const
121 {
122     return m_target;
123 }
124 
fromMap(const QVariantMap & map)125 bool ProjectConfiguration::fromMap(const QVariantMap &map)
126 {
127     Utils::Id id = Utils::Id::fromSetting(map.value(QLatin1String(CONFIGURATION_ID_KEY)));
128     // Note: This is only "startsWith", not ==, as RunConfigurations currently still
129     // mangle in their build keys.
130     QTC_ASSERT(id.toString().startsWith(m_id.toString()), return false);
131 
132     m_displayName.fromMap(map, DISPLAY_NAME_KEY);
133     m_aspects.fromMap(map);
134     return true;
135 }
136 
aspect(Utils::Id id) const137 Utils::BaseAspect *ProjectConfiguration::aspect(Utils::Id id) const
138 {
139     return m_aspects.aspect(id);
140 }
141 
acquaintAspects()142 void ProjectConfiguration::acquaintAspects()
143 {
144     for (Utils::BaseAspect *aspect : m_aspects)
145         aspect->acquaintSiblings(m_aspects);
146 }
147 
mapFromBuildDeviceToGlobalPath(const FilePath & path) const148 FilePath ProjectConfiguration::mapFromBuildDeviceToGlobalPath(const FilePath &path) const
149 {
150     IDevice::ConstPtr dev = BuildDeviceKitAspect::device(kit());
151     QTC_ASSERT(dev, return path);
152     return dev->mapToGlobalPath(path);
153 }
154 
idFromMap(const QVariantMap & map)155 Utils::Id ProjectExplorer::idFromMap(const QVariantMap &map)
156 {
157     return Utils::Id::fromSetting(map.value(QLatin1String(CONFIGURATION_ID_KEY)));
158 }
159 
expandedDisplayName() const160 QString ProjectConfiguration::expandedDisplayName() const
161 {
162     return m_target->macroExpander()->expand(m_displayName.value());
163 }
164