summaryrefslogtreecommitdiff
path: root/src/plugins/pythoneditor/wizard/pythonsourcegenerator.cpp
blob: 6dcfaaf59a7afd33fbcff4dfd7132649009d25b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
91
92
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
124
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "pythonsourcegenerator.h"
#include <QSet>

namespace PythonEditor {
namespace Internal {

static const char BASH_RUN_HEADER[] = "#!/usr/bin/env python\n";
static const char ENCODING_HEADER[] = "# -*- coding: utf-8 -*-\n";

SourceGenerator::SourceGenerator()
    : m_pythonQtBinding(PySide)
    , m_pythonQtVersion(Qt4)
{
}

SourceGenerator::~SourceGenerator()
{
}

void SourceGenerator::setPythonQtBinding(QtBinding binding)
{
    m_pythonQtBinding = binding;
}

void SourceGenerator::setPythonQtVersion(SourceGenerator::QtVersion version)
{
    m_pythonQtVersion = version;
}

QString SourceGenerator::generateClass(const QString &className,
                                       const QString &baseClass,
                                       Utils::NewClassWidget::ClassType classType) const
{
    QSet<QString> modules;
    bool hasUserBaseClass = !baseClass.isEmpty();
    // heuristic
    bool wasInheritedFromQt = hasUserBaseClass && (baseClass.at(0) == QLatin1Char('Q'));

    QString actualBase = baseClass;

    switch (classType) {
    case Utils::NewClassWidget::NoClassType:
        break;
    case Utils::NewClassWidget::SharedDataClass:
    case Utils::NewClassWidget::ClassInheritsQQuickItem:
        break;
    case Utils::NewClassWidget::ClassInheritsQObject:
        wasInheritedFromQt = true;
        modules.insert(QLatin1String("QtCore"));
        if (!hasUserBaseClass)
            actualBase = QLatin1String("QtCore.QObject");
        break;

    case Utils::NewClassWidget::ClassInheritsQWidget:
        wasInheritedFromQt = true;
        modules.insert(QLatin1String("QtCore"));
        modules.insert(moduleForQWidget());
        if (!hasUserBaseClass)
            actualBase = moduleForQWidget() + QLatin1String(".QWidget");
        break;

    case Utils::NewClassWidget::ClassInheritsQDeclarativeItem:
        wasInheritedFromQt = true;
        modules.insert(QLatin1String("QtCore"));
        modules.insert(QLatin1String("QtDeclarative"));
        if (!hasUserBaseClass)
            actualBase = QLatin1String("QtDeclarative.QDeclarativeItem");
        break;
    }

    QString nonQtModule; // empty
    if (hasUserBaseClass) {
        int dotIndex = baseClass.lastIndexOf(QLatin1Char('.'));
        if (dotIndex != -1) {
            if (wasInheritedFromQt)
                modules.insert(baseClass.left(dotIndex));
            else
                nonQtModule = baseClass.left(dotIndex);
        }
    }

    QString ret;
    ret.reserve(1024);
    ret += QLatin1String(ENCODING_HEADER);
    ret += QLatin1Char('\n');

    if (!modules.isEmpty()) {
        ret += qtModulesImport(modules);
        ret += QLatin1Char('\n');
    }

    if (!nonQtModule.isEmpty())
        ret += QString::fromLatin1("import %1\n\n").arg(nonQtModule);

    if (actualBase.isEmpty())
        ret += QString::fromLatin1("class %1:\n").arg(className);
    else
        ret += QString::fromLatin1("class %1(%2):\n").arg(className).arg(actualBase);

    ret += QLatin1String("    def __init__(self):\n");
    if (wasInheritedFromQt)
        ret += QString::fromLatin1("        %1.__init__(self)\n").arg(actualBase);
    ret += QLatin1String("        pass\n");

    return ret;
}

/**
* @brief Generates main file of PyQt/PySide application
*
* Class MainWindow should be defined in 'mainwindow' module.
* @param windowTitle Title for created window instance
*/
QString SourceGenerator::generateQtMain(const QString &windowTitle) const
{
    QSet<QString> qtModules;
    qtModules.insert(QLatin1String("QtCore"));
    qtModules.insert(moduleForQWidget());

    QString ret;
    ret.reserve(1024);
    ret += QLatin1String(BASH_RUN_HEADER);
    ret += QLatin1String(ENCODING_HEADER);
    ret += QLatin1Char('\n');
    ret += QLatin1String("import sys\n");
    ret += qtModulesImport(qtModules);
    ret += QLatin1String("from mainwindow import MainWindow\n");
    ret += QLatin1Char('\n');

    ret += QString::fromLatin1(
                "if __name__ == \'__main__\':\n"
                "    app = %1.QApplication(sys.argv)\n"
                "    win = MainWindow()\n"
                "    win.setWindowTitle(u\'%2\')\n"
                "    win.show()\n"
                "    app.exec_()\n"
                ).arg(moduleForQWidget()).arg(windowTitle);

    return ret;
}

QString SourceGenerator::qtModulesImport(const QSet<QString> &modules) const
{
    QString slotsImport;
    if (modules.contains(QLatin1String("QtCore")))
        slotsImport = QLatin1String("    from PyQt4.QtCore import pyqtSlot as Slot\n");

    QLatin1String defaultBinding("PySide");
    QLatin1String fallbackBinding("PyQt4");
    if (m_pythonQtBinding == PyQt)
        qSwap(defaultBinding, fallbackBinding);

    QString ret;
    ret.reserve(256);
    ret += QLatin1String("try:\n");
    if (m_pythonQtBinding == PyQt)
        ret += slotsImport;
    foreach (const QString &name, modules)
        ret += QString::fromLatin1("    from %1 import %2\n")
                .arg(defaultBinding)
                .arg(name);

    ret += QLatin1String("except:\n");
    if (m_pythonQtBinding != PyQt)
        ret += slotsImport;
    foreach (const QString &name, modules)
        ret += QString::fromLatin1("    from %1 import %2\n")
                .arg(fallbackBinding)
                .arg(name);

    return ret;
}

QString SourceGenerator::moduleForQWidget() const
{
    if (m_pythonQtVersion == Qt4)
        return QLatin1String("QtGui");
    else
        return QLatin1String("QtWidgets");
}

} // namespace Internal
} // namespace PythonEditor