qt4runconfiguration.cpp 28.3 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "qt4runconfiguration.h"
hjk's avatar
hjk committed
34

hjk's avatar
hjk committed
35
#include "makestep.h"
con's avatar
con committed
36
#include "qt4nodes.h"
hjk's avatar
hjk committed
37
#include "qt4project.h"
Tobias Hunger's avatar
Tobias Hunger committed
38
#include "qt4target.h"
39
#include "qt4buildconfiguration.h"
40
#include "qt4projectmanagerconstants.h"
41
#include "qmakestep.h"
con's avatar
con committed
42

43
#include <coreplugin/coreconstants.h>
con's avatar
con committed
44 45 46
#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/variablemanager.h>
47
#include <coreplugin/idocument.h>
48
#include <coreplugin/helpmanager.h>
con's avatar
con committed
49
#include <projectexplorer/buildstep.h>
50
#include <projectexplorer/environmentwidget.h>
51
#include <projectexplorer/toolchain.h>
hjk's avatar
hjk committed
52
#include <utils/qtcassert.h>
53
#include <utils/qtcprocess.h>
54 55
#include <utils/pathchooser.h>
#include <utils/detailswidget.h>
56
#include <utils/stringutils.h>
Jarek Kobus's avatar
Jarek Kobus committed
57
#include <utils/persistentsettings.h>
58 59 60
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/baseqtversion.h>
#include <qtsupport/profilereader.h>
con's avatar
con committed
61

62 63 64 65 66 67 68 69 70
#include <QFormLayout>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QToolButton>
#include <QComboBox>
#include <QFileInfo>
#include <QDir>
con's avatar
con committed
71 72 73

using namespace Qt4ProjectManager::Internal;
using namespace Qt4ProjectManager;
74
using ProjectExplorer::LocalApplicationRunConfiguration;
Jarek Kobus's avatar
Jarek Kobus committed
75 76
using Utils::PersistentSettingsReader;
using Utils::PersistentSettingsWriter;
con's avatar
con committed
77

78 79 80 81 82 83 84 85 86 87 88 89 90
namespace {
const char * const QT4_RC_ID("Qt4ProjectManager.Qt4RunConfiguration");
const char * const QT4_RC_PREFIX("Qt4ProjectManager.Qt4RunConfiguration.");

const char * const COMMAND_LINE_ARGUMENTS_KEY("Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments");
const char * const PRO_FILE_KEY("Qt4ProjectManager.Qt4RunConfiguration.ProFile");
const char * const USE_TERMINAL_KEY("Qt4ProjectManager.Qt4RunConfiguration.UseTerminal");
const char * const USE_DYLD_IMAGE_SUFFIX_KEY("Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix");
const char * const USER_ENVIRONMENT_CHANGES_KEY("Qt4ProjectManager.Qt4RunConfiguration.UserEnvironmentChanges");
const char * const BASE_ENVIRONMENT_BASE_KEY("Qt4ProjectManager.Qt4RunConfiguration.BaseEnvironmentBase");
const char * const USER_WORKING_DIRECTORY_KEY("Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory");

QString pathFromId(const QString &id)
con's avatar
con committed
91
{
92 93 94 95
    if (!id.startsWith(QLatin1String(QT4_RC_PREFIX)))
        return QString();
    return id.mid(QString::fromLatin1(QT4_RC_PREFIX).size());
}
96

97
} // namespace
98

99 100 101 102
//
// Qt4RunConfiguration
//

103
Qt4RunConfiguration::Qt4RunConfiguration(Qt4BaseTarget *parent, const QString &proFilePath) :
104 105 106 107
    LocalApplicationRunConfiguration(parent, QLatin1String(QT4_RC_ID)),
    m_proFilePath(proFilePath),
    m_runMode(Gui),
    m_isUsingDyldImageSuffix(false),
108
    m_baseEnvironmentBase(Qt4RunConfiguration::BuildEnvironmentBase),
109 110
    m_parseSuccess(parent->qt4Project()->validParse(m_proFilePath)),
    m_parseInProgress(parent->qt4Project()->parseInProgress(m_proFilePath))
111 112
{
    ctor();
con's avatar
con committed
113 114
}

115
Qt4RunConfiguration::Qt4RunConfiguration(Qt4BaseTarget *parent, Qt4RunConfiguration *source) :
116 117 118 119 120 121 122
    LocalApplicationRunConfiguration(parent, source),
    m_commandLineArguments(source->m_commandLineArguments),
    m_proFilePath(source->m_proFilePath),
    m_runMode(source->m_runMode),
    m_isUsingDyldImageSuffix(source->m_isUsingDyldImageSuffix),
    m_userWorkingDirectory(source->m_userWorkingDirectory),
    m_userEnvironmentChanges(source->m_userEnvironmentChanges),
123
    m_baseEnvironmentBase(source->m_baseEnvironmentBase),
124 125
    m_parseSuccess(source->m_parseSuccess),
    m_parseInProgress(source->m_parseInProgress)
con's avatar
con committed
126
{
127
    ctor();
con's avatar
con committed
128 129
}

130
Qt4RunConfiguration::~Qt4RunConfiguration()
dt's avatar
dt committed
131 132 133
{
}

134
Qt4BaseTarget *Qt4RunConfiguration::qt4Target() const
con's avatar
con committed
135
{
136
    return static_cast<Qt4BaseTarget *>(target());
con's avatar
con committed
137 138
}

139
bool Qt4RunConfiguration::isEnabled() const
140
{
141
    return m_parseSuccess && !m_parseInProgress;
142
}
143

dt_'s avatar
dt_ committed
144 145
QString Qt4RunConfiguration::disabledReason() const
{
146 147
    if (m_parseInProgress)
        return tr("The .pro file is currently being parsed.");
dt_'s avatar
dt_ committed
148
    if (!m_parseSuccess)
149
        return tr("The .pro file could not be parsed.");
dt_'s avatar
dt_ committed
150 151 152
    return QString();
}

153
void Qt4RunConfiguration::proFileUpdated(Qt4ProjectManager::Qt4ProFileNode *pro, bool success, bool parseInProgress)
154
{
155 156 157 158 159 160
    if (m_proFilePath != pro->path()) {
        if (!parseInProgress) {
            // We depend on all .pro files for the LD_LIBRARY_PATH so we emit a signal for all .pro files
            // This can be optimized by checking whether LD_LIBRARY_PATH changed
            emit baseEnvironmentChanged();
        }
161
        return;
162
    }
163

164 165
    bool enabled = isEnabled();
    m_parseSuccess = success;
166
    m_parseInProgress = parseInProgress;
167
    if (enabled != isEnabled())
168
        emit enabledChanged();
169

170 171 172 173
    if (!parseInProgress) {
        emit effectiveTargetInformationChanged();
        emit baseEnvironmentChanged();
    }
174 175
}

176 177
void Qt4RunConfiguration::ctor()
{
178
    setDefaultDisplayName(defaultDisplayName());
179

Tobias Hunger's avatar
Tobias Hunger committed
180
    connect(qt4Target(), SIGNAL(environmentChanged()),
181
            this, SIGNAL(baseEnvironmentChanged()));
182 183
    connect(qt4Target()->qt4Project(), SIGNAL(proFileUpdated(Qt4ProjectManager::Qt4ProFileNode*,bool,bool)),
            this, SLOT(proFileUpdated(Qt4ProjectManager::Qt4ProFileNode*,bool,bool)));
184 185
}

186 187 188 189 190
//////
/// Qt4RunConfigurationWidget
/////

Qt4RunConfigurationWidget::Qt4RunConfigurationWidget(Qt4RunConfiguration *qt4RunConfiguration, QWidget *parent)
191 192 193
    : QWidget(parent),
    m_qt4RunConfiguration(qt4RunConfiguration),
    m_ignoreChange(false),
mae's avatar
mae committed
194 195
    m_usingDyldImageSuffix(0),
    m_isShown(false)
con's avatar
con committed
196
{
dt's avatar
dt committed
197 198 199
    QVBoxLayout *vboxTopLayout = new QVBoxLayout(this);
    vboxTopLayout->setMargin(0);

200 201 202
    QHBoxLayout *hl = new QHBoxLayout();
    hl->addStretch();
    m_disabledIcon = new QLabel(this);
203
    m_disabledIcon->setPixmap(QPixmap(QLatin1String(":/projectexplorer/images/compile_warning.png")));
204 205 206 207 208 209 210
    hl->addWidget(m_disabledIcon);
    m_disabledReason = new QLabel(this);
    m_disabledReason->setVisible(false);
    hl->addWidget(m_disabledReason);
    hl->addStretch();
    vboxTopLayout->addLayout(hl);

dt's avatar
dt committed
211
    m_detailsContainer = new Utils::DetailsWidget(this);
212
    m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
dt's avatar
dt committed
213 214 215 216
    vboxTopLayout->addWidget(m_detailsContainer);
    QWidget *detailsWidget = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(detailsWidget);
    QFormLayout *toplayout = new QFormLayout(detailsWidget);
con's avatar
con committed
217
    toplayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
con's avatar
con committed
218 219
    toplayout->setMargin(0);

220 221 222
    m_executableLineEdit = new QLineEdit(m_qt4RunConfiguration->executable(), this);
    m_executableLineEdit->setEnabled(false);
    toplayout->addRow(tr("Executable:"), m_executableLineEdit);
con's avatar
con committed
223

Tobias Hunger's avatar
Tobias Hunger committed
224
    QLabel *argumentsLabel = new QLabel(tr("Arguments:"), this);
225
    m_argumentsLineEdit = new QLineEdit(qt4RunConfiguration->rawCommandLineArguments(), this);
226 227 228
    argumentsLabel->setBuddy(m_argumentsLineEdit);
    toplayout->addRow(argumentsLabel, m_argumentsLineEdit);

Tobias Hunger's avatar
Tobias Hunger committed
229
    m_workingDirectoryEdit = new Utils::PathChooser(this);
230
    m_workingDirectoryEdit->setExpectedKind(Utils::PathChooser::Directory);
231
    m_workingDirectoryEdit->setPath(m_qt4RunConfiguration->baseWorkingDirectory());
232
    m_workingDirectoryEdit->setBaseDirectory(m_qt4RunConfiguration->target()->project()->projectDirectory());
233
    m_workingDirectoryEdit->setEnvironment(m_qt4RunConfiguration->environment());
234
    m_workingDirectoryEdit->setPromptDialogTitle(tr("Select Working Directory"));
235

Tobias Hunger's avatar
Tobias Hunger committed
236
    QToolButton *resetButton = new QToolButton(this);
237
    resetButton->setToolTip(tr("Reset to default"));
238
    resetButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_RESET)));
239 240

    QHBoxLayout *boxlayout = new QHBoxLayout();
241
    boxlayout->setMargin(0);
242 243
    boxlayout->addWidget(m_workingDirectoryEdit);
    boxlayout->addWidget(resetButton);
244
    toplayout->addRow(tr("Working directory:"), boxlayout);
con's avatar
con committed
245

246
    m_useTerminalCheck = new QCheckBox(tr("Run in terminal"), this);
247
    m_useTerminalCheck->setChecked(m_qt4RunConfiguration->runMode() == ProjectExplorer::LocalApplicationRunConfiguration::Console);
248
    toplayout->addRow(QString(), m_useTerminalCheck);
249
    m_useTerminalCheck->setVisible(qt4RunConfiguration->target()->id() != QLatin1String(Constants::QT_SIMULATOR_TARGET_ID));
250

251
#ifdef Q_OS_MAC
Tobias Hunger's avatar
Tobias Hunger committed
252
    m_usingDyldImageSuffix = new QCheckBox(tr("Use debug version of frameworks (DYLD_IMAGE_SUFFIX=_debug)"), this);
253 254 255 256 257 258
    m_usingDyldImageSuffix->setChecked(m_qt4RunConfiguration->isUsingDyldImageSuffix());
    toplayout->addRow(QString(), m_usingDyldImageSuffix);
    connect(m_usingDyldImageSuffix, SIGNAL(toggled(bool)),
            this, SLOT(usingDyldImageSuffixToggled(bool)));
#endif

259 260 261 262 263 264
    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);
dt's avatar
dt committed
265
    vboxTopLayout->addWidget(environmentLabel);
266

Tobias Hunger's avatar
Tobias Hunger committed
267
    QWidget *baseEnvironmentWidget = new QWidget(this);
268 269
    QHBoxLayout *baseEnvironmentLayout = new QHBoxLayout(baseEnvironmentWidget);
    baseEnvironmentLayout->setMargin(0);
270
    QLabel *label = new QLabel(tr("Base environment for this run configuration:"), this);
271
    baseEnvironmentLayout->addWidget(label);
272
    m_baseEnvironmentComboBox = new QComboBox(this);
273 274 275 276 277 278
    m_baseEnvironmentComboBox->addItems(QStringList()
                                        << tr("Clean Environment")
                                        << tr("System Environment")
                                        << tr("Build Environment"));
    m_baseEnvironmentComboBox->setCurrentIndex(qt4RunConfiguration->baseEnvironmentBase());
    connect(m_baseEnvironmentComboBox, SIGNAL(currentIndexChanged(int)),
279
            this, SLOT(baseEnvironmentSelected(int)));
280 281
    baseEnvironmentLayout->addWidget(m_baseEnvironmentComboBox);
    baseEnvironmentLayout->addStretch(10);
dt's avatar
dt committed
282

283
    m_environmentWidget = new ProjectExplorer::EnvironmentWidget(this, baseEnvironmentWidget);
284
    m_environmentWidget->setBaseEnvironment(m_qt4RunConfiguration->baseEnvironment());
285
    m_environmentWidget->setBaseEnvironmentText(m_qt4RunConfiguration->baseEnvironmentText());
286
    m_environmentWidget->setUserChanges(m_qt4RunConfiguration->userEnvironmentChanges());
dt's avatar
dt committed
287
    m_environmentWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
dt's avatar
dt committed
288
    vboxTopLayout->addWidget(m_environmentWidget);
289

290
    runConfigurationEnabledChange();
291

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

    connect(resetButton, SIGNAL(clicked()),
296
            this, SLOT(workingDirectoryReseted()));
297

298
    connect(m_argumentsLineEdit, SIGNAL(textEdited(QString)),
299
            this, SLOT(argumentsEdited(QString)));
300 301 302
    connect(m_useTerminalCheck, SIGNAL(toggled(bool)),
            this, SLOT(termToggled(bool)));

303 304
    connect(m_environmentWidget, SIGNAL(userChangesChanged()),
            this, SLOT(userChangesEdited()));
305

306
    connect(qt4RunConfiguration, SIGNAL(baseWorkingDirectoryChanged(QString)),
307 308
            this, SLOT(workingDirectoryChanged(QString)));

309
    connect(qt4RunConfiguration, SIGNAL(commandLineArgumentsChanged(QString)),
310
            this, SLOT(commandLineArgumentsChanged(QString)));
311 312
    connect(qt4RunConfiguration, SIGNAL(runModeChanged(ProjectExplorer::LocalApplicationRunConfiguration::RunMode)),
            this, SLOT(runModeChanged(ProjectExplorer::LocalApplicationRunConfiguration::RunMode)));
313 314
    connect(qt4RunConfiguration, SIGNAL(usingDyldImageSuffixChanged(bool)),
            this, SLOT(usingDyldImageSuffixChanged(bool)));
315 316
    connect(qt4RunConfiguration, SIGNAL(effectiveTargetInformationChanged()),
            this, SLOT(effectiveTargetInformationChanged()), Qt::QueuedConnection);
317

318 319
    connect(qt4RunConfiguration, SIGNAL(userEnvironmentChangesChanged(QList<Utils::EnvironmentItem>)),
            this, SLOT(userEnvironmentChangesChanged(QList<Utils::EnvironmentItem>)));
320

321 322
    connect(qt4RunConfiguration, SIGNAL(baseEnvironmentChanged()),
            this, SLOT(baseEnvironmentChanged()));
323

324 325
    connect(qt4RunConfiguration, SIGNAL(enabledChanged()),
            this, SLOT(runConfigurationEnabledChange()));
326 327
}

Tobias Hunger's avatar
Tobias Hunger committed
328 329 330 331
Qt4RunConfigurationWidget::~Qt4RunConfigurationWidget()
{
}

332
void Qt4RunConfigurationWidget::baseEnvironmentSelected(int index)
333 334
{
    m_ignoreChange = true;
335
    m_qt4RunConfiguration->setBaseEnvironmentBase(Qt4RunConfiguration::BaseEnvironmentBase(index));
336 337

    m_environmentWidget->setBaseEnvironment(m_qt4RunConfiguration->baseEnvironment());
338
    m_environmentWidget->setBaseEnvironmentText(m_qt4RunConfiguration->baseEnvironmentText());
339 340 341
    m_ignoreChange = false;
}

342 343
void Qt4RunConfigurationWidget::baseEnvironmentChanged()
{
344 345 346
    if (m_ignoreChange)
        return;

347
    m_baseEnvironmentComboBox->setCurrentIndex(m_qt4RunConfiguration->baseEnvironmentBase());
348
    m_environmentWidget->setBaseEnvironment(m_qt4RunConfiguration->baseEnvironment());
349
    m_environmentWidget->setBaseEnvironmentText(m_qt4RunConfiguration->baseEnvironmentText());
350 351
}

352
void Qt4RunConfigurationWidget::userEnvironmentChangesChanged(const QList<Utils::EnvironmentItem> &userChanges)
353 354 355 356 357 358
{
    if (m_ignoreChange)
        return;
    m_environmentWidget->setUserChanges(userChanges);
}

359
void Qt4RunConfigurationWidget::userChangesEdited()
360 361 362 363
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->setUserEnvironmentChanges(m_environmentWidget->userChanges());
    m_ignoreChange = false;
364 365
}

366
void Qt4RunConfigurationWidget::runConfigurationEnabledChange()
367
{
368
    bool enabled = m_qt4RunConfiguration->isEnabled();
369 370 371 372 373
    m_detailsContainer->setEnabled(enabled);
    m_environmentWidget->setEnabled(enabled);
    m_disabledIcon->setVisible(!enabled);
    m_disabledReason->setVisible(!enabled);
    m_disabledReason->setText(m_qt4RunConfiguration->disabledReason());
374 375
}

376
void Qt4RunConfigurationWidget::workDirectoryEdited()
377 378 379 380
{
    if (m_ignoreChange)
        return;
    m_ignoreChange = true;
381
    m_qt4RunConfiguration->setBaseWorkingDirectory(m_workingDirectoryEdit->rawPath());
382 383 384
    m_ignoreChange = false;
}

385
void Qt4RunConfigurationWidget::workingDirectoryReseted()
386 387 388
{
    // This emits a signal connected to workingDirectoryChanged()
    // that sets the m_workingDirectoryEdit
389
    m_qt4RunConfiguration->setBaseWorkingDirectory(QString());
390 391
}

392
void Qt4RunConfigurationWidget::argumentsEdited(const QString &args)
393 394
{
    m_ignoreChange = true;
395
    m_qt4RunConfiguration->setCommandLineArguments(args);
396 397 398
    m_ignoreChange = false;
}

399 400 401
void Qt4RunConfigurationWidget::termToggled(bool on)
{
    m_ignoreChange = true;
402 403
    m_qt4RunConfiguration->setRunMode(on ? LocalApplicationRunConfiguration::Console
                                         : LocalApplicationRunConfiguration::Gui);
404 405 406
    m_ignoreChange = false;
}

407 408 409 410 411 412 413
void Qt4RunConfigurationWidget::usingDyldImageSuffixToggled(bool state)
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->setUsingDyldImageSuffix(state);
    m_ignoreChange = false;
}

414 415 416 417 418 419
void Qt4RunConfigurationWidget::workingDirectoryChanged(const QString &workingDirectory)
{
    if (!m_ignoreChange)
        m_workingDirectoryEdit->setPath(workingDirectory);
}

420 421
void Qt4RunConfigurationWidget::commandLineArgumentsChanged(const QString &args)
{
dt's avatar
dt committed
422 423 424
    if (m_ignoreChange)
        return;
    m_argumentsLineEdit->setText(args);
425 426
}

427
void Qt4RunConfigurationWidget::runModeChanged(LocalApplicationRunConfiguration::RunMode runMode)
428 429
{
    if (!m_ignoreChange)
430
        m_useTerminalCheck->setChecked(runMode == LocalApplicationRunConfiguration::Console);
431 432
}

433 434 435 436 437 438
void Qt4RunConfigurationWidget::usingDyldImageSuffixChanged(bool state)
{
    if (!m_ignoreChange && m_usingDyldImageSuffix)
        m_usingDyldImageSuffix->setChecked(state);
}

439
void Qt4RunConfigurationWidget::effectiveTargetInformationChanged()
440
{
441
    if (m_isShown) {
442
        m_executableLineEdit->setText(QDir::toNativeSeparators(m_qt4RunConfiguration->executable()));
443
        m_ignoreChange = true;
444
        m_workingDirectoryEdit->setPath(QDir::toNativeSeparators(m_qt4RunConfiguration->baseWorkingDirectory()));
445
        m_ignoreChange = false;
446
    }
447 448
}

449
void Qt4RunConfigurationWidget::showEvent(QShowEvent *event)
450
{
451 452 453
    m_isShown = true;
    effectiveTargetInformationChanged();
    QWidget::showEvent(event);
454 455
}

456 457 458 459 460
void Qt4RunConfigurationWidget::hideEvent(QHideEvent *event)
{
    m_isShown = false;
    QWidget::hideEvent(event);
}
461

462
////// TODO c&p above
463
QWidget *Qt4RunConfiguration::createConfigurationWidget()
464 465
{
    return new Qt4RunConfigurationWidget(this, 0);
con's avatar
con committed
466 467
}

468
QVariantMap Qt4RunConfiguration::toMap() const
con's avatar
con committed
469
{
470
    const QDir projectDir = QDir(target()->project()->projectDirectory());
471 472 473 474 475
    QVariantMap map(LocalApplicationRunConfiguration::toMap());
    map.insert(QLatin1String(COMMAND_LINE_ARGUMENTS_KEY), m_commandLineArguments);
    map.insert(QLatin1String(PRO_FILE_KEY), projectDir.relativeFilePath(m_proFilePath));
    map.insert(QLatin1String(USE_TERMINAL_KEY), m_runMode == Console);
    map.insert(QLatin1String(USE_DYLD_IMAGE_SUFFIX_KEY), m_isUsingDyldImageSuffix);
476
    map.insert(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY), Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
477 478 479
    map.insert(QLatin1String(BASE_ENVIRONMENT_BASE_KEY), m_baseEnvironmentBase);
    map.insert(QLatin1String(USER_WORKING_DIRECTORY_KEY), m_userWorkingDirectory);
    return map;
con's avatar
con committed
480 481
}

482
bool Qt4RunConfiguration::fromMap(const QVariantMap &map)
483
{
484
    const QDir projectDir = QDir(target()->project()->projectDirectory());
485
    m_commandLineArguments = map.value(QLatin1String(COMMAND_LINE_ARGUMENTS_KEY)).toString();
486
    m_proFilePath = QDir::cleanPath(projectDir.filePath(map.value(QLatin1String(PRO_FILE_KEY)).toString()));
487 488 489 490 491
    m_runMode = map.value(QLatin1String(USE_TERMINAL_KEY), false).toBool() ? Console : Gui;
    m_isUsingDyldImageSuffix = map.value(QLatin1String(USE_DYLD_IMAGE_SUFFIX_KEY), false).toBool();

    m_userWorkingDirectory = map.value(QLatin1String(USER_WORKING_DIRECTORY_KEY)).toString();

492
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(map.value(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY)).toStringList());
493 494
    m_baseEnvironmentBase = static_cast<BaseEnvironmentBase>(map.value(QLatin1String(BASE_ENVIRONMENT_BASE_KEY), static_cast<int>(Qt4RunConfiguration::BuildEnvironmentBase)).toInt());

495
    m_parseSuccess = qt4Target()->qt4Project()->validParse(m_proFilePath);
496
    m_parseInProgress = qt4Target()->qt4Project()->parseInProgress(m_proFilePath);
497

498
    return RunConfiguration::fromMap(map);
con's avatar
con committed
499 500 501 502
}

QString Qt4RunConfiguration::executable() const
{
dt's avatar
dt committed
503
    Qt4Project *pro = qt4Target()->qt4Project();
504
    TargetInformation ti = pro->rootQt4ProjectNode()->targetInformation(m_proFilePath);
dt's avatar
dt committed
505 506 507
    if (!ti.valid)
        return QString();
    return ti.executable;
con's avatar
con committed
508 509
}

510
LocalApplicationRunConfiguration::RunMode Qt4RunConfiguration::runMode() const
con's avatar
con committed
511 512 513 514
{
    return m_runMode;
}

515 516 517 518 519 520 521 522 523 524 525
bool Qt4RunConfiguration::isUsingDyldImageSuffix() const
{
    return m_isUsingDyldImageSuffix;
}

void Qt4RunConfiguration::setUsingDyldImageSuffix(bool state)
{
    m_isUsingDyldImageSuffix = state;
    emit usingDyldImageSuffixChanged(state);
}

con's avatar
con committed
526
QString Qt4RunConfiguration::workingDirectory() const
527
{
528 529
    return QDir::cleanPath(environment().expandVariables(
                Utils::expandMacros(baseWorkingDirectory(), macroExpander())));
530 531 532
}

QString Qt4RunConfiguration::baseWorkingDirectory() const
con's avatar
con committed
533
{
534
    // if the user overrode us, then return his working directory
535
    if (!m_userWorkingDirectory.isEmpty())
536 537 538
        return m_userWorkingDirectory;

    // else what the pro file reader tells us
dt's avatar
dt committed
539
    Qt4Project *pro = qt4Target()->qt4Project();
540
    TargetInformation ti = pro->rootQt4ProjectNode()->targetInformation(m_proFilePath);
hjk's avatar
hjk committed
541
    if (!ti.valid)
dt's avatar
dt committed
542 543
        return QString();
    return ti.workingDir;
con's avatar
con committed
544 545
}

546
QString Qt4RunConfiguration::commandLineArguments() const
547 548 549 550 551
{
    return Utils::QtcProcess::expandMacros(m_commandLineArguments, macroExpander());
}

QString Qt4RunConfiguration::rawCommandLineArguments() const
con's avatar
con committed
552 553 554 555
{
    return m_commandLineArguments;
}

556 557 558 559 560 561 562 563
QString Qt4RunConfiguration::baseEnvironmentText() const
{
    if (m_baseEnvironmentBase == Qt4RunConfiguration::CleanEnvironmentBase)
        return tr("Clean Environment");
    else  if (m_baseEnvironmentBase == Qt4RunConfiguration::SystemEnvironmentBase)
        return tr("System Environment");
    else  if (m_baseEnvironmentBase == Qt4RunConfiguration::BuildEnvironmentBase)
        return tr("Build Environment");
564
    return QString();
565 566
}

567
Utils::Environment Qt4RunConfiguration::baseEnvironment() const
con's avatar
con committed
568
{
569
    Utils::Environment env;
570 571 572
    if (m_baseEnvironmentBase == Qt4RunConfiguration::CleanEnvironmentBase) {
        // Nothing
    } else  if (m_baseEnvironmentBase == Qt4RunConfiguration::SystemEnvironmentBase) {
573
        env = Utils::Environment::systemEnvironment();
574
    } else  if (m_baseEnvironmentBase == Qt4RunConfiguration::BuildEnvironmentBase) {
Tobias Hunger's avatar
Tobias Hunger committed
575
        env = target()->activeBuildConfiguration()->environment();
576
    }
577
    if (m_isUsingDyldImageSuffix) {
578
        env.set(QLatin1String("DYLD_IMAGE_SUFFIX"), QLatin1String("_debug"));
579
    }
580

581
    // The user could be linking to a library found via a -L/some/dir switch
582
    // to find those libraries while actually running we explicitly prepend those
583
    // dirs to the library search path
584
    const Qt4ProFileNode *node = qt4Target()->qt4Project()->rootQt4ProjectNode()->findProFileFor(m_proFilePath);
585 586 587
    if (node) {
        const QStringList libDirectories = node->variableValue(LibDirectoriesVar);
        if (!libDirectories.isEmpty()) {
588
            const QString proDirectory = node->buildDir();
589 590 591 592 593 594 595 596 597
            foreach (QString dir, libDirectories) {
                // Fix up relative entries like "LIBS+=-L.."
                const QFileInfo fi(dir);
                if (!fi.isAbsolute())
                    dir = QDir::cleanPath(proDirectory + QLatin1Char('/') + dir);
                env.prependOrSetLibrarySearchPath(dir);
            } // foreach
        } // libDirectories
    } // node
598 599 600 601

    QtSupport::BaseQtVersion *qtVersion = qt4Target()->activeQt4BuildConfiguration()->qtVersion();
    if (qtVersion)
        env.prependOrSetLibrarySearchPath(qtVersion->versionInfo().value(QLatin1String("QT_INSTALL_LIBS")));
602
    return env;
con's avatar
con committed
603 604
}

605
Utils::Environment Qt4RunConfiguration::environment() const
606
{
607
    Utils::Environment env = baseEnvironment();
608 609 610 611
    env.modify(userEnvironmentChanges());
    return env;
}

612
QList<Utils::EnvironmentItem> Qt4RunConfiguration::userEnvironmentChanges() const
613 614 615 616
{
    return m_userEnvironmentChanges;
}

617
void Qt4RunConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
618 619 620 621 622 623 624
{
    if (m_userEnvironmentChanges != diff) {
        m_userEnvironmentChanges = diff;
        emit userEnvironmentChangesChanged(diff);
    }
}

625
void Qt4RunConfiguration::setBaseWorkingDirectory(const QString &wd)
626
{
627 628 629 630 631 632 633
    const QString &oldWorkingDirectory = workingDirectory();

    m_userWorkingDirectory = wd;

    const QString &newWorkingDirectory = workingDirectory();
    if (oldWorkingDirectory != newWorkingDirectory)
        emit baseWorkingDirectoryChanged(newWorkingDirectory);
634 635
}

636
void Qt4RunConfiguration::setCommandLineArguments(const QString &argumentsString)
con's avatar
con committed
637
{
638 639
    m_commandLineArguments = argumentsString;
    emit commandLineArgumentsChanged(argumentsString);
con's avatar
con committed
640 641
}

642 643 644 645 646 647
void Qt4RunConfiguration::setRunMode(RunMode runMode)
{
    m_runMode = runMode;
    emit runModeChanged(runMode);
}

con's avatar
con committed
648 649 650 651 652
QString Qt4RunConfiguration::proFilePath() const
{
    return m_proFilePath;
}

653 654
QString Qt4RunConfiguration::dumperLibrary() const
{
655
    QtSupport::BaseQtVersion *version = qt4Target()->activeQt4BuildConfiguration()->qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
656
    if (version)
657
        return version->gdbDebuggingHelperLibrary();
658
    return QString();
659 660
}

661 662
QStringList Qt4RunConfiguration::dumperLibraryLocations() const
{
663
    QtSupport::BaseQtVersion *version = qt4Target()->activeQt4BuildConfiguration()->qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
664
    if (version)
665
        return version->debuggingHelperLibraryLocations();
666
    return QStringList();
667 668
}

669
QString Qt4RunConfiguration::defaultDisplayName()
670
{
671
    QString defaultName;
672
    if (!m_proFilePath.isEmpty())
673
        defaultName = QFileInfo(m_proFilePath).completeBaseName();
674
    else
675
        defaultName = tr("Qt4 Run Configuration");
676
    return defaultName;
677 678
}

679 680 681 682 683 684 685 686 687 688 689 690
void Qt4RunConfiguration::setBaseEnvironmentBase(BaseEnvironmentBase env)
{
    if (m_baseEnvironmentBase == env)
        return;
    m_baseEnvironmentBase = env;
    emit baseEnvironmentChanged();
}

Qt4RunConfiguration::BaseEnvironmentBase Qt4RunConfiguration::baseEnvironmentBase() const
{
    return m_baseEnvironmentBase;
}
691

con's avatar
con committed
692
Utils::OutputFormatter *Qt4RunConfiguration::createOutputFormatter() const
dt's avatar
dt committed
693
{
694
    return new QtSupport::QtOutputFormatter(qt4Target()->qt4Project());
dt's avatar
dt committed
695 696
}

con's avatar
con committed
697 698 699 700 701
///
/// Qt4RunConfigurationFactory
/// This class is used to restore run settings (saved in .user files)
///

702 703
Qt4RunConfigurationFactory::Qt4RunConfigurationFactory(QObject *parent) :
    ProjectExplorer::IRunConfigurationFactory(parent)
con's avatar
con committed
704 705 706 707 708 709 710
{
}

Qt4RunConfigurationFactory::~Qt4RunConfigurationFactory()
{
}

Tobias Hunger's avatar
Tobias Hunger committed
711
bool Qt4RunConfigurationFactory::canCreate(ProjectExplorer::Target *parent, const QString &id) const
con's avatar
con committed
712
{
713
    Qt4BaseTarget *t = qobject_cast<Qt4BaseTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
714 715
    if (!t)
        return false;
716 717
    if (t->id() != QLatin1String(Constants::DESKTOP_TARGET_ID)
            && t->id() != QLatin1String(Constants::QT_SIMULATOR_TARGET_ID))
718
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
719
    return t->qt4Project()->hasApplicationProFile(pathFromId(id));
con's avatar
con committed
720 721
}

Tobias Hunger's avatar
Tobias Hunger committed
722
ProjectExplorer::RunConfiguration *Qt4RunConfigurationFactory::create(ProjectExplorer::Target *parent, const QString &id)
con's avatar
con committed
723
{
724 725
    if (!canCreate(parent, id))
        return 0;
726
    Qt4BaseTarget *t = static_cast<Qt4BaseTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
727
    return new Qt4RunConfiguration(t, pathFromId(id));
728 729
}

Tobias Hunger's avatar
Tobias Hunger committed
730
bool Qt4RunConfigurationFactory::canRestore(ProjectExplorer::Target *parent, const QVariantMap &map) const
731
{
732
    if (!qobject_cast<Qt4BaseTarget *>(parent))
Tobias Hunger's avatar
Tobias Hunger committed
733
        return false;
734 735
    if (parent->id() != QLatin1String(Constants::DESKTOP_TARGET_ID)
            && parent->id() != QLatin1String(Constants::QT_SIMULATOR_TARGET_ID))
736 737 738 739 740
        return false;
    QString id(ProjectExplorer::idFromMap(map));
    return id.startsWith(QLatin1String(QT4_RC_ID));
}

Tobias Hunger's avatar
Tobias Hunger committed
741
ProjectExplorer::RunConfiguration *Qt4RunConfigurationFactory::restore(ProjectExplorer::Target *parent, const QVariantMap &map)
742 743 744
{
    if (!canRestore(parent, map))
        return 0;
745
    Qt4BaseTarget *t = static_cast<Qt4BaseTarget *>(parent);
746
    Qt4RunConfiguration *rc = new Qt4RunConfiguration(t, QString());
747 748 749 750 751 752 753
    if (rc->fromMap(map))
        return rc;

    delete rc;
    return 0;
}

Tobias Hunger's avatar
Tobias Hunger committed
754
bool Qt4RunConfigurationFactory::canClone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration *source) const
755
{
Tobias Hunger's avatar
Tobias Hunger committed
756
    return canCreate(parent, source->id());
757 758
}

Tobias Hunger's avatar
Tobias Hunger committed
759
ProjectExplorer::RunConfiguration *Qt4RunConfigurationFactory::clone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration *source)
760 761 762
{
    if (!canClone(parent, source))
        return 0;
763
    Qt4BaseTarget *t = static_cast<Qt4BaseTarget *>(parent);
764
    Qt4RunConfiguration *old = static_cast<Qt4RunConfiguration *>(source);
Tobias Hunger's avatar
Tobias Hunger committed
765
    return new Qt4RunConfiguration(t, old);
con's avatar
con committed
766 767
}

Tobias Hunger's avatar
Tobias Hunger committed
768
QStringList Qt4RunConfigurationFactory::availableCreationIds(ProjectExplorer::Target *parent) const
con's avatar
con committed
769
{
770
    Qt4BaseTarget *t = qobject_cast<Qt4BaseTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
771 772
    if (!t)
        return QStringList();
773
    if (t->id() != QLatin1String(Constants::DESKTOP_TARGET_ID)
774
        && t->id() != QLatin1String(Constants::QT_SIMULATOR_TARGET_ID))
con's avatar
con committed
775
        return QStringList();
Tobias Hunger's avatar
Tobias Hunger committed
776
    return t->qt4Project()->applicationProFilePathes(QLatin1String(QT4_RC_PREFIX));
con's avatar
con committed
777 778
}

779
QString Qt4RunConfigurationFactory::displayNameForId(const QString &id) const
con's avatar
con committed
780
{
781
    return QFileInfo(pathFromId(id)).completeBaseName();
con's avatar
con committed
782
}