cmakerunconfiguration.cpp 20.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
dt's avatar
dt committed
2
**
hjk's avatar
hjk committed
3
4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** 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/debugginghelper.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());
Tobias Hunger's avatar
Tobias Hunger committed
106
}
107

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

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

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

dt's avatar
dt committed
123
QString CMakeRunConfiguration::workingDirectory() const
124
{
125
126
    return QDir::cleanPath(environment().expandVariables(
                Utils::expandMacros(baseWorkingDirectory(), macroExpander())));
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
{
138
    return Utils::QtcProcess::expandMacros(m_arguments, macroExpander());
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
{
175
176
177
178
179
180
    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);
181
    map.insert(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY), Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
182
    map.insert(QLatin1String(BASE_ENVIRONMENT_BASE_KEY), m_baseEnvironmentBase);
183

184
    return map;
dt's avatar
dt committed
185
186
}

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

199
200
201
QString CMakeRunConfiguration::defaultDisplayName() const
{
    if (m_title.isEmpty())
202
        return tr("Run CMake kit");
203
204
205
    return m_title + (m_enabled ? "" : tr(" (disabled)"));
}

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

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

216
217
QString CMakeRunConfiguration::dumperLibrary() const
{
218
219
220
    Utils::FileName qmakePath = QtSupport::DebuggingHelperLibrary::findSystemQt(environment());
    QString qtInstallData = QtSupport::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
    QString dhl = QtSupport::DebuggingHelperLibrary::debuggingHelperLibraryByInstallData(qtInstallData);
221
    return dhl;
222
223
}

224
225
QStringList CMakeRunConfiguration::dumperLibraryLocations() const
{
226
227
228
    Utils::FileName qmakePath = QtSupport::DebuggingHelperLibrary::findSystemQt(environment());
    QString qtInstallData = QtSupport::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
    return QtSupport::DebuggingHelperLibrary::debuggingHelperLibraryDirectories(qtInstallData);
229
230
}

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

244
245
246
247
248
249
250
251
252
253
254
255
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();
}

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

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

269
Utils::Environment CMakeRunConfiguration::environment() const
270
{
271
    Utils::Environment env = baseEnvironment();
272
273
274
275
    env.modify(userEnvironmentChanges());
    return env;
}

276
QList<Utils::EnvironmentItem> CMakeRunConfiguration::userEnvironmentChanges() const
277
278
279
280
{
    return m_userEnvironmentChanges;
}

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

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

298
bool CMakeRunConfiguration::isEnabled() const
299
{
300
    return m_enabled;
301
}
302

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

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

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

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

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

337
    fl->addRow(tr("Working directory:"), boxlayout);
338

339
340
341
    QCheckBox *runInTerminal = new QCheckBox;
    fl->addRow(tr("Run in Terminal"), runInTerminal);

342
    m_detailsContainer = new Utils::DetailsWidget(this);
343
    m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
dt's avatar
dt committed
344

345
346
347
    QWidget *m_details = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(m_details);
    m_details->setLayout(fl);
dt's avatar
dt committed
348

349
    QVBoxLayout *vbx = new QVBoxLayout(this);
350
351
    vbx->setMargin(0);;
    vbx->addWidget(m_detailsContainer);
352
353
354
355
356
357
358
359

    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);
360

361
362
363
    QWidget *baseEnvironmentWidget = new QWidget;
    QHBoxLayout *baseEnvironmentLayout = new QHBoxLayout(baseEnvironmentWidget);
    baseEnvironmentLayout->setMargin(0);
364
    QLabel *label = new QLabel(tr("Base environment for this runconfiguration:"), this);
365
    baseEnvironmentLayout->addWidget(label);
366
    m_baseEnvironmentComboBox = new QComboBox(this);
367
368
369
370
371
372
373
    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)));
374
375
    baseEnvironmentLayout->addWidget(m_baseEnvironmentComboBox);
    baseEnvironmentLayout->addStretch(10);
376

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

    vbx->addWidget(m_environmentWidget);

con's avatar
con committed
384
    connect(m_workingDirectoryEdit, SIGNAL(changed(QString)),
385
386
387
388
389
            this, SLOT(setWorkingDirectory()));

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

390
391
392
    connect(runInTerminal, SIGNAL(toggled(bool)),
            this, SLOT(runInTerminalToggled(bool)));

393
394
    connect(m_environmentWidget, SIGNAL(userChangesChanged()),
            this, SLOT(userChangesChanged()));
395

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

403
    setEnabled(m_cmakeRunConfiguration->isEnabled());
404
405
406
407
408
409
410
}

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

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

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

431
432
433
434
435
436
void CMakeRunConfigurationWidget::runInTerminalToggled(bool toggled)
{
    m_cmakeRunConfiguration->setRunMode(toggled ? ProjectExplorer::LocalApplicationRunConfiguration::Console
                                                : ProjectExplorer::LocalApplicationRunConfiguration::Gui);
}

437
void CMakeRunConfigurationWidget::userChangesChanged()
438
439
440
441
{
    m_cmakeRunConfiguration->setUserEnvironmentChanges(m_environmentWidget->userChanges());
}

442
void CMakeRunConfigurationWidget::baseEnvironmentComboBoxChanged(int index)
443
444
{
    m_ignoreChange = true;
445
    m_cmakeRunConfiguration->setBaseEnvironmentBase(CMakeRunConfiguration::BaseEnvironmentBase(index));
446
447

    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
448
    m_environmentWidget->setBaseEnvironmentText(m_cmakeRunConfiguration->baseEnvironmentText());
449
450
451
    m_ignoreChange = false;
}

452
453
void CMakeRunConfigurationWidget::baseEnvironmentChanged()
{
454
455
456
    if (m_ignoreChange)
        return;

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

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

void CMakeRunConfigurationWidget::setArguments(const QString &args)
{
469
    m_cmakeRunConfiguration->setCommandLineArguments(args);
dt's avatar
dt committed
470
471
}

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

CMakeRunConfigurationFactory::~CMakeRunConfigurationFactory()
{
}

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

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

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

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

514
515
ProjectExplorer::RunConfiguration *CMakeRunConfigurationFactory::create(ProjectExplorer::Target *parent,
                                                                        const Core::Id id)
dt's avatar
dt committed
516
{
517
518
    if (!canCreate(parent, id))
        return 0;
Tobias Hunger's avatar
Tobias Hunger committed
519
    CMakeProject *project = static_cast<CMakeProject *>(parent->project());
520
    const QString title(buildTargetFromId(id));
Tobias Hunger's avatar
Tobias Hunger committed
521
522
    const CMakeBuildTarget &ct = project->buildTargetForTitle(title);
    return new CMakeRunConfiguration(parent, id, ct.executable, ct.workingDirectory, ct.title);
523
524
}

Tobias Hunger's avatar
Tobias Hunger committed
525
bool CMakeRunConfigurationFactory::canClone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration *source) const
526
{
Tobias Hunger's avatar
Tobias Hunger committed
527
    if (!canHandle(parent))
528
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
529
    return source->id().toString().startsWith(QLatin1String(CMAKE_RC_PREFIX));
530
531
}

Tobias Hunger's avatar
Tobias Hunger committed
532
ProjectExplorer::RunConfiguration *CMakeRunConfigurationFactory::clone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration * source)
533
534
535
536
{
    if (!canClone(parent, source))
        return 0;
    CMakeRunConfiguration *crc(static_cast<CMakeRunConfiguration *>(source));
Tobias Hunger's avatar
Tobias Hunger committed
537
    return new CMakeRunConfiguration(parent, crc);
538
539
}

Tobias Hunger's avatar
Tobias Hunger committed
540
bool CMakeRunConfigurationFactory::canRestore(ProjectExplorer::Target *parent, const QVariantMap &map) const
541
{
Tobias Hunger's avatar
Tobias Hunger committed
542
    if (!qobject_cast<CMakeProject *>(parent->project()))
543
        return false;
544
    QString id = QString::fromUtf8(ProjectExplorer::idFromMap(map).name());
Tobias Hunger's avatar
Tobias Hunger committed
545
    return id.startsWith(QLatin1String(CMAKE_RC_PREFIX));
546
547
}

Tobias Hunger's avatar
Tobias Hunger committed
548
ProjectExplorer::RunConfiguration *CMakeRunConfigurationFactory::restore(ProjectExplorer::Target *parent, const QVariantMap &map)
549
550
551
{
    if (!canRestore(parent, map))
        return 0;
Tobias Hunger's avatar
Tobias Hunger committed
552
553
    CMakeRunConfiguration *rc(new CMakeRunConfiguration(parent, ProjectExplorer::idFromMap(map),
                                                        QString(), QString(), QString()));
554
    if (rc->fromMap(map))
dt's avatar
dt committed
555
        return rc;
556
557
    delete rc;
    return 0;
dt's avatar
dt committed
558
}
559
560
561
562
563
564
565
566
567
568
569
570
571
572

QString CMakeRunConfigurationFactory::buildTargetFromId(Core::Id id)
{
    QString idstr = QString::fromUtf8(id.name());
    if (!idstr.startsWith(QLatin1String(CMAKE_RC_PREFIX)))
        return QString();
    return idstr.mid(QString::fromLatin1(CMAKE_RC_PREFIX).length());
}

Core::Id CMakeRunConfigurationFactory::idFromBuildTarget(const QString &target)
{
    QString id = QString::fromLatin1(CMAKE_RC_PREFIX) + target;
    return Core::Id(id.toUtf8().constData());
}