1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef CPPWRITEINITIALIZATION_H
5#define CPPWRITEINITIALIZATION_H
6
7#include "treewalker.h"
8#include <qpair.h>
9#include <qhash.h>
10#include <qset.h>
11#include <qmap.h>
12#include <qstack.h>
13#include <qtextstream.h>
14
15enum class ConnectionSyntax;
16namespace language { struct SignalSlot; }
17
18QT_BEGIN_NAMESPACE
19
20class Driver;
21class Uic;
22class DomBrush;
23class DomFont;
24class DomResourceIcon;
25class DomSizePolicy;
26class DomStringList;
27struct Option;
28
29namespace CPP {
30 // Handle for a flat DOM font to get comparison functionality required for maps
31 class FontHandle {
32 public:
33 FontHandle(const DomFont *domFont);
34 int compare(const FontHandle &) const;
35 private:
36 const DomFont *m_domFont;
37 };
38 inline bool operator ==(const FontHandle &f1, const FontHandle &f2) { return f1.compare(f2) == 0; }
39 inline bool operator <(const FontHandle &f1, const FontHandle &f2) { return f1.compare(f2) < 0; }
40
41 // Handle for a flat DOM icon to get comparison functionality required for maps
42 class IconHandle {
43 public:
44 IconHandle(const DomResourceIcon *domIcon);
45 int compare(const IconHandle &) const;
46 private:
47 const DomResourceIcon *m_domIcon;
48 };
49 inline bool operator ==(const IconHandle &i1, const IconHandle &i2) { return i1.compare(i2) == 0; }
50 inline bool operator <(const IconHandle &i1, const IconHandle &i2) { return i1.compare(i2) < 0; }
51
52 // Handle for a flat DOM size policy to get comparison functionality required for maps
53 class SizePolicyHandle {
54 public:
55 SizePolicyHandle(const DomSizePolicy *domSizePolicy);
56 int compare(const SizePolicyHandle &) const;
57 private:
58 const DomSizePolicy *m_domSizePolicy;
59 };
60 inline bool operator ==(const SizePolicyHandle &f1, const SizePolicyHandle &f2) { return f1.compare(f2) == 0; }
61 inline bool operator <(const SizePolicyHandle &f1, const SizePolicyHandle &f2) { return f1.compare(f2) < 0; }
62
63
64struct WriteInitialization : public TreeWalker
65{
66 using DomPropertyList = QList<DomProperty*>;
67 using DomPropertyMap = QHash<QString, DomProperty*>;
68
69 WriteInitialization(Uic *uic);
70
71//
72// widgets
73//
74 void acceptUI(DomUI *node) override;
75 void acceptWidget(DomWidget *node) override;
76
77 void acceptLayout(DomLayout *node) override;
78 void acceptSpacer(DomSpacer *node) override;
79 void acceptLayoutItem(DomLayoutItem *node) override;
80
81//
82// actions
83//
84 void acceptActionGroup(DomActionGroup *node) override;
85 void acceptAction(DomAction *node) override;
86 void acceptActionRef(DomActionRef *node) override;
87
88//
89// tab stops
90//
91 void acceptTabStops(DomTabStops *tabStops) override;
92
93//
94// custom widgets
95//
96 void acceptCustomWidgets(DomCustomWidgets *node) override;
97 void acceptCustomWidget(DomCustomWidget *node) override;
98
99//
100// layout defaults/functions
101//
102 void acceptLayoutDefault(DomLayoutDefault *node) override { m_LayoutDefaultHandler.acceptLayoutDefault(node); }
103 void acceptLayoutFunction(DomLayoutFunction *node) override { m_LayoutDefaultHandler.acceptLayoutFunction(node); }
104
105//
106// signal/slot connections
107//
108 void acceptConnection(DomConnection *connection) override;
109
110 enum {
111 Use43UiFile = 0,
112 TopLevelMargin,
113 ChildMargin,
114 SubLayoutMargin
115 };
116
117private:
118 static QString domColor2QString(const DomColor *c);
119
120 QString iconCall(const DomProperty *prop);
121 QString pixCall(const DomProperty *prop) const;
122 QString pixCall(QLatin1StringView type, const QString &text) const;
123 QString trCall(const QString &str, const QString &comment = QString(), const QString &id = QString()) const;
124 QString trCall(DomString *str, const QString &defaultString = QString()) const;
125 QString noTrCall(DomString *str, const QString &defaultString = QString()) const;
126 QString autoTrCall(DomString *str, const QString &defaultString = QString()) const;
127 inline QTextStream &autoTrOutput(const DomProperty *str);
128 QTextStream &autoTrOutput(const DomString *str, const QString &defaultString = QString());
129 // Apply a comma-separated list of values using a function "setSomething(int idx, value)"
130 void writePropertyList(const QString &varName, const QString &setFunction, const QString &value, const QString &defaultValue);
131
132 enum { WritePropertyIgnoreMargin = 1, WritePropertyIgnoreSpacing = 2, WritePropertyIgnoreObjectName = 4 };
133 QString writeStringListProperty(const DomStringList *list) const;
134 void writeProperties(const QString &varName, const QString &className, const DomPropertyList &lst, unsigned flags = 0);
135 void writeColorGroup(DomColorGroup *colorGroup, const QString &group, const QString &paletteName);
136 void writeBrush(const DomBrush *brush, const QString &brushName);
137
138//
139// special initialization
140//
141 class Item {
142 Q_DISABLE_COPY_MOVE(Item)
143 public:
144 Item(const QString &itemClassName, const QString &indent, QTextStream &setupUiStream, QTextStream &retranslateUiStream, Driver *driver);
145 ~Item();
146 enum EmptyItemPolicy {
147 DontConstruct,
148 ConstructItemOnly,
149 ConstructItemAndVariable
150 };
151 QString writeSetupUi(const QString &parent, EmptyItemPolicy emptyItemPolicy = ConstructItemOnly);
152 void writeRetranslateUi(const QString &parentPath);
153 void addSetter(const QString &setter, const QString &directive = QString(), bool translatable = false); // don't call it if you already added *this as a child of another Item
154 void addChild(Item *child); // all setters should already been added
155 private:
156 struct ItemData
157 {
158 QMultiMap<QString, QString> setters; // directive to setter
159 QSet<QString> directives;
160 enum TemporaryVariableGeneratorPolicy { // policies with priority, number describes the priority
161 DontGenerate = 1,
162 GenerateWithMultiDirective = 2,
163 Generate = 3
164 } policy = DontGenerate;
165 };
166 ItemData m_setupUiData;
167 ItemData m_retranslateUiData;
168 QList<Item *> m_children;
169 Item *m_parent = nullptr;
170
171 const QString m_itemClassName;
172 const QString m_indent;
173 QTextStream &m_setupUiStream;
174 QTextStream &m_retranslateUiStream;
175 Driver *m_driver;
176 };
177 using Items = QList<Item *>;
178
179 void addInitializer(Item *item,
180 const QString &name, int column, const QString &value, const QString &directive = QString(), bool translatable = false) const;
181 void addQtFlagsInitializer(Item *item, const DomPropertyMap &properties,
182 const QString &name, int column = -1) const;
183 void addQtEnumInitializer(Item *item,
184 const DomPropertyMap &properties, const QString &name, int column = -1) const;
185 void addBrushInitializer(Item *item,
186 const DomPropertyMap &properties, const QString &name, int column = -1);
187 void addStringInitializer(Item *item,
188 const DomPropertyMap &properties, const QString &name, int column = -1, const QString &directive = QString()) const;
189 void addCommonInitializers(Item *item,
190 const DomPropertyMap &properties, int column = -1);
191
192 void initializeMenu(DomWidget *w, const QString &parentWidget);
193 void initializeComboBox(DomWidget *w);
194 void initializeListWidget(DomWidget *w);
195 void initializeTreeWidget(DomWidget *w);
196 Items initializeTreeWidgetItems(const QList<DomItem *> &domItems);
197 void initializeTableWidget(DomWidget *w);
198
199 QString disableSorting(DomWidget *w, const QString &varName);
200 void enableSorting(DomWidget *w, const QString &varName, const QString &tempName);
201
202 struct Declaration
203 {
204 QString name;
205 QString className;
206 };
207
208 Declaration findDeclaration(const QString &name);
209
210private:
211 QString writeFontProperties(const DomFont *f);
212 QString writeIconProperties(const DomResourceIcon *i);
213 void writePixmapFunctionIcon(QTextStream &output, const QString &iconName,
214 const QString &indent, const DomResourceIcon *i) const;
215 QString writeSizePolicy(const DomSizePolicy *sp);
216 QString writeBrushInitialization(const DomBrush *brush);
217 void addButtonGroup(const DomWidget *node, const QString &varName);
218 void addWizardPage(const QString &pageVarName, const DomWidget *page, const QString &parentWidget);
219 bool isCustomWidget(const QString &className) const;
220 ConnectionSyntax connectionSyntax(const language::SignalSlot &sender,
221 const language::SignalSlot &receiver) const;
222
223 const Uic *m_uic;
224 Driver *m_driver;
225 QTextStream &m_output;
226 const Option &m_option;
227 QString m_indent;
228 QString m_dindent;
229 bool m_stdsetdef = true;
230
231 struct Buddy
232 {
233 QString labelVarName;
234 QString buddyAttributeName;
235 };
236 friend class QTypeInfo<Buddy>;
237
238 QStack<DomWidget*> m_widgetChain;
239 QStack<DomLayout*> m_layoutChain;
240 QStack<DomActionGroup*> m_actionGroupChain;
241 QList<Buddy> m_buddies;
242
243 QSet<QString> m_buttonGroups;
244 using ColorBrushHash = QHash<uint, QString>;
245 ColorBrushHash m_colorBrushHash;
246 // Map from font properties to font variable name for reuse
247 // Map from size policy to variable for reuse
248 using FontPropertiesNameMap = QMap<FontHandle, QString>;
249 using IconPropertiesNameMap = QMap<IconHandle, QString>;
250 using SizePolicyNameMap = QMap<SizePolicyHandle, QString>;
251 FontPropertiesNameMap m_fontPropertiesNameMap;
252 IconPropertiesNameMap m_iconPropertiesNameMap;
253 SizePolicyNameMap m_sizePolicyNameMap;
254
255 class LayoutDefaultHandler {
256 public:
257 LayoutDefaultHandler();
258 void acceptLayoutDefault(DomLayoutDefault *node);
259 void acceptLayoutFunction(DomLayoutFunction *node);
260
261 // Write out the layout margin and spacing properties applying the defaults.
262 void writeProperties(const QString &indent, const QString &varName,
263 const DomPropertyMap &pm, int marginType,
264 bool suppressMarginDefault, QTextStream &str) const;
265 private:
266 void writeProperty(int p, const QString &indent, const QString &objectName, const DomPropertyMap &pm,
267 const QString &propertyName, const QString &setter, int defaultStyleValue,
268 bool suppressDefault, QTextStream &str) const;
269
270 enum Properties { Margin, Spacing, NumProperties };
271 enum StateFlags { HasDefaultValue = 1, HasDefaultFunction = 2};
272 unsigned m_state[NumProperties];
273 int m_defaultValues[NumProperties];
274 QString m_functions[NumProperties];
275 };
276
277 // layout defaults
278 LayoutDefaultHandler m_LayoutDefaultHandler;
279 int m_layoutMarginType = TopLevelMargin;
280
281 QString m_generatedClass;
282 QString m_mainFormVarName;
283 QStringList m_customSlots;
284 QStringList m_customSignals;
285 bool m_mainFormUsedInRetranslateUi = false;
286
287 QString m_delayedInitialization;
288 QTextStream m_delayedOut;
289
290 QString m_refreshInitialization;
291 QTextStream m_refreshOut;
292
293 QString m_delayedActionInitialization;
294 QTextStream m_actionOut;
295
296 bool m_layoutWidget = false;
297 bool m_firstThemeIcon = true;
298 bool m_connectSlotsByName = true;
299};
300
301} // namespace CPP
302
303Q_DECLARE_TYPEINFO(CPP::WriteInitialization::Buddy, Q_RELOCATABLE_TYPE);
304
305QT_END_NAMESPACE
306
307#endif // CPPWRITEINITIALIZATION_H
308

source code of qtbase/src/tools/uic/cpp/cppwriteinitialization.h