From 2f806ff10fcb3d3d9aadc894e7bf791acff828f6 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@qt.io>
Date: Sat, 6 May 2023 22:29:26 +0200
Subject: [PATCH] WIP: Reproduce 2316

Task-number: PYSIDE-2316
Change-Id: I52b4c7c93c6e175dc82ee48d21744b844119d478
---
 examples/scriptableapplication/CMakeLists.txt |  2 +
 examples/scriptableapplication/mainwindow.cpp | 92 ++++++++++++++++++-
 examples/scriptableapplication/mainwindow.h   | 18 ++++
 examples/scriptableapplication/myclass.h      | 16 ++++
 .../scriptableapplication.pro                 |  3 +-
 .../scriptableapplication.xml                 |  3 +
 .../scriptableapplication/wrappedclasses.h    |  1 +
 7 files changed, 131 insertions(+), 4 deletions(-)
 create mode 100644 examples/scriptableapplication/myclass.h

diff --git a/examples/scriptableapplication/CMakeLists.txt b/examples/scriptableapplication/CMakeLists.txt
index 31174628c..0deebce45 100644
--- a/examples/scriptableapplication/CMakeLists.txt
+++ b/examples/scriptableapplication/CMakeLists.txt
@@ -113,6 +113,8 @@ set(SHIBOKEN_OPTIONS --generator-set=shiboken --enable-parent-ctor-heuristic
 # Specify which sources will be generated by shiboken, and their dependencies.
 set(GENERATED_SOURCES
     ${CMAKE_CURRENT_BINARY_DIR}/AppLib/applib_module_wrapper.cpp
+    ${CMAKE_CURRENT_BINARY_DIR}/AppLib/qsharedpointer_myclass_wrapper.cpp
+    ${CMAKE_CURRENT_BINARY_DIR}/AppLib/myclass_wrapper.cpp
     ${CMAKE_CURRENT_BINARY_DIR}/AppLib/mainwindow_wrapper.cpp)
 
 set(GENERATED_SOURCES_DEPENDENCIES
diff --git a/examples/scriptableapplication/mainwindow.cpp b/examples/scriptableapplication/mainwindow.cpp
index 27e6f1247..18f50ec5c 100644
--- a/examples/scriptableapplication/mainwindow.cpp
+++ b/examples/scriptableapplication/mainwindow.cpp
@@ -19,10 +19,49 @@
 #include <QtCore/QDebug>
 #include <QtCore/QTextStream>
 
+#include <QtCore/QTimer>
+
 static const char defaultScript[] = R"(
-import AppLib
-print("Hello, world")
-mainWindow.testFunction1()
+
+from PySide6.QtCore import *
+
+def slot_ListOfItems(items):
+    print("ListOfItems Works", items)
+
+@Slot('QSharedPointer<MyClass>')
+def slot_QSharedPointerItems(item):
+    print("QSharedPointerItems Works", item)
+
+def slot_ListOfQSharedPointerItems(items):
+    print("ListOfQSharedPointerItems Works", items)
+
+def slot_RawPointer(item):
+    print("RawPointer Works", item)
+
+def slot_ListOfRawPointers(items):
+    print("ListOfRawPointers Works", items)
+
+def slot_Reference(item):
+    print("Reference Works", item)
+
+def slot_MyClassList(items):
+    print("MyClassList Works", items)
+
+def slot_MyClassPtrList(items):
+    print("MyClassPtrList Works", items)
+
+def slot_MyClassListPtr(items):
+    print("MyClassListPtr Works", items)
+
+mainWindow.sig_Reference.connect(slot_Reference)
+mainWindow.sig_RawPointer.connect(slot_RawPointer)
+mainWindow.sig_ListOfItems.connect(slot_ListOfItems)
+mainWindow.sig_ListOfRawPointers.connect(slot_ListOfRawPointers)
+mainWindow.sig_QSharedPointerItems.connect(slot_QSharedPointerItems)
+mainWindow.sig_ListOfQSharedPointerItems.connect(slot_ListOfQSharedPointerItems)
+mainWindow.sig_MyClassList.connect(slot_MyClassList)
+mainWindow.sig_MyClassPtrList.connect(slot_MyClassPtrList)
+mainWindow.sig_MyClassListPtr.connect(slot_MyClassListPtr)
 )";
 
 MainWindow::MainWindow()
@@ -64,6 +103,53 @@ MainWindow::MainWindow()
 
     if (!PythonUtils::bindAppObject("__main__", "mainWindow", PythonUtils::MainWindowType, this))
        statusBar()->showMessage(tr("Error loading the application module"));
+
+    QTimer::singleShot(500, [this](){
+        slotRunScript();
+    });
+
+    auto timer = new QTimer(this);
+    timer->start(100);
+
+    connect(timer, &QTimer::timeout, this, [this](){
+        qDebug() << "";
+        qDebug() << "";
+
+        auto itemPtr = new MyClass();
+        itemPtr->uid = 4;
+        itemPtr->str = "ItemPtr";
+
+        sig_RawPointer(itemPtr);
+
+        static MyClass refClass;
+        refClass.uid = 6;
+        refClass.str = "RefClass";
+
+        sig_Reference(refClass);
+
+        sig_ListOfRawPointers(QList<MyClass*>({itemPtr, itemPtr}));
+
+        MyClass c1;
+        c1.uid = 1;
+        c1.str = "C1";
+        sig_ListOfItems({c1});
+
+        QSharedPointer<MyClass> shpr1(new MyClass());
+        QSharedPointer<MyClass> shpr2(new MyClass());
+
+        shpr1->str = "shpr1";
+        shpr2->str = "shpr2";
+
+        sig_QSharedPointerItems(shpr1);
+
+        sig_ListOfQSharedPointerItems({shpr1, shpr2});
+
+        sig_MyClassList({c1, refClass});
+
+        sig_MyClassPtrList({itemPtr, itemPtr});
+
+        sig_MyClassListPtr(new QList<MyClass>({c1, refClass}));
+    });
 }
 
 void MainWindow::slotRunScript()
diff --git a/examples/scriptableapplication/mainwindow.h b/examples/scriptableapplication/mainwindow.h
index b7cafc3e6..09063192e 100644
--- a/examples/scriptableapplication/mainwindow.h
+++ b/examples/scriptableapplication/mainwindow.h
@@ -4,10 +4,17 @@
 #ifndef MAINWINDOW_H
 #define MAINWINDOW_H
 
+#include <myclass.h>
 #include <QtWidgets/QMainWindow>
+#include <QtCore/QSharedPointer>
+#include <QtCore/QList>
 
 class QPlainTextEdit;
 
+using MyClassList = QList<MyClass>;
+using MyClassPtrList = QList<MyClass*>;
+using MyClassListPtr = QList<MyClass>*;
+
 class MainWindow : public QMainWindow
 {
     Q_OBJECT
@@ -16,6 +23,17 @@ public:
 
     void testFunction1();
 
+Q_SIGNALS:
+    void sig_RawPointer(MyClass* classItem);
+    void sig_Reference(MyClass& classItem);
+    void sig_ListOfItems(QList<MyClass> classList);
+    void sig_MyClassList(MyClassList classList);
+    void sig_MyClassPtrList(MyClassPtrList classList);
+    void sig_MyClassListPtr(MyClassListPtr classList);
+    void sig_ListOfRawPointers(QList<MyClass*> classItem);
+    void sig_QSharedPointerItems(QSharedPointer<MyClass> classItem);
+    void sig_ListOfQSharedPointerItems(QList<QSharedPointer<MyClass>> classList);
+
 private Q_SLOTS:
     void slotRunScript();
     void slotPrintDiagnostics();
diff --git a/examples/scriptableapplication/myclass.h b/examples/scriptableapplication/myclass.h
new file mode 100644
index 000000000..ce1955f71
--- /dev/null
+++ b/examples/scriptableapplication/myclass.h
@@ -0,0 +1,16 @@
+#ifndef MYCLASS_H
+#define MYCLASS_H
+
+#include<QtCore/QString>
+#include<QtCore/QList>
+
+class MyClass {
+public:
+    MyClass(){
+
+    }
+    int uid=0;
+    QString str;
+};
+
+#endif
diff --git a/examples/scriptableapplication/scriptableapplication.pro b/examples/scriptableapplication/scriptableapplication.pro
index 148a22546..466123b2c 100644
--- a/examples/scriptableapplication/scriptableapplication.pro
+++ b/examples/scriptableapplication/scriptableapplication.pro
@@ -35,7 +35,8 @@ shiboken.variable_out = GENERATED_SOURCES
 
 # A dummy command that pretends to produce the class wrappers from the headers
 # depending on the module wrapper
-WRAPPED_CLASSES = mainwindow.h
+WRAPPED_CLASSES = mainwindow.h myclass.h 
+SOURCES += $$OUT_PWD/AppLib/qsharedpointer_myclass_wrapper.cpp
 module_wrapper_dummy_command.output = $$WRAPPER_DIR/${QMAKE_FILE_BASE}_wrapper.cpp
 module_wrapper_dummy_command.commands = echo ${QMAKE_FILE_IN}
 module_wrapper_dummy_command.depends = $$WRAPPER_DIR/applib_module_wrapper.cpp
diff --git a/examples/scriptableapplication/scriptableapplication.xml b/examples/scriptableapplication/scriptableapplication.xml
index ef401cb62..54ac32cf2 100644
--- a/examples/scriptableapplication/scriptableapplication.xml
+++ b/examples/scriptableapplication/scriptableapplication.xml
@@ -6,4 +6,7 @@
 <typesystem package="AppLib">
     <load-typesystem name="typesystem_widgets.xml" generate="no"/>
     <object-type name="MainWindow"/>
+    <object-type name="MyClass"/>
+    <smart-pointer-type name="QSharedPointer" type="shared" getter="data"/>
+    
 </typesystem>
diff --git a/examples/scriptableapplication/wrappedclasses.h b/examples/scriptableapplication/wrappedclasses.h
index b31ca7f44..4052d4068 100644
--- a/examples/scriptableapplication/wrappedclasses.h
+++ b/examples/scriptableapplication/wrappedclasses.h
@@ -5,5 +5,6 @@
 #define WRAPPEDCLASSES_H
 
 #include <mainwindow.h>
+#include <myclass.h>
 
 #endif // WRAPPEDCLASSES_H
-- 
2.34.1

