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 <cristian.maureira-fredes@qt.io>
This commit is contained in:
parent
5b8676578d
commit
5a5a96560e
@ -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("<<connection->elementSignal()<<')'
|
||||
<< ", "
|
||||
<< receiver
|
||||
<< ", "
|
||||
<< "SLOT("<<connection->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<QString> &directives)
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -31,6 +31,7 @@
|
||||
|
||||
#include <QtCore/qstring.h>
|
||||
#include <QtCore/qstringview.h>
|
||||
#include <QtCore/qstring.h>
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS(QTextStream)
|
||||
|
||||
@ -189,6 +190,15 @@ inline QTextStream &operator<<(QTextStream &str, const _stackVariable<withInitPa
|
||||
using stackVariable = _stackVariable<false>;
|
||||
using stackVariableWithInitParameters = _stackVariable<true>;
|
||||
|
||||
struct SignalSlot
|
||||
{
|
||||
QString name;
|
||||
QString signature;
|
||||
QString className;
|
||||
};
|
||||
|
||||
void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSlot &receiver);
|
||||
|
||||
} // namespace language
|
||||
|
||||
#endif // LANGUAGE_H
|
||||
|
Loading…
Reference in New Issue
Block a user