Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cppwritedeclaration.cpp
Go to the documentation of this file.
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
6#include "driver.h"
7#include "ui4.h"
8#include "uic.h"
9#include "databaseinfo.h"
11
12#include <qtextstream.h>
13#include <qdebug.h>
14
16
17using namespace Qt::StringLiterals;
18
19namespace {
20 void openNameSpaces(const QStringList &namespaceList, QTextStream &output)
21 {
22 for (const QString &n : namespaceList) {
23 if (!n.isEmpty())
24 output << "namespace " << n << " {\n";
25 }
26 }
27
28 void closeNameSpaces(const QStringList &namespaceList, QTextStream &output) {
29 for (auto it = namespaceList.rbegin(), end = namespaceList.rend(); it != end; ++it) {
30 if (!it->isEmpty())
31 output << "} // namespace " << *it << "\n";
32 }
33 }
34}
35
36namespace CPP {
37
39 m_uic(uic),
40 m_driver(uic->driver()),
41 m_output(uic->output()),
42 m_option(uic->option())
43{
44}
45
46void WriteDeclaration::acceptUI(DomUI *node)
47{
48 QString qualifiedClassName = node->elementClass() + m_option.postfix;
49 QString className = qualifiedClassName;
50
51 m_driver->findOrInsertWidget(node->elementWidget());
52
53 QString exportMacro = node->elementExportMacro();
54 if (!exportMacro.isEmpty())
55 exportMacro.append(u' ');
56
57 QStringList namespaceList = qualifiedClassName.split("::"_L1);
58 if (!namespaceList.isEmpty()) {
59 className = namespaceList.last();
60 namespaceList.removeLast();
61 }
62
63 // This is a bit of the hack but covers the cases Qt in/without namespaces
64 // and User defined classes in/without namespaces. The "strange" case
65 // is a User using Qt-in-namespace having his own classes not in a namespace.
66 // In this case the generated Ui helper classes will also end up in
67 // the Qt namespace (which is harmless, but not "pretty")
68 const bool needsMacro = m_option.qtNamespace &&
69 (namespaceList.isEmpty() || namespaceList.at(0) == "qdesigner_internal"_L1);
70
71 if (needsMacro)
72 m_output << "QT_BEGIN_NAMESPACE\n\n";
73
74 openNameSpaces(namespaceList, m_output);
75
76 if (!namespaceList.isEmpty())
77 m_output << "\n";
78
79 m_output << "class " << exportMacro << m_option.prefix << className << "\n"
80 << "{\n"
81 << "public:\n";
82
83 const QStringList connections = m_uic->databaseInfo()->connections();
84 for (const QString &connection : connections) {
85 if (connection != "(default)"_L1)
86 m_output << m_option.indent << "QSqlDatabase " << connection << "Connection;\n";
87 }
88
89 TreeWalker::acceptWidget(node->elementWidget());
90 if (const DomButtonGroups *domButtonGroups = node->elementButtonGroups())
91 acceptButtonGroups(domButtonGroups);
92
93 m_output << "\n";
94
96
97 m_output << "};\n\n";
98
99 closeNameSpaces(namespaceList, m_output);
100
101 if (!namespaceList.isEmpty())
102 m_output << "\n";
103
104 if (m_option.generateNamespace && !m_option.prefix.isEmpty()) {
105 namespaceList.append("Ui"_L1);
106
107 openNameSpaces(namespaceList, m_output);
108
109 m_output << m_option.indent << "class " << exportMacro << className << ": public " << m_option.prefix << className << " {};\n";
110
111 closeNameSpaces(namespaceList, m_output);
112
113 if (!namespaceList.isEmpty())
114 m_output << "\n";
115 }
116
117 if (needsMacro)
118 m_output << "QT_END_NAMESPACE\n\n";
119}
120
121void WriteDeclaration::acceptWidget(DomWidget *node)
122{
123 QString className = u"QWidget"_s;
124 if (node->hasAttributeClass())
125 className = node->attributeClass();
126
127 m_output << m_option.indent << CustomWidgetsInfo::realClassName(className) << " *" << m_driver->findOrInsertWidget(node) << ";\n";
128
130}
131
132void WriteDeclaration::acceptSpacer(DomSpacer *node)
133{
134 m_output << m_option.indent << "QSpacerItem *" << m_driver->findOrInsertSpacer(node) << ";\n";
136}
137
138void WriteDeclaration::acceptLayout(DomLayout *node)
139{
140 QString className = u"QLayout"_s;
141 if (node->hasAttributeClass())
142 className = node->attributeClass();
143
144 m_output << m_option.indent << className << " *" << m_driver->findOrInsertLayout(node) << ";\n";
145
147}
148
149void WriteDeclaration::acceptActionGroup(DomActionGroup *node)
150{
151 m_output << m_option.indent << "QActionGroup *" << m_driver->findOrInsertActionGroup(node) << ";\n";
152
154}
155
156void WriteDeclaration::acceptAction(DomAction *node)
157{
158 m_output << m_option.indent << "QAction *" << m_driver->findOrInsertAction(node) << ";\n";
159
161}
162
163void WriteDeclaration::acceptButtonGroup(const DomButtonGroup *buttonGroup)
164{
165 m_output << m_option.indent << "QButtonGroup *" << m_driver->findOrInsertButtonGroup(buttonGroup) << ";\n";
167}
168
169} // namespace CPP
170
171QT_END_NAMESPACE
static QString realClassName(const QString &className)
QString findOrInsertButtonGroup(const DomButtonGroup *ui_group)
Definition driver.cpp:110
QString findOrInsertWidget(const DomWidget *ui_widget)
Definition driver.cpp:65
QString findOrInsertActionGroup(const DomActionGroup *ui_group)
Definition driver.cpp:100
QString findOrInsertAction(const DomAction *ui_action)
Definition driver.cpp:105
QString findOrInsertLayout(const DomLayout *ui_layout)
Definition driver.cpp:77
QString findOrInsertSpacer(const DomSpacer *ui_spacer)
Definition driver.cpp:72
Definition uic.h:30
QTextStream & output()
Definition uic.h:41
const Option & option() const
Definition uic.h:44
Driver * driver() const
Definition uic.h:38
Combined button and popup list for selecting options.
void acceptWidget(DomWidget *node) override
void acceptAction(DomAction *node) override
void acceptUI(DomUI *node) override
void acceptButtonGroup(const DomButtonGroup *buttonGroup) override
void acceptActionGroup(DomActionGroup *node) override
void acceptLayout(DomLayout *node) override
void acceptSpacer(DomSpacer *node) override
void acceptUI(DomUI *node) override
unsigned int generateNamespace
Definition option.h:23
virtual void acceptAction(DomAction *action)
virtual void acceptActionGroup(DomActionGroup *actionGroup)
virtual void acceptButtonGroup(const DomButtonGroup *buttonGroup)
virtual void acceptSpacer(DomSpacer *spacer)
virtual void acceptWidget(DomWidget *widget)
virtual void acceptButtonGroups(const DomButtonGroups *buttonGroups)
virtual void acceptLayout(DomLayout *layout)