| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the tools applications of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef LANGUAGE_H |
| 30 | #define LANGUAGE_H |
| 31 | |
| 32 | #include <QtCore/qstring.h> |
| 33 | #include <QtCore/qstringview.h> |
| 34 | #include <QtCore/qstring.h> |
| 35 | |
| 36 | QT_FORWARD_DECLARE_CLASS(QTextStream) |
| 37 | |
| 38 | enum class Language { Cpp, Python }; |
| 39 | |
| 40 | enum class ConnectionSyntax { StringBased, MemberFunctionPtr }; |
| 41 | |
| 42 | namespace language { |
| 43 | |
| 44 | Language language(); |
| 45 | void setLanguage(Language); |
| 46 | |
| 47 | ConnectionSyntax connectionSyntax(); |
| 48 | void setConnectionSyntax(ConnectionSyntax cs); |
| 49 | |
| 50 | extern QString derefPointer; |
| 51 | extern QString nullPtr; |
| 52 | extern QString operatorNew; |
| 53 | extern QString qtQualifier; |
| 54 | extern QString qualifier; |
| 55 | extern QString self; |
| 56 | extern QString eol; |
| 57 | extern QString emptyString; |
| 58 | |
| 59 | extern QString cppQualifier; |
| 60 | extern QString cppTrue; |
| 61 | extern QString cppFalse; |
| 62 | |
| 63 | // Base class for streamable objects with one QStringView parameter |
| 64 | class StringViewStreamable |
| 65 | { |
| 66 | public: |
| 67 | StringViewStreamable(QStringView parameter) : m_parameter(parameter) {} |
| 68 | |
| 69 | QStringView parameter() const { return m_parameter; } |
| 70 | |
| 71 | private: |
| 72 | QStringView m_parameter; |
| 73 | }; |
| 74 | |
| 75 | class qtConfig : public StringViewStreamable |
| 76 | { |
| 77 | public: |
| 78 | qtConfig(QStringView name) : StringViewStreamable(name) {} |
| 79 | }; |
| 80 | |
| 81 | QTextStream &operator<<(QTextStream &str, const qtConfig &c); |
| 82 | |
| 83 | class openQtConfig : public StringViewStreamable |
| 84 | { |
| 85 | public: |
| 86 | openQtConfig(QStringView name) : StringViewStreamable(name) {} |
| 87 | }; |
| 88 | |
| 89 | QTextStream &operator<<(QTextStream &str, const openQtConfig &c); |
| 90 | |
| 91 | class closeQtConfig : public StringViewStreamable |
| 92 | { |
| 93 | public: |
| 94 | closeQtConfig(QStringView name) : StringViewStreamable(name) {} |
| 95 | }; |
| 96 | |
| 97 | QTextStream &operator<<(QTextStream &, const closeQtConfig &c); |
| 98 | |
| 99 | QString fixClassName(QString className); |
| 100 | |
| 101 | const char *toolbarArea(int v); |
| 102 | const char *sizePolicy(int v); |
| 103 | const char *dockWidgetArea(int v); |
| 104 | const char *paletteColorRole(int v); |
| 105 | |
| 106 | enum class Encoding { Utf8, Unicode }; |
| 107 | |
| 108 | void _formatString(QTextStream &str, const QString &value, const QString &indent, |
| 109 | bool qString); |
| 110 | |
| 111 | template <bool AsQString> |
| 112 | class _string |
| 113 | { |
| 114 | public: |
| 115 | explicit _string(const QString &value, const QString &indent = QString()) |
| 116 | : m_value(value), m_indent(indent) {} |
| 117 | |
| 118 | void format(QTextStream &str) const |
| 119 | { _formatString(str, m_value, m_indent, AsQString); } |
| 120 | |
| 121 | private: |
| 122 | const QString &m_value; |
| 123 | const QString &m_indent; |
| 124 | }; |
| 125 | |
| 126 | template <bool AsQString> |
| 127 | inline QTextStream &operator<<(QTextStream &str, const language::_string<AsQString> &s) |
| 128 | { |
| 129 | s.format(str); |
| 130 | return str; |
| 131 | } |
| 132 | |
| 133 | using charliteral = _string<false>; |
| 134 | using qstring = _string<true>; |
| 135 | |
| 136 | class repeat { |
| 137 | public: |
| 138 | explicit repeat(int count, char c) : m_count(count), m_char(c) {} |
| 139 | |
| 140 | friend QTextStream &operator<<(QTextStream &str, const repeat &r); |
| 141 | |
| 142 | private: |
| 143 | const int m_count; |
| 144 | const char m_char; |
| 145 | }; |
| 146 | |
| 147 | class startFunctionDefinition1 { |
| 148 | public: |
| 149 | explicit startFunctionDefinition1(const char *name, const QString ¶meterType, |
| 150 | const QString ¶meterName, |
| 151 | const QString &indent, |
| 152 | const char *returnType = nullptr); |
| 153 | |
| 154 | friend QTextStream &operator<<(QTextStream &str, const startFunctionDefinition1 &f); |
| 155 | private: |
| 156 | const char *m_name; |
| 157 | const QString &m_parameterType; |
| 158 | const QString &m_parameterName; |
| 159 | const QString &m_indent; |
| 160 | const char *m_return; |
| 161 | }; |
| 162 | |
| 163 | class endFunctionDefinition { |
| 164 | public: |
| 165 | explicit endFunctionDefinition(const char *name); |
| 166 | |
| 167 | friend QTextStream &operator<<(QTextStream &str, const endFunctionDefinition &f); |
| 168 | private: |
| 169 | const char *m_name; |
| 170 | }; |
| 171 | |
| 172 | void _formatStackVariable(QTextStream &str, const char *className, QStringView varName, bool withInitParameters); |
| 173 | |
| 174 | template <bool withInitParameters> |
| 175 | class _stackVariable { |
| 176 | public: |
| 177 | explicit _stackVariable(const char *className, QStringView varName) : |
| 178 | m_className(className), m_varName(varName) {} |
| 179 | |
| 180 | void format(QTextStream &str) const |
| 181 | { _formatStackVariable(str, m_className, m_varName, withInitParameters); } |
| 182 | |
| 183 | private: |
| 184 | const char *m_className; |
| 185 | QStringView m_varName; |
| 186 | QStringView m_parameters; |
| 187 | }; |
| 188 | |
| 189 | template <bool withInitParameters> |
| 190 | inline QTextStream &operator<<(QTextStream &str, const _stackVariable<withInitParameters> &s) |
| 191 | { |
| 192 | s.format(str); |
| 193 | return str; |
| 194 | } |
| 195 | |
| 196 | using stackVariable = _stackVariable<false>; |
| 197 | using stackVariableWithInitParameters = _stackVariable<true>; |
| 198 | |
| 199 | struct SignalSlot |
| 200 | { |
| 201 | QString name; |
| 202 | QString signature; |
| 203 | QString className; |
| 204 | }; |
| 205 | |
| 206 | void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSlot &receiver, |
| 207 | ConnectionSyntax connectionSyntax); |
| 208 | |
| 209 | QString boolValue(bool v); |
| 210 | |
| 211 | QString enumValue(const QString &value); |
| 212 | |
| 213 | } // namespace language |
| 214 | |
| 215 | #endif // LANGUAGE_H |
| 216 | |