qt4runconfiguration.cpp 15.5 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
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.
15
**
16
** GNU Lesser General Public License Usage
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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

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

hjk's avatar
hjk committed
32
#include "makestep.h"
con's avatar
con committed
33
34
#include "profilereader.h"
#include "qt4nodes.h"
hjk's avatar
hjk committed
35
#include "qt4project.h"
con's avatar
con committed
36
37
38
39
40

#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/variablemanager.h>
#include <projectexplorer/buildstep.h>
hjk's avatar
hjk committed
41
#include <utils/qtcassert.h>
con's avatar
con committed
42
43
44

#include <QtGui/QFormLayout>
#include <QtGui/QInputDialog>
45
#include <QtGui/QLabel>
46
#include <QtGui/QCheckBox>
con's avatar
con committed
47
48
49
50
51
52
53
54

using namespace Qt4ProjectManager::Internal;
using namespace Qt4ProjectManager;
using ProjectExplorer::ApplicationRunConfiguration;
using ProjectExplorer::PersistentSettingsReader;
using ProjectExplorer::PersistentSettingsWriter;

Qt4RunConfiguration::Qt4RunConfiguration(Qt4Project *pro, QString proFilePath)
55
56
    : ApplicationRunConfiguration(pro),
      m_proFilePath(proFilePath),
57
      m_runMode(Gui),
58
59
60
      m_userSetName(false),
      m_configWidget(0),
      m_executableLabel(0),
61
62
      m_workingDirectoryLabel(0),
      m_cachedTargetInformationValid(false)
con's avatar
con committed
63
64
65
66
67
{
    setName(tr("Qt4RunConfiguration"));
    if (!m_proFilePath.isEmpty()) {
        setName(QFileInfo(m_proFilePath).baseName());
    }
68

69
    connect(pro, SIGNAL(activeBuildConfigurationChanged()),
70
            this, SLOT(invalidateCachedTargetInformation()));
con's avatar
con committed
71
72
73
74
}

Qt4RunConfiguration::~Qt4RunConfiguration()
{
75

con's avatar
con committed
76
77
78
79
80
81
82
}

QString Qt4RunConfiguration::type() const
{
    return "Qt4ProjectManager.Qt4RunConfiguration";
}

83
84
85
86
87
88

//////
/// Qt4RunConfigurationWidget
/////

Qt4RunConfigurationWidget::Qt4RunConfigurationWidget(Qt4RunConfiguration *qt4RunConfiguration, QWidget *parent)
89
    : QWidget(parent), m_qt4RunConfiguration(qt4RunConfiguration), m_ignoreChange(false), m_isShown(false)
con's avatar
con committed
90
{
91
    QFormLayout *toplayout = new QFormLayout(this);
con's avatar
con committed
92
93
94
    toplayout->setMargin(0);

    QLabel *nameLabel = new QLabel(tr("Name:"));
95
96
97
    m_nameLineEdit = new QLineEdit(m_qt4RunConfiguration->name());
    nameLabel->setBuddy(m_nameLineEdit);
    toplayout->addRow(nameLabel, m_nameLineEdit);
con's avatar
con committed
98

99
100
    m_executableLabel = new QLabel(m_qt4RunConfiguration->executable());
    toplayout->addRow(tr("Executable:"), m_executableLabel);
con's avatar
con committed
101

102
103
    m_workingDirectoryLabel = new QLabel(m_qt4RunConfiguration->workingDirectory());
    toplayout->addRow(tr("Working Directory:"), m_workingDirectoryLabel);
con's avatar
con committed
104
105

    QLabel *argumentsLabel = new QLabel(tr("&Arguments:"));
106
107
108
    m_argumentsLineEdit = new QLineEdit(ProjectExplorer::Environment::joinArgumentList(qt4RunConfiguration->commandLineArguments()));
    argumentsLabel->setBuddy(m_argumentsLineEdit);
    toplayout->addRow(argumentsLabel, m_argumentsLineEdit);
con's avatar
con committed
109

110
111
112
113
    m_useTerminalCheck = new QCheckBox(tr("Run in &Terminal"));
    m_useTerminalCheck->setChecked(m_qt4RunConfiguration->runMode() == ProjectExplorer::ApplicationRunConfiguration::Console);
    toplayout->addRow(QString(), m_useTerminalCheck);

114
115
    connect(m_argumentsLineEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCommandLineArguments(QString)));
con's avatar
con committed
116

117
118
    connect(m_nameLineEdit, SIGNAL(textEdited(QString)),
            this, SLOT(nameEdited(QString)));
con's avatar
con committed
119

120
121
122
    connect(m_useTerminalCheck, SIGNAL(toggled(bool)),
            this, SLOT(termToggled(bool)));

123
124
125
126
    connect(qt4RunConfiguration, SIGNAL(commandLineArgumentsChanged(QString)),
            this, SLOT(commandLineArgumentsChanged(QString)));
    connect(qt4RunConfiguration, SIGNAL(nameChanged(QString)),
            this, SLOT(nameChanged(QString)));
127
128
    connect(qt4RunConfiguration, SIGNAL(runModeChanged(ProjectExplorer::ApplicationRunConfiguration::RunMode)),
            this, SLOT(runModeChanged(ProjectExplorer::ApplicationRunConfiguration::RunMode)));
129

130
131
    connect(qt4RunConfiguration, SIGNAL(effectiveTargetInformationChanged()),
            this, SLOT(effectiveTargetInformationChanged()), Qt::QueuedConnection);
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
}

void Qt4RunConfigurationWidget::setCommandLineArguments(const QString &args)
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->setCommandLineArguments(args);
    m_ignoreChange = false;
}

void Qt4RunConfigurationWidget::nameEdited(const QString &name)
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->nameEdited(name);
    m_ignoreChange = false;
}

148
149
150
151
152
153
154
155
void Qt4RunConfigurationWidget::termToggled(bool on)
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->setRunMode(on ? ApplicationRunConfiguration::Console
                                         : ApplicationRunConfiguration::Gui);
    m_ignoreChange = false;
}

156
157
158
159
160
161
162
163
164
165
166
167
void Qt4RunConfigurationWidget::commandLineArgumentsChanged(const QString &args)
{
    if (!m_ignoreChange)
        m_argumentsLineEdit->setText(args);
}

void Qt4RunConfigurationWidget::nameChanged(const QString &name)
{
    if (!m_ignoreChange)
        m_nameLineEdit->setText(name);
}

168
169
170
171
172
173
void Qt4RunConfigurationWidget::runModeChanged(ApplicationRunConfiguration::RunMode runMode)
{
    if (!m_ignoreChange)
        m_useTerminalCheck->setChecked(runMode == ApplicationRunConfiguration::Console);
}

174
void Qt4RunConfigurationWidget::effectiveTargetInformationChanged()
175
{
176
177
178
179
    if (m_isShown) {
        m_executableLabel->setText(m_qt4RunConfiguration->executable());
        m_workingDirectoryLabel->setText(m_qt4RunConfiguration->workingDirectory());
    }
180
181
}

182
void Qt4RunConfigurationWidget::showEvent(QShowEvent *event)
183
{
184
185
186
    m_isShown = true;
    effectiveTargetInformationChanged();
    QWidget::showEvent(event);
187
188
}

189
190
191
192
193
void Qt4RunConfigurationWidget::hideEvent(QHideEvent *event)
{
    m_isShown = false;
    QWidget::hideEvent(event);
}
194

195
////// TODO c&p above
196
197
198
QWidget *Qt4RunConfiguration::configurationWidget()
{
    return new Qt4RunConfigurationWidget(this, 0);
con's avatar
con committed
199
200
201
202
203
204
205
}

void Qt4RunConfiguration::save(PersistentSettingsWriter &writer) const
{
    writer.saveValue("CommandLineArguments", m_commandLineArguments);
    writer.saveValue("ProFile", m_proFilePath);
    writer.saveValue("UserSetName", m_userSetName);
206
    writer.saveValue("UseTerminal", m_runMode == Console);
con's avatar
con committed
207
208
209
210
211
212
213
214
215
    ApplicationRunConfiguration::save(writer);
}

void Qt4RunConfiguration::restore(const PersistentSettingsReader &reader)
{
    ApplicationRunConfiguration::restore(reader);
    m_commandLineArguments = reader.restoreValue("CommandLineArguments").toStringList();
    m_proFilePath = reader.restoreValue("ProFile").toString();
    m_userSetName = reader.restoreValue("UserSetName").toBool();
216
    m_runMode = reader.restoreValue("UseTerminal").toBool() ? Console : Gui;
con's avatar
con committed
217
    if (!m_proFilePath.isEmpty()) {
218
        m_cachedTargetInformationValid = false;
con's avatar
con committed
219
220
221
222
223
224
225
        if (!m_userSetName)
            setName(QFileInfo(m_proFilePath).baseName());
    }
}

QString Qt4RunConfiguration::executable() const
{
226
227
    const_cast<Qt4RunConfiguration *>(this)->updateTarget();
    return m_executable;
con's avatar
con committed
228
229
230
231
232
233
234
235
236
}

ApplicationRunConfiguration::RunMode Qt4RunConfiguration::runMode() const
{
    return m_runMode;
}

QString Qt4RunConfiguration::workingDirectory() const
{
237
238
    const_cast<Qt4RunConfiguration *>(this)->updateTarget();
    return m_workingDir;
con's avatar
con committed
239
240
241
242
243
244
245
246
247
248
}

QStringList Qt4RunConfiguration::commandLineArguments() const
{
    return m_commandLineArguments;
}

ProjectExplorer::Environment Qt4RunConfiguration::environment() const
{
    Qt4Project *pro = qobject_cast<Qt4Project *>(project());
dt's avatar
dt committed
249
    Q_ASSERT(pro);
con's avatar
con committed
250
251
252
253
254
255
    return pro->environment(pro->activeBuildConfiguration());
}

void Qt4RunConfiguration::setCommandLineArguments(const QString &argumentsString)
{
    m_commandLineArguments = ProjectExplorer::Environment::parseCombinedArgString(argumentsString);
256
    emit commandLineArgumentsChanged(argumentsString);
con's avatar
con committed
257
258
}

259
260
261
262
263
264
void Qt4RunConfiguration::setRunMode(RunMode runMode)
{
    m_runMode = runMode;
    emit runModeChanged(runMode);
}

con's avatar
con committed
265
266
267
268
269
270
271
272
273
void Qt4RunConfiguration::nameEdited(const QString &name)
{
    if (name == "") {
        setName(tr("Qt4RunConfiguration"));
        m_userSetName = false;
    } else {
        setName(name);
        m_userSetName = true;
    }
274
    emit nameChanged(name);
con's avatar
con committed
275
276
277
278
279
280
281
}

QString Qt4RunConfiguration::proFilePath() const
{
    return m_proFilePath;
}

282
void Qt4RunConfiguration::updateTarget()
con's avatar
con committed
283
{
284
285
286
287
288
    if (m_cachedTargetInformationValid)
        return;
    //qDebug()<<"updateTarget";
    Qt4Project *pro = static_cast<Qt4Project *>(project());
    ProFileReader *reader = pro->createProFileReader();
289
    reader->setCumulative(false);
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    reader->setQtVersion(pro->qtVersion(pro->activeBuildConfiguration()));

    // Find out what flags we pass on to qmake, this code is duplicated in the qmake step
    QtVersion::QmakeBuildConfig defaultBuildConfiguration = pro->qtVersion(pro->activeBuildConfiguration())->defaultBuildConfig();
    QtVersion::QmakeBuildConfig projectBuildConfiguration = QtVersion::QmakeBuildConfig(pro->qmakeStep()->value(pro->activeBuildConfiguration(), "buildConfiguration").toInt());
    QStringList addedUserConfigArguments;
    QStringList removedUserConfigArguments;
    if ((defaultBuildConfiguration & QtVersion::BuildAll) && !(projectBuildConfiguration & QtVersion::BuildAll))
        removedUserConfigArguments << "debug_and_release";
    if (!(defaultBuildConfiguration & QtVersion::BuildAll) && (projectBuildConfiguration & QtVersion::BuildAll))
        addedUserConfigArguments << "debug_and_release";
    if ((defaultBuildConfiguration & QtVersion::DebugBuild) && !(projectBuildConfiguration & QtVersion::DebugBuild))
        addedUserConfigArguments << "release";
    if (!(defaultBuildConfiguration & QtVersion::DebugBuild) && (projectBuildConfiguration & QtVersion::DebugBuild))
        addedUserConfigArguments << "debug";

    reader->setUserConfigCmdArgs(addedUserConfigArguments, removedUserConfigArguments);

    QHash<QString, QStringList>::const_iterator it;

con's avatar
con committed
310
311
    if (!reader->readProFile(m_proFilePath)) {
        delete reader;
312
        Core::ICore::instance()->messageManager()->printToOutputPane(QString("Could not parse %1. The Qt4 run configuration %2 can not be started.").arg(m_proFilePath).arg(name()));
con's avatar
con committed
313
314
315
        return;
    }

316
317
318
    // Extract data
    QString relSubDir = QFileInfo(project()->file()->fileName()).absoluteDir().relativeFilePath(QFileInfo(m_proFilePath).path());
    QString baseDir = QDir(project()->buildDirectory(project()->activeBuildConfiguration())).absoluteFilePath(relSubDir);
con's avatar
con committed
319

320
321
322
    //qDebug()<<relSubDir<<baseDir;

    // Working Directory
con's avatar
con committed
323
    if (reader->contains("DESTDIR")) {
324
325
326
327
328
        //qDebug()<<"reader contains destdir:"<<reader->value("DESTDIR");
        m_workingDir = reader->value("DESTDIR");
        if (QDir::isRelativePath(m_workingDir)) {
            m_workingDir = baseDir + QLatin1Char('/') + m_workingDir;
            //qDebug()<<"was relative and expanded to"<<m_workingDir;
con's avatar
con committed
329
330
        }
    } else {
331
332
333
        //qDebug()<<"reader didn't contain DESTDIR, setting to "<<baseDir;
        m_workingDir = baseDir;

con's avatar
con committed
334
#if defined(Q_OS_WIN)
335
336
337
        QString qmakeBuildConfig = "release";
        if (projectBuildConfiguration & QtVersion::DebugBuild)
            qmakeBuildConfig = "debug";
con's avatar
con committed
338
        if (!reader->contains("DESTDIR"))
dt's avatar
dt committed
339
            m_workingDir += QLatin1Char('/') + qmakeBuildConfig;
con's avatar
con committed
340
341
342
343
#endif
    }

#if defined (Q_OS_MAC)
con's avatar
con committed
344
    if (reader->values("CONFIG").contains("app_bundle")) {
345
346
347
        QString qmakeBuildConfig = "release";
        if (projectBuildConfiguration & QtVersion::DebugBuild)
            qmakeBuildConfig = "debug";
dt's avatar
dt committed
348
        m_workingDir += QLatin1Char('/')
349
                   + qmakeBuildConfig
con's avatar
con committed
350
351
352
                   + QLatin1String(".app/Contents/MacOS");
    }
#endif
353
354
355
356

    m_workingDir = QDir::cleanPath(m_workingDir);
    m_executable = QDir::cleanPath(m_workingDir + QLatin1Char('/') + reader->value("TARGET"));
    //qDebug()<<"##### updateTarget sets:"<<m_workingDir<<m_executable;
con's avatar
con committed
357
358
359
360
361
362

#if defined (Q_OS_WIN)
    m_executable += QLatin1String(".exe");
#endif

    delete reader;
363

364
    m_cachedTargetInformationValid = true;
con's avatar
con committed
365

366
    emit effectiveTargetInformationChanged();
con's avatar
con committed
367
368
}

369
void Qt4RunConfiguration::invalidateCachedTargetInformation()
con's avatar
con committed
370
{
371
372
    m_cachedTargetInformationValid = false;
    emit effectiveTargetInformationChanged();
con's avatar
con committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
}

///
/// Qt4RunConfigurationFactory
/// This class is used to restore run settings (saved in .user files)
///

Qt4RunConfigurationFactory::Qt4RunConfigurationFactory()
{
}

Qt4RunConfigurationFactory::~Qt4RunConfigurationFactory()
{
}

// used to recreate the runConfigurations when restoring settings
bool Qt4RunConfigurationFactory::canCreate(const QString &type) const
{
    return type == "Qt4ProjectManager.Qt4RunConfiguration";
}

hjk's avatar
hjk committed
394
395
QSharedPointer<ProjectExplorer::RunConfiguration> Qt4RunConfigurationFactory::create
    (ProjectExplorer::Project *project, const QString &type)
con's avatar
con committed
396
397
{
    Qt4Project *p = qobject_cast<Qt4Project *>(project);
dt's avatar
dt committed
398
399
    Q_ASSERT(p);
    Q_ASSERT(type == "Qt4ProjectManager.Qt4RunConfiguration");
con's avatar
con committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
    // The right path is set in restoreSettings
    QSharedPointer<ProjectExplorer::RunConfiguration> rc(new Qt4RunConfiguration(p, QString::null));
    return rc;
}

QStringList Qt4RunConfigurationFactory::canCreate(ProjectExplorer::Project *pro) const
{
    Qt4Project *qt4project = qobject_cast<Qt4Project *>(pro);
    if (qt4project)
        return QStringList();
    else
        return QStringList();
}

QString Qt4RunConfigurationFactory::nameForType(const QString &type) const
{
    Q_UNUSED(type);
    return "Run Qt4 application";
}

///
/// Qt4RunConfigurationFactoryUser
/// This class is used to create new RunConfiguration from the runsettings page
///

Qt4RunConfigurationFactoryUser::Qt4RunConfigurationFactoryUser()
{
}

Qt4RunConfigurationFactoryUser::~Qt4RunConfigurationFactoryUser()
{
}

bool Qt4RunConfigurationFactoryUser::canCreate(const QString &type) const
{
    Q_UNUSED(type);
    return false;
}

QSharedPointer<ProjectExplorer::RunConfiguration> Qt4RunConfigurationFactoryUser::create(ProjectExplorer::Project *project, const QString &type)
{
    Qt4Project *p = qobject_cast<Qt4Project *>(project);
dt's avatar
dt committed
442
    Q_ASSERT(p);
con's avatar
con committed
443
444
445
446
447
448
449
450
451
452
453

    QString fileName = type.mid(QString("Qt4RunConfiguration.").size());
    return QSharedPointer<ProjectExplorer::RunConfiguration>(new Qt4RunConfiguration(p, fileName));
}

QStringList Qt4RunConfigurationFactoryUser::canCreate(ProjectExplorer::Project *pro) const
{
    Qt4Project *qt4project = qobject_cast<Qt4Project *>(pro);
    if (qt4project) {
        QStringList applicationProFiles;
        QList<Qt4ProFileNode *> list = qt4project->applicationProFiles();
hjk's avatar
hjk committed
454
        foreach (Qt4ProFileNode * node, list) {
con's avatar
con committed
455
456
457
458
459
460
461
462
463
464
465
466
467
            applicationProFiles.append("Qt4RunConfiguration." + node->path());
        }
        return applicationProFiles;
    } else {
        return QStringList();
    }
}

QString Qt4RunConfigurationFactoryUser::nameForType(const QString &type) const
{
    QString fileName = type.mid(QString("Qt4RunConfiguration.").size());
    return QFileInfo(fileName).baseName();
}