qmlproject.cpp 12.3 KB
Newer Older
1
2
3
4
5
6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** 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.
27
28
29
30
31
**
**************************************************************************/

#include "qmlproject.h"
#include "qmlprojectconstants.h"
32
#include "qmlmakestep.h"
33
34
35
36
37
38
39

#include <projectexplorer/toolchain.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
40
#include <coreplugin/editormanager/editormanager.h>
41

42
43
#include <duieditor/duimodelmanagerinterface.h>

44
#include <utils/synchronousprocess.h>
45
#include <utils/pathchooser.h>
46

47
48
49
50
51
52
53
54
55
#include <QtCore/QtDebug>
#include <QtCore/QDir>
#include <QtCore/QSettings>
#include <QtCore/QProcess>
#include <QtCore/QCoreApplication>

#include <QtGui/QFormLayout>
#include <QtGui/QMainWindow>
#include <QtGui/QComboBox>
56
#include <QtGui/QMessageBox>
57
58
59
60
61
62
63
64
65
66

using namespace QmlProjectManager;
using namespace QmlProjectManager::Internal;

////////////////////////////////////////////////////////////////////////////////////
// QmlProject
////////////////////////////////////////////////////////////////////////////////////

QmlProject::QmlProject(Manager *manager, const QString &fileName)
    : m_manager(manager),
67
68
      m_fileName(fileName),
      m_modelManager(ExtensionSystem::PluginManager::instance()->getObject<DuiEditor::DuiModelManagerInterface>())
69
70
{
    QFileInfo fileInfo(m_fileName);
71
    m_projectName = fileInfo.completeBaseName();
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    m_file = new QmlProjectFile(this, fileName);
    m_rootNode = new QmlProjectNode(this, m_file);

    m_manager->registerProject(this);
}

QmlProject::~QmlProject()
{
    m_manager->unregisterProject(this);

    delete m_rootNode;
}

86
87
88
89
90
QDir QmlProject::projectDir() const
{
    return QFileInfo(file()->fileName()).dir();
}

91
QString QmlProject::filesFileName() const
92
{ return m_fileName; }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

static QStringList readLines(const QString &absoluteFileName)
{
    QStringList lines;

    QFile file(absoluteFileName);
    if (file.open(QFile::ReadOnly)) {
        QTextStream stream(&file);

        forever {
            QString line = stream.readLine();
            if (line.isNull())
                break;

            line = line.trimmed();
            if (line.isEmpty())
                continue;

            lines.append(line);
        }
    }

    return lines;
}


void QmlProject::parseProject(RefreshOptions options)
{
    if (options & Files) {
        m_files = convertToAbsoluteFiles(readLines(filesFileName()));
        m_files.removeDuplicates();
124
        m_modelManager->updateSourceFiles(m_files);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    }

    if (options & Configuration) {
        // update configuration
    }

    if (options & Files)
        emit fileListChanged();
}

void QmlProject::refresh(RefreshOptions options)
{
    QSet<QString> oldFileList;
    if (!(options & Configuration))
        oldFileList = m_files.toSet();

    parseProject(options);

    if (options & Files)
        m_rootNode->refresh();
}

QStringList QmlProject::convertToAbsoluteFiles(const QStringList &paths) const
{
    const QDir projectDir(QFileInfo(m_fileName).dir());
    QStringList absolutePaths;
    foreach (const QString &file, paths) {
        QFileInfo fileInfo(projectDir, file);
        absolutePaths.append(fileInfo.absoluteFilePath());
    }
    absolutePaths.removeDuplicates();
    return absolutePaths;
}

QStringList QmlProject::files() const
{ return m_files; }

QString QmlProject::buildParser(const QString &) const
{
    return QString();
}

QString QmlProject::name() const
{
    return m_projectName;
}

Core::IFile *QmlProject::file() const
{
    return m_file;
}

Roberto Raggi's avatar
Roberto Raggi committed
177
Manager *QmlProject::projectManager() const
178
179
180
181
182
183
184
185
186
187
188
189
190
191
{
    return m_manager;
}

QList<ProjectExplorer::Project *> QmlProject::dependsOn()
{
    return QList<Project *>();
}

bool QmlProject::isApplication() const
{
    return true;
}

192
193
194
195
196
bool QmlProject::hasBuildSettings() const
{
    return false;
}

197
198
199
200
201
ProjectExplorer::Environment QmlProject::environment(const QString &) const
{
    return ProjectExplorer::Environment::systemEnvironment();
}

Roberto Raggi's avatar
Roberto Raggi committed
202
QString QmlProject::buildDirectory(const QString &) const
203
{
Roberto Raggi's avatar
Roberto Raggi committed
204
    return QString();
205
206
}

dt's avatar
dt committed
207
ProjectExplorer::BuildConfigWidget *QmlProject::createConfigWidget()
208
{
209
    return 0;
210
211
}

dt's avatar
dt committed
212
QList<ProjectExplorer::BuildConfigWidget*> QmlProject::subConfigWidgets()
213
{
dt's avatar
dt committed
214
    return QList<ProjectExplorer::BuildConfigWidget*>();
215
216
}

217
218
219
void QmlProject::newBuildConfiguration(const QString &)
{
}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

QmlProjectNode *QmlProject::rootProjectNode() const
{
    return m_rootNode;
}

QStringList QmlProject::files(FilesMode) const
{
    return m_files;
}

QStringList QmlProject::targets() const
{
    QStringList targets;
    return targets;
}

QmlMakeStep *QmlProject::makeStep() const
{
239
240
241
242
    foreach (ProjectExplorer::BuildStep *bs, buildSteps()) {
        if (QmlMakeStep *ms = qobject_cast<QmlMakeStep *>(bs))
            return ms;
    }
243
244
245
    return 0;
}

dt's avatar
dt committed
246
bool QmlProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader)
247
248
249
{
    Project::restoreSettingsImpl(reader);

250
251
252
253
254
    if (runConfigurations().isEmpty()) {
        QSharedPointer<QmlRunConfiguration> runConf(new QmlRunConfiguration(this));
        addRunConfiguration(runConf);
    }

255
256
257
258
259
    if (buildSteps().isEmpty()) {
        QmlMakeStep *makeStep = new QmlMakeStep(this);
        insertBuildStep(0, makeStep);
    }

260
    refresh(Everything);
dt's avatar
dt committed
261
    return true;
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
}

void QmlProject::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
    Project::saveSettingsImpl(writer);
}

////////////////////////////////////////////////////////////////////////////////////
// QmlProjectFile
////////////////////////////////////////////////////////////////////////////////////

QmlProjectFile::QmlProjectFile(QmlProject *parent, QString fileName)
    : Core::IFile(parent),
      m_project(parent),
      m_fileName(fileName)
{ }

QmlProjectFile::~QmlProjectFile()
{ }

bool QmlProjectFile::save(const QString &)
{
    return false;
}

QString QmlProjectFile::fileName() const
{
    return m_fileName;
}

QString QmlProjectFile::defaultPath() const
{
    return QString();
}

QString QmlProjectFile::suggestedFileName() const
{
    return QString();
}

QString QmlProjectFile::mimeType() const
{
    return Constants::QMLMIMETYPE;
}

bool QmlProjectFile::isModified() const
{
    return false;
}

bool QmlProjectFile::isReadOnly() const
{
    return true;
}

bool QmlProjectFile::isSaveAsAllowed() const
{
    return false;
}

void QmlProjectFile::modified(ReloadBehavior *)
{
}
325

326
QmlRunConfiguration::QmlRunConfiguration(QmlProject *pro)
327
    : ProjectExplorer::ApplicationRunConfiguration(pro),
328
329
      m_project(pro),
      m_type(Constants::QMLRUNCONFIGURATION)
330
{
331
    setName(tr("QML Viewer"));
332
333

    m_qmlViewer = Core::Utils::SynchronousProcess::locateBinary(QLatin1String("qmlviewer"));
334
335
}

336
QmlRunConfiguration::~QmlRunConfiguration()
337
338
339
{
}

340
QString QmlRunConfiguration::type() const
341
{
342
    return m_type;
343
344
}

345
QString QmlRunConfiguration::executable() const
346
{
347
    // No need to verify if the QML Viewer exists. The console will tell us anyway when we try to launch it.
348
    return m_qmlViewer;
349
350
}

351
QmlRunConfiguration::RunMode QmlRunConfiguration::runMode() const
352
353
354
355
{
    return Gui;
}

356
QString QmlRunConfiguration::workingDirectory() const
357
358
{
    QFileInfo projectFile(m_project->file()->fileName());
Roberto Raggi's avatar
Roberto Raggi committed
359
    return projectFile.absolutePath();
360
361
}

362
QStringList QmlRunConfiguration::commandLineArguments() const
363
364
365
366
367
368
369
370
371
372
{
    QStringList args;

    const QString s = mainScript();
    if (! s.isEmpty())
        args.append(s);

    return args;
}

373
ProjectExplorer::Environment QmlRunConfiguration::environment() const
374
{
375
    return ProjectExplorer::Environment::systemEnvironment();
376
377
}

378
QString QmlRunConfiguration::dumperLibrary() const
379
380
381
382
{
    return QString();
}

383
384
385
386
387
QStringList QmlRunConfiguration::dumperLibraryLocations() const
{
    return QStringList();
}

388
QWidget *QmlRunConfiguration::configurationWidget()
389
{
390
391
392
    QWidget *config = new QWidget;
    QFormLayout *form = new QFormLayout(config);

393
    QComboBox *combo = new QComboBox;
394
395
396
397

    QDir projectDir = m_project->projectDir();
    QStringList files;

398
399
400
401
    files.append(tr("<Current File>"));

    int currentIndex = -1;

402
403
404
405
406
    foreach (const QString &fn, m_project->files()) {
        QFileInfo fileInfo(fn);
        if (fileInfo.suffix() != QLatin1String("qml"))
            continue;

407
408
409
410
411
        QString fileName = projectDir.relativeFilePath(fn);
        if (fileName == m_scriptFile)
            currentIndex = files.size();

        files.append(fileName);
412
413
414
    }

    combo->addItems(files);
415
416
417
418
    if (currentIndex != -1)
        combo->setCurrentIndex(currentIndex);

    connect(combo, SIGNAL(activated(QString)), this, SLOT(setMainScript(QString)));
419

420
421
422
    Core::Utils::PathChooser *qmlViewer = new Core::Utils::PathChooser;
    qmlViewer->setExpectedKind(Core::Utils::PathChooser::Command);
    qmlViewer->setPath(executable());
423
    connect(qmlViewer, SIGNAL(changed(QString)), this, SLOT(onQmlViewerChanged()));
424
425

    form->addRow(tr("QML Viewer"), qmlViewer);
Michael Brasser's avatar
Michael Brasser committed
426
    form->addRow(tr("Main QML File:"), combo);
427
428

    return config;
429
430
}

431
QString QmlRunConfiguration::mainScript() const
432
433
434
435
436
437
438
439
{
    if (m_scriptFile.isEmpty() || m_scriptFile == tr("<Current File>")) {
        Core::EditorManager *editorManager = Core::ICore::instance()->editorManager();
        if (Core::IEditor *editor = editorManager->currentEditor()) {
            return editor->file()->fileName();
        }
    }

440
    return m_project->projectDir().absoluteFilePath(m_scriptFile);
441
442
}

443
void QmlRunConfiguration::setMainScript(const QString &scriptFile)
444
445
446
447
{
    m_scriptFile = scriptFile;
}

448
449
450
451
452
453
454
455
void QmlRunConfiguration::onQmlViewerChanged()
{
    if (Core::Utils::PathChooser *chooser = qobject_cast<Core::Utils::PathChooser *>(sender())) {
        m_qmlViewer = chooser->path();
    }
}

void QmlRunConfiguration::save(ProjectExplorer::PersistentSettingsWriter &writer) const
456
457
{
    ProjectExplorer::ApplicationRunConfiguration::save(writer);
458
459
460

    writer.saveValue(QLatin1String("qmlviewer"), m_qmlViewer);
    writer.saveValue(QLatin1String("mainscript"), m_scriptFile);
461
462
}

463
void QmlRunConfiguration::restore(const ProjectExplorer::PersistentSettingsReader &reader)
464
465
{
    ProjectExplorer::ApplicationRunConfiguration::restore(reader);
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

    m_qmlViewer = reader.restoreValue(QLatin1String("qmlviewer")).toString();
    m_scriptFile = reader.restoreValue(QLatin1String("mainscript")).toString();

    if (m_qmlViewer.isEmpty())
        m_qmlViewer = Core::Utils::SynchronousProcess::locateBinary(QLatin1String("qmlviewer"));

    if (m_scriptFile.isEmpty())
        m_scriptFile = tr("<Current File>");
}

QmlRunConfigurationFactory::QmlRunConfigurationFactory()
    : m_type(Constants::QMLRUNCONFIGURATION)
{
}

QmlRunConfigurationFactory::~QmlRunConfigurationFactory()
{
}

con's avatar
con committed
486
bool QmlRunConfigurationFactory::canRestore(const QString &type) const
487
488
489
490
491
492
493
{
    if (type.startsWith(m_type))
        return true;

    return false;
}

con's avatar
con committed
494
QStringList QmlRunConfigurationFactory::availableCreationTypes(ProjectExplorer::Project *) const
495
496
{
    return QStringList();
497
}
498

con's avatar
con committed
499
QString QmlRunConfigurationFactory::displayNameForType(const QString &type) const
500
501
502
503
504
505
506
507
508
509
510
511
512
{
    return type;
}

QSharedPointer<ProjectExplorer::RunConfiguration> QmlRunConfigurationFactory::create(ProjectExplorer::Project *project,
                                                                                     const QString &)
{
    QmlProject *pro = qobject_cast<QmlProject *>(project);
    QSharedPointer<ProjectExplorer::RunConfiguration> rc(new QmlRunConfiguration(pro));
    return rc;
}