summaryrefslogtreecommitdiff
path: root/src/plugins/debugger/debuggerrunconfigurationaspect.cpp
blob: c02e1922130ece4789cc2d868ff8e5061d5cb56e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "debuggerrunconfigurationaspect.h"

#include "debuggerconstants.h"

#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <coreplugin/helpmanager.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/target.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildstep.h>
#include <projectexplorer/buildsteplist.h>

#include <QCheckBox>
#include <QDebug>
#include <QFormLayout>
#include <QLabel>
#include <QTextEdit>

using namespace ProjectExplorer;
using namespace Debugger::Internal;

namespace Debugger {
namespace Internal {

enum DebuggerLanguageStatus {
    DisabledLanguage = 0,
    EnabledLanguage,
    AutoEnabledLanguage
};

class DebuggerLanguageAspect : public ProjectConfigurationAspect
{
public:
    DebuggerLanguageAspect() = default;

    void addToLayout(LayoutBuilder &builder) override;

    bool value() const;
    void setValue(bool val);

    void setAutoSettingsKey(const QString &settingsKey);
    void setLabel(const QString &label);
    void setInfoLabelText(const QString &text) { m_infoLabelText = text; }

    void setClickCallBack(const std::function<void (bool)> &clickCallBack)
    {
        m_clickCallBack = clickCallBack;
    }

    void fromMap(const QVariantMap &map) override;
    void toMap(QVariantMap &map) const override;

public:
    DebuggerLanguageStatus m_value = AutoEnabledLanguage;
    bool m_defaultValue = false;
    QString m_label;
    QString m_infoLabelText;
    QPointer<QCheckBox> m_checkBox; // Owned by configuration widget
    QPointer<QLabel> m_infoLabel; // Owned by configuration widget
    QString m_autoSettingsKey;

    std::function<void(bool)> m_clickCallBack;
};

void DebuggerLanguageAspect::addToLayout(LayoutBuilder &builder)
{
    QTC_CHECK(!m_checkBox);
    m_checkBox = new QCheckBox(m_label);
    m_checkBox->setChecked(m_value);

    QTC_CHECK(m_clickCallBack);
    connect(m_checkBox, &QAbstractButton::clicked, this, m_clickCallBack, Qt::QueuedConnection);

    connect(m_checkBox.data(), &QAbstractButton::clicked, this, [this] {
        m_value = m_checkBox->isChecked() ? EnabledLanguage : DisabledLanguage;
        emit changed();
    });
    builder.addItem(m_checkBox.data());

    if (!m_infoLabelText.isEmpty()) {
        QTC_CHECK(!m_infoLabel);
        m_infoLabel = new QLabel(m_infoLabelText);
        connect(m_infoLabel, &QLabel::linkActivated, [](const QString &link) {
            Core::HelpManager::showHelpUrl(link);
        });
        builder.addItem(m_infoLabel.data());
    }
}

void DebuggerLanguageAspect::setAutoSettingsKey(const QString &settingsKey)
{
    m_autoSettingsKey = settingsKey;
}

void DebuggerLanguageAspect::fromMap(const QVariantMap &map)
{
    const bool val = map.value(settingsKey(), false).toBool();
    const bool autoVal = map.value(m_autoSettingsKey, false).toBool();
    m_value = autoVal ? AutoEnabledLanguage : val ? EnabledLanguage : DisabledLanguage;
}

void DebuggerLanguageAspect::toMap(QVariantMap &data) const
{
    data.insert(settingsKey(), m_value == EnabledLanguage);
    data.insert(m_autoSettingsKey, m_value == AutoEnabledLanguage);
}


bool DebuggerLanguageAspect::value() const
{
    return m_value;
}

void DebuggerLanguageAspect::setValue(bool value)
{
    m_value = value ? EnabledLanguage : DisabledLanguage;
    if (m_checkBox)
        m_checkBox->setChecked(m_value);
}

void DebuggerLanguageAspect::setLabel(const QString &label)
{
    m_label = label;
}

} // Internal

/*!
    \class Debugger::DebuggerRunConfigurationAspect
*/

DebuggerRunConfigurationAspect::DebuggerRunConfigurationAspect(Target *target)
    : m_target(target)
{
    setId("DebuggerAspect");
    setDisplayName(tr("Debugger settings"));

    setConfigWidgetCreator([this] {
        QWidget *w = new QWidget;
        LayoutBuilder builder(w);
        m_cppAspect->addToLayout(builder);
        m_qmlAspect->addToLayout(builder.startNewRow());
        m_overrideStartupAspect->addToLayout(builder.startNewRow());

        static const QByteArray env = qgetenv("QTC_DEBUGGER_MULTIPROCESS");
        if (env.toInt())
            m_multiProcessAspect->addToLayout(builder.startNewRow());

        return w;
    });

    m_cppAspect = new DebuggerLanguageAspect;
    m_cppAspect->setLabel(tr("Enable C++"));
    m_cppAspect->setSettingsKey("RunConfiguration.UseCppDebugger");
    m_cppAspect->setAutoSettingsKey("RunConfiguration.UseCppDebuggerAuto");

    m_qmlAspect = new DebuggerLanguageAspect;
    m_qmlAspect->setLabel(tr("Enable QML"));
    m_qmlAspect->setSettingsKey("RunConfiguration.UseQmlDebugger");
    m_qmlAspect->setAutoSettingsKey("RunConfiguration.UseQmlDebuggerAuto");
    m_qmlAspect->setInfoLabelText(tr("<a href=\""
        "qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html"
        "\">What are the prerequisites?</a>"));

    // Make sure at least one of the debuggers is set to be active.
    m_cppAspect->setClickCallBack([this](bool on) {
        if (!on && !m_qmlAspect->value())
            m_qmlAspect->setValue(true);
    });
    m_qmlAspect->setClickCallBack([this](bool on) {
        if (!on && !m_cppAspect->value())
            m_cppAspect->setValue(true);
    });

    m_multiProcessAspect = new BaseBoolAspect;
    m_multiProcessAspect->setSettingsKey("RunConfiguration.UseMultiProcess");
    m_multiProcessAspect->setLabel(tr("Enable Debugging of Subprocesses"));

    m_overrideStartupAspect = new BaseStringAspect;
    m_overrideStartupAspect->setSettingsKey("RunConfiguration.OverrideDebuggerStartup");
    m_overrideStartupAspect->setDisplayStyle(BaseStringAspect::TextEditDisplay);
    m_overrideStartupAspect->setLabelText(tr("Additional startup commands:"));
}

DebuggerRunConfigurationAspect::~DebuggerRunConfigurationAspect()
{
    delete m_cppAspect;
    delete m_qmlAspect;
    delete m_multiProcessAspect;
    delete m_overrideStartupAspect;
}

void DebuggerRunConfigurationAspect::setUseQmlDebugger(bool value)
{
    m_qmlAspect->setValue(value);
}

bool DebuggerRunConfigurationAspect::useCppDebugger() const
{
    if (m_cppAspect->m_value == AutoEnabledLanguage)
        return m_target->project()->projectLanguages().contains(
                    ProjectExplorer::Constants::CXX_LANGUAGE_ID);
    return m_cppAspect->m_value == EnabledLanguage;
}

bool DebuggerRunConfigurationAspect::useQmlDebugger() const
{
    if (m_qmlAspect->m_value == AutoEnabledLanguage) {
        const Core::Context languages = m_target->project()->projectLanguages();
        if (!languages.contains(ProjectExplorer::Constants::QMLJS_LANGUAGE_ID))
            return false;

        //
        // Try to find a build step (qmake) to check whether qml debugging is enabled there
        // (Using the Qt metatype system to avoid a hard qt4projectmanager dependency)
        //
        if (BuildConfiguration *bc = m_target->activeBuildConfiguration()) {
            if (BuildStepList *bsl = bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD)) {
                foreach (BuildStep *step, bsl->steps()) {
                    QVariant linkProperty = step->property("linkQmlDebuggingLibrary");
                    if (linkProperty.isValid() && linkProperty.canConvert(QVariant::Bool))
                        return linkProperty.toBool();
                }
            }
        }

        return !languages.contains(ProjectExplorer::Constants::CXX_LANGUAGE_ID);
    }
    return m_qmlAspect->m_value == EnabledLanguage;
}

bool DebuggerRunConfigurationAspect::useMultiProcess() const
{
    return m_multiProcessAspect->value();
}

void DebuggerRunConfigurationAspect::setUseMultiProcess(bool value)
{
    m_multiProcessAspect->setValue(value);
}

QString DebuggerRunConfigurationAspect::overrideStartup() const
{
    return m_overrideStartupAspect->value();
}

int DebuggerRunConfigurationAspect::portsUsedByDebugger() const
{
    int ports = 0;
    if (useQmlDebugger())
        ++ports;
    if (useCppDebugger())
        ++ports;
    return ports;
}

void DebuggerRunConfigurationAspect::toMap(QVariantMap &map) const
{
    m_cppAspect->toMap(map);
    m_qmlAspect->toMap(map);
    m_multiProcessAspect->toMap(map);
    m_overrideStartupAspect->toMap(map);
}

void DebuggerRunConfigurationAspect::fromMap(const QVariantMap &map)
{
    m_cppAspect->fromMap(map);
    m_qmlAspect->fromMap(map);
    m_multiProcessAspect->fromMap(map);
    m_overrideStartupAspect->fromMap(map);
}

} // namespace Debugger