OSDN Git Service

ffba3630a06954679bcbf545889ee999d1d4d46d
[qt-creator-jp/qt-creator-jp.git] / src / plugins / glsleditor / glslfilewizard.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "glsleditorconstants.h"
35 #include "glslfilewizard.h"
36
37 #include <utils/filewizarddialog.h>
38 #include <utils/qtcassert.h>
39 #include <utils/filewizarddialog.h>
40
41 #include <QtCore/QFileInfo>
42 #include <QtCore/QTextStream>
43 #include <QtGui/QWizard>
44 #include <QtGui/QPushButton>
45
46 namespace {
47 class GLSLFileWizardDialog : public Utils::FileWizardDialog
48 {
49     Q_OBJECT
50 public:
51     GLSLFileWizardDialog(QWidget *parent = 0)
52         : Utils::FileWizardDialog(parent)
53     {
54     }
55 };
56 } // anonymous namespace
57
58 using namespace GLSLEditor;
59
60 GLSLFileWizard::GLSLFileWizard(const BaseFileWizardParameters &parameters,
61                                ShaderType shaderType, QObject *parent):
62     Core::BaseFileWizard(parameters, parent),
63     m_shaderType(shaderType)
64 {
65 }
66
67 Core::GeneratedFiles GLSLFileWizard::generateFiles(const QWizard *w,
68                                                  QString * /*errorMessage*/) const
69 {
70     const GLSLFileWizardDialog *wizardDialog = qobject_cast<const GLSLFileWizardDialog *>(w);
71     const QString path = wizardDialog->path();
72     const QString name = wizardDialog->fileName();
73
74     const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(m_shaderType));
75
76     Core::GeneratedFile file(fileName);
77     file.setContents(fileContents(fileName, m_shaderType));
78     file.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
79     return Core::GeneratedFiles() << file;
80 }
81
82 QString GLSLFileWizard::fileContents(const QString &, ShaderType shaderType) const
83 {
84     QString contents;
85     QTextStream str(&contents);
86
87     switch (shaderType) {
88     case GLSLFileWizard::VertexShaderES:
89         str << QLatin1String("attribute highp vec4 qt_Vertex;\n")
90             << QLatin1String("attribute highp vec4 qt_MultiTexCoord0;\n")
91             << QLatin1String("uniform highp mat4 qt_ModelViewProjectionMatrix;\n")
92             << QLatin1String("varying highp vec4 qt_TexCoord0;\n")
93             << QLatin1String("\n")
94             << QLatin1String("void main(void)\n")
95             << QLatin1String("{\n")
96             << QLatin1String("    gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex;\n")
97             << QLatin1String("    qt_TexCoord0 = qt_MultiTexCoord0;\n")
98             << QLatin1String("}\n");
99         break;
100     case GLSLFileWizard::FragmentShaderES:
101         str << QLatin1String("uniform sampler2D qt_Texture0;\n")
102             << QLatin1String("varying highp vec4 qt_TexCoord0;\n")
103             << QLatin1String("\n")
104             << QLatin1String("void main(void)\n")
105             << QLatin1String("{\n")
106             << QLatin1String("    gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st);\n")
107             << QLatin1String("}\n");
108         break;
109     case GLSLFileWizard::VertexShaderDesktop:
110         str << QLatin1String("attribute vec4 qt_Vertex;\n")
111             << QLatin1String("attribute vec4 qt_MultiTexCoord0;\n")
112             << QLatin1String("uniform mat4 qt_ModelViewProjectionMatrix;\n")
113             << QLatin1String("varying vec4 qt_TexCoord0;\n")
114             << QLatin1String("\n")
115             << QLatin1String("void main(void)\n")
116             << QLatin1String("{\n")
117             << QLatin1String("    gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex;\n")
118             << QLatin1String("    qt_TexCoord0 = qt_MultiTexCoord0;\n")
119             << QLatin1String("}\n");
120         break;
121     case GLSLFileWizard::FragmentShaderDesktop:
122         str << QLatin1String("uniform sampler2D qt_Texture0;\n")
123             << QLatin1String("varying vec4 qt_TexCoord0;\n")
124             << QLatin1String("\n")
125             << QLatin1String("void main(void)\n")
126             << QLatin1String("{\n")
127             << QLatin1String("    gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st);\n")
128             << QLatin1String("}\n");
129         break;
130     default: break;
131     }
132
133     return contents;
134 }
135
136 QWizard *GLSLFileWizard::createWizardDialog(QWidget *parent, const QString &defaultPath,
137                                           const WizardPageList &extensionPages) const
138 {
139     GLSLFileWizardDialog *wizardDialog = new GLSLFileWizardDialog(parent);
140     wizardDialog->setWindowTitle(tr("New %1").arg(displayName()));
141     setupWizard(wizardDialog);
142     wizardDialog->setPath(defaultPath);
143     foreach (QWizardPage *p, extensionPages)
144         BaseFileWizard::applyExtensionPageShortTitle(wizardDialog, wizardDialog->addPage(p));
145     return wizardDialog;
146 }
147
148 QString GLSLFileWizard::preferredSuffix(ShaderType shaderType) const
149 {
150     switch (shaderType) {
151     case GLSLFileWizard::VertexShaderES:
152         return QLatin1String("vsh");
153     case GLSLFileWizard::FragmentShaderES:
154         return QLatin1String("fsh");
155     case GLSLFileWizard::VertexShaderDesktop:
156         return QLatin1String("vert");
157     case GLSLFileWizard::FragmentShaderDesktop:
158         return QLatin1String("frag");
159     default:
160         return QLatin1String("glsl");
161     }
162 }
163
164 #include "glslfilewizard.moc"