jsfilewizard.cpp 4.68 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>

34 35 36
#include <utils/filewizarddialog.h>
#include <utils/qtcassert.h>

37 38 39 40 41 42
#include <QFileInfo>
#include <QTextStream>
#include <QWizard>
#include <QPushButton>
#include <QBoxLayout>
#include <QCheckBox>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

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

        QVBoxLayout *layout = new QVBoxLayout;
        m_statelessLibrary = new QCheckBox(tr("Stateless library"));
        m_statelessLibrary->setToolTip(
                    tr("Usually each QML component instance has a unique copy of\n"
                       "imported JavaScript libraries. Indicating that a library is\n"
                       "stateless means that a single instance will be shared among\n"
                       "all components. Stateless libraries will not be able to access\n"
                       "QML component instance objects and properties directly."));
        layout->addWidget(m_statelessLibrary);
        setLayout(layout);
    }

    bool statelessLibrary() const
    {
        return m_statelessLibrary->isChecked();
    }

private:
    QCheckBox *m_statelessLibrary;
};

class JsFileWizardDialog : public Utils::FileWizardDialog
{
    Q_OBJECT
public:
    JsFileWizardDialog(QWidget *parent = 0)
        : Utils::FileWizardDialog(parent)
        , m_optionsPage(new JsFileOptionsPage)
    {
        addPage(m_optionsPage);
    }

    JsFileOptionsPage *m_optionsPage;
};
} // 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 100 101
{
    const JsFileWizardDialog *wizardDialog = qobject_cast<const JsFileWizardDialog *>(w);
    const QString path = wizardDialog->path();
    const QString name = wizardDialog->fileName();

102
    const QString mimeType = QLatin1String(QmlJSTools::Constants::JS_MIMETYPE);
103 104 105 106 107 108 109 110 111 112 113 114 115
    const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(mimeType));

    Core::GeneratedFile file(fileName);
    file.setContents(fileContents(fileName, wizardDialog->m_optionsPage->statelessLibrary()));
    file.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
    return Core::GeneratedFiles() << file;
}

QString JsFileWizard::fileContents(const QString &, bool statelessLibrary) const
{
    QString contents;
    QTextStream str(&contents);

116
    if (statelessLibrary)
117 118 119 120 121 122 123 124
        str << QLatin1String(".pragma library\n\n");
    str << QLatin1String("function func() {\n")
        << QLatin1String("\n")
        << QLatin1String("}\n");

    return contents;
}

125 126
QWizard *JsFileWizard::createWizardDialog(QWidget *parent,
                                          const Core::WizardDialogParameters &wizardDialogParameters) const
127 128 129
{
    JsFileWizardDialog *wizardDialog = new JsFileWizardDialog(parent);
    wizardDialog->setWindowTitle(tr("New %1").arg(displayName()));
130 131
    wizardDialog->setPath(wizardDialogParameters.defaultPath());
    foreach (QWizardPage *p, wizardDialogParameters.extensionPages())
132 133 134 135 136
        BaseFileWizard::applyExtensionPageShortTitle(wizardDialog, wizardDialog->addPage(p));
    return wizardDialog;
}

#include "jsfilewizard.moc"