From 5a5a96560e36af6b5d7ab375651b8c622bb5a7c3 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 28 Nov 2018 11:30:41 +0100 Subject: [PATCH] uic: Adapt connection syntax to Python Extend WriteInitialization::findDeclaration() to return the class name (on this occasion preparing for generating Qt 5 connection syntax) and add a helper function for formatting the connection. Task-number: PYSIDE-797 Change-Id: I7507f604c8275c93d347b7a6a5d5b5a2a5f3ffd5 Reviewed-by: Cristian Maureira-Fredes --- src/tools/uic/cpp/cppwriteinitialization.cpp | 42 ++++++++++++-------- src/tools/uic/cpp/cppwriteinitialization.h | 8 +++- src/tools/uic/shared/language.cpp | 17 ++++++++ src/tools/uic/shared/language.h | 10 +++++ 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/tools/uic/cpp/cppwriteinitialization.cpp b/src/tools/uic/cpp/cppwriteinitialization.cpp index f5ac59df08..9986e7306d 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.cpp +++ b/src/tools/uic/cpp/cppwriteinitialization.cpp @@ -2430,34 +2430,42 @@ QTextStream &WriteInitialization::autoTrOutput(const DomString *str, const QStri return m_output; } -QString WriteInitialization::findDeclaration(const QString &name) +WriteInitialization::Declaration WriteInitialization::findDeclaration(const QString &name) { if (const DomWidget *widget = m_driver->widgetByName(name)) - return m_driver->findOrInsertWidget(widget); + return {m_driver->findOrInsertWidget(widget), widget->attributeClass()}; if (const DomAction *action = m_driver->actionByName(name)) - return m_driver->findOrInsertAction(action); + return {m_driver->findOrInsertAction(action), QStringLiteral("QAction")}; if (const DomButtonGroup *group = m_driver->findButtonGroup(name)) - return m_driver->findOrInsertButtonGroup(group); - return QString(); + return {m_driver->findOrInsertButtonGroup(group), QStringLiteral("QButtonGroup")}; + return {}; } void WriteInitialization::acceptConnection(DomConnection *connection) { - const QString sender = findDeclaration(connection->elementSender()); - const QString receiver = findDeclaration(connection->elementReceiver()); + const QString senderName = connection->elementSender(); + const QString receiverName = connection->elementReceiver(); - if (sender.isEmpty() || receiver.isEmpty()) + const auto senderDecl = findDeclaration(senderName); + const auto receiverDecl = findDeclaration(receiverName); + + if (senderDecl.name.isEmpty() || receiverDecl.name.isEmpty()) { + QString message; + QTextStream(&message) << m_option.messagePrefix() + << ": Warning: Invalid signal/slot connection: \"" + << senderName << "\" -> \"" << receiverName << "\"."; + fprintf(stderr, "%s\n", qPrintable(message)); return; + } - m_output << m_indent << "QObject::connect(" - << sender - << ", " - << "SIGNAL("<elementSignal()<<')' - << ", " - << receiver - << ", " - << "SLOT("<elementSlot()<<')' - << ");\n"; + language::SignalSlot theSignal{senderDecl.name, connection->elementSignal(), + senderDecl.className}; + language::SignalSlot theSlot{receiverDecl.name, connection->elementSlot(), + receiverDecl.className}; + + m_output << m_indent; + language::formatConnection(m_output, theSignal, theSlot); + m_output << ";\n"; } static void generateMultiDirectiveBegin(QTextStream &outputStream, const QSet &directives) diff --git a/src/tools/uic/cpp/cppwriteinitialization.h b/src/tools/uic/cpp/cppwriteinitialization.h index c408c44b40..b90ffe00a7 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.h +++ b/src/tools/uic/cpp/cppwriteinitialization.h @@ -220,7 +220,13 @@ private: QString disableSorting(DomWidget *w, const QString &varName); void enableSorting(DomWidget *w, const QString &varName, const QString &tempName); - QString findDeclaration(const QString &name); + struct Declaration + { + QString name; + QString className; + }; + + Declaration findDeclaration(const QString &name); private: QString writeFontProperties(const DomFont *f); diff --git a/src/tools/uic/shared/language.cpp b/src/tools/uic/shared/language.cpp index d05f3c935f..fe44f95285 100644 --- a/src/tools/uic/shared/language.cpp +++ b/src/tools/uic/shared/language.cpp @@ -384,4 +384,21 @@ void _formatStackVariable(QTextStream &str, const char *className, QStringView v } } +void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSlot &receiver) +{ + switch (language()) { + case Language::Cpp: + str << "QObject::connect(" << sender.name << ", SIGNAL("<< sender.signature + << "), " << receiver.name << ", SLOT("<< receiver.signature << "))"; + break; + case Language::Python: + str << sender.name << '.' + << sender.signature.leftRef(sender.signature.indexOf(QLatin1Char('('))) + << ".connect(" << receiver.name << '.' + << receiver.signature.leftRef(receiver.signature.indexOf(QLatin1Char('('))) + << ')'; + break; + } +} + } // namespace language diff --git a/src/tools/uic/shared/language.h b/src/tools/uic/shared/language.h index a1c871a549..eb50a2db7f 100644 --- a/src/tools/uic/shared/language.h +++ b/src/tools/uic/shared/language.h @@ -31,6 +31,7 @@ #include #include +#include QT_FORWARD_DECLARE_CLASS(QTextStream) @@ -189,6 +190,15 @@ inline QTextStream &operator<<(QTextStream &str, const _stackVariable; using stackVariableWithInitParameters = _stackVariable; +struct SignalSlot +{ + QString name; + QString signature; + QString className; +}; + +void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSlot &receiver); + } // namespace language #endif // LANGUAGE_H