cmakerunconfiguration.cpp 16.2 KB
Newer Older
1
/**************************************************************************
dt's avatar
dt committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
dt's avatar
dt committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
dt's avatar
dt committed
8
**
9
** Commercial Usage
dt's avatar
dt committed
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
dt's avatar
dt committed
15
**
16
** GNU Lesser General Public License Usage
dt's avatar
dt committed
17
**
18
19
20
21
22
23
** 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.
dt's avatar
dt committed
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
dt's avatar
dt committed
27
**
28
**************************************************************************/
dt's avatar
dt committed
29
30

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

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

#include <projectexplorer/environment.h>
37
#include <projectexplorer/debugginghelper.h>
hjk's avatar
hjk committed
38
#include <utils/qtcassert.h>
39
40
#include <QtGui/QFormLayout>
#include <QtGui/QLineEdit>
41
42
#include <QtGui/QGroupBox>
#include <QtGui/QLabel>
43
#include <QtGui/QComboBox>
44
#include <QtGui/QToolButton>
dt's avatar
dt committed
45
46
47
48

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;

dt's avatar
dt committed
49
CMakeRunConfiguration::CMakeRunConfiguration(CMakeProject *pro, const QString &target, const QString &workingDirectory, const QString &title)
50
    : ProjectExplorer::LocalApplicationRunConfiguration(pro)
51
52
53
54
    , m_runMode(Gui)
    , m_target(target)
    , m_workingDirectory(workingDirectory)
    , m_title(title)
55
    , m_baseEnvironmentBase(CMakeRunConfiguration::BuildEnvironmentBase)
dt's avatar
dt committed
56
{
dt's avatar
dt committed
57
    setName(title);
58
59
60
61

    connect(pro, SIGNAL(activeBuildConfigurationChanged()),
            this, SIGNAL(baseEnvironmentChanged()));

62
    // TODO
dt's avatar
dt committed
63
    connect(pro, SIGNAL(environmentChanged(ProjectExplorer::BuildConfiguration *)),
64
            this, SIGNAL(baseEnvironmentChanged()));
dt's avatar
dt committed
65
}
hjk's avatar
hjk committed
66

dt's avatar
dt committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
CMakeRunConfiguration::~CMakeRunConfiguration()
{
}

QString CMakeRunConfiguration::type() const
{
    return Constants::CMAKERUNCONFIGURATION;
}

QString CMakeRunConfiguration::executable() const
{
    return m_target;
}

81
ProjectExplorer::LocalApplicationRunConfiguration::RunMode CMakeRunConfiguration::runMode() const
dt's avatar
dt committed
82
{
83
    return m_runMode;
dt's avatar
dt committed
84
85
86
87
}

QString CMakeRunConfiguration::workingDirectory() const
{
88
89
    if (!m_userWorkingDirectory.isEmpty())
        return m_userWorkingDirectory;
dt's avatar
dt committed
90
91
92
93
94
    return m_workingDirectory;
}

QStringList CMakeRunConfiguration::commandLineArguments() const
{
95
    return ProjectExplorer::Environment::parseCombinedArgString(m_arguments);
dt's avatar
dt committed
96
97
}

dt's avatar
dt committed
98
99
100
101
102
103
104
105
106
107
QString CMakeRunConfiguration::title() const
{
    return m_title;
}

void CMakeRunConfiguration::setExecutable(const QString &executable)
{
    m_target = executable;
}

108
void CMakeRunConfiguration::setWorkingDirectory(const QString &wd)
dt's avatar
dt committed
109
{
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    const QString & oldWorkingDirectory = workingDirectory();

    m_workingDirectory = wd;

    const QString &newWorkingDirectory = workingDirectory();
    if (oldWorkingDirectory != newWorkingDirectory)
        emit workingDirectoryChanged(newWorkingDirectory);
}

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

    m_userWorkingDirectory = wd;

    const QString &newWorkingDirectory = workingDirectory();
    if (oldWorkingDirectory != newWorkingDirectory)
        emit workingDirectoryChanged(newWorkingDirectory);
dt's avatar
dt committed
128
129
}

dt's avatar
dt committed
130
131
void CMakeRunConfiguration::save(ProjectExplorer::PersistentSettingsWriter &writer) const
{
132
    ProjectExplorer::LocalApplicationRunConfiguration::save(writer);
133
134
    writer.saveValue("CMakeRunConfiguration.Target", m_target);
    writer.saveValue("CMakeRunConfiguration.WorkingDirectory", m_workingDirectory);
135
    writer.saveValue("CMakeRunConfiguration.UserWorkingDirectory", m_userWorkingDirectory);
136
    writer.saveValue("CMakeRunConfiguration.UseTerminal", m_runMode == Console);
dt's avatar
dt committed
137
    writer.saveValue("CMakeRunConfiguation.Title", m_title);
138
    writer.saveValue("CMakeRunConfiguration.Arguments", m_arguments);
139
    writer.saveValue("CMakeRunConfiguration.UserEnvironmentChanges", ProjectExplorer::EnvironmentItem::toStringList(m_userEnvironmentChanges));
140
    writer.saveValue("BaseEnvironmentBase", m_baseEnvironmentBase);
141

dt's avatar
dt committed
142
143
144
145
}

void CMakeRunConfiguration::restore(const ProjectExplorer::PersistentSettingsReader &reader)
{
146
    ProjectExplorer::LocalApplicationRunConfiguration::restore(reader);
147
148
    m_target = reader.restoreValue("CMakeRunConfiguration.Target").toString();
    m_workingDirectory = reader.restoreValue("CMakeRunConfiguration.WorkingDirectory").toString();
149
    m_userWorkingDirectory = reader.restoreValue("CMakeRunConfiguration.UserWorkingDirectory").toString();
150
    m_runMode = reader.restoreValue("CMakeRunConfiguration.UseTerminal").toBool() ? Console : Gui;
dt's avatar
dt committed
151
    m_title = reader.restoreValue("CMakeRunConfiguation.Title").toString();
152
    m_arguments = reader.restoreValue("CMakeRunConfiguration.Arguments").toString();
153
    m_userEnvironmentChanges = ProjectExplorer::EnvironmentItem::fromStringList(reader.restoreValue("CMakeRunConfiguration.UserEnvironmentChanges").toStringList());
154
155
    QVariant tmp = reader.restoreValue("BaseEnvironmentBase");
    m_baseEnvironmentBase = tmp.isValid() ? BaseEnvironmentBase(tmp.toInt()) : CMakeRunConfiguration::BuildEnvironmentBase;
dt's avatar
dt committed
156
157
158
159
}

QWidget *CMakeRunConfiguration::configurationWidget()
{
160
    return new CMakeRunConfigurationWidget(this);
161
162
163
164
165
}

void CMakeRunConfiguration::setArguments(const QString &newText)
{
    m_arguments = newText;
dt's avatar
dt committed
166
167
}

168
169
QString CMakeRunConfiguration::dumperLibrary() const
{
170
    QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
dt's avatar
dt committed
171
172
    QString qtInstallData = ProjectExplorer::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
    QString dhl = ProjectExplorer::DebuggingHelperLibrary::debuggingHelperLibraryByInstallData(qtInstallData);
173
    return dhl;
174
175
}

176
177
178
QStringList CMakeRunConfiguration::dumperLibraryLocations() const
{
    QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
dt's avatar
dt committed
179
180
    QString qtInstallData = ProjectExplorer::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
    return ProjectExplorer::DebuggingHelperLibrary::debuggingHelperLibraryLocationsByInstallData(qtInstallData);
181
182
}

183
184
ProjectExplorer::Environment CMakeRunConfiguration::baseEnvironment() const
{
185
186
187
188
189
190
    ProjectExplorer::Environment env;
    if (m_baseEnvironmentBase == CMakeRunConfiguration::CleanEnvironmentBase) {
        // Nothing
    } else  if (m_baseEnvironmentBase == CMakeRunConfiguration::SystemEnvironmentBase) {
        env = ProjectExplorer::Environment::systemEnvironment();
    } else  if (m_baseEnvironmentBase == CMakeRunConfiguration::BuildEnvironmentBase) {
191
        env = environment();
192
    }
193
194
195
    return env;
}

196
197
198
199
200
201
202
203
204
205
206
207
208
void CMakeRunConfiguration::setBaseEnvironmentBase(BaseEnvironmentBase env)
{
    if (m_baseEnvironmentBase == env)
        return;
    m_baseEnvironmentBase = env;
    emit baseEnvironmentChanged();
}

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

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
ProjectExplorer::Environment CMakeRunConfiguration::environment() const
{
    ProjectExplorer::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

QList<ProjectExplorer::EnvironmentItem> CMakeRunConfiguration::userEnvironmentChanges() const
{
    return m_userEnvironmentChanges;
}

void CMakeRunConfiguration::setUserEnvironmentChanges(const QList<ProjectExplorer::EnvironmentItem> &diff)
{
    if (m_userEnvironmentChanges != diff) {
        m_userEnvironmentChanges = diff;
        emit userEnvironmentChangesChanged(diff);
    }
}

229
230
ProjectExplorer::ToolChain::ToolChainType CMakeRunConfiguration::toolChainType() const
{
231
232
    CMakeBuildConfiguration *bc = static_cast<CMakeBuildConfiguration *>(project()->activeBuildConfiguration());
    return bc->toolChainType();
233
234
}

235
236
237
238
// Configuration widget


CMakeRunConfigurationWidget::CMakeRunConfigurationWidget(CMakeRunConfiguration *cmakeRunConfiguration, QWidget *parent)
239
    : QWidget(parent), m_ignoreChange(false), m_cmakeRunConfiguration(cmakeRunConfiguration)
240
241
242
{

    QFormLayout *fl = new QFormLayout();
243
244
    fl->setMargin(0);
    fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
245
246
247
248
249
250
    QLineEdit *argumentsLineEdit = new QLineEdit();
    argumentsLineEdit->setText(ProjectExplorer::Environment::joinArgumentList(cmakeRunConfiguration->commandLineArguments()));
    connect(argumentsLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(setArguments(QString)));
    fl->addRow(tr("Arguments:"), argumentsLineEdit);

251
    m_workingDirectoryEdit = new Utils::PathChooser();
252
    m_workingDirectoryEdit->setPath(m_cmakeRunConfiguration->workingDirectory());
253
    m_workingDirectoryEdit->setExpectedKind(Utils::PathChooser::Directory);
254
255
256
257
258
259
260
261
262
263
264
265
    m_workingDirectoryEdit->setPromptDialogTitle(tr("Select the working directory"));

    QToolButton *resetButton = new QToolButton();
    resetButton->setToolTip(tr("Reset to default"));
    resetButton->setIcon(QIcon(":/core/images/reset.png"));

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

    fl->addRow(tr("Working Directory:"), boxlayout);

266
    m_detailsContainer = new Utils::DetailsWidget(this);
dt's avatar
dt committed
267

268
269
270
    QWidget *m_details = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(m_details);
    m_details->setLayout(fl);
dt's avatar
dt committed
271

272
    QVBoxLayout *vbx = new QVBoxLayout(this);
273
274
    vbx->setMargin(0);;
    vbx->addWidget(m_detailsContainer);
275
276
277
278
279
280
281
282

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

284
285
286
    QWidget *baseEnvironmentWidget = new QWidget;
    QHBoxLayout *baseEnvironmentLayout = new QHBoxLayout(baseEnvironmentWidget);
    baseEnvironmentLayout->setMargin(0);
287
    QLabel *label = new QLabel(tr("Base environment for this runconfiguration:"), this);
288
    baseEnvironmentLayout->addWidget(label);
289
    m_baseEnvironmentComboBox = new QComboBox(this);
290
291
292
293
294
295
296
    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)));
297
298
    baseEnvironmentLayout->addWidget(m_baseEnvironmentComboBox);
    baseEnvironmentLayout->addStretch(10);
299

dt's avatar
dt committed
300
301
302
303
304
305
306
307
    m_environmentWidget = new ProjectExplorer::EnvironmentWidget(this, baseEnvironmentWidget);
    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
    m_environmentWidget->setUserChanges(m_cmakeRunConfiguration->userEnvironmentChanges());

    vbx->addWidget(m_environmentWidget);

    updateSummary();

con's avatar
con committed
308
    connect(m_workingDirectoryEdit, SIGNAL(changed(QString)),
309
310
311
312
313
            this, SLOT(setWorkingDirectory()));

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

314
315
316
    connect(m_environmentWidget, SIGNAL(userChangesUpdated()),
            this, SLOT(userChangesUpdated()));

317
318
    connect(m_cmakeRunConfiguration, SIGNAL(workingDirectoryChanged(QString)),
            this, SLOT(workingDirectoryChanged(QString)));
319
320
321
322
    connect(m_cmakeRunConfiguration, SIGNAL(baseEnvironmentChanged()),
            this, SLOT(baseEnvironmentChanged()));
    connect(m_cmakeRunConfiguration, SIGNAL(userEnvironmentChangesChanged(QList<ProjectExplorer::EnvironmentItem>)),
            this, SLOT(userEnvironmentChangesChanged()));
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

}

void CMakeRunConfigurationWidget::setWorkingDirectory()
{
    if (m_ignoreChange)
        return;
    m_ignoreChange = true;
    m_cmakeRunConfiguration->setUserWorkingDirectory(m_workingDirectoryEdit->path());
    m_ignoreChange = false;
}

void CMakeRunConfigurationWidget::workingDirectoryChanged(const QString &workingDirectory)
{
    if (!m_ignoreChange)
        m_workingDirectoryEdit->setPath(workingDirectory);
}

void CMakeRunConfigurationWidget::resetWorkingDirectory()
{
    // This emits a signal connected to workingDirectoryChanged()
    // that sets the m_workingDirectoryEdit
    m_cmakeRunConfiguration->setUserWorkingDirectory("");
346
347
348
349
350
351
352
}

void CMakeRunConfigurationWidget::userChangesUpdated()
{
    m_cmakeRunConfiguration->setUserEnvironmentChanges(m_environmentWidget->userChanges());
}

353
void CMakeRunConfigurationWidget::baseEnvironmentComboBoxChanged(int index)
354
355
{
    m_ignoreChange = true;
356
    m_cmakeRunConfiguration->setBaseEnvironmentBase(CMakeRunConfiguration::BaseEnvironmentBase(index));
357
358
359
360
361

    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
    m_ignoreChange = false;
}

362
363
void CMakeRunConfigurationWidget::baseEnvironmentChanged()
{
364
365
366
    if (m_ignoreChange)
        return;

367
    m_baseEnvironmentComboBox->setCurrentIndex(m_cmakeRunConfiguration->baseEnvironmentBase());
368
369
370
371
372
373
374
375
376
377
378
    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
}

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

void CMakeRunConfigurationWidget::setArguments(const QString &args)
{
    m_cmakeRunConfiguration->setArguments(args);
dt's avatar
dt committed
379
380
381
382
383
384
385
386
    updateSummary();
}

void CMakeRunConfigurationWidget::updateSummary()
{
    QString text = tr("Running executable: <b>%1</b> %2")
                   .arg(QFileInfo(m_cmakeRunConfiguration->executable()).fileName(),
                        ProjectExplorer::Environment::joinArgumentList(m_cmakeRunConfiguration->commandLineArguments()));
387
    m_detailsContainer->setSummaryText(text);
388
389
390
}


dt's avatar
dt committed
391
392
393
394
395
396
397
398
399
400
401
402
// Factory
CMakeRunConfigurationFactory::CMakeRunConfigurationFactory()
{

}

CMakeRunConfigurationFactory::~CMakeRunConfigurationFactory()
{

}

// used to recreate the runConfigurations when restoring settings
con's avatar
con committed
403
bool CMakeRunConfigurationFactory::canRestore(const QString &type) const
dt's avatar
dt committed
404
405
406
407
408
409
410
{
    if (type.startsWith(Constants::CMAKERUNCONFIGURATION))
        return true;
    return false;
}

// used to show the list of possible additons to a project, returns a list of types
con's avatar
con committed
411
QStringList CMakeRunConfigurationFactory::availableCreationTypes(ProjectExplorer::Project *project) const
dt's avatar
dt committed
412
413
414
415
{
    CMakeProject *pro = qobject_cast<CMakeProject *>(project);
    if (!pro)
        return QStringList();
416
417
418
419
420
    QStringList allTargets = pro->targets();
    for (int i=0; i<allTargets.size(); ++i) {
        allTargets[i] = Constants::CMAKERUNCONFIGURATION + allTargets[i];
    }
    return allTargets;
dt's avatar
dt committed
421
422
423
}

// used to translate the types to names to display to the user
con's avatar
con committed
424
QString CMakeRunConfigurationFactory::displayNameForType(const QString &type) const
dt's avatar
dt committed
425
{
dt's avatar
dt committed
426
    Q_ASSERT(type.startsWith(Constants::CMAKERUNCONFIGURATION));
dt's avatar
dt committed
427
428
429
430
431
432
433

    if (type == Constants::CMAKERUNCONFIGURATION)
        return "CMake"; // Doesn't happen
    else
        return type.mid(QString(Constants::CMAKERUNCONFIGURATION).length());
}

434
ProjectExplorer::RunConfiguration* CMakeRunConfigurationFactory::create(ProjectExplorer::Project *project, const QString &type)
dt's avatar
dt committed
435
436
{
    CMakeProject *pro = qobject_cast<CMakeProject *>(project);
dt's avatar
dt committed
437
    Q_ASSERT(pro);
dt's avatar
dt committed
438
439
    if (type == Constants::CMAKERUNCONFIGURATION) {
        // Restoring, filename will be added by restoreSettings
440
        ProjectExplorer::RunConfiguration* rc = new CMakeRunConfiguration(pro, QString::null, QString::null, QString::null);
dt's avatar
dt committed
441
442
443
        return rc;
    } else {
        // Adding new
444
445
        const QString title = type.mid(QString(Constants::CMAKERUNCONFIGURATION).length());
        const CMakeTarget &ct = pro->targetForTitle(title);
446
        ProjectExplorer::RunConfiguration * rc = new CMakeRunConfiguration(pro, ct.executable, ct.workingDirectory, ct.title);
dt's avatar
dt committed
447
448
449
        return rc;
    }
}