1 // This is the implementation of the QPyQuickWindow classes.
2 //
3 // Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
4 //
5 // This file is part of PyQt5.
6 //
7 // This file may be used under the terms of the GNU General Public License
8 // version 3.0 as published by the Free Software Foundation and appearing in
9 // the file LICENSE included in the packaging of this file.  Please review the
10 // following information to ensure the GNU General Public License version 3.0
11 // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 //
13 // If you do not wish to use this file under the terms of the GPL version 3.0
14 // then you may purchase a commercial license.  For more information contact
15 // info@riverbankcomputing.com.
16 //
17 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 
20 
21 #include <Python.h>
22 
23 #include <QQmlListProperty>
24 
25 #include "qpyquickview.h"
26 
27 #include "sipAPIQtQuick.h"
28 
29 
30 // The maximum number of Python QQuickView types.
31 const int NrOfQuickViewTypes = 20;
32 
33 // The list of registered Python types.
34 static QList<PyTypeObject *> pyqt_types;
35 
36 // The registration data for the canned types.
37 static QQmlPrivate::RegisterType canned_types[NrOfQuickViewTypes];
38 
39 // External declarations.
40 extern const QMetaObject *qpyquick_pick_metaobject(const QMetaObject *super_mo,
41         const QMetaObject *static_mo);
42 
43 
44 #define QPYQUICKVIEW_INIT(n) \
45     case n##U: \
46         QPyQuickView##n::staticMetaObject = *mo; \
47         rt->typeId = qRegisterNormalizedMetaType<QPyQuickView##n *>(ptr_name); \
48         rt->listId = qRegisterNormalizedMetaType<QQmlListProperty<QPyQuickView##n> >(list_name); \
49         rt->objectSize = sizeof(QPyQuickView##n); \
50         rt->create = QQmlPrivate::createInto<QPyQuickView##n>; \
51         rt->metaObject = mo; \
52         rt->attachedPropertiesFunction = QQmlPrivate::attachedPropertiesFunc<QPyQuickView##n>(); \
53         rt->attachedPropertiesMetaObject = QQmlPrivate::attachedPropertiesMetaObject<QPyQuickView##n>(); \
54         rt->parserStatusCast = QQmlPrivate::StaticCastSelector<QPyQuickView##n,QQmlParserStatus>::cast(); \
55         rt->valueSourceCast = QQmlPrivate::StaticCastSelector<QPyQuickView##n,QQmlPropertyValueSource>::cast(); \
56         rt->valueInterceptorCast = QQmlPrivate::StaticCastSelector<QPyQuickView##n,QQmlPropertyValueInterceptor>::cast(); \
57         break
58 
59 
60 // The ctor.
QPyQuickView(QWindow * parent)61 QPyQuickView::QPyQuickView(QWindow *parent) : sipQQuickView(parent)
62 {
63 }
64 
65 
66 // Add a new Python type and return its number.
addType(PyTypeObject * type,const QMetaObject * mo,const QByteArray & ptr_name,const QByteArray & list_name)67 QQmlPrivate::RegisterType *QPyQuickView::addType(PyTypeObject *type,
68         const QMetaObject *mo, const QByteArray &ptr_name,
69         const QByteArray &list_name)
70 {
71     int type_nr = pyqt_types.size();
72 
73     // Check we have a spare canned type.
74     if (type_nr >= NrOfQuickViewTypes)
75     {
76         PyErr_Format(PyExc_TypeError,
77                 "a maximum of %d QQuickView types may be registered with QML",
78                 NrOfQuickViewTypes);
79         return 0;
80     }
81 
82     pyqt_types.append(type);
83 
84     QQmlPrivate::RegisterType *rt = &canned_types[type_nr];
85 
86     // Initialise those members that depend on the C++ type.
87     switch (type_nr)
88     {
89         QPYQUICKVIEW_INIT(0);
90         QPYQUICKVIEW_INIT(1);
91         QPYQUICKVIEW_INIT(2);
92         QPYQUICKVIEW_INIT(3);
93         QPYQUICKVIEW_INIT(4);
94         QPYQUICKVIEW_INIT(5);
95         QPYQUICKVIEW_INIT(6);
96         QPYQUICKVIEW_INIT(7);
97         QPYQUICKVIEW_INIT(8);
98         QPYQUICKVIEW_INIT(9);
99         QPYQUICKVIEW_INIT(10);
100         QPYQUICKVIEW_INIT(11);
101         QPYQUICKVIEW_INIT(12);
102         QPYQUICKVIEW_INIT(13);
103         QPYQUICKVIEW_INIT(14);
104         QPYQUICKVIEW_INIT(15);
105         QPYQUICKVIEW_INIT(16);
106         QPYQUICKVIEW_INIT(17);
107         QPYQUICKVIEW_INIT(18);
108         QPYQUICKVIEW_INIT(19);
109     }
110 
111     return rt;
112 }
113 
114 
115 // Create the Python instance.
createPyObject(QWindow * parent)116 void QPyQuickView::createPyObject(QWindow *parent)
117 {
118     SIP_BLOCK_THREADS
119 
120     // Assume C++ owns everything.
121     PyObject *obj = sipConvertFromNewPyType(this, pyqt_types.at(typeNr()),
122             NULL, &sipPySelf, "D", parent, sipType_QWindow, NULL);
123 
124     if (!obj)
125         pyqt5_qtquick_err_print();
126 
127     SIP_UNBLOCK_THREADS
128 }
129 
130 
131 // The canned type implementations.
132 #define QPYQUICKVIEW_IMPL(n) \
133 QPyQuickView##n::QPyQuickView##n(QWindow *parent) : QPyQuickView(parent) \
134 { \
135     createPyObject(parent); \
136 } \
137 const QMetaObject *QPyQuickView##n::metaObject() const \
138 { \
139     return qpyquick_pick_metaobject(QPyQuickView::metaObject(), &staticMetaObject); \
140 } \
141 QMetaObject QPyQuickView##n::staticMetaObject
142 
143 
144 QPYQUICKVIEW_IMPL(0);
145 QPYQUICKVIEW_IMPL(1);
146 QPYQUICKVIEW_IMPL(2);
147 QPYQUICKVIEW_IMPL(3);
148 QPYQUICKVIEW_IMPL(4);
149 QPYQUICKVIEW_IMPL(5);
150 QPYQUICKVIEW_IMPL(6);
151 QPYQUICKVIEW_IMPL(7);
152 QPYQUICKVIEW_IMPL(8);
153 QPYQUICKVIEW_IMPL(9);
154 QPYQUICKVIEW_IMPL(10);
155 QPYQUICKVIEW_IMPL(11);
156 QPYQUICKVIEW_IMPL(12);
157 QPYQUICKVIEW_IMPL(13);
158 QPYQUICKVIEW_IMPL(14);
159 QPYQUICKVIEW_IMPL(15);
160 QPYQUICKVIEW_IMPL(16);
161 QPYQUICKVIEW_IMPL(17);
162 QPYQUICKVIEW_IMPL(18);
163 QPYQUICKVIEW_IMPL(19);
164