cmakerunconfiguration.cpp 20 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
dt's avatar
dt committed
2
**
3
** Copyright (C) 2013 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/environmentwidget.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 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";
const char USER_ENVIRONMENT_CHANGES_KEY[] = "CMakeProjectManager.CMakeRunConfiguration.UserEnvironmentChanges";
const char BASE_ENVIRONMENT_BASE_KEY[] = "CMakeProjectManager.BaseEnvironmentBase";
68 69 70

} // namespace

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

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

99
CMakeRunConfiguration::~CMakeRunConfiguration()
dt's avatar
dt committed
100
{
dt's avatar
dt committed
101 102
}

103
void CMakeRunConfiguration::ctor()
dt's avatar
dt committed
104
{
105
    setDefaultDisplayName(defaultDisplayName());
106 107
    connect(target(), SIGNAL(environmentChanged()),
            this, SIGNAL(baseEnvironmentChanged()));
Tobias Hunger's avatar
Tobias Hunger committed
108
}
109

dt's avatar
dt committed
110 111
QString CMakeRunConfiguration::executable() const
{
112
    return m_buildTarget;
dt's avatar
dt committed
113 114
}

115
ProjectExplorer::LocalApplicationRunConfiguration::RunMode CMakeRunConfiguration::runMode() const
dt's avatar
dt committed
116
{
117
    return m_runMode;
dt's avatar
dt committed
118 119
}

120 121 122 123 124
void CMakeRunConfiguration::setRunMode(RunMode runMode)
{
    m_runMode = runMode;
}

dt's avatar
dt committed
125
QString CMakeRunConfiguration::workingDirectory() const
126
{
127 128
    return QDir::cleanPath(environment().expandVariables(
                Utils::expandMacros(baseWorkingDirectory(), macroExpander())));
129 130 131
}

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

138
QString CMakeRunConfiguration::commandLineArguments() const
139
{
140
    return Utils::QtcProcess::expandMacros(m_arguments, macroExpander());
dt's avatar
dt committed
141 142
}

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

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

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

    m_workingDirectory = wd;

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

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

    m_userWorkingDirectory = wd;

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

175
QVariantMap CMakeRunConfiguration::toMap() const
dt's avatar
dt committed
176
{
177 178 179 180 181 182
    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);
183
    map.insert(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY), Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
184
    map.insert(QLatin1String(BASE_ENVIRONMENT_BASE_KEY), m_baseEnvironmentBase);
185

186
    return map;
dt's avatar
dt committed
187 188
}

189
bool CMakeRunConfiguration::fromMap(const QVariantMap &map)
dt's avatar
dt committed
190
{
191 192 193 194
    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();
195
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(map.value(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY)).toStringList());
196 197 198
    m_baseEnvironmentBase = static_cast<BaseEnvironmentBase>(map.value(QLatin1String(BASE_ENVIRONMENT_BASE_KEY), static_cast<int>(CMakeRunConfiguration::BuildEnvironmentBase)).toInt());

    return RunConfiguration::fromMap(map);
dt's avatar
dt committed
199 200
}

201 202 203
QString CMakeRunConfiguration::defaultDisplayName() const
{
    if (m_title.isEmpty())
204
        return tr("Run CMake kit");
205
    return m_title + (m_enabled ? QString() : tr(" (disabled)"));
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
QString CMakeRunConfiguration::dumperLibrary() const
{
220
    return QtSupport::QtKitInformation::dumperLibrary(target()->kit());
221 222
}

223 224
QStringList CMakeRunConfiguration::dumperLibraryLocations() const
{
225
    return QtSupport::QtKitInformation::dumperLibraryLocations(target()->kit());
226 227
}

228
Utils::Environment CMakeRunConfiguration::baseEnvironment() const
229
{
230
    Utils::Environment env;
231 232 233
    if (m_baseEnvironmentBase == CMakeRunConfiguration::CleanEnvironmentBase) {
        // Nothing
    } else  if (m_baseEnvironmentBase == CMakeRunConfiguration::SystemEnvironmentBase) {
234
        env = Utils::Environment::systemEnvironment();
235
    } else  if (m_baseEnvironmentBase == CMakeRunConfiguration::BuildEnvironmentBase) {
Tobias Hunger's avatar
Tobias Hunger committed
236
        env = activeBuildConfiguration()->environment();
237
    }
238 239 240
    return env;
}

241 242 243 244 245 246 247 248 249 250 251 252
QString CMakeRunConfiguration::baseEnvironmentText() const
{
    if (m_baseEnvironmentBase == CMakeRunConfiguration::CleanEnvironmentBase) {
        return tr("Clean Environment");
    } else  if (m_baseEnvironmentBase == CMakeRunConfiguration::SystemEnvironmentBase) {
        return tr("System Environment");
    } else  if (m_baseEnvironmentBase == CMakeRunConfiguration::BuildEnvironmentBase) {
        return tr("Build Environment");
    }
    return QString();
}

253 254 255 256 257 258 259 260 261 262 263 264 265
void CMakeRunConfiguration::setBaseEnvironmentBase(BaseEnvironmentBase env)
{
    if (m_baseEnvironmentBase == env)
        return;
    m_baseEnvironmentBase = env;
    emit baseEnvironmentChanged();
}

CMakeRunConfiguration::BaseEnvironmentBase CMakeRunConfiguration::baseEnvironmentBase() const
{
    return m_baseEnvironmentBase;
}

266
Utils::Environment CMakeRunConfiguration::environment() const
267
{
268
    Utils::Environment env = baseEnvironment();
269 270 271 272
    env.modify(userEnvironmentChanges());
    return env;
}

273
QList<Utils::EnvironmentItem> CMakeRunConfiguration::userEnvironmentChanges() const
274 275 276 277
{
    return m_userEnvironmentChanges;
}

278
void CMakeRunConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
279 280 281 282 283 284 285
{
    if (m_userEnvironmentChanges != diff) {
        m_userEnvironmentChanges = diff;
        emit userEnvironmentChangesChanged(diff);
    }
}

286 287 288 289 290
void CMakeRunConfiguration::setEnabled(bool b)
{
    if (m_enabled == b)
        return;
    m_enabled = b;
291
    emit enabledChanged();
292
    setDefaultDisplayName(defaultDisplayName());
293
}
294

295
bool CMakeRunConfiguration::isEnabled() const
296
{
297
    return m_enabled;
298
}
299

dt_'s avatar
dt_ committed
300 301 302
QString CMakeRunConfiguration::disabledReason() const
{
    if (!m_enabled)
Friedemann Kleint's avatar
Friedemann Kleint committed
303
        return tr("The executable is not built by the current build configuration");
dt_'s avatar
dt_ committed
304 305 306
    return QString();
}

307
// Configuration widget
308
CMakeRunConfigurationWidget::CMakeRunConfigurationWidget(CMakeRunConfiguration *cmakeRunConfiguration, QWidget *parent)
309
    : QWidget(parent), m_ignoreChange(false), m_cmakeRunConfiguration(cmakeRunConfiguration)
310 311
{
    QFormLayout *fl = new QFormLayout();
312 313
    fl->setMargin(0);
    fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
314
    QLineEdit *argumentsLineEdit = new QLineEdit();
315
    argumentsLineEdit->setText(cmakeRunConfiguration->commandLineArguments());
316 317 318 319
    connect(argumentsLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(setArguments(QString)));
    fl->addRow(tr("Arguments:"), argumentsLineEdit);

320
    m_workingDirectoryEdit = new Utils::PathChooser();
321
    m_workingDirectoryEdit->setExpectedKind(Utils::PathChooser::Directory);
322
    m_workingDirectoryEdit->setBaseDirectory(m_cmakeRunConfiguration->target()->project()->projectDirectory());
323
    m_workingDirectoryEdit->setPath(m_cmakeRunConfiguration->baseWorkingDirectory());
324
    m_workingDirectoryEdit->setPromptDialogTitle(tr("Select Working Directory"));
325 326 327

    QToolButton *resetButton = new QToolButton();
    resetButton->setToolTip(tr("Reset to default"));
328
    resetButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_RESET)));
329 330 331 332 333

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

334
    fl->addRow(tr("Working directory:"), boxlayout);
335

336 337 338
    QCheckBox *runInTerminal = new QCheckBox;
    fl->addRow(tr("Run in Terminal"), runInTerminal);

339
    m_detailsContainer = new Utils::DetailsWidget(this);
340
    m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
dt's avatar
dt committed
341

342 343 344
    QWidget *m_details = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(m_details);
    m_details->setLayout(fl);
dt's avatar
dt committed
345

346
    QVBoxLayout *vbx = new QVBoxLayout(this);
347 348
    vbx->setMargin(0);;
    vbx->addWidget(m_detailsContainer);
349 350 351 352 353 354 355 356

    QLabel *environmentLabel = new QLabel(this);
    environmentLabel->setText(tr("Run Environment"));
    QFont f = environmentLabel->font();
    f.setBold(true);
    f.setPointSizeF(f.pointSizeF() *1.2);
    environmentLabel->setFont(f);
    vbx->addWidget(environmentLabel);
357

358 359 360
    QWidget *baseEnvironmentWidget = new QWidget;
    QHBoxLayout *baseEnvironmentLayout = new QHBoxLayout(baseEnvironmentWidget);
    baseEnvironmentLayout->setMargin(0);
361
    QLabel *label = new QLabel(tr("Base environment for this runconfiguration:"), this);
362
    baseEnvironmentLayout->addWidget(label);
363
    m_baseEnvironmentComboBox = new QComboBox(this);
364 365 366 367 368 369 370
    m_baseEnvironmentComboBox->addItems(QStringList()
                                        << tr("Clean Environment")
                                        << tr("System Environment")
                                        << tr("Build Environment"));
    m_baseEnvironmentComboBox->setCurrentIndex(m_cmakeRunConfiguration->baseEnvironmentBase());
    connect(m_baseEnvironmentComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(baseEnvironmentComboBoxChanged(int)));
371 372
    baseEnvironmentLayout->addWidget(m_baseEnvironmentComboBox);
    baseEnvironmentLayout->addStretch(10);
373

dt's avatar
dt committed
374 375
    m_environmentWidget = new ProjectExplorer::EnvironmentWidget(this, baseEnvironmentWidget);
    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
376
    m_environmentWidget->setBaseEnvironmentText(m_cmakeRunConfiguration->baseEnvironmentText());
dt's avatar
dt committed
377 378 379 380
    m_environmentWidget->setUserChanges(m_cmakeRunConfiguration->userEnvironmentChanges());

    vbx->addWidget(m_environmentWidget);

con's avatar
con committed
381
    connect(m_workingDirectoryEdit, SIGNAL(changed(QString)),
382 383 384 385 386
            this, SLOT(setWorkingDirectory()));

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

387 388 389
    connect(runInTerminal, SIGNAL(toggled(bool)),
            this, SLOT(runInTerminalToggled(bool)));

390 391
    connect(m_environmentWidget, SIGNAL(userChangesChanged()),
            this, SLOT(userChangesChanged()));
392

393
    connect(m_cmakeRunConfiguration, SIGNAL(baseWorkingDirectoryChanged(QString)),
394
            this, SLOT(workingDirectoryChanged(QString)));
395 396
    connect(m_cmakeRunConfiguration, SIGNAL(baseEnvironmentChanged()),
            this, SLOT(baseEnvironmentChanged()));
397
    connect(m_cmakeRunConfiguration, SIGNAL(userEnvironmentChangesChanged(QList<Utils::EnvironmentItem>)),
398
            this, SLOT(userEnvironmentChangesChanged()));
399

400
    setEnabled(m_cmakeRunConfiguration->isEnabled());
401 402 403 404 405 406 407
}

void CMakeRunConfigurationWidget::setWorkingDirectory()
{
    if (m_ignoreChange)
        return;
    m_ignoreChange = true;
408
    m_cmakeRunConfiguration->setUserWorkingDirectory(m_workingDirectoryEdit->rawPath());
409 410 411 412 413
    m_ignoreChange = false;
}

void CMakeRunConfigurationWidget::workingDirectoryChanged(const QString &workingDirectory)
{
414 415
    if (!m_ignoreChange) {
        m_ignoreChange = true;
416
        m_workingDirectoryEdit->setPath(workingDirectory);
417 418
        m_ignoreChange = false;
    }
419 420 421 422 423 424
}

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

428 429 430 431 432 433
void CMakeRunConfigurationWidget::runInTerminalToggled(bool toggled)
{
    m_cmakeRunConfiguration->setRunMode(toggled ? ProjectExplorer::LocalApplicationRunConfiguration::Console
                                                : ProjectExplorer::LocalApplicationRunConfiguration::Gui);
}

434
void CMakeRunConfigurationWidget::userChangesChanged()
435 436 437 438
{
    m_cmakeRunConfiguration->setUserEnvironmentChanges(m_environmentWidget->userChanges());
}

439
void CMakeRunConfigurationWidget::baseEnvironmentComboBoxChanged(int index)
440 441
{
    m_ignoreChange = true;
442
    m_cmakeRunConfiguration->setBaseEnvironmentBase(CMakeRunConfiguration::BaseEnvironmentBase(index));
443 444

    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
445
    m_environmentWidget->setBaseEnvironmentText(m_cmakeRunConfiguration->baseEnvironmentText());
446 447 448
    m_ignoreChange = false;
}

449 450
void CMakeRunConfigurationWidget::baseEnvironmentChanged()
{
451 452 453
    if (m_ignoreChange)
        return;

454
    m_baseEnvironmentComboBox->setCurrentIndex(m_cmakeRunConfiguration->baseEnvironmentBase());
455
    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
456
    m_environmentWidget->setBaseEnvironmentText(m_cmakeRunConfiguration->baseEnvironmentText());
457 458 459 460 461 462 463 464 465
}

void CMakeRunConfigurationWidget::userEnvironmentChangesChanged()
{
    m_environmentWidget->setUserChanges(m_cmakeRunConfiguration->userEnvironmentChanges());
}

void CMakeRunConfigurationWidget::setArguments(const QString &args)
{
466
    m_cmakeRunConfiguration->setCommandLineArguments(args);
dt's avatar
dt committed
467 468
}

dt's avatar
dt committed
469
// Factory
470 471
CMakeRunConfigurationFactory::CMakeRunConfigurationFactory(QObject *parent) :
    ProjectExplorer::IRunConfigurationFactory(parent)
Tobias Hunger's avatar
Tobias Hunger committed
472
{ setObjectName(QLatin1String("CMakeRunConfigurationFactory")); }
dt's avatar
dt committed
473 474 475 476 477

CMakeRunConfigurationFactory::~CMakeRunConfigurationFactory()
{
}

478
// used to show the list of possible additons to a project, returns a list of ids
479
QList<Core::Id> CMakeRunConfigurationFactory::availableCreationIds(ProjectExplorer::Target *parent) const
dt's avatar
dt committed
480
{
Tobias Hunger's avatar
Tobias Hunger committed
481
    if (!canHandle(parent))
482
        return QList<Core::Id>();
483
    CMakeProject *project = static_cast<CMakeProject *>(parent->project());
484
    QList<Core::Id> allIds;
485
    foreach (const QString &buildTarget, project->buildTargetTitles(true))
486
        allIds << idFromBuildTarget(buildTarget);
487
    return allIds;
dt's avatar
dt committed
488 489
}

490
// used to translate the ids to names to display to the user
491
QString CMakeRunConfigurationFactory::displayNameForId(const Core::Id id) const
dt's avatar
dt committed
492
{
493
    return buildTargetFromId(id);
494
}
dt's avatar
dt committed
495

Tobias Hunger's avatar
Tobias Hunger committed
496 497
bool CMakeRunConfigurationFactory::canHandle(ProjectExplorer::Target *parent) const
{
Tobias Hunger's avatar
Tobias Hunger committed
498
    if (!parent->project()->supportsKit(parent->kit()))
Tobias Hunger's avatar
Tobias Hunger committed
499 500 501 502
        return false;
    return qobject_cast<CMakeProject *>(parent->project());
}

503
bool CMakeRunConfigurationFactory::canCreate(ProjectExplorer::Target *parent, const Core::Id id) const
504
{
Tobias Hunger's avatar
Tobias Hunger committed
505
    if (!canHandle(parent))
506
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
507 508
    CMakeProject *project = static_cast<CMakeProject *>(parent->project());
    return project->hasBuildTarget(buildTargetFromId(id));
dt's avatar
dt committed
509 510
}

511 512
ProjectExplorer::RunConfiguration *CMakeRunConfigurationFactory::doCreate(ProjectExplorer::Target *parent,
                                                                          const Core::Id id)
dt's avatar
dt committed
513
{
Tobias Hunger's avatar
Tobias Hunger committed
514
    CMakeProject *project = static_cast<CMakeProject *>(parent->project());
515
    const QString title(buildTargetFromId(id));
Tobias Hunger's avatar
Tobias Hunger committed
516 517
    const CMakeBuildTarget &ct = project->buildTargetForTitle(title);
    return new CMakeRunConfiguration(parent, id, ct.executable, ct.workingDirectory, ct.title);
518 519
}

Tobias Hunger's avatar
Tobias Hunger committed
520
bool CMakeRunConfigurationFactory::canClone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration *source) const
521
{
Tobias Hunger's avatar
Tobias Hunger committed
522
    if (!canHandle(parent))
523
        return false;
hjk's avatar
hjk committed
524
    return source->id().name().startsWith(CMAKE_RC_PREFIX);
525 526
}

Tobias Hunger's avatar
Tobias Hunger committed
527
ProjectExplorer::RunConfiguration *CMakeRunConfigurationFactory::clone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration * source)
528 529 530 531
{
    if (!canClone(parent, source))
        return 0;
    CMakeRunConfiguration *crc(static_cast<CMakeRunConfiguration *>(source));
Tobias Hunger's avatar
Tobias Hunger committed
532
    return new CMakeRunConfiguration(parent, crc);
533 534
}

Tobias Hunger's avatar
Tobias Hunger committed
535
bool CMakeRunConfigurationFactory::canRestore(ProjectExplorer::Target *parent, const QVariantMap &map) const
536
{
Tobias Hunger's avatar
Tobias Hunger committed
537
    if (!qobject_cast<CMakeProject *>(parent->project()))
538
        return false;
hjk's avatar
hjk committed
539
    return ProjectExplorer::idFromMap(map).name().startsWith(CMAKE_RC_PREFIX);
540 541
}

542 543
ProjectExplorer::RunConfiguration *CMakeRunConfigurationFactory::doRestore(ProjectExplorer::Target *parent,
                                                                           const QVariantMap &map)
544
{
545 546
    return new CMakeRunConfiguration(parent, ProjectExplorer::idFromMap(map),
                                     QString(), QString(), QString());
dt's avatar
dt committed
547
}
548 549 550

QString CMakeRunConfigurationFactory::buildTargetFromId(Core::Id id)
{
hjk's avatar
hjk committed
551
    return id.suffixAfter(CMAKE_RC_PREFIX);
552 553 554 555
}

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