glslfilewizard.cpp 5.92 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

#include "glslfilewizard.h"
31
#include "glsleditorconstants.h"
32

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

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

38
39
40
41
#include <QFileInfo>
#include <QTextStream>
#include <QWizard>
#include <QPushButton>
42
43
44

using namespace GLSLEditor;

hjk's avatar
hjk committed
45
46
GLSLFileWizard::GLSLFileWizard(ShaderType shaderType)
    : m_shaderType(shaderType)
47
{
Tobias Hunger's avatar
Tobias Hunger committed
48
    setFlags(Core::IWizardFactory::PlatformIndependent);
49
50
}

51
52
53
Core::GeneratedFiles GLSLFileWizard::generateFiles(const QWizard *w,
                                                 QString * /*errorMessage*/) const
{
Tobias Hunger's avatar
Tobias Hunger committed
54
    const Core::BaseFileWizard *wizard = qobject_cast<const Core::BaseFileWizard *>(w);
55
56
57
58
59
    Utils::FileWizardPage *page = wizard->find<Utils::FileWizardPage>();
    QTC_ASSERT(page, return Core::GeneratedFiles());

    const QString path = page->path();
    const QString name = page->fileName();
60

61
    const QString fileName = Core::BaseFileWizardFactory::buildFileName(path, name, preferredSuffix(m_shaderType));
62
63
64
65
66
67
68
69
70
71
72
73
74

    Core::GeneratedFile file(fileName);
    file.setContents(fileContents(fileName, m_shaderType));
    file.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
    return Core::GeneratedFiles() << file;
}

QString GLSLFileWizard::fileContents(const QString &, ShaderType shaderType) const
{
    QString contents;
    QTextStream str(&contents);

    switch (shaderType) {
75
    case GLSLFileWizard::VertexShaderES:
76
77
78
79
        str << QLatin1String("attribute highp vec4 qt_Vertex;\n")
            << QLatin1String("attribute highp vec4 qt_MultiTexCoord0;\n")
            << QLatin1String("uniform highp mat4 qt_ModelViewProjectionMatrix;\n")
            << QLatin1String("varying highp vec4 qt_TexCoord0;\n")
80
81
82
            << QLatin1String("\n")
            << QLatin1String("void main(void)\n")
            << QLatin1String("{\n")
83
84
            << QLatin1String("    gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex;\n")
            << QLatin1String("    qt_TexCoord0 = qt_MultiTexCoord0;\n")
85
86
            << QLatin1String("}\n");
        break;
87
    case GLSLFileWizard::FragmentShaderES:
88
89
        str << QLatin1String("uniform sampler2D qt_Texture0;\n")
            << QLatin1String("varying highp vec4 qt_TexCoord0;\n")
90
91
92
            << QLatin1String("\n")
            << QLatin1String("void main(void)\n")
            << QLatin1String("{\n")
93
            << QLatin1String("    gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st);\n")
94
95
            << QLatin1String("}\n");
        break;
96
    case GLSLFileWizard::VertexShaderDesktop:
97
98
99
100
        str << QLatin1String("attribute vec4 qt_Vertex;\n")
            << QLatin1String("attribute vec4 qt_MultiTexCoord0;\n")
            << QLatin1String("uniform mat4 qt_ModelViewProjectionMatrix;\n")
            << QLatin1String("varying vec4 qt_TexCoord0;\n")
101
102
103
            << QLatin1String("\n")
            << QLatin1String("void main(void)\n")
            << QLatin1String("{\n")
104
105
            << QLatin1String("    gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex;\n")
            << QLatin1String("    qt_TexCoord0 = qt_MultiTexCoord0;\n")
106
107
108
            << QLatin1String("}\n");
        break;
    case GLSLFileWizard::FragmentShaderDesktop:
109
110
        str << QLatin1String("uniform sampler2D qt_Texture0;\n")
            << QLatin1String("varying vec4 qt_TexCoord0;\n")
111
112
113
            << QLatin1String("\n")
            << QLatin1String("void main(void)\n")
            << QLatin1String("{\n")
114
            << QLatin1String("    gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st);\n")
115
116
            << QLatin1String("}\n");
        break;
117
118
119
120
121
122
    default: break;
    }

    return contents;
}

Tobias Hunger's avatar
Tobias Hunger committed
123
Core::BaseFileWizard *GLSLFileWizard::create(QWidget *parent, const Core::WizardDialogParameters &parameters) const
124
{
Tobias Hunger's avatar
Tobias Hunger committed
125
126
    Core::BaseFileWizard *wizard = new Core::BaseFileWizard(parent);
    wizard->setWindowTitle(tr("New %1").arg(displayName()));
127
128
129
130
    Utils::FileWizardPage *page = new Utils::FileWizardPage;
    page->setPath(parameters.defaultPath());
    wizard->addPage(page);

131
    foreach (QWizardPage *p, parameters.extensionPages())
132
        wizard->addPage(p);
Tobias Hunger's avatar
Tobias Hunger committed
133
    return wizard;
134
135
136
137
138
}

QString GLSLFileWizard::preferredSuffix(ShaderType shaderType) const
{
    switch (shaderType) {
139
140
141
142
143
    case GLSLFileWizard::VertexShaderES:
        return QLatin1String("vsh");
    case GLSLFileWizard::FragmentShaderES:
        return QLatin1String("fsh");
    case GLSLFileWizard::VertexShaderDesktop:
144
        return QLatin1String("vert");
145
    case GLSLFileWizard::FragmentShaderDesktop:
146
147
148
149
150
        return QLatin1String("frag");
    default:
        return QLatin1String("glsl");
    }
}