14#include <pythonwritedeclaration.h>
15#include <pythonwriteimports.h>
19#include <qxmlstream.h>
21#include <qscopedpointer.h>
22#include <qtextstream.h>
26using namespace Qt::StringLiterals;
39 QString fileName = opt.inputFile;
42 if (fileName.isEmpty()) {
43 if (!f.open(stdin, QIODevice::ReadOnly))
46 f.setFileName(fileName);
47 if (!f.open(QIODevice::ReadOnly))
53 QXmlStreamReader reader;
55 ui = parseUiFile(reader);
60 if (DomIncludes *includes = ui->elementIncludes()) {
61 const auto incls = includes->elementInclude();
62 for (DomInclude *incl : incls) {
63 QString file = incl->text();
67 fprintf(stdout,
"%s\n", file.toLocal8Bit().constData());
71 if (DomCustomWidgets *customWidgets = ui->elementCustomWidgets()) {
72 const auto elementCustomWidget = customWidgets->elementCustomWidget();
73 for (DomCustomWidget *customWidget : elementCustomWidget) {
74 if (DomHeader *header = customWidget->elementHeader()) {
75 QString file = header->text();
79 fprintf(stdout,
"%s\n", file.toLocal8Bit().constData());
89void Uic::writeCopyrightHeaderCpp(
const DomUI *ui)
const
91 QString comment = ui->elementComment();
92 if (!comment.isEmpty())
93 out <<
"/*\n" << comment <<
"\n*/\n\n";
95 out <<
"/********************************************************************************\n";
96 out <<
"** Form generated from reading UI file '" << QFileInfo(opt.inputFile).fileName() <<
"'\n";
98 out <<
"** Created by: Qt User Interface Compiler version " << QT_VERSION_STR <<
"\n";
100 out <<
"** WARNING! All changes made in this file will be lost when recompiling UI file!\n";
101 out <<
"********************************************************************************/\n\n";
109 return c == u'/' || c == u'*';
115 for (
const qsizetype size = s.size(); i < size && isCppCommentChar(s.at(i)); ++i) {
120void Uic::writeCopyrightHeaderPython(
const DomUI *ui)
const
122 QString comment = ui->elementComment();
123 if (!comment.isEmpty()) {
124 const auto lines = QStringView{comment}.split(u'\n');
125 for (
const auto &line : lines) {
126 if (
const auto leadingCommentChars = leadingCppCommentCharCount(line)) {
127 out << language::repeat(leadingCommentChars,
'#')
128 << line.right(line.size() - leadingCommentChars);
130 if (!line.startsWith(u'#'))
139 out << language::repeat(80,
'#') <<
"\n## Form generated from reading UI file '"
140 << QFileInfo(opt.inputFile).fileName()
141 <<
"'\n##\n## Created by: Qt User Interface Compiler version " << QT_VERSION_STR
142 <<
"\n##\n## WARNING! All changes made in this file will be lost when recompiling UI file!\n"
143 << language::repeat(80,
'#') <<
"\n\n";
150 const QXmlStreamAttributes attributes = reader.attributes();
151 const auto versionAttribute =
"version"_L1;
152 if (!attributes.hasAttribute(versionAttribute))
154 const QStringView version = attributes.value(versionAttribute);
155 return version.toDouble();
158DomUI *
Uic::parseUiFile(QXmlStreamReader &reader)
162 const auto uiElement =
"ui"_L1;
163 while (!reader.atEnd()) {
164 if (reader.readNext() == QXmlStreamReader::StartElement) {
165 if (reader.name().compare(uiElement, Qt::CaseInsensitive) == 0
167 const double version = versionFromUiAttribute(reader);
169 const QString msg = QString::fromLatin1(
"uic: File generated with too old version of Qt Widgets Designer (%1)").arg(version);
177 reader.raiseError(
"Unexpected element "_L1 + reader.name().toString());
181 if (reader.hasError()) {
184 fprintf(stderr,
"%s\n",
qPrintable(QString::fromLatin1(
"uic: Error in line %1, column %2 : %3")
185 .arg(reader.lineNumber()).arg(reader.columnNumber())
186 .arg(reader.errorString())));
194 QScopedPointer<DomUI> ui;
196 QXmlStreamReader reader;
197 reader.setDevice(in);
198 ui.reset(parseUiFile(reader));
204 double version = ui->attributeVersion().toDouble();
206 fprintf(stderr,
"uic: File generated with too old version of Qt Widgets Designer\n");
210 const QString &language = ui->attributeLanguage();
213 if (!language.isEmpty() && language.compare(
"c++"_L1, Qt::CaseInsensitive) != 0) {
214 fprintf(stderr,
"uic: File is not a \"c++\" ui file, language=%s\n",
qPrintable(language));
218 return write(ui.data());
223 if (!ui || !ui->elementWidget())
229 if (!validator.errors().isEmpty()) {
230 for (
const QString &error : validator.errors())
239 out <<
"# -*- coding: utf-8 -*-\n\n";
244 writeCopyrightHeaderCpp(ui);
247 writeCopyrightHeaderPython(ui);
253 writeHeaderProtectionStart();
257 pixFunction = ui->elementPixmapFunction();
258 if (pixFunction ==
"QPixmap::fromMimeSource"_L1 || pixFunction ==
"qPixmapFromMimeSource"_L1) {
259 fprintf(stderr,
"%s: Warning: Obsolete pixmap function '%s' specified in the UI file.\n",
265 cWidgetsInfo.acceptUI(ui);
283 writeHeaderProtectionEnd();
288void Uic::writeHeaderProtectionStart()
290 QString h = drv->headerFileName();
291 out <<
"#ifndef " << h <<
"\n"
292 <<
"#define " << h <<
"\n";
295void Uic::writeHeaderProtectionEnd()
297 QString h = drv->headerFileName();
298 out <<
"#endif // " << h <<
"\n";
303 static const QStringList buttons = {
304 u"QRadioButton"_s, u"QToolButton"_s,
305 u"QCheckBox"_s, u"QPushButton"_s,
306 u"QCommandLinkButton"_s
313 static const QStringList containers = {
314 u"QStackedWidget"_s, u"QToolBox"_s,
315 u"QTabWidget"_s, u"QScrollArea"_s,
316 u"QMdiArea"_s, u"QWizard"_s,
325 static const QStringList menus = {
326 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)