cmakerunconfiguration.cpp 15.6 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
26
** contact the sales department at http://www.qtsoftware.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"
hjk's avatar
hjk committed
33
#include "cmakeprojectconstants.h"
dt's avatar
dt committed
34
35

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

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;

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

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

    connect(pro, SIGNAL(environmentChanged(QString)),
            this, SIGNAL(baseEnvironmentChanged()));
dt's avatar
dt committed
63
}
hjk's avatar
hjk committed
64

dt's avatar
dt committed
65
66
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;
}

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

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

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

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

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

106
void CMakeRunConfiguration::setWorkingDirectory(const QString &wd)
dt's avatar
dt committed
107
{
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    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
126
127
}

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

dt's avatar
dt committed
140
141
142
143
144
}

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

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

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

166
167
QString CMakeRunConfiguration::dumperLibrary() const
{
168
169
    QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
    QString dhl = ProjectExplorer::DebuggingHelperLibrary::debuggingHelperLibrary(qmakePath);
170
    return dhl;
171
172
}

173
174
175
176
177
178
QStringList CMakeRunConfiguration::dumperLibraryLocations() const
{
    QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
    return ProjectExplorer::DebuggingHelperLibrary::debuggingHelperLibraryLocations(qmakePath);
}

179
180
ProjectExplorer::Environment CMakeRunConfiguration::baseEnvironment() const
{
181
182
183
184
185
186
187
188
189
    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) {
        QString config = project()->activeBuildConfiguration();
        env = project()->environment(project()->activeBuildConfiguration());
    }
190
191
192
    return env;
}

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

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

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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);
    }
}

226
227
228
229
230
231
ProjectExplorer::ToolChain::ToolChainType CMakeRunConfiguration::toolChainType() const
{
    CMakeProject *pro = static_cast<CMakeProject *>(project());
    return pro->toolChainType();
}

232
233
234
235
// Configuration widget


CMakeRunConfigurationWidget::CMakeRunConfigurationWidget(CMakeRunConfiguration *cmakeRunConfiguration, QWidget *parent)
236
    : QWidget(parent), m_ignoreChange(false), m_cmakeRunConfiguration(cmakeRunConfiguration)
237
238
239
240
241
242
243
244
245
{

    QFormLayout *fl = new QFormLayout();
    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);

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    m_workingDirectoryEdit = new Core::Utils::PathChooser();
    m_workingDirectoryEdit->setPath(m_cmakeRunConfiguration->workingDirectory());
    m_workingDirectoryEdit->setExpectedKind(Core::Utils::PathChooser::Directory);
    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);

261
262
263
264
265
266
267
268
269
270
    QVBoxLayout *vbx = new QVBoxLayout(this);
    vbx->addLayout(fl);

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

272
    QFormLayout *formlayout = new QFormLayout();
273
274
    QLabel *label = new QLabel(tr("Base environment for this runconfiguration:"), this);

275
276

    m_baseEnvironmentComboBox = new QComboBox(this);
277
278
279
280
281
    m_baseEnvironmentComboBox->addItems(QStringList()
                                        << tr("Clean Environment")
                                        << tr("System Environment")
                                        << tr("Build Environment"));
    formlayout->addRow(label, m_baseEnvironmentComboBox);
282
    vbx->addLayout(formlayout);
283
284
285
286
287
288
289
290
    label->setVisible(false);
    m_baseEnvironmentComboBox->setVisible(false);

    m_baseEnvironmentComboBox->setCurrentIndex(m_cmakeRunConfiguration->baseEnvironmentBase());

    connect(m_baseEnvironmentComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(baseEnvironmentComboBoxChanged(int)));

291

con's avatar
con committed
292
    connect(m_workingDirectoryEdit, SIGNAL(changed(QString)),
293
294
295
296
297
            this, SLOT(setWorkingDirectory()));

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

298
299
300
301
    m_environmentWidget = new ProjectExplorer::EnvironmentWidget(this);
    m_environmentWidget->setBaseEnvironment(m_cmakeRunConfiguration->baseEnvironment());
    m_environmentWidget->setUserChanges(m_cmakeRunConfiguration->userEnvironmentChanges());

302
303
304
305
306
307
308
309
310
311
    connect(m_environmentWidget, SIGNAL(switchedToSummary()),
            m_baseEnvironmentComboBox, SLOT(hide()));
    connect(m_environmentWidget, SIGNAL(switchedToDetails()),
            m_baseEnvironmentComboBox, SLOT(show()));

    connect(m_environmentWidget, SIGNAL(switchedToSummary()),
            label, SLOT(hide()));
    connect(m_environmentWidget, SIGNAL(switchedToDetails()),
            label, SLOT(show()));

312
    vbx->addWidget(m_environmentWidget);
313

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
379
380
381
    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
382
383
384
385
386
387
388
389
390
391
392
393
// Factory
CMakeRunConfigurationFactory::CMakeRunConfigurationFactory()
{

}

CMakeRunConfigurationFactory::~CMakeRunConfigurationFactory()
{

}

// used to recreate the runConfigurations when restoring settings
con's avatar
con committed
394
bool CMakeRunConfigurationFactory::canRestore(const QString &type) const
dt's avatar
dt committed
395
396
397
398
399
400
401
{
    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
402
QStringList CMakeRunConfigurationFactory::availableCreationTypes(ProjectExplorer::Project *project) const
dt's avatar
dt committed
403
404
405
406
{
    CMakeProject *pro = qobject_cast<CMakeProject *>(project);
    if (!pro)
        return QStringList();
407
408
409
410
411
    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
412
413
414
}

// used to translate the types to names to display to the user
con's avatar
con committed
415
QString CMakeRunConfigurationFactory::displayNameForType(const QString &type) const
dt's avatar
dt committed
416
{
dt's avatar
dt committed
417
    Q_ASSERT(type.startsWith(Constants::CMAKERUNCONFIGURATION));
dt's avatar
dt committed
418
419
420
421
422
423
424
425
426
427

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

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