qt4runconfiguration.cpp 30.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con 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
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con 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.
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
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "qt4runconfiguration.h"
hjk's avatar
hjk committed
31

32 33 34 35 36 37
#include "../makestep.h"
#include "../qt4nodes.h"
#include "../qt4project.h"
#include "../qt4buildconfiguration.h"
#include "../qt4projectmanagerconstants.h"
#include "../qmakestep.h"
con's avatar
con committed
38

39
#include <coreplugin/coreconstants.h>
con's avatar
con committed
40 41 42
#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/variablemanager.h>
43
#include <coreplugin/idocument.h>
44
#include <coreplugin/helpmanager.h>
con's avatar
con committed
45
#include <projectexplorer/buildstep.h>
46
#include <projectexplorer/environmentwidget.h>
Tobias Hunger's avatar
Tobias Hunger committed
47 48
#include <projectexplorer/target.h>
#include <projectexplorer/projectexplorerconstants.h>
hjk's avatar
hjk committed
49
#include <utils/qtcassert.h>
50
#include <utils/qtcprocess.h>
51 52
#include <utils/pathchooser.h>
#include <utils/detailswidget.h>
53
#include <utils/stringutils.h>
Jarek Kobus's avatar
Jarek Kobus committed
54
#include <utils/persistentsettings.h>
Tobias Hunger's avatar
Tobias Hunger committed
55
#include <qtsupport/customexecutablerunconfiguration.h>
56
#include <qtsupport/qtoutputformatter.h>
Tobias Hunger's avatar
Tobias Hunger committed
57
#include <qtsupport/qtsupportconstants.h>
58 59
#include <qtsupport/baseqtversion.h>
#include <qtsupport/profilereader.h>
Tobias Hunger's avatar
Tobias Hunger committed
60
#include <qtsupport/qtkitinformation.h>
61
#include <utils/hostosinfo.h>
con's avatar
con committed
62

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

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

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

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

90
QString pathFromId(Core::Id id)
con's avatar
con committed
91
{
Tobias Hunger's avatar
Tobias Hunger committed
92
    QString idstr = id.toString();
93 94
    const QString prefix = QLatin1String(QT4_RC_PREFIX);
    if (!idstr.startsWith(prefix))
95
        return QString();
96
    return idstr.mid(prefix.size());
97
}
98

99
} // namespace
100

101 102 103 104
//
// Qt4RunConfiguration
//

Tobias Hunger's avatar
Tobias Hunger committed
105 106 107
Qt4RunConfiguration::Qt4RunConfiguration(ProjectExplorer::Target *parent, Core::Id id) :
    LocalApplicationRunConfiguration(parent, id),
    m_proFilePath(pathFromId(id)),
108 109
    m_runMode(Gui),
    m_isUsingDyldImageSuffix(false),
Tobias Hunger's avatar
Tobias Hunger committed
110
    m_baseEnvironmentBase(Qt4RunConfiguration::BuildEnvironmentBase)
111
{
Tobias Hunger's avatar
Tobias Hunger committed
112 113 114 115
    Qt4Project *project = static_cast<Qt4Project *>(parent->project());
    m_parseSuccess = project->validParse(m_proFilePath);
    m_parseInProgress = project->parseInProgress(m_proFilePath);

116
    ctor();
con's avatar
con committed
117 118
}

Tobias Hunger's avatar
Tobias Hunger committed
119
Qt4RunConfiguration::Qt4RunConfiguration(ProjectExplorer::Target *parent, Qt4RunConfiguration *source) :
120 121 122 123 124 125 126
    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),
127
    m_baseEnvironmentBase(source->m_baseEnvironmentBase),
128 129
    m_parseSuccess(source->m_parseSuccess),
    m_parseInProgress(source->m_parseInProgress)
con's avatar
con committed
130
{
131
    ctor();
con's avatar
con committed
132 133
}

134
Qt4RunConfiguration::~Qt4RunConfiguration()
dt's avatar
dt committed
135 136 137
{
}

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

dt_'s avatar
dt_ committed
143 144
QString Qt4RunConfiguration::disabledReason() const
{
145
    if (m_parseInProgress)
146 147 148
        return tr("The .pro file '%1' is currently being parsed.")
                .arg(QFileInfo(m_proFilePath).fileName());

dt_'s avatar
dt_ committed
149
    if (!m_parseSuccess)
Tobias Hunger's avatar
Tobias Hunger committed
150
        return static_cast<Qt4Project *>(target()->project())->disabledReasonForRunConfiguration(m_proFilePath);
dt_'s avatar
dt_ committed
151 152 153
    return QString();
}

154
void Qt4RunConfiguration::proFileUpdated(Qt4ProjectManager::Qt4ProFileNode *pro, bool success, bool parseInProgress)
155
{
156 157 158 159 160 161
    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();
        }
162
        return;
163
    }
164

165
    bool enabled = isEnabled();
166
    QString reason = disabledReason();
167
    m_parseSuccess = success;
168
    m_parseInProgress = parseInProgress;
169
    if (enabled != isEnabled() || reason != disabledReason())
170
        emit enabledChanged();
171

172 173 174 175
    if (!parseInProgress) {
        emit effectiveTargetInformationChanged();
        emit baseEnvironmentChanged();
    }
176 177
}

178 179
void Qt4RunConfiguration::ctor()
{
180
    setDefaultDisplayName(defaultDisplayName());
181

Tobias Hunger's avatar
Tobias Hunger committed
182
    QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(target()->kit());
183
    m_forcedGuiMode = (version && version->type() == QLatin1String(QtSupport::Constants::SIMULATORQT));
Tobias Hunger's avatar
Tobias Hunger committed
184 185

    connect(target(), SIGNAL(environmentChanged()),
186
            this, SIGNAL(baseEnvironmentChanged()));
187 188
    connect(target()->project(), SIGNAL(proFileUpdated(Qt4ProjectManager::Qt4ProFileNode*,bool,bool)),
            this, SLOT(proFileUpdated(Qt4ProjectManager::Qt4ProFileNode*,bool,bool)));
Tobias Hunger's avatar
Tobias Hunger committed
189 190
    connect(target(), SIGNAL(kitChanged()),
            this, SLOT(kitChanged()));
Tobias Hunger's avatar
Tobias Hunger committed
191 192
}

Tobias Hunger's avatar
Tobias Hunger committed
193
void Qt4RunConfiguration::kitChanged()
Tobias Hunger's avatar
Tobias Hunger committed
194
{
Tobias Hunger's avatar
Tobias Hunger committed
195
    QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(target()->kit());
196
    m_forcedGuiMode = (version && version->type() == QLatin1String(QtSupport::Constants::SIMULATORQT));
Tobias Hunger's avatar
Tobias Hunger committed
197
    emit runModeChanged(runMode()); // Always emit
198 199
}

200 201 202 203 204
//////
/// Qt4RunConfigurationWidget
/////

Qt4RunConfigurationWidget::Qt4RunConfigurationWidget(Qt4RunConfiguration *qt4RunConfiguration, QWidget *parent)
205 206 207
    : QWidget(parent),
    m_qt4RunConfiguration(qt4RunConfiguration),
    m_ignoreChange(false),
mae's avatar
mae committed
208 209
    m_usingDyldImageSuffix(0),
    m_isShown(false)
con's avatar
con committed
210
{
dt's avatar
dt committed
211 212 213
    QVBoxLayout *vboxTopLayout = new QVBoxLayout(this);
    vboxTopLayout->setMargin(0);

214 215 216
    QHBoxLayout *hl = new QHBoxLayout();
    hl->addStretch();
    m_disabledIcon = new QLabel(this);
217
    m_disabledIcon->setPixmap(QPixmap(QLatin1String(":/projectexplorer/images/compile_warning.png")));
218 219 220 221 222 223 224
    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
225
    m_detailsContainer = new Utils::DetailsWidget(this);
226
    m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
dt's avatar
dt committed
227 228 229 230
    vboxTopLayout->addWidget(m_detailsContainer);
    QWidget *detailsWidget = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(detailsWidget);
    QFormLayout *toplayout = new QFormLayout(detailsWidget);
con's avatar
con committed
231
    toplayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
con's avatar
con committed
232 233
    toplayout->setMargin(0);

234 235 236
    m_executableLineEdit = new QLineEdit(m_qt4RunConfiguration->executable(), this);
    m_executableLineEdit->setEnabled(false);
    toplayout->addRow(tr("Executable:"), m_executableLineEdit);
con's avatar
con committed
237

Tobias Hunger's avatar
Tobias Hunger committed
238
    QLabel *argumentsLabel = new QLabel(tr("Arguments:"), this);
239
    m_argumentsLineEdit = new QLineEdit(qt4RunConfiguration->rawCommandLineArguments(), this);
240 241 242
    argumentsLabel->setBuddy(m_argumentsLineEdit);
    toplayout->addRow(argumentsLabel, m_argumentsLineEdit);

Tobias Hunger's avatar
Tobias Hunger committed
243
    m_workingDirectoryEdit = new Utils::PathChooser(this);
244
    m_workingDirectoryEdit->setExpectedKind(Utils::PathChooser::Directory);
245
    m_workingDirectoryEdit->setPath(m_qt4RunConfiguration->baseWorkingDirectory());
246
    m_workingDirectoryEdit->setBaseDirectory(m_qt4RunConfiguration->target()->project()->projectDirectory());
247
    m_workingDirectoryEdit->setEnvironment(m_qt4RunConfiguration->environment());
248
    m_workingDirectoryEdit->setPromptDialogTitle(tr("Select Working Directory"));
249

Tobias Hunger's avatar
Tobias Hunger committed
250
    QToolButton *resetButton = new QToolButton(this);
251
    resetButton->setToolTip(tr("Reset to default"));
252
    resetButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_RESET)));
253 254

    QHBoxLayout *boxlayout = new QHBoxLayout();
255
    boxlayout->setMargin(0);
256 257
    boxlayout->addWidget(m_workingDirectoryEdit);
    boxlayout->addWidget(resetButton);
258
    toplayout->addRow(tr("Working directory:"), boxlayout);
con's avatar
con committed
259

260
    m_useTerminalCheck = new QCheckBox(tr("Run in terminal"), this);
261
    m_useTerminalCheck->setChecked(m_qt4RunConfiguration->runMode() == ProjectExplorer::LocalApplicationRunConfiguration::Console);
262
    toplayout->addRow(QString(), m_useTerminalCheck);
Tobias Hunger's avatar
Tobias Hunger committed
263
    m_useTerminalCheck->setVisible(!m_qt4RunConfiguration->forcedGuiMode());
264

265 266 267 268 269 270 271
    if (Utils::HostOsInfo::isMacHost()) {
        m_usingDyldImageSuffix = new QCheckBox(tr("Use debug version of frameworks (DYLD_IMAGE_SUFFIX=_debug)"), this);
        m_usingDyldImageSuffix->setChecked(m_qt4RunConfiguration->isUsingDyldImageSuffix());
        toplayout->addRow(QString(), m_usingDyldImageSuffix);
        connect(m_usingDyldImageSuffix, SIGNAL(toggled(bool)),
                this, SLOT(usingDyldImageSuffixToggled(bool)));
    }
272

273 274 275 276 277 278
    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
279
    vboxTopLayout->addWidget(environmentLabel);
280

Tobias Hunger's avatar
Tobias Hunger committed
281
    QWidget *baseEnvironmentWidget = new QWidget(this);
282 283
    QHBoxLayout *baseEnvironmentLayout = new QHBoxLayout(baseEnvironmentWidget);
    baseEnvironmentLayout->setMargin(0);
284
    QLabel *label = new QLabel(tr("Base environment for this run configuration:"), this);
285
    baseEnvironmentLayout->addWidget(label);
286
    m_baseEnvironmentComboBox = new QComboBox(this);
287 288 289 290 291 292
    m_baseEnvironmentComboBox->addItems(QStringList()
                                        << tr("Clean Environment")
                                        << tr("System Environment")
                                        << tr("Build Environment"));
    m_baseEnvironmentComboBox->setCurrentIndex(qt4RunConfiguration->baseEnvironmentBase());
    connect(m_baseEnvironmentComboBox, SIGNAL(currentIndexChanged(int)),
293
            this, SLOT(baseEnvironmentSelected(int)));
294 295
    baseEnvironmentLayout->addWidget(m_baseEnvironmentComboBox);
    baseEnvironmentLayout->addStretch(10);
dt's avatar
dt committed
296

297
    m_environmentWidget = new ProjectExplorer::EnvironmentWidget(this, baseEnvironmentWidget);
298
    m_environmentWidget->setBaseEnvironment(m_qt4RunConfiguration->baseEnvironment());
299
    m_environmentWidget->setBaseEnvironmentText(m_qt4RunConfiguration->baseEnvironmentText());
300
    m_environmentWidget->setUserChanges(m_qt4RunConfiguration->userEnvironmentChanges());
dt's avatar
dt committed
301
    m_environmentWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
dt's avatar
dt committed
302
    vboxTopLayout->addWidget(m_environmentWidget);
303

304
    runConfigurationEnabledChange();
305

con's avatar
con committed
306
    connect(m_workingDirectoryEdit, SIGNAL(changed(QString)),
307
            this, SLOT(workDirectoryEdited()));
308 309

    connect(resetButton, SIGNAL(clicked()),
310
            this, SLOT(workingDirectoryReseted()));
311

312
    connect(m_argumentsLineEdit, SIGNAL(textEdited(QString)),
313
            this, SLOT(argumentsEdited(QString)));
314 315 316
    connect(m_useTerminalCheck, SIGNAL(toggled(bool)),
            this, SLOT(termToggled(bool)));

317 318
    connect(m_environmentWidget, SIGNAL(userChangesChanged()),
            this, SLOT(userChangesEdited()));
319

320
    connect(qt4RunConfiguration, SIGNAL(baseWorkingDirectoryChanged(QString)),
321 322
            this, SLOT(workingDirectoryChanged(QString)));

323
    connect(qt4RunConfiguration, SIGNAL(commandLineArgumentsChanged(QString)),
324
            this, SLOT(commandLineArgumentsChanged(QString)));
325 326
    connect(qt4RunConfiguration, SIGNAL(runModeChanged(ProjectExplorer::LocalApplicationRunConfiguration::RunMode)),
            this, SLOT(runModeChanged(ProjectExplorer::LocalApplicationRunConfiguration::RunMode)));
327 328
    connect(qt4RunConfiguration, SIGNAL(usingDyldImageSuffixChanged(bool)),
            this, SLOT(usingDyldImageSuffixChanged(bool)));
329 330
    connect(qt4RunConfiguration, SIGNAL(effectiveTargetInformationChanged()),
            this, SLOT(effectiveTargetInformationChanged()), Qt::QueuedConnection);
331

332 333
    connect(qt4RunConfiguration, SIGNAL(userEnvironmentChangesChanged(QList<Utils::EnvironmentItem>)),
            this, SLOT(userEnvironmentChangesChanged(QList<Utils::EnvironmentItem>)));
334

335 336
    connect(qt4RunConfiguration, SIGNAL(baseEnvironmentChanged()),
            this, SLOT(baseEnvironmentChanged()));
337

338 339
    connect(qt4RunConfiguration, SIGNAL(enabledChanged()),
            this, SLOT(runConfigurationEnabledChange()));
340 341
}

Tobias Hunger's avatar
Tobias Hunger committed
342 343 344 345
Qt4RunConfigurationWidget::~Qt4RunConfigurationWidget()
{
}

346
void Qt4RunConfigurationWidget::baseEnvironmentSelected(int index)
347 348
{
    m_ignoreChange = true;
349
    m_qt4RunConfiguration->setBaseEnvironmentBase(Qt4RunConfiguration::BaseEnvironmentBase(index));
350 351

    m_environmentWidget->setBaseEnvironment(m_qt4RunConfiguration->baseEnvironment());
352
    m_environmentWidget->setBaseEnvironmentText(m_qt4RunConfiguration->baseEnvironmentText());
353 354 355
    m_ignoreChange = false;
}

356 357
void Qt4RunConfigurationWidget::baseEnvironmentChanged()
{
358 359 360
    if (m_ignoreChange)
        return;

361
    m_baseEnvironmentComboBox->setCurrentIndex(m_qt4RunConfiguration->baseEnvironmentBase());
362
    m_environmentWidget->setBaseEnvironment(m_qt4RunConfiguration->baseEnvironment());
363
    m_environmentWidget->setBaseEnvironmentText(m_qt4RunConfiguration->baseEnvironmentText());
364 365
}

366
void Qt4RunConfigurationWidget::userEnvironmentChangesChanged(const QList<Utils::EnvironmentItem> &userChanges)
367 368 369 370 371 372
{
    if (m_ignoreChange)
        return;
    m_environmentWidget->setUserChanges(userChanges);
}

373
void Qt4RunConfigurationWidget::userChangesEdited()
374 375 376 377
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->setUserEnvironmentChanges(m_environmentWidget->userChanges());
    m_ignoreChange = false;
378 379
}

380
void Qt4RunConfigurationWidget::runConfigurationEnabledChange()
381
{
382
    bool enabled = m_qt4RunConfiguration->isEnabled();
383 384 385 386 387
    m_detailsContainer->setEnabled(enabled);
    m_environmentWidget->setEnabled(enabled);
    m_disabledIcon->setVisible(!enabled);
    m_disabledReason->setVisible(!enabled);
    m_disabledReason->setText(m_qt4RunConfiguration->disabledReason());
388 389
}

390
void Qt4RunConfigurationWidget::workDirectoryEdited()
391 392 393 394
{
    if (m_ignoreChange)
        return;
    m_ignoreChange = true;
395
    m_qt4RunConfiguration->setBaseWorkingDirectory(m_workingDirectoryEdit->rawPath());
396 397 398
    m_ignoreChange = false;
}

399
void Qt4RunConfigurationWidget::workingDirectoryReseted()
400 401 402
{
    // This emits a signal connected to workingDirectoryChanged()
    // that sets the m_workingDirectoryEdit
403
    m_qt4RunConfiguration->setBaseWorkingDirectory(QString());
404 405
}

406
void Qt4RunConfigurationWidget::argumentsEdited(const QString &args)
407 408
{
    m_ignoreChange = true;
409
    m_qt4RunConfiguration->setCommandLineArguments(args);
410 411 412
    m_ignoreChange = false;
}

413 414 415
void Qt4RunConfigurationWidget::termToggled(bool on)
{
    m_ignoreChange = true;
416 417
    m_qt4RunConfiguration->setRunMode(on ? LocalApplicationRunConfiguration::Console
                                         : LocalApplicationRunConfiguration::Gui);
418 419 420
    m_ignoreChange = false;
}

421 422 423 424 425 426 427
void Qt4RunConfigurationWidget::usingDyldImageSuffixToggled(bool state)
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->setUsingDyldImageSuffix(state);
    m_ignoreChange = false;
}

428 429 430 431 432 433
void Qt4RunConfigurationWidget::workingDirectoryChanged(const QString &workingDirectory)
{
    if (!m_ignoreChange)
        m_workingDirectoryEdit->setPath(workingDirectory);
}

434 435
void Qt4RunConfigurationWidget::commandLineArgumentsChanged(const QString &args)
{
dt's avatar
dt committed
436 437 438
    if (m_ignoreChange)
        return;
    m_argumentsLineEdit->setText(args);
439 440
}

441
void Qt4RunConfigurationWidget::runModeChanged(LocalApplicationRunConfiguration::RunMode runMode)
442
{
Tobias Hunger's avatar
Tobias Hunger committed
443 444
    if (!m_ignoreChange) {
        m_useTerminalCheck->setVisible(!m_qt4RunConfiguration->forcedGuiMode());
445
        m_useTerminalCheck->setChecked(runMode == LocalApplicationRunConfiguration::Console);
Tobias Hunger's avatar
Tobias Hunger committed
446
    }
447 448
}

449 450 451 452 453 454
void Qt4RunConfigurationWidget::usingDyldImageSuffixChanged(bool state)
{
    if (!m_ignoreChange && m_usingDyldImageSuffix)
        m_usingDyldImageSuffix->setChecked(state);
}

455
void Qt4RunConfigurationWidget::effectiveTargetInformationChanged()
456
{
457
    if (m_isShown) {
458
        m_executableLineEdit->setText(QDir::toNativeSeparators(m_qt4RunConfiguration->executable()));
459
        m_ignoreChange = true;
460
        m_workingDirectoryEdit->setPath(QDir::toNativeSeparators(m_qt4RunConfiguration->baseWorkingDirectory()));
461
        m_ignoreChange = false;
462
    }
463 464
}

465
void Qt4RunConfigurationWidget::showEvent(QShowEvent *event)
466
{
467 468 469
    m_isShown = true;
    effectiveTargetInformationChanged();
    QWidget::showEvent(event);
470 471
}

472 473 474 475 476
void Qt4RunConfigurationWidget::hideEvent(QHideEvent *event)
{
    m_isShown = false;
    QWidget::hideEvent(event);
}
477

478
QWidget *Qt4RunConfiguration::createConfigurationWidget()
479 480
{
    return new Qt4RunConfigurationWidget(this, 0);
con's avatar
con committed
481 482
}

483
QVariantMap Qt4RunConfiguration::toMap() const
con's avatar
con committed
484
{
485
    const QDir projectDir = QDir(target()->project()->projectDirectory());
486 487 488 489 490
    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);
491
    map.insert(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY), Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
492 493 494
    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
495 496
}

497
bool Qt4RunConfiguration::fromMap(const QVariantMap &map)
498
{
499
    const QDir projectDir = QDir(target()->project()->projectDirectory());
500
    m_commandLineArguments = map.value(QLatin1String(COMMAND_LINE_ARGUMENTS_KEY)).toString();
501
    m_proFilePath = QDir::cleanPath(projectDir.filePath(map.value(QLatin1String(PRO_FILE_KEY)).toString()));
502 503 504 505 506
    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();

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

Tobias Hunger's avatar
Tobias Hunger committed
510 511
    m_parseSuccess = static_cast<Qt4Project *>(target()->project())->validParse(m_proFilePath);
    m_parseInProgress = static_cast<Qt4Project *>(target()->project())->parseInProgress(m_proFilePath);
512

513
    return RunConfiguration::fromMap(map);
con's avatar
con committed
514 515 516 517
}

QString Qt4RunConfiguration::executable() const
{
Tobias Hunger's avatar
Tobias Hunger committed
518
    Qt4Project *pro = static_cast<Qt4Project *>(target()->project());
519
    TargetInformation ti = pro->rootQt4ProjectNode()->targetInformation(m_proFilePath);
dt's avatar
dt committed
520 521 522
    if (!ti.valid)
        return QString();
    return ti.executable;
con's avatar
con committed
523 524
}

525
LocalApplicationRunConfiguration::RunMode Qt4RunConfiguration::runMode() const
con's avatar
con committed
526
{
Tobias Hunger's avatar
Tobias Hunger committed
527 528
    if (m_forcedGuiMode)
        return LocalApplicationRunConfiguration::Gui;
con's avatar
con committed
529 530 531
    return m_runMode;
}

Tobias Hunger's avatar
Tobias Hunger committed
532 533 534 535 536
bool Qt4RunConfiguration::forcedGuiMode() const
{
    return m_forcedGuiMode;
}

537 538 539 540 541 542 543 544 545 546 547
bool Qt4RunConfiguration::isUsingDyldImageSuffix() const
{
    return m_isUsingDyldImageSuffix;
}

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

con's avatar
con committed
548
QString Qt4RunConfiguration::workingDirectory() const
549
{
550 551
    return QDir::cleanPath(environment().expandVariables(
                Utils::expandMacros(baseWorkingDirectory(), macroExpander())));
552 553 554
}

QString Qt4RunConfiguration::baseWorkingDirectory() const
con's avatar
con committed
555
{
556
    // if the user overrode us, then return his working directory
557
    if (!m_userWorkingDirectory.isEmpty())
558 559 560
        return m_userWorkingDirectory;

    // else what the pro file reader tells us
Tobias Hunger's avatar
Tobias Hunger committed
561
    Qt4Project *pro = static_cast<Qt4Project *>(target()->project());
562
    TargetInformation ti = pro->rootQt4ProjectNode()->targetInformation(m_proFilePath);
hjk's avatar
hjk committed
563
    if (!ti.valid)
dt's avatar
dt committed
564 565
        return QString();
    return ti.workingDir;
con's avatar
con committed
566 567
}

568
QString Qt4RunConfiguration::commandLineArguments() const
569 570 571 572 573
{
    return Utils::QtcProcess::expandMacros(m_commandLineArguments, macroExpander());
}

QString Qt4RunConfiguration::rawCommandLineArguments() const
con's avatar
con committed
574 575 576 577
{
    return m_commandLineArguments;
}

578 579 580 581 582 583 584 585
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");
586
    return QString();
587 588
}

589
Utils::Environment Qt4RunConfiguration::baseEnvironment() const
con's avatar
con committed
590
{
591
    Utils::Environment env;
592 593 594
    if (m_baseEnvironmentBase == Qt4RunConfiguration::CleanEnvironmentBase) {
        // Nothing
    } else  if (m_baseEnvironmentBase == Qt4RunConfiguration::SystemEnvironmentBase) {
595
        env = Utils::Environment::systemEnvironment();
Tobias Hunger's avatar
Tobias Hunger committed
596 597
    } else  if (m_baseEnvironmentBase == Qt4RunConfiguration::BuildEnvironmentBase
                && target()->activeBuildConfiguration()) {
Tobias Hunger's avatar
Tobias Hunger committed
598
        env = target()->activeBuildConfiguration()->environment();
599
    }
600
    if (m_isUsingDyldImageSuffix) {
601
        env.set(QLatin1String("DYLD_IMAGE_SUFFIX"), QLatin1String("_debug"));
602
    }
603

604
    // The user could be linking to a library found via a -L/some/dir switch
605
    // to find those libraries while actually running we explicitly prepend those
606
    // dirs to the library search path
Tobias Hunger's avatar
Tobias Hunger committed
607
    const Qt4ProFileNode *node = static_cast<Qt4Project *>(target()->project())->rootQt4ProjectNode()->findProFileFor(m_proFilePath);
608 609 610
    if (node) {
        const QStringList libDirectories = node->variableValue(LibDirectoriesVar);
        if (!libDirectories.isEmpty()) {
611
            const QString proDirectory = node->buildDir();
612 613 614 615 616 617 618 619 620
            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
621

Tobias Hunger's avatar
Tobias Hunger committed
622
    QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitInformation::qtVersion(target()->kit());
623
    if (qtVersion)
624
        env.prependOrSetLibrarySearchPath(qtVersion->qmakeProperty("QT_INSTALL_LIBS"));
625
    return env;
con's avatar
con committed
626 627
}

628
Utils::Environment Qt4RunConfiguration::environment() const
629
{
630
    Utils::Environment env = baseEnvironment();
631 632 633 634
    env.modify(userEnvironmentChanges());
    return env;
}

635
QList<Utils::EnvironmentItem> Qt4RunConfiguration::userEnvironmentChanges() const
636 637 638 639
{
    return m_userEnvironmentChanges;
}

640
void Qt4RunConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
641 642 643 644 645 646 647
{
    if (m_userEnvironmentChanges != diff) {
        m_userEnvironmentChanges = diff;
        emit userEnvironmentChangesChanged(diff);
    }
}

648
void Qt4RunConfiguration::setBaseWorkingDirectory(const QString &wd)
649
{
650 651 652 653 654 655 656
    const QString &oldWorkingDirectory = workingDirectory();

    m_userWorkingDirectory = wd;

    const QString &newWorkingDirectory = workingDirectory();
    if (oldWorkingDirectory != newWorkingDirectory)
        emit baseWorkingDirectoryChanged(newWorkingDirectory);
657 658
}

659
void Qt4RunConfiguration::setCommandLineArguments(const QString &argumentsString)
con's avatar
con committed
660
{
661 662
    m_commandLineArguments = argumentsString;
    emit commandLineArgumentsChanged(argumentsString);
con's avatar
con committed
663 664
}

665 666 667 668 669 670
void Qt4RunConfiguration::setRunMode(RunMode runMode)
{
    m_runMode = runMode;
    emit runModeChanged(runMode);
}

con's avatar
con committed
671 672 673 674 675
QString Qt4RunConfiguration::proFilePath() const
{
    return m_proFilePath;
}

676 677
QString Qt4RunConfiguration::dumperLibrary() const
{
Tobias Hunger's avatar
Tobias Hunger committed
678
    QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(target()->kit());
Tobias Hunger's avatar
Tobias Hunger committed
679
    if (version)
680
        return version->gdbDebuggingHelperLibrary();
681
    return QString();
682 683
}

684 685
QStringList Qt4RunConfiguration::dumperLibraryLocations() const
{
Tobias Hunger's avatar
Tobias Hunger committed
686
    QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(target()->kit());
Tobias Hunger's avatar
Tobias Hunger committed
687
    if (version)
688
        return version->debuggingHelperLibraryLocations();
689
    return QStringList();
690 691
}

692
QString Qt4RunConfiguration::defaultDisplayName()
693
{
694
    QString defaultName;
695
    if (!m_proFilePath.isEmpty())
696
        defaultName = QFileInfo(m_proFilePath).completeBaseName();
697
    else
Caroline Chao's avatar
Caroline Chao committed
698
        defaultName = tr("Qt Run Configuration");
699
    return defaultName;
700 701
}

702 703 704 705 706 707 708 709 710 711 712 713
void Qt4RunConfiguration::setBaseEnvironmentBase(BaseEnvironmentBase env)
{
    if (m_baseEnvironmentBase == env)
        return;
    m_baseEnvironmentBase = env;
    emit baseEnvironmentChanged();
}

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

con's avatar
con committed
715
Utils::OutputFormatter *Qt4RunConfiguration::createOutputFormatter() const
dt's avatar
dt committed
716
{
Tobias Hunger's avatar
Tobias Hunger committed
717
    return new QtSupport::QtOutputFormatter(target()->project());
dt's avatar
dt committed
718 719
}

con's avatar
con committed
720 721 722 723 724
///
/// Qt4RunConfigurationFactory
/// This class is used to restore run settings (saved in .user files)
///

725
Qt4RunConfigurationFactory::Qt4RunConfigurationFactory(QObject *parent) :
Tobias Hunger's avatar
Tobias Hunger committed
726 727
    QmakeRunConfigurationFactory(parent)
{ setObjectName(QLatin1String("Qt4RunConfigurationFactory")); }
con's avatar
con committed
728 729

Qt4RunConfigurationFactory::~Qt4RunConfigurationFactory()
Tobias Hunger's avatar
Tobias Hunger committed
730
{ }
con's avatar
con committed
731

732
bool Qt4RunConfigurationFactory::canCreate(ProjectExplorer::Target *parent, const Core::Id id) const
con's avatar
con committed
733
{
Tobias Hunger's avatar
Tobias Hunger committed
734
    if (!canHandle(parent))
Tobias Hunger's avatar
Tobias Hunger committed
735
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
736 737
    Qt4Project *project = static_cast<Qt4Project *>(parent->project());
    return project->hasApplicationProFile(pathFromId(id));
con's avatar
con committed
738 739
}

740
ProjectExplorer::RunConfiguration *Qt4RunConfigurationFactory::create(ProjectExplorer::Target *parent, const Core::Id id)
con's avatar
con committed
741
{
742 743
    if (!canCreate(parent, id))
        return 0;
Tobias Hunger's avatar
Tobias Hunger committed
744 745 746 747 748 749 750 751 752 753 754 755

    Qt4RunConfiguration *rc = new Qt4RunConfiguration(parent, id);
    QList<Qt4ProFileNode *> profiles = static_cast<Qt4Project *>(parent->project())->applicationProFiles();
    foreach (Qt4ProFileNode *node, profiles) {
        if (node->path() != rc->proFilePath())
            continue;
        rc->setRunMode(node->variableValue(ConfigVar).contains(QLatin1String("console"))
                       ? ProjectExplorer::LocalApplicationRunConfiguration::Console
                       : ProjectExplorer::LocalApplicationRunConfiguration::Gui);
        break;
    }
    return rc;
756 757
}

Tobias Hunger's avatar
Tobias Hunger committed
758
bool Qt4RunConfigurationFactory::canRestore(ProjectExplorer::Target *parent, const QVariantMap &map) const
759
{
Tobias Hunger's avatar
Tobias Hunger committed
760
    if (!canHandle(parent))
761
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
762
    return ProjectExplorer::idFromMap(map).toString().startsWith(QLatin1String(QT4_RC_PREFIX));
763 764
}

Tobias Hunger's avatar
Tobias Hunger committed
765
ProjectExplorer::RunConfiguration *Qt4RunConfigurationFactory::restore(ProjectExplorer::Target *parent, const QVariantMap &map)
766 767 768
{
    if (!canRestore(parent, map))
        return 0;
Tobias Hunger's avatar
Tobias Hunger committed
769
    Qt4RunConfiguration *rc = new Qt4RunConfiguration(parent, ProjectExplorer::idFromMap(map));
770 771 772 773 774 775 776
    if (rc->fromMap(map))
        return rc;

    delete rc;
    return 0;
}

Tobias Hunger's avatar
Tobias Hunger committed
777
bool Qt4RunConfigurationFactory::canClone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration *source) const
778
{
Tobias Hunger's avatar
Tobias Hunger committed
779
    return canCreate(parent, source->id());
780 781
}

Tobias Hunger's avatar
Tobias Hunger committed
782
ProjectExplorer::RunConfiguration *Qt4RunConfigurationFactory::clone(ProjectExplorer::Target *parent, ProjectExplorer::RunConfiguration *source)
783 784 785
{
    if (!canClone(parent, source))
        return 0;
786
    Qt4RunConfiguration *old = static_cast<Qt4RunConfiguration *>(source);
Tobias Hunger's avatar
Tobias Hunger committed
787
    return new Qt4RunConfiguration(parent, old);
con's avatar
con committed
788 789
}