qtkitinformation.cpp 9.07 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
Tobias Hunger's avatar
Tobias Hunger committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
30

Tobias Hunger's avatar
Tobias Hunger committed
31
#include "qtkitinformation.h"
Tobias Hunger's avatar
Tobias Hunger committed
32

Tobias Hunger's avatar
Tobias Hunger committed
33
#include "qtkitconfigwidget.h"
34
#include "qtsupportconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
35
#include "qtversionmanager.h"
36
#include "qtparser.h"
Tobias Hunger's avatar
Tobias Hunger committed
37

38
#include <utils/algorithm.h>
39
#include <utils/buildablehelperlibrary.h>
40
#include <utils/macroexpander.h>
41
#include <utils/qtcassert.h>
Tobias Hunger's avatar
Tobias Hunger committed
42

43
using namespace ProjectExplorer;
44
using namespace Utils;
45

Tobias Hunger's avatar
Tobias Hunger committed
46
namespace QtSupport {
47

Tobias Hunger's avatar
Tobias Hunger committed
48
QtKitInformation::QtKitInformation()
Tobias Hunger's avatar
Tobias Hunger committed
49
{
Tobias Hunger's avatar
Tobias Hunger committed
50
    setObjectName(QLatin1String("QtKitInformation"));
51
    setId(QtKitInformation::id());
52
53
    setPriority(26000);

54
55
    connect(ProjectExplorer::KitManager::instance(), SIGNAL(kitsLoaded()),
            this, SLOT(kitsWereLoaded()));
Tobias Hunger's avatar
Tobias Hunger committed
56
57
}

58
QVariant QtKitInformation::defaultValue(ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
59
{
60
    Q_UNUSED(k);
Tobias Hunger's avatar
Tobias Hunger committed
61
62

    // find "Qt in PATH":
63
    QList<BaseQtVersion *> versionList = QtVersionManager::unsortedVersions();
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    BaseQtVersion *result = findOrDefault(versionList, [](const BaseQtVersion *v) {
        return v->autodetectionSource() == QLatin1String("PATH");
    });

    if (result)
        return result->uniqueId();

    // Legacy: Check for system qmake path: Remove in 3.5 (or later):
    // This check is expensive as it will potentially run binaries (qmake --version)!
    const FileName qmakePath
            = BuildableHelperLibrary::findSystemQt(Utils::Environment::systemEnvironment());

    if (!qmakePath.isEmpty()) {
        result = findOrDefault(versionList, [qmakePath](const BaseQtVersion *v) {
            return v->qmakeCommand() == qmakePath;
        });
80
81
        if (result)
            return result->uniqueId();
Tobias Hunger's avatar
Tobias Hunger committed
82
83
    }

84
85
86
87
88
    // Use *any* desktop Qt:
    result = findOrDefault(versionList, [](const BaseQtVersion *v) {
        return v->type() == QLatin1String(QtSupport::Constants::DESKTOPQT);
    });
    return result ? result->uniqueId() : -1;
Tobias Hunger's avatar
Tobias Hunger committed
89
90
}

91
QList<ProjectExplorer::Task> QtKitInformation::validate(const ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
92
{
93
    QList<ProjectExplorer::Task> result;
hjk's avatar
hjk committed
94
    QTC_ASSERT(QtVersionManager::isLoaded(), return result);
95
96
    BaseQtVersion *version = qtVersion(k);
    if (!version)
97
        return result;
98
    return version->validateKit(k);
Tobias Hunger's avatar
Tobias Hunger committed
99
100
}

101
102
void QtKitInformation::fix(ProjectExplorer::Kit *k)
{
hjk's avatar
hjk committed
103
    QTC_ASSERT(QtVersionManager::isLoaded(), return);
104
    BaseQtVersion *version = qtVersion(k);
105
    if (!version && qtVersionId(k) >= 0) {
106
        qWarning("Qt version is no longer known, removing from kit \"%s\".", qPrintable(k->displayName()));
107
        setQtVersionId(k, -1);
108
    }
109
110
}

111
ProjectExplorer::KitConfigWidget *QtKitInformation::createConfigWidget(ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
112
{
113
    return new Internal::QtKitConfigWidget(k, this);
Tobias Hunger's avatar
Tobias Hunger committed
114
115
}

116
QString QtKitInformation::displayNamePostfix(const ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
117
{
118
    BaseQtVersion *version = qtVersion(k);
Tobias Hunger's avatar
Tobias Hunger committed
119
120
121
    return version ? version->displayName() : QString();
}

Tobias Hunger's avatar
Tobias Hunger committed
122
ProjectExplorer::KitInformation::ItemList
123
QtKitInformation::toUserOutput(const ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
124
{
125
    BaseQtVersion *version = qtVersion(k);
Tobias Hunger's avatar
Tobias Hunger committed
126
127
128
    return ItemList() << qMakePair(tr("Qt version"), version ? version->displayName() : tr("None"));
}

129
void QtKitInformation::addToEnvironment(const ProjectExplorer::Kit *k, Utils::Environment &env) const
Tobias Hunger's avatar
Tobias Hunger committed
130
{
131
    BaseQtVersion *version = qtVersion(k);
Tobias Hunger's avatar
Tobias Hunger committed
132
    if (version)
133
        version->addToEnvironment(k, env);
Tobias Hunger's avatar
Tobias Hunger committed
134
135
}

136
137
138
139
140
141
142
ProjectExplorer::IOutputParser *QtKitInformation::createOutputParser(const ProjectExplorer::Kit *k) const
{
    if (qtVersion(k))
        return new QtParser;
    return 0;
}

hjk's avatar
hjk committed
143
void QtKitInformation::addToMacroExpander(Kit *kit, MacroExpander *expander) const
144
{
hjk's avatar
hjk committed
145
146
147
148
149
150
    expander->registerSubProvider(
                [this, kit]() -> MacroExpander * {
                    BaseQtVersion *version = qtVersion(kit);
                    return version ? version->macroExpander() : 0;
                });

hjk's avatar
hjk committed
151
    expander->registerVariable("Qt:Name", tr("Name of Qt Version"),
hjk's avatar
hjk committed
152
153
154
155
                [this, kit]() -> QString {
                   BaseQtVersion *version = qtVersion(kit);
                   return version ? version->displayName() : tr("unknown");
                });
156
157
}

158
159
160
161
162
Core::Id QtKitInformation::id()
{
    return "QtSupport.QtInformation";
}

163
int QtKitInformation::qtVersionId(const ProjectExplorer::Kit *k)
Tobias Hunger's avatar
Tobias Hunger committed
164
{
165
    if (!k)
Tobias Hunger's avatar
Tobias Hunger committed
166
        return -1;
167
168

    int id = -1;
169
    QVariant data = k->value(QtKitInformation::id(), -1);
170
171
172
173
174
175
176
    if (data.type() == QVariant::Int) {
        bool ok;
        id = data.toInt(&ok);
        if (!ok)
            id = -1;
    } else {
        QString source = data.toString();
177
        foreach (BaseQtVersion *v, QtVersionManager::unsortedVersions()) {
178
179
180
181
182
183
            if (v->autodetectionSource() != source)
                continue;
            id = v->uniqueId();
            break;
        }
    }
Tobias Hunger's avatar
Tobias Hunger committed
184
185
186
    return id;
}

187
void QtKitInformation::setQtVersionId(ProjectExplorer::Kit *k, const int id)
Tobias Hunger's avatar
Tobias Hunger committed
188
{
189
    k->setValue(QtKitInformation::id(), id);
Tobias Hunger's avatar
Tobias Hunger committed
190
191
}

192
BaseQtVersion *QtKitInformation::qtVersion(const ProjectExplorer::Kit *k)
Tobias Hunger's avatar
Tobias Hunger committed
193
{
hjk's avatar
hjk committed
194
    return QtVersionManager::version(qtVersionId(k));
Tobias Hunger's avatar
Tobias Hunger committed
195
196
}

197
void QtKitInformation::setQtVersion(ProjectExplorer::Kit *k, const BaseQtVersion *v)
Tobias Hunger's avatar
Tobias Hunger committed
198
199
{
    if (!v)
200
        setQtVersionId(k, -1);
Tobias Hunger's avatar
Tobias Hunger committed
201
    else
202
        setQtVersionId(k, v->uniqueId());
Tobias Hunger's avatar
Tobias Hunger committed
203
204
}

205
206
207
208
209
210
void QtKitInformation::qtVersionsChanged(const QList<int> &addedIds,
                                         const QList<int> &removedIds,
                                         const QList<int> &changedIds)
{
    Q_UNUSED(addedIds);
    Q_UNUSED(removedIds);
211
212
213
    foreach (ProjectExplorer::Kit *k, ProjectExplorer::KitManager::kits()) {
        if (changedIds.contains(qtVersionId(k))) {
            k->validate(); // Qt version may have become (in)valid
214
            notifyAboutUpdate(k);
215
216
        }
    }
217
218
}

219
220
void QtKitInformation::kitsWereLoaded()
{
221
    foreach (ProjectExplorer::Kit *k, ProjectExplorer::KitManager::kits())
222
223
224
225
226
227
        fix(k);

    connect(QtVersionManager::instance(), SIGNAL(qtVersionsChanged(QList<int>,QList<int>,QList<int>)),
            this, SLOT(qtVersionsChanged(QList<int>,QList<int>,QList<int>)));
}

228
KitMatcher QtKitInformation::platformMatcher(const QString &platform)
Tobias Hunger's avatar
Tobias Hunger committed
229
{
230
231
232
233
    return std::function<bool(const Kit *)>([platform](const Kit *kit) -> bool {
        BaseQtVersion *version = QtKitInformation::qtVersion(kit);
        return version && version->platformName() == platform;
    });
Tobias Hunger's avatar
Tobias Hunger committed
234
235
}

236
237
KitMatcher QtKitInformation::qtVersionMatcher(const Core::FeatureSet &required,
    const QtVersionNumber &min, const QtVersionNumber &max)
Tobias Hunger's avatar
Tobias Hunger committed
238
{
239
240
241
242
243
244
245
246
247
248
249
    return std::function<bool(const Kit *)>([required, min, max](const Kit *kit) -> bool {
        BaseQtVersion *version = QtKitInformation::qtVersion(kit);
        if (!version)
            return false;
        QtVersionNumber current = version->qtVersion();
        if (min.majorVersion > -1 && current < min)
            return false;
        if (max.majorVersion > -1 && current > max)
            return false;
        return version->availableFeatures().contains(required);
    });
Tobias Hunger's avatar
Tobias Hunger committed
250
251
}

252
253
254
255
QSet<QString> QtKitInformation::availablePlatforms(const Kit *k) const
{
    QSet<QString> result;
    BaseQtVersion *version = QtKitInformation::qtVersion(k);
256
257
258
259
260
    if (version) {
        QString platform = version->platformName();
        if (!platform.isEmpty())
            result.insert(version->platformName());
    }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    return result;
}

QString QtKitInformation::displayNameForPlatform(const Kit *k, const QString &platform) const
{
    BaseQtVersion *version = QtKitInformation::qtVersion(k);
    if (version && version->platformName() == platform)
        return version->platformDisplayName();
    return QString();
}

Core::FeatureSet QtKitInformation::availableFeatures(const Kit *k) const
{
    BaseQtVersion *version = QtKitInformation::qtVersion(k);
    return version ? version->availableFeatures() : Core::FeatureSet();
}

Tobias Hunger's avatar
Tobias Hunger committed
278
} // namespace QtSupport