uic: Generate parameters for ambiguous signals

Fixes: PYSIDE-1720
Pick-to: 6.2
Change-Id: I71b77db73471bdec7826bf69c7df521d7686537e
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Friedemann Kleint 2021-11-26 08:44:29 +01:00
parent c001216eed
commit 49a8ab50ba
5 changed files with 47 additions and 6 deletions

View File

@ -27,6 +27,7 @@
****************************************************************************/
#include "cppwriteinitialization.h"
#include "customwidgetsinfo.h"
#include "driver.h"
#include "ui4.h"
#include "utils.h"
@ -2640,10 +2641,14 @@ void WriteInitialization::acceptConnection(DomConnection *connection)
return;
}
const QString senderSignature = connection->elementSignal();
language::SignalSlotOptions signalOptions;
if (m_uic->customWidgetsInfo()->isAmbiguousSignal(senderDecl.className, senderSignature))
signalOptions.setFlag(language::SignalSlotOption::Ambiguous);
language::SignalSlot theSignal{senderDecl.name, senderSignature,
senderDecl.className};
senderDecl.className, signalOptions};
language::SignalSlot theSlot{receiverDecl.name, connection->elementSlot(),
receiverDecl.className};
receiverDecl.className, {}};
m_output << m_indent;
language::formatConnection(m_output, theSignal, theSlot,

View File

@ -101,6 +101,21 @@ bool CustomWidgetsInfo::isCustomWidgetContainer(const QString &className) const
return false;
}
// Is it ambiguous, resulting in different signals for Python
// "QAbstractButton::clicked(checked=false)"
bool CustomWidgetsInfo::isAmbiguousSignal(const QString &className,
const QString &signalSignature) const
{
if (signalSignature.startsWith(u"triggered") && extends(className, "QAction"))
return true;
if (signalSignature.startsWith(u"clicked(")
&& extendsOneOf(className, {u"QCommandLinkButton"_qs, u"QCheckBox"_qs,
u"QPushButton"_qs, u"QRadioButton"_qs, u"QToolButton"_qs})) {
return true;
}
return false;
}
QString CustomWidgetsInfo::realClassName(const QString &className) const
{
if (className == QLatin1String("Line"))

View File

@ -61,6 +61,9 @@ public:
bool isCustomWidgetContainer(const QString &className) const;
bool isAmbiguousSignal(const QString &className,
const QString &signalSignature) const;
private:
using NameCustomWidgetMap = QMap<QString, DomCustomWidget*>;
NameCustomWidgetMap m_customWidgets;

View File

@ -456,12 +456,22 @@ void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSl
break;
}
break;
case Language::Python:
str << sender.name << '.'
<< QStringView{sender.signature}.left(sender.signature.indexOf(QLatin1Char('(')))
<< ".connect(" << receiver.name << '.'
case Language::Python: {
const auto paren = sender.signature.indexOf(u'(');
auto senderSignature = QStringView{sender.signature};
str << sender.name << '.' << senderSignature.left(paren);
// Signals like "QAbstractButton::clicked(checked=false)" require
// the parameter if it is used.
if (sender.options.testFlag(SignalSlotOption::Ambiguous)) {
const QStringView parameters =
senderSignature.mid(paren + 1, senderSignature.size() - paren - 2);
if (!parameters.isEmpty() && !parameters.contains(u','))
str << "[\"" << parameters << "\"]";
}
str << ".connect(" << receiver.name << '.'
<< QStringView{receiver.signature}.left(receiver.signature.indexOf(QLatin1Char('(')))
<< ')';
}
break;
}
}

View File

@ -198,11 +198,19 @@ inline QTextStream &operator<<(QTextStream &str, const _stackVariable<withInitPa
using stackVariable = _stackVariable<false>;
using stackVariableWithInitParameters = _stackVariable<true>;
enum class SignalSlotOption
{
Ambiguous = 0x1
};
Q_DECLARE_FLAGS(SignalSlotOptions, SignalSlotOption)
struct SignalSlot
{
QString name;
QString signature;
QString className;
SignalSlotOptions options;
};
void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSlot &receiver,