jsfilewizard.cpp 4.76 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
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
****************************************************************************/
29
30
31

#include "jsfilewizard.h"

32
33
#include <qmljstools/qmljstoolsconstants.h>

Tobias Hunger's avatar
Tobias Hunger committed
34
35
#include <coreplugin/basefilewizard.h>

36
#include <utils/filewizardpage.h>
37
38
#include <utils/qtcassert.h>

39
40
41
42
43
44
#include <QFileInfo>
#include <QTextStream>
#include <QWizard>
#include <QPushButton>
#include <QBoxLayout>
#include <QCheckBox>
45
46
47
48
49
50
51
52
53
54
55

namespace {
class JsFileOptionsPage : public QWizardPage
{
    Q_OBJECT
public:
    JsFileOptionsPage()
    {
        setTitle(tr("Options"));

        QVBoxLayout *layout = new QVBoxLayout;
56
57
        m_library = new QCheckBox(tr("JavaScript library"));
        m_library->setToolTip(
58
                    tr("Usually each QML component instance has a unique copy of\n"
59
60
61
                       "imported JavaScript libraries. Indicating that a JavaScript file is\n"
                       "a library means that a single instance will be shared among\n"
                       "all components. JavaScript libraries will not be able to access\n"
62
                       "QML component instance objects and properties directly."));
63
        layout->addWidget(m_library);
64
65
66
        setLayout(layout);
    }

67
    bool isLibrary() const
68
    {
69
        return m_library->isChecked();
70
71
72
    }

private:
73
    QCheckBox *m_library;
74
75
};

Tobias Hunger's avatar
Tobias Hunger committed
76
class JsFileWizardDialog : public Core::BaseFileWizard
77
78
79
{
    Q_OBJECT
public:
Tobias Hunger's avatar
Tobias Hunger committed
80
    JsFileWizardDialog(QWidget *parent = 0) :
81
        Core::BaseFileWizard(parent)
82
    {
83
84
        addPage(new Utils::FileWizardPage);
        addPage(new JsFileOptionsPage);
85
86
87
88
89
90
    }
};
} // anonymous namespace

using namespace QmlJSEditor;

hjk's avatar
hjk committed
91
JsFileWizard::JsFileWizard()
92
93
94
95
{
}

Core::GeneratedFiles JsFileWizard::generateFiles(const QWizard *w,
Tobias Hunger's avatar
Tobias Hunger committed
96
                                                 QString * /*errorMessage*/) const
97
{
98
99
    const Core::BaseFileWizard *wizard = qobject_cast<const Core::BaseFileWizard *>(w);
    Utils::FileWizardPage *filePage = wizard->find<Utils::FileWizardPage>();
100
    QTC_ASSERT(filePage, return Core::GeneratedFiles());
101
    JsFileOptionsPage *optionPage = wizard->find<JsFileOptionsPage>();
102
    QTC_ASSERT(optionPage, return Core::GeneratedFiles());
103
104
105
106


    const QString path = filePage->path();
    const QString name = filePage->fileName();
107

108
    const QString mimeType = QLatin1String(QmlJSTools::Constants::JS_MIMETYPE);
109
    const QString fileName = Core::BaseFileWizardFactory::buildFileName(path, name, preferredSuffix(mimeType));
110
111

    Core::GeneratedFile file(fileName);
112
    file.setContents(fileContents(fileName, optionPage->isLibrary()));
113
114
115
116
    file.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
    return Core::GeneratedFiles() << file;
}

117
QString JsFileWizard::fileContents(const QString &, bool isLibrary) const
118
119
120
121
{
    QString contents;
    QTextStream str(&contents);

122
    if (isLibrary)
123
124
125
126
127
128
129
130
        str << QLatin1String(".pragma library\n\n");
    str << QLatin1String("function func() {\n")
        << QLatin1String("\n")
        << QLatin1String("}\n");

    return contents;
}

Tobias Hunger's avatar
Tobias Hunger committed
131
Core::BaseFileWizard *JsFileWizard::create(QWidget *parent, const Core::WizardDialogParameters &parameters) const
132
{
133
134
135
136
137
138
    JsFileWizardDialog *wizard = new JsFileWizardDialog(parent);
    wizard->setWindowTitle(tr("New %1").arg(displayName()));

    Utils::FileWizardPage *page = wizard->find<Utils::FileWizardPage>();
    page->setPath(parameters.defaultPath());

139
    foreach (QWizardPage *p, parameters.extensionPages())
140
141
        wizard->addPage(p);
    return wizard;
142
143
144
}

#include "jsfilewizard.moc"