13#include <pythonwritedeclaration.h>
14#include <pythonwriteimports.h>
18#include <qxmlstream.h>
20#include <qscopedpointer.h>
21#include <qtextstream.h>
25using namespace Qt::StringLiterals;
38 QString fileName = opt.inputFile;
41 if (fileName.isEmpty()) {
42 if (!f.open(stdin, QIODevice::ReadOnly))
45 f.setFileName(fileName);
46 if (!f.open(QIODevice::ReadOnly))
52 QXmlStreamReader reader;
54 ui = parseUiFile(reader);
59 if (DomIncludes *includes = ui->elementIncludes()) {
60 const auto incls = includes->elementInclude();
61 for (DomInclude *incl : incls) {
62 QString file = incl->text();
66 fprintf(stdout,
"%s\n", file.toLocal8Bit().constData());
70 if (DomCustomWidgets *customWidgets = ui->elementCustomWidgets()) {
71 const auto elementCustomWidget = customWidgets->elementCustomWidget();
72 for (DomCustomWidget *customWidget : elementCustomWidget) {
73 if (DomHeader *header = customWidget->elementHeader()) {
74 QString file = header->text();
78 fprintf(stdout,
"%s\n", file.toLocal8Bit().constData());
88void Uic::writeCopyrightHeaderCpp(
const DomUI *ui)
const
90 QString comment = ui->elementComment();
91 if (!comment.isEmpty())
92 out <<
"/*\n" << comment <<
"\n*/\n\n";
94 out <<
"/********************************************************************************\n";
95 out <<
"** Form generated from reading UI file '" << QFileInfo(opt.inputFile).fileName() <<
"'\n";
97 out <<
"** Created by: Qt User Interface Compiler version " << QT_VERSION_STR <<
"\n";
99 out <<
"** WARNING! All changes made in this file will be lost when recompiling UI file!\n";
100 out <<
"********************************************************************************/\n\n";
108 return c == u'/' || c == u'*';
114 for (
const qsizetype size = s.size(); i < size && isCppCommentChar(s.at(i)); ++i) {
119void Uic::writeCopyrightHeaderPython(
const DomUI *ui)
const
121 QString comment = ui->elementComment();
122 if (!comment.isEmpty()) {
123 const auto lines = QStringView{comment}.split(u'\n');
124 for (
const auto &line : lines) {
125 if (
const auto leadingCommentChars = leadingCppCommentCharCount(line)) {
126 out << language::repeat(leadingCommentChars,
'#')
127 << line.right(line.size() - leadingCommentChars);
129 if (!line.startsWith(u'#'))
138 out << language::repeat(80,
'#') <<
"\n## Form generated from reading UI file '"
139 << QFileInfo(opt.inputFile).fileName()
140 <<
"'\n##\n## Created by: Qt User Interface Compiler version " << QT_VERSION_STR
141 <<
"\n##\n## WARNING! All changes made in this file will be lost when recompiling UI file!\n"
142 << language::repeat(80,
'#') <<
"\n\n";
149 const QXmlStreamAttributes attributes = reader.attributes();
150 const auto versionAttribute =
"version"_L1;
151 if (!attributes.hasAttribute(versionAttribute))
153 const QStringView version = attributes.value(versionAttribute);
154 return version.toDouble();
157DomUI *
Uic::parseUiFile(QXmlStreamReader &reader)
161 const auto uiElement =
"ui"_L1;
162 while (!reader.atEnd()) {
163 if (reader.readNext() == QXmlStreamReader::StartElement) {
164 if (reader.name().compare(uiElement, Qt::CaseInsensitive) == 0
166 const double version = versionFromUiAttribute(reader);
168 const QString msg = QString::fromLatin1(
"uic: File generated with too old version of Qt Widgets Designer (%1)").arg(version);
176 reader.raiseError(
"Unexpected element "_L1 + reader.name().toString());
180 if (reader.hasError()) {
183 fprintf(stderr,
"%s\n",
qPrintable(QString::fromLatin1(
"uic: Error in line %1, column %2 : %3")
184 .arg(reader.lineNumber()).arg(reader.columnNumber())
185 .arg(reader.errorString())));
193 QScopedPointer<DomUI> ui;
195 QXmlStreamReader reader;
196 reader.setDevice(in);
197 ui.reset(parseUiFile(reader));
203 double version = ui->attributeVersion().toDouble();
205 fprintf(stderr,
"uic: File generated with too old version of Qt Widgets Designer\n");
209 const QString &language = ui->attributeLanguage();
212 if (!language.isEmpty() && language.compare(
"c++"_L1, Qt::CaseInsensitive) != 0) {
213 fprintf(stderr,
"uic: File is not a \"c++\" ui file, language=%s\n",
qPrintable(language));
217 return write(ui.data());
222 if (!ui || !ui->elementWidget())
228 out <<
"# -*- coding: utf-8 -*-\n\n";
233 writeCopyrightHeaderCpp(ui);
236 writeCopyrightHeaderPython(ui);
242 writeHeaderProtectionStart();
246 pixFunction = ui->elementPixmapFunction();
247 if (pixFunction ==
"QPixmap::fromMimeSource"_L1 || pixFunction ==
"qPixmapFromMimeSource"_L1) {
248 fprintf(stderr,
"%s: Warning: Obsolete pixmap function '%s' specified in the UI file.\n",
254 cWidgetsInfo.acceptUI(ui);
274 writeHeaderProtectionEnd();
279void Uic::writeHeaderProtectionStart()
281 QString h = drv->headerFileName();
282 out <<
"#ifndef " << h <<
"\n"
283 <<
"#define " << h <<
"\n";
286void Uic::writeHeaderProtectionEnd()
288 QString h = drv->headerFileName();
289 out <<
"#endif // " << h <<
"\n";
294 static const QStringList buttons = {
295 u"QRadioButton"_s, u"QToolButton"_s,
296 u"QCheckBox"_s, u"QPushButton"_s,
297 u"QCommandLinkButton"_s
304 static const QStringList containers = {
305 u"QStackedWidget"_s, u"QToolBox"_s,
306 u"QTabWidget"_s, u"QScrollArea"_s,
307 u"QMdiArea"_s, u"QWizard"_s,
316 static const QStringList menus = {
317 u"QMenu"_s, u"QPopupMenu"_s
void acceptUI(DomUI *node) override
QTextStream & output() const
void setUseIdBasedTranslations(bool u)
void acceptUI(DomUI *node) override
bool isButton(const QString &className) const
bool write(QIODevice *in)
const CustomWidgetsInfo * customWidgetsInfo() const
bool isMenu(const QString &className) const
bool isContainer(const QString &className) const
Combined button and popup list for selecting options.
const QString & asString(const QString &s)
#define qPrintable(string)
void acceptUI(DomUI *node) override
WriteDeclaration(Uic *uic)
unsigned int headerProtection
unsigned int copyrightHeader
void acceptUI(DomUI *node) override
WriteDeclaration(Uic *uic)
void acceptUI(DomUI *node) override
static double versionFromUiAttribute(QXmlStreamReader &reader)
static qsizetype leadingCppCommentCharCount(QStringView s)
static bool isCppCommentChar(QChar c)