cmakerunconfiguration.cpp 14.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
dt's avatar
dt committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
dt's avatar
dt committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
dt's avatar
dt committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12
** 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 Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
dt's avatar
dt 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 25 26
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
dt's avatar
dt committed
30 31

#include "cmakerunconfiguration.h"
hjk's avatar
hjk committed
32

33
#include "cmakebuildconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
#include "cmakeproject.h"
hjk's avatar
hjk committed
35
#include "cmakeprojectconstants.h"
dt's avatar
dt committed
36

37
#include <coreplugin/coreconstants.h>
38
#include <coreplugin/helpmanager.h>
39
#include <qtsupport/qtkitinformation.h>
40
#include <projectexplorer/localenvironmentaspect.h>
Tobias Hunger's avatar
Tobias Hunger committed
41
#include <projectexplorer/target.h>
42 43 44

#include <utils/pathchooser.h>
#include <utils/detailswidget.h>
hjk's avatar
hjk committed
45
#include <utils/qtcassert.h>
46 47
#include <utils/qtcprocess.h>
#include <utils/stringutils.h>
48

49 50 51 52 53 54 55
#include <QFormLayout>
#include <QLineEdit>
#include <QGroupBox>
#include <QLabel>
#include <QComboBox>
#include <QToolButton>
#include <QCheckBox>
dt's avatar
dt committed
56 57 58

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
Orgad Shaneh's avatar
Orgad Shaneh committed
59
using namespace ProjectExplorer;
dt's avatar
dt committed
60

61
namespace {
62
const char CMAKE_RC_PREFIX[] = "CMakeProjectManager.CMakeRunConfiguration.";
63

64 65 66 67
const char USER_WORKING_DIRECTORY_KEY[] = "CMakeProjectManager.CMakeRunConfiguration.UserWorkingDirectory";
const char USE_TERMINAL_KEY[] = "CMakeProjectManager.CMakeRunConfiguration.UseTerminal";
const char TITLE_KEY[] = "CMakeProjectManager.CMakeRunConfiguation.Title";
const char ARGUMENTS_KEY[] = "CMakeProjectManager.CMakeRunConfiguration.Arguments";
68 69 70

} // namespace

Orgad Shaneh's avatar
Orgad Shaneh committed
71
CMakeRunConfiguration::CMakeRunConfiguration(Target *parent, Core::Id id, const QString &target,
Tobias Hunger's avatar
Tobias Hunger committed
72
                                             const QString &workingDirectory, const QString &title) :
Orgad Shaneh's avatar
Orgad Shaneh committed
73
    LocalApplicationRunConfiguration(parent, id),
74
    m_runMode(ProjectExplorer::ApplicationLauncher::Gui),
75
    m_buildTarget(target),
76 77
    m_workingDirectory(workingDirectory),
    m_title(title),
78
    m_enabled(true)
dt's avatar
dt committed
79
{
Orgad Shaneh's avatar
Orgad Shaneh committed
80
    addExtraAspect(new LocalEnvironmentAspect(this));
81 82
    ctor();
}
dt's avatar
dt committed
83

Orgad Shaneh's avatar
Orgad Shaneh committed
84 85
CMakeRunConfiguration::CMakeRunConfiguration(Target *parent, CMakeRunConfiguration *source) :
    LocalApplicationRunConfiguration(parent, source),
86
    m_runMode(source->m_runMode),
87
    m_buildTarget(source->m_buildTarget),
88 89 90 91
    m_workingDirectory(source->m_workingDirectory),
    m_userWorkingDirectory(source->m_userWorkingDirectory),
    m_title(source->m_title),
    m_arguments(source->m_arguments),
92
    m_enabled(source->m_enabled)
93 94
{
    ctor();
dt's avatar
dt committed
95 96
}

97
CMakeRunConfiguration::~CMakeRunConfiguration()
dt's avatar
dt committed
98
{
dt's avatar
dt committed
99 100
}

101
void CMakeRunConfiguration::ctor()
dt's avatar
dt committed
102
{
103
    setDefaultDisplayName(defaultDisplayName());
Tobias Hunger's avatar
Tobias Hunger committed
104
}
105

dt's avatar
dt committed
106 107
QString CMakeRunConfiguration::executable() const
{
108
    return m_buildTarget;
dt's avatar
dt committed
109 110
}

111
ApplicationLauncher::Mode CMakeRunConfiguration::runMode() const
dt's avatar
dt committed
112
{
113
    return m_runMode;
dt's avatar
dt committed
114 115
}

116
void CMakeRunConfiguration::setRunMode(ApplicationLauncher::Mode runMode)
117 118 119 120
{
    m_runMode = runMode;
}

dt's avatar
dt committed
121
QString CMakeRunConfiguration::workingDirectory() const
122
{
Orgad Shaneh's avatar
Orgad Shaneh committed
123
    EnvironmentAspect *aspect = extraAspect<EnvironmentAspect>();
124 125
    QTC_ASSERT(aspect, return QString());
    return QDir::cleanPath(aspect->environment().expandVariables(
126
                macroExpander()->expand(baseWorkingDirectory())));
127 128 129
}

QString CMakeRunConfiguration::baseWorkingDirectory() const
dt's avatar
dt committed
130
{
131 132
    if (!m_userWorkingDirectory.isEmpty())
        return m_userWorkingDirectory;
dt's avatar
dt committed
133 134 135
    return m_workingDirectory;
}

136
QString CMakeRunConfiguration::commandLineArguments() const
137
{
hjk's avatar
hjk committed
138
    return macroExpander()->expandProcessArgs(m_arguments);
dt's avatar
dt committed
139 140
}

dt's avatar
dt committed
141 142 143 144 145 146 147
QString CMakeRunConfiguration::title() const
{
    return m_title;
}

void CMakeRunConfiguration::setExecutable(const QString &executable)
{
148
    m_buildTarget = executable;
dt's avatar
dt committed
149 150
}

151
void CMakeRunConfiguration::setBaseWorkingDirectory(const QString &wd)
dt's avatar
dt committed
152
{
153
    const QString &oldWorkingDirectory = workingDirectory();
154 155 156 157 158

    m_workingDirectory = wd;

    const QString &newWorkingDirectory = workingDirectory();
    if (oldWorkingDirectory != newWorkingDirectory)
159
        emit baseWorkingDirectoryChanged(newWorkingDirectory);
160 161 162 163 164 165 166 167 168 169
}

void CMakeRunConfiguration::setUserWorkingDirectory(const QString &wd)
{
    const QString & oldWorkingDirectory = workingDirectory();

    m_userWorkingDirectory = wd;

    const QString &newWorkingDirectory = workingDirectory();
    if (oldWorkingDirectory != newWorkingDirectory)
170
        emit baseWorkingDirectoryChanged(newWorkingDirectory);
dt's avatar
dt committed
171 172
}

173
QVariantMap CMakeRunConfiguration::toMap() const
dt's avatar
dt committed
174
{
Orgad Shaneh's avatar
Orgad Shaneh committed
175
    QVariantMap map(LocalApplicationRunConfiguration::toMap());
176 177

    map.insert(QLatin1String(USER_WORKING_DIRECTORY_KEY), m_userWorkingDirectory);
178
    map.insert(QLatin1String(USE_TERMINAL_KEY), m_runMode == ApplicationLauncher::Console);
179 180
    map.insert(QLatin1String(TITLE_KEY), m_title);
    map.insert(QLatin1String(ARGUMENTS_KEY), m_arguments);
181

182
    return map;
dt's avatar
dt committed
183 184
}

185
bool CMakeRunConfiguration::fromMap(const QVariantMap &map)
dt's avatar
dt committed
186
{
187
    m_userWorkingDirectory = map.value(QLatin1String(USER_WORKING_DIRECTORY_KEY)).toString();
188 189
    m_runMode = map.value(QLatin1String(USE_TERMINAL_KEY)).toBool() ? ApplicationLauncher::Console
                                                                    : ApplicationLauncher::Gui;
190 191 192 193
    m_title = map.value(QLatin1String(TITLE_KEY)).toString();
    m_arguments = map.value(QLatin1String(ARGUMENTS_KEY)).toString();

    return RunConfiguration::fromMap(map);
dt's avatar
dt committed
194 195
}

196 197 198
QString CMakeRunConfiguration::defaultDisplayName() const
{
    if (m_title.isEmpty())
199
        return tr("Run CMake kit");
200 201 202 203 204 205
    QString result = m_title;
    if (!m_enabled) {
        result += QLatin1Char(' ');
        result += tr("(disabled)");
    }
    return result;
206 207
}

208
QWidget *CMakeRunConfiguration::createConfigurationWidget()
dt's avatar
dt committed
209
{
210
    return new CMakeRunConfigurationWidget(this);
211 212
}

213
void CMakeRunConfiguration::setCommandLineArguments(const QString &newText)
214 215
{
    m_arguments = newText;
dt's avatar
dt committed
216 217
}

218 219 220 221 222
void CMakeRunConfiguration::setEnabled(bool b)
{
    if (m_enabled == b)
        return;
    m_enabled = b;
223
    emit enabledChanged();
224
    setDefaultDisplayName(defaultDisplayName());
225
}
226

227
bool CMakeRunConfiguration::isEnabled() const
228
{
229
    return m_enabled;
230
}
231

dt_'s avatar
dt_ committed
232 233 234
QString CMakeRunConfiguration::disabledReason() const
{
    if (!m_enabled)
Friedemann Kleint's avatar
Friedemann Kleint committed
235
        return tr("The executable is not built by the current build configuration");
dt_'s avatar
dt_ committed
236 237 238
    return QString();
}

239
// Configuration widget
240
CMakeRunConfigurationWidget::CMakeRunConfigurationWidget(CMakeRunConfiguration *cmakeRunConfiguration, QWidget *parent)
241
    : QWidget(parent), m_ignoreChange(false), m_cmakeRunConfiguration(cmakeRunConfiguration)
242 243
{
    QFormLayout *fl = new QFormLayout();
244 245
    fl->setMargin(0);
    fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
246
    QLineEdit *argumentsLineEdit = new QLineEdit();
247
    argumentsLineEdit->setText(cmakeRunConfiguration->commandLineArguments());
Montel Laurent's avatar
Montel Laurent committed
248
    connect(argumentsLineEdit, &QLineEdit::textChanged, this, &CMakeRunConfigurationWidget::setArguments);
249 250
    fl->addRow(tr("Arguments:"), argumentsLineEdit);

251
    m_workingDirectoryEdit = new Utils::PathChooser();
252
    m_workingDirectoryEdit->setExpectedKind(Utils::PathChooser::Directory);
253
    m_workingDirectoryEdit->setBaseFileName(m_cmakeRunConfiguration->target()->project()->projectDirectory());
254
    m_workingDirectoryEdit->setPath(m_cmakeRunConfiguration->baseWorkingDirectory());
255
    m_workingDirectoryEdit->setHistoryCompleter(QLatin1String("CMake.WorkingDir.History"));
Orgad Shaneh's avatar
Orgad Shaneh committed
256 257
    EnvironmentAspect *aspect
            = m_cmakeRunConfiguration->extraAspect<EnvironmentAspect>();
258
    if (aspect) {
Montel Laurent's avatar
Montel Laurent committed
259 260
        connect(aspect, &EnvironmentAspect::environmentChanged,
                this, &CMakeRunConfigurationWidget::environmentWasChanged);
261 262
        environmentWasChanged();
    }
263
    m_workingDirectoryEdit->setPromptDialogTitle(tr("Select Working Directory"));
264 265

    QToolButton *resetButton = new QToolButton();
Jarek Kobus's avatar
Jarek Kobus committed
266
    resetButton->setToolTip(tr("Reset to default."));
267
    resetButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_RESET)));
268 269 270 271 272

    QHBoxLayout *boxlayout = new QHBoxLayout();
    boxlayout->addWidget(m_workingDirectoryEdit);
    boxlayout->addWidget(resetButton);

273
    fl->addRow(tr("Working directory:"), boxlayout);
274

275 276 277
    QCheckBox *runInTerminal = new QCheckBox;
    fl->addRow(tr("Run in Terminal"), runInTerminal);

278
    m_detailsContainer = new Utils::DetailsWidget(this);
279
    m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
dt's avatar
dt committed
280

281 282 283
    QWidget *m_details = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(m_details);
    m_details->setLayout(fl);
dt's avatar
dt committed
284

285
    QVBoxLayout *vbx = new QVBoxLayout(this);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
286
    vbx->setMargin(0);
287
    vbx->addWidget(m_detailsContainer);
288

Montel Laurent's avatar
Montel Laurent committed
289 290
    connect(m_workingDirectoryEdit, &Utils::PathChooser::changed,
            this, &CMakeRunConfigurationWidget::setWorkingDirectory);
291

Montel Laurent's avatar
Montel Laurent committed
292 293
    connect(resetButton, &QToolButton::clicked,
            this, &CMakeRunConfigurationWidget::resetWorkingDirectory);
294

Montel Laurent's avatar
Montel Laurent committed
295 296
    connect(runInTerminal, &QCheckBox::toggled,
            this, &CMakeRunConfigurationWidget::runInTerminalToggled);
297

Montel Laurent's avatar
Montel Laurent committed
298 299
    connect(m_cmakeRunConfiguration, &CMakeRunConfiguration::baseWorkingDirectoryChanged,
            this, &CMakeRunConfigurationWidget::workingDirectoryChanged);
300

301
    setEnabled(m_cmakeRunConfiguration->isEnabled());
302 303 304 305 306 307 308
}

void CMakeRunConfigurationWidget::setWorkingDirectory()
{
    if (m_ignoreChange)
        return;
    m_ignoreChange = true;
309
    m_cmakeRunConfiguration->setUserWorkingDirectory(m_workingDirectoryEdit->rawPath());
310 311 312 313 314
    m_ignoreChange = false;
}

void CMakeRunConfigurationWidget::workingDirectoryChanged(const QString &workingDirectory)
{
315 316
    if (!m_ignoreChange) {
        m_ignoreChange = true;
317
        m_workingDirectoryEdit->setPath(workingDirectory);
318 319
        m_ignoreChange = false;
    }
320 321 322 323 324 325
}

void CMakeRunConfigurationWidget::resetWorkingDirectory()
{
    // This emits a signal connected to workingDirectoryChanged()
    // that sets the m_workingDirectoryEdit
326
    m_cmakeRunConfiguration->setUserWorkingDirectory(QString());
327 328
}

329 330
void CMakeRunConfigurationWidget::runInTerminalToggled(bool toggled)
{
331 332
    m_cmakeRunConfiguration->setRunMode(toggled ? ApplicationLauncher::Console
                                                : ApplicationLauncher::Gui);
333 334
}

335 336
void CMakeRunConfigurationWidget::environmentWasChanged()
{
Orgad Shaneh's avatar
Orgad Shaneh committed
337
    EnvironmentAspect *aspect = m_cmakeRunConfiguration->extraAspect<EnvironmentAspect>();
338 339 340
    QTC_ASSERT(aspect, return);
    m_workingDirectoryEdit->setEnvironment(aspect->environment());
}
341 342
void CMakeRunConfigurationWidget::setArguments(const QString &args)
{
343
    m_cmakeRunConfiguration->setCommandLineArguments(args);
dt's avatar
dt committed
344 345
}

dt's avatar
dt committed
346
// Factory
347
CMakeRunConfigurationFactory::CMakeRunConfigurationFactory(QObject *parent) :
Orgad Shaneh's avatar
Orgad Shaneh committed
348
    IRunConfigurationFactory(parent)
Tobias Hunger's avatar
Tobias Hunger committed
349
{ setObjectName(QLatin1String("CMakeRunConfigurationFactory")); }
dt's avatar
dt committed
350 351 352 353 354

CMakeRunConfigurationFactory::~CMakeRunConfigurationFactory()
{
}

355
// used to show the list of possible additons to a project, returns a list of ids
356
QList<Core::Id> CMakeRunConfigurationFactory::availableCreationIds(Target *parent, CreationMode mode) const
dt's avatar
dt committed
357
{
358
    Q_UNUSED(mode)
Tobias Hunger's avatar
Tobias Hunger committed
359
    if (!canHandle(parent))
360
        return QList<Core::Id>();
361
    CMakeProject *project = static_cast<CMakeProject *>(parent->project());
362
    QList<Core::Id> allIds;
363
    foreach (const QString &buildTarget, project->buildTargetTitles(true))
364
        allIds << idFromBuildTarget(buildTarget);
365
    return allIds;
dt's avatar
dt committed
366 367
}

368
// used to translate the ids to names to display to the user
369
QString CMakeRunConfigurationFactory::displayNameForId(Core::Id id) const
dt's avatar
dt committed
370
{
371
    return buildTargetFromId(id);
372
}
dt's avatar
dt committed
373

Orgad Shaneh's avatar
Orgad Shaneh committed
374
bool CMakeRunConfigurationFactory::canHandle(Target *parent) const
Tobias Hunger's avatar
Tobias Hunger committed
375
{
Tobias Hunger's avatar
Tobias Hunger committed
376
    if (!parent->project()->supportsKit(parent->kit()))
Tobias Hunger's avatar
Tobias Hunger committed
377 378 379 380
        return false;
    return qobject_cast<CMakeProject *>(parent->project());
}

381
bool CMakeRunConfigurationFactory::canCreate(Target *parent, Core::Id id) const
382
{
Tobias Hunger's avatar
Tobias Hunger committed
383
    if (!canHandle(parent))
384
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
385 386
    CMakeProject *project = static_cast<CMakeProject *>(parent->project());
    return project->hasBuildTarget(buildTargetFromId(id));
dt's avatar
dt committed
387 388
}

389
RunConfiguration *CMakeRunConfigurationFactory::doCreate(Target *parent, Core::Id id)
dt's avatar
dt committed
390
{
Tobias Hunger's avatar
Tobias Hunger committed
391
    CMakeProject *project = static_cast<CMakeProject *>(parent->project());
392
    const QString title(buildTargetFromId(id));
Tobias Hunger's avatar
Tobias Hunger committed
393 394
    const CMakeBuildTarget &ct = project->buildTargetForTitle(title);
    return new CMakeRunConfiguration(parent, id, ct.executable, ct.workingDirectory, ct.title);
395 396
}

Orgad Shaneh's avatar
Orgad Shaneh committed
397
bool CMakeRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const
398
{
Tobias Hunger's avatar
Tobias Hunger committed
399
    if (!canHandle(parent))
400
        return false;
hjk's avatar
hjk committed
401
    return source->id().name().startsWith(CMAKE_RC_PREFIX);
402 403
}

Orgad Shaneh's avatar
Orgad Shaneh committed
404
RunConfiguration *CMakeRunConfigurationFactory::clone(Target *parent, RunConfiguration * source)
405 406 407 408
{
    if (!canClone(parent, source))
        return 0;
    CMakeRunConfiguration *crc(static_cast<CMakeRunConfiguration *>(source));
Tobias Hunger's avatar
Tobias Hunger committed
409
    return new CMakeRunConfiguration(parent, crc);
410 411
}

Orgad Shaneh's avatar
Orgad Shaneh committed
412
bool CMakeRunConfigurationFactory::canRestore(Target *parent, const QVariantMap &map) const
413
{
Tobias Hunger's avatar
Tobias Hunger committed
414
    if (!qobject_cast<CMakeProject *>(parent->project()))
415
        return false;
Orgad Shaneh's avatar
Orgad Shaneh committed
416
    return idFromMap(map).name().startsWith(CMAKE_RC_PREFIX);
417 418
}

Orgad Shaneh's avatar
Orgad Shaneh committed
419
RunConfiguration *CMakeRunConfigurationFactory::doRestore(Target *parent, const QVariantMap &map)
420
{
Orgad Shaneh's avatar
Orgad Shaneh committed
421
    return new CMakeRunConfiguration(parent, idFromMap(map), QString(), QString(), QString());
dt's avatar
dt committed
422
}
423 424 425

QString CMakeRunConfigurationFactory::buildTargetFromId(Core::Id id)
{
hjk's avatar
hjk committed
426
    return id.suffixAfter(CMAKE_RC_PREFIX);
427 428 429 430
}

Core::Id CMakeRunConfigurationFactory::idFromBuildTarget(const QString &target)
{
hjk's avatar
hjk committed
431
    return Core::Id(CMAKE_RC_PREFIX).withSuffix(target);
432
}