cmakerunconfiguration.cpp 14.7 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 13 14
** 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
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
dt's avatar
dt committed
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** 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
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
dt's avatar
dt committed
29 30

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

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

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

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

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

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;

59
namespace {
60
const char CMAKE_RC_PREFIX[] = "CMakeProjectManager.CMakeRunConfiguration.";
61

62 63 64 65
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";
66 67 68

} // namespace

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

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

95
CMakeRunConfiguration::~CMakeRunConfiguration()
dt's avatar
dt committed
96
{
dt's avatar
dt committed
97 98
}

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

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

109
ProjectExplorer::LocalApplicationRunConfiguration::RunMode CMakeRunConfiguration::runMode() const
dt's avatar
dt committed
110
{
111
    return m_runMode;
dt's avatar
dt committed
112 113
}

114 115 116 117 118
void CMakeRunConfiguration::setRunMode(RunMode runMode)
{
    m_runMode = runMode;
}

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

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

134
QString CMakeRunConfiguration::commandLineArguments() const
135
{
136
    return Utils::QtcProcess::expandMacros(m_arguments, macroExpander());
dt's avatar
dt committed
137 138
}

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

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

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

    m_workingDirectory = wd;

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

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

    m_userWorkingDirectory = wd;

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

171
QVariantMap CMakeRunConfiguration::toMap() const
dt's avatar
dt committed
172
{
173 174 175 176 177 178
    QVariantMap map(ProjectExplorer::LocalApplicationRunConfiguration::toMap());

    map.insert(QLatin1String(USER_WORKING_DIRECTORY_KEY), m_userWorkingDirectory);
    map.insert(QLatin1String(USE_TERMINAL_KEY), m_runMode == Console);
    map.insert(QLatin1String(TITLE_KEY), m_title);
    map.insert(QLatin1String(ARGUMENTS_KEY), m_arguments);
179

180
    return map;
dt's avatar
dt committed
181 182
}

183
bool CMakeRunConfiguration::fromMap(const QVariantMap &map)
dt's avatar
dt committed
184
{
185 186 187 188 189 190
    m_userWorkingDirectory = map.value(QLatin1String(USER_WORKING_DIRECTORY_KEY)).toString();
    m_runMode = map.value(QLatin1String(USE_TERMINAL_KEY)).toBool() ? Console : Gui;
    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
191 192
}

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

205
QWidget *CMakeRunConfiguration::createConfigurationWidget()
dt's avatar
dt committed
206
{
207
    return new CMakeRunConfigurationWidget(this);
208 209
}

210
void CMakeRunConfiguration::setCommandLineArguments(const QString &newText)
211 212
{
    m_arguments = newText;
dt's avatar
dt committed
213 214
}

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

224
bool CMakeRunConfiguration::isEnabled() const
225
{
226
    return m_enabled;
227
}
228

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

236
// Configuration widget
237
CMakeRunConfigurationWidget::CMakeRunConfigurationWidget(CMakeRunConfiguration *cmakeRunConfiguration, QWidget *parent)
238
    : QWidget(parent), m_ignoreChange(false), m_cmakeRunConfiguration(cmakeRunConfiguration)
239 240
{
    QFormLayout *fl = new QFormLayout();
241 242
    fl->setMargin(0);
    fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
243
    QLineEdit *argumentsLineEdit = new QLineEdit();
244
    argumentsLineEdit->setText(cmakeRunConfiguration->commandLineArguments());
245 246 247 248
    connect(argumentsLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(setArguments(QString)));
    fl->addRow(tr("Arguments:"), argumentsLineEdit);

249
    m_workingDirectoryEdit = new Utils::PathChooser();
250
    m_workingDirectoryEdit->setExpectedKind(Utils::PathChooser::Directory);
251
    m_workingDirectoryEdit->setBaseDirectory(m_cmakeRunConfiguration->target()->project()->projectDirectory());
252
    m_workingDirectoryEdit->setPath(m_cmakeRunConfiguration->baseWorkingDirectory());
253
    m_workingDirectoryEdit->setHistoryCompleter(QLatin1String("CMake.WorkingDir.History"));
254 255 256 257 258 259
    ProjectExplorer::EnvironmentAspect *aspect
            = m_cmakeRunConfiguration->extraAspect<ProjectExplorer::EnvironmentAspect>();
    if (aspect) {
        connect(aspect, SIGNAL(environmentChanged()), this, SLOT(environmentWasChanged()));
        environmentWasChanged();
    }
260
    m_workingDirectoryEdit->setPromptDialogTitle(tr("Select Working Directory"));
261 262 263

    QToolButton *resetButton = new QToolButton();
    resetButton->setToolTip(tr("Reset to default"));
264
    resetButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_RESET)));
265 266 267 268 269

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

270
    fl->addRow(tr("Working directory:"), boxlayout);
271

272 273 274
    QCheckBox *runInTerminal = new QCheckBox;
    fl->addRow(tr("Run in Terminal"), runInTerminal);

275
    m_detailsContainer = new Utils::DetailsWidget(this);
276
    m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
dt's avatar
dt committed
277

278 279 280
    QWidget *m_details = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(m_details);
    m_details->setLayout(fl);
dt's avatar
dt committed
281

282
    QVBoxLayout *vbx = new QVBoxLayout(this);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
283
    vbx->setMargin(0);
284
    vbx->addWidget(m_detailsContainer);
285

con's avatar
con committed
286
    connect(m_workingDirectoryEdit, SIGNAL(changed(QString)),
287 288 289 290 291
            this, SLOT(setWorkingDirectory()));

    connect(resetButton, SIGNAL(clicked()),
            this, SLOT(resetWorkingDirectory()));

292 293 294
    connect(runInTerminal, SIGNAL(toggled(bool)),
            this, SLOT(runInTerminalToggled(bool)));

295
    connect(m_cmakeRunConfiguration, SIGNAL(baseWorkingDirectoryChanged(QString)),
296 297
            this, SLOT(workingDirectoryChanged(QString)));

298
    setEnabled(m_cmakeRunConfiguration->isEnabled());
299 300 301 302 303 304 305
}

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

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

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

326 327 328 329 330 331
void CMakeRunConfigurationWidget::runInTerminalToggled(bool toggled)
{
    m_cmakeRunConfiguration->setRunMode(toggled ? ProjectExplorer::LocalApplicationRunConfiguration::Console
                                                : ProjectExplorer::LocalApplicationRunConfiguration::Gui);
}

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

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

CMakeRunConfigurationFactory::~CMakeRunConfigurationFactory()
{
}

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

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

Tobias Hunger's avatar
Tobias Hunger committed
371 372
bool CMakeRunConfigurationFactory::canHandle(ProjectExplorer::Target *parent) const
{
Tobias Hunger's avatar
Tobias Hunger committed
373
    if (!parent->project()->supportsKit(parent->kit()))
Tobias Hunger's avatar
Tobias Hunger committed
374 375 376 377
        return false;
    return qobject_cast<CMakeProject *>(parent->project());
}

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

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

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

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

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

417 418
ProjectExplorer::RunConfiguration *CMakeRunConfigurationFactory::doRestore(ProjectExplorer::Target *parent,
                                                                           const QVariantMap &map)
419
{
420 421
    return new CMakeRunConfiguration(parent, ProjectExplorer::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
}