1 // This is the definition of the QPyQmlObject 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 #ifndef _QPYQMLOBJECT_H
22 #define _QPYQMLOBJECT_H
23 
24 
25 #include <Python.h>
26 
27 #include <QAbstractItemModel>
28 #include <QList>
29 #include <QPointer>
30 #include <QQmlParserStatus>
31 #include <QQmlProperty>
32 #include <QQmlPropertyValueSource>
33 #include <QSet>
34 #include <QSize>
35 
36 
37 class QPyQmlObjectProxy : public QAbstractItemModel
38 {
39 public:
40     QPyQmlObjectProxy(QObject *parent = 0);
41     virtual ~QPyQmlObjectProxy();
42 
43     virtual const QMetaObject *metaObject() const;
44     virtual void *qt_metacast(const char *_clname);
45     virtual int qt_metacall(QMetaObject::Call, int, void **);
46 
47     virtual int typeNr() const = 0;
48 
49     static int addType(PyTypeObject *type);
50     void createPyObject(QObject *parent);
51 
52     static QObject *createAttachedProperties(PyTypeObject *py_type,
53             QObject *parent);
54 
55     static void *resolveProxy(void *proxy);
56 
57     void pyClassBegin();
58     void pyComponentComplete();
59 
60     void pySetTarget(const QQmlProperty &target);
61 
62     // The set of proxies in existence.
63     static QSet<QObject *> proxies;
64 
65     // The real object.
66     QPointer<QObject> proxied;
67 
68     // The real object if it is an item model.
69     QAbstractItemModel *proxied_model;
70 
71     // QAbstractItemModel virtuals.
72     virtual QModelIndex index(int row, int column,
73             const QModelIndex &parent = QModelIndex()) const;
74     virtual QModelIndex parent(const QModelIndex &child) const;
75     virtual QModelIndex sibling(int row, int column, const QModelIndex &idx)
76             const;
77     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
78     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
79     virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
80     virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole)
81             const;
82     virtual bool setData(const QModelIndex &index, const QVariant &value,
83             int role = Qt::EditRole);
84     virtual QVariant headerData(int section, Qt::Orientation orientation,
85             int role = Qt::DisplayRole) const;
86     virtual bool setHeaderData(int section, Qt::Orientation orientation,
87             const QVariant &value, int role = Qt::EditRole);
88     virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
89     virtual bool setItemData(const QModelIndex &index,
90             const QMap<int, QVariant> &roles);
91     virtual QStringList mimeTypes() const;
92     virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
93     virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action,
94             int row, int column, const QModelIndex &parent) const;
95     virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action,
96             int row, int column, const QModelIndex &parent);
97     virtual Qt::DropActions supportedDropActions() const;
98     virtual Qt::DropActions supportedDragActions() const;
99     virtual bool insertRows(int row, int count,
100             const QModelIndex &parent = QModelIndex());
101     virtual bool insertColumns(int column, int count,
102             const QModelIndex &parent = QModelIndex());
103     virtual bool removeRows(int row, int count,
104             const QModelIndex &parent = QModelIndex());
105     virtual bool removeColumns(int column, int count,
106             const QModelIndex &parent = QModelIndex());
107     virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow,
108             int count, const QModelIndex &destinationParent,
109             int destinationChild);
110     virtual bool moveColumns(const QModelIndex &sourceParent, int sourceColumn,
111             int count, const QModelIndex &destinationParent,
112             int destinationChild);
113     virtual void fetchMore(const QModelIndex &parent);
114     virtual bool canFetchMore(const QModelIndex &parent) const;
115     virtual Qt::ItemFlags flags(const QModelIndex &index) const;
116     virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
117     virtual QModelIndex buddy(const QModelIndex &index) const;
118     virtual QModelIndexList match(const QModelIndex &start, int role,
119             const QVariant &value, int hits = 1,
120             Qt::MatchFlags flags =
121                     Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const;
122     virtual QSize span(const QModelIndex &index) const;
123     virtual QHash<int,QByteArray> roleNames() const;
124 
125 protected:
126     void connectNotify(const QMetaMethod &signal);
127 
128 private:
129     // These can by cast to sipWrapperType.
130     static QList<PyTypeObject *> pyqt_types;
131 
132     // The wrapped proxied object.
133     PyObject *py_proxied;
134 
135     QPyQmlObjectProxy(const QPyQmlObjectProxy &);
136 };
137 
138 
139 // The proxy type declarations.
140 #define QPYQML_PROXY_DECL(n) \
141 class QPyQmlObject##n : public QPyQmlObjectProxy, public QQmlParserStatus, public QQmlPropertyValueSource  \
142 { \
143 public: \
144     QPyQmlObject##n(QObject *parent = 0); \
145     static QMetaObject staticMetaObject; \
146     virtual int typeNr() const {return n##U;} \
147     static PyTypeObject *attachedPyType; \
148     static QObject *attachedProperties(QObject *parent); \
149     virtual void classBegin(); \
150     virtual void componentComplete(); \
151     virtual void setTarget(const QQmlProperty &target); \
152 private: \
153     QPyQmlObject##n(const QPyQmlObject##n &); \
154 }
155 
156 
157 QPYQML_PROXY_DECL(0);
158 QPYQML_PROXY_DECL(1);
159 QPYQML_PROXY_DECL(2);
160 QPYQML_PROXY_DECL(3);
161 QPYQML_PROXY_DECL(4);
162 QPYQML_PROXY_DECL(5);
163 QPYQML_PROXY_DECL(6);
164 QPYQML_PROXY_DECL(7);
165 QPYQML_PROXY_DECL(8);
166 QPYQML_PROXY_DECL(9);
167 QPYQML_PROXY_DECL(10);
168 QPYQML_PROXY_DECL(11);
169 QPYQML_PROXY_DECL(12);
170 QPYQML_PROXY_DECL(13);
171 QPYQML_PROXY_DECL(14);
172 QPYQML_PROXY_DECL(15);
173 QPYQML_PROXY_DECL(16);
174 QPYQML_PROXY_DECL(17);
175 QPYQML_PROXY_DECL(18);
176 QPYQML_PROXY_DECL(19);
177 QPYQML_PROXY_DECL(20);
178 QPYQML_PROXY_DECL(21);
179 QPYQML_PROXY_DECL(22);
180 QPYQML_PROXY_DECL(23);
181 QPYQML_PROXY_DECL(24);
182 QPYQML_PROXY_DECL(25);
183 QPYQML_PROXY_DECL(26);
184 QPYQML_PROXY_DECL(27);
185 QPYQML_PROXY_DECL(28);
186 QPYQML_PROXY_DECL(29);
187 QPYQML_PROXY_DECL(30);
188 QPYQML_PROXY_DECL(31);
189 QPYQML_PROXY_DECL(32);
190 QPYQML_PROXY_DECL(33);
191 QPYQML_PROXY_DECL(34);
192 QPYQML_PROXY_DECL(35);
193 QPYQML_PROXY_DECL(36);
194 QPYQML_PROXY_DECL(37);
195 QPYQML_PROXY_DECL(38);
196 QPYQML_PROXY_DECL(39);
197 QPYQML_PROXY_DECL(40);
198 QPYQML_PROXY_DECL(41);
199 QPYQML_PROXY_DECL(42);
200 QPYQML_PROXY_DECL(43);
201 QPYQML_PROXY_DECL(44);
202 QPYQML_PROXY_DECL(45);
203 QPYQML_PROXY_DECL(46);
204 QPYQML_PROXY_DECL(47);
205 QPYQML_PROXY_DECL(48);
206 QPYQML_PROXY_DECL(49);
207 QPYQML_PROXY_DECL(50);
208 QPYQML_PROXY_DECL(51);
209 QPYQML_PROXY_DECL(52);
210 QPYQML_PROXY_DECL(53);
211 QPYQML_PROXY_DECL(54);
212 QPYQML_PROXY_DECL(55);
213 QPYQML_PROXY_DECL(56);
214 QPYQML_PROXY_DECL(57);
215 QPYQML_PROXY_DECL(58);
216 QPYQML_PROXY_DECL(59);
217 
218 
219 #endif
220