cmakerunconfiguration.cpp 16.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
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"
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>
dt's avatar
dt committed
38
#include <utils/detailsbutton.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 62 63

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

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

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

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

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

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

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

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

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

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

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

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

dt's avatar
dt committed
141 142 143 144
}

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

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

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

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

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

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

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

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

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

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

234 235 236 237
// Configuration widget


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

    QFormLayout *fl = new QFormLayout();
242 243
    fl->setMargin(0);
    fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
244 245 246 247 248 249
    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);

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    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);

dt's avatar
dt committed
265 266 267 268 269 270 271 272 273 274 275
    m_detailsWidget = new QWidget(this);
    m_detailsWidget->setLayout(fl);
    m_detailsWidget->setVisible(false);

    m_summaryLabel = new QLabel(this);
    m_detailsButton = new Utils::DetailsButton(this);

    QHBoxLayout *hbox = new QHBoxLayout();
    hbox->addWidget(m_summaryLabel);
    hbox->addWidget(m_detailsButton);

276
    QVBoxLayout *vbx = new QVBoxLayout(this);
277
    vbx->setContentsMargins(0, -1, 0, -1);
dt's avatar
dt committed
278 279
    vbx->addLayout(hbox);
    vbx->addWidget(m_detailsWidget);
280 281 282 283 284 285 286 287

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

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

dt's avatar
dt committed
305 306 307 308 309 310 311 312
    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
313
    connect(m_workingDirectoryEdit, SIGNAL(changed(QString)),
314 315 316 317 318
            this, SLOT(setWorkingDirectory()));

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

dt's avatar
dt committed
319 320
    connect(m_detailsButton, SIGNAL(clicked()),
            this, SLOT(toggleDetails()));
321

322 323 324
    connect(m_environmentWidget, SIGNAL(userChangesUpdated()),
            this, SLOT(userChangesUpdated()));

325 326
    connect(m_cmakeRunConfiguration, SIGNAL(workingDirectoryChanged(QString)),
            this, SLOT(workingDirectoryChanged(QString)));
327 328 329 330
    connect(m_cmakeRunConfiguration, SIGNAL(baseEnvironmentChanged()),
            this, SLOT(baseEnvironmentChanged()));
    connect(m_cmakeRunConfiguration, SIGNAL(userEnvironmentChangesChanged(QList<ProjectExplorer::EnvironmentItem>)),
            this, SLOT(userEnvironmentChangesChanged()));
331 332 333

}

dt's avatar
dt committed
334 335 336 337 338
void CMakeRunConfigurationWidget::toggleDetails()
{
    m_detailsWidget->setVisible(!m_detailsWidget->isVisible());
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
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("");
359 360 361 362 363 364 365
}

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

366
void CMakeRunConfigurationWidget::baseEnvironmentComboBoxChanged(int index)
367 368
{
    m_ignoreChange = true;
369
    m_cmakeRunConfiguration->setBaseEnvironmentBase(CMakeRunConfiguration::BaseEnvironmentBase(index));
370 371 372 373 374

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

375 376
void CMakeRunConfigurationWidget::baseEnvironmentChanged()
{
377 378 379
    if (m_ignoreChange)
        return;

380
    m_baseEnvironmentComboBox->setCurrentIndex(m_cmakeRunConfiguration->baseEnvironmentBase());
381 382 383 384 385 386 387 388 389 390 391
    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
392 393 394 395 396 397 398 399 400
    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()));
    m_summaryLabel->setText(text);
401 402 403
}


dt's avatar
dt committed
404 405 406 407 408 409 410 411 412 413 414 415
// Factory
CMakeRunConfigurationFactory::CMakeRunConfigurationFactory()
{

}

CMakeRunConfigurationFactory::~CMakeRunConfigurationFactory()
{

}

// used to recreate the runConfigurations when restoring settings
con's avatar
con committed
416
bool CMakeRunConfigurationFactory::canRestore(const QString &type) const
dt's avatar
dt committed
417 418 419 420 421 422 423
{
    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
424
QStringList CMakeRunConfigurationFactory::availableCreationTypes(ProjectExplorer::Project *project) const
dt's avatar
dt committed
425 426 427 428
{
    CMakeProject *pro = qobject_cast<CMakeProject *>(project);
    if (!pro)
        return QStringList();
429 430 431 432 433
    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
434 435 436
}

// used to translate the types to names to display to the user
con's avatar
con committed
437
QString CMakeRunConfigurationFactory::displayNameForType(const QString &type) const
dt's avatar
dt committed
438
{
dt's avatar
dt committed
439
    Q_ASSERT(type.startsWith(Constants::CMAKERUNCONFIGURATION));
dt's avatar
dt committed
440 441 442 443 444 445 446 447 448 449

    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
450
    Q_ASSERT(pro);
dt's avatar
dt committed
451 452
    if (type == Constants::CMAKERUNCONFIGURATION) {
        // Restoring, filename will be added by restoreSettings
dt's avatar
dt committed
453
        QSharedPointer<ProjectExplorer::RunConfiguration> rc(new CMakeRunConfiguration(pro, QString::null, QString::null, QString::null));
dt's avatar
dt committed
454 455 456
        return rc;
    } else {
        // Adding new
457 458 459
        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
460 461 462
        return rc;
    }
}