1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of Qt for Python.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "objecttype.h"
30 #include "objecttypelayout.h"
31 #include <algorithm>
32 #include <iostream>
33 #include <string>
34 #include <assert.h>
35 
36 using namespace std;
37 
ObjectType(ObjectType * parent)38 ObjectType::ObjectType(ObjectType* parent) : m_parent(nullptr), m_layout(nullptr), m_call_id(-1)
39 {
40     setParent(parent);
41 }
42 
~ObjectType()43 ObjectType::~ObjectType()
44 {
45     for (ObjectTypeList::iterator child_iter = m_children.begin();
46          child_iter != m_children.end(); ++child_iter)
47         delete *child_iter;
48 }
49 
50 ObjectType*
createWithChild()51 ObjectType::createWithChild()
52 {
53     ObjectType* parent = create();
54     ObjectType* child = create();
55     child->setObjectName("child");
56     child->setParent(parent);
57     return parent;
58 }
59 
defaultInstance()60 const ObjectType *ObjectType::defaultInstance()
61 {
62     static ObjectType result;
63     return &result;
64 }
65 
66 void
removeChild(ObjectType * child)67 ObjectType::removeChild(ObjectType* child)
68 {
69     if (!child)
70         return;
71 
72     ObjectTypeList::iterator child_iter = std::find(m_children.begin(), m_children.end(), child);
73     if (child_iter != m_children.end()) {
74         m_children.erase(child_iter);
75         child->m_parent = nullptr;
76     }
77 }
78 
79 ObjectType*
takeChild(ObjectType * child)80 ObjectType::takeChild(ObjectType* child)
81 {
82     if (!child)
83         return nullptr;
84 
85     ObjectTypeList::iterator child_iter = std::find(m_children.begin(), m_children.end(), child);
86     if (child_iter != m_children.end()) {
87         m_children.erase(child_iter);
88         child->m_parent = nullptr;
89         return child;
90     }
91     return nullptr;
92 }
93 
94 ObjectType*
takeChild(const Str & name)95 ObjectType::takeChild(const Str& name)
96 {
97     return takeChild(findChild(name));
98 
99 }
100 
101 ObjectType*
findChild(const Str & name)102 ObjectType::findChild(const Str& name)
103 {
104     for (ObjectTypeList::iterator child_iter = m_children.begin();
105          child_iter != m_children.end(); ++child_iter) {
106 
107         if ((*child_iter)->objectName() == name)
108             return *child_iter;
109     }
110     return nullptr;
111 }
112 
113 void
killChild(const Str & name)114 ObjectType::killChild(const Str& name)
115 {
116     for (ObjectTypeList::iterator child_iter = m_children.begin();
117          child_iter != m_children.end(); ++child_iter) {
118 
119         if ((*child_iter)->objectName() == name) {
120             ObjectType* child = *child_iter;
121             removeChild(child);
122             delete child;
123             break;
124         }
125     }
126 }
127 
128 void
setParent(ObjectType * parent)129 ObjectType::setParent(ObjectType* parent)
130 {
131     if (m_parent == parent)
132         return;
133 
134     if (m_parent)
135         m_parent->removeChild(this);
136 
137     m_parent = parent;
138     if (m_parent)
139         m_parent->m_children.push_back(this);
140 }
141 
142 void
setObjectName(const Str & name)143 ObjectType::setObjectName(const Str& name)
144 {
145     m_objectName = name;
146 }
147 
148 Str
objectName() const149 ObjectType::objectName() const
150 {
151     return m_objectName;
152 }
153 
154 bool
causeEvent(Event::EventType eventType)155 ObjectType::causeEvent(Event::EventType eventType)
156 {
157     Event e(eventType);
158     return event(&e);
159 }
160 
161 bool
event(Event * event)162 ObjectType::event(Event* event)
163 {
164     return true;
165 }
166 
167 int
processEvent(ObjectTypeList objects,Event * event)168 ObjectType::processEvent(ObjectTypeList objects, Event *event)
169 {
170     int evaluated = 0;
171 
172     for (ObjectTypeList::iterator obj_iter = objects.begin();
173          obj_iter != objects.end(); ++obj_iter) {
174         if((*obj_iter)->event(event))
175             evaluated++;
176     }
177 
178     return evaluated;
179 
180 }
181 
182 void
callInvalidateEvent(Event * event)183 ObjectType::callInvalidateEvent(Event* event)
184 {
185     invalidateEvent(event);
186 }
187 
188 void
setLayout(ObjectTypeLayout * l)189 ObjectType::setLayout(ObjectTypeLayout* l)
190 {
191     if (!l) {
192         cerr << "[WARNING] ObjectType::setLayout: Cannot set layout to 0." << endl;
193         return;
194     }
195 
196     if (layout()) {
197         if (layout() != l) {
198             cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring();
199             cerr << "' on ObjectType '" << objectName().cstring() << "', which already has a layout." << endl;
200         }
201         return;
202     }
203 
204     ObjectType* oldParent = l->parent();
205     if (oldParent && oldParent != this) {
206         if (oldParent->isLayoutType()) {
207             cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring();
208             cerr << "' on ObjectType '" << objectName().cstring() << "', when the ObjectTypeLayout already has a parent layout." << endl;
209             return;
210         } else {
211             // Steal the layout from an ObjectType parent.
212             oldParent->takeLayout();
213         }
214     }
215 
216     m_layout = l;
217     if (oldParent != this) {
218         l->setParent(this);
219         l->reparentChildren(this);
220     }
221 }
222 
takeLayout()223 ObjectTypeLayout* ObjectType::takeLayout()
224 {
225     ObjectTypeLayout* l = layout();
226     if (!l)
227         return nullptr;
228     m_layout = nullptr;
229     l->setParent(0);
230     return l;
231 }
232 
233 unsigned int
objectTypeHash(const ObjectType * objectType)234 objectTypeHash(const ObjectType* objectType)
235 {
236     return reinterpret_cast<std::size_t>(objectType);
237 }
238 
239 unsigned char
callWithEnum(const Str & prefix,Event::EventType type,unsigned char value)240 ObjectType::callWithEnum(const Str& prefix, Event::EventType type, unsigned char value){
241     return value*value;
242 }
243 
244 unsigned char
callWithEnum(const Str & prefix,unsigned char value)245 ObjectType::callWithEnum(const Str& prefix, unsigned char value) {
246     return value;
247 }
248 
249 void
setObjectSplittedName(const char *,const Str & prefix,const Str & suffix)250 ObjectType::setObjectSplittedName(const char*, const Str& prefix, const Str& suffix)
251 {
252     std::string result(prefix.cstring());
253     result += suffix.cstring();
254     m_objectName = result.c_str();
255 }
256 
257 void
setObjectNameWithSize(const char *,int size,const Str & name)258 ObjectType::setObjectNameWithSize(const char*, int size, const Str& name)
259 {
260     std::string result(name.cstring(), size);
261     m_objectName = result.c_str();
262 }
263 
264 void
setObjectNameWithSize(const Str & name,int size)265 ObjectType::setObjectNameWithSize(const Str& name, int size)
266 {
267     setObjectNameWithSize("", size, name);
268 }
269 
setObject(ObjectType *)270 void ObjectType::setObject(ObjectType *)
271 {
272     m_call_id = 0;
273 }
274 
setObject(const Null &)275 void ObjectType::setObject(const Null&)
276 {
277     m_call_id = 1;
278 }
279 
callId() const280 int ObjectType::callId() const
281 {
282     return m_call_id;
283 }
284 
285 
callVirtualCreateChild()286 void ObjectType::callVirtualCreateChild()
287 {
288     ObjectType* fake_parent = new ObjectType();
289     ObjectType* fake_child = createChild(fake_parent);
290     assert(fake_child->isPython());
291     (void)fake_child;
292     delete fake_parent;
293 }
294 
createChild(ObjectType * parent)295 ObjectType* ObjectType::createChild(ObjectType* parent)
296 {
297     return new ObjectType(parent);
298 }
299 
createObjectType()300 std::size_t ObjectType::createObjectType()
301 {
302     void* addr = new ObjectType();
303     return (std::size_t) addr;
304 }
305 
~OtherBase()306 OtherBase::~OtherBase()
307 {
308 }
309 
~ObjectTypeDerived()310 ObjectTypeDerived::~ObjectTypeDerived()
311 {
312 }
313 
314 bool
event(Event * event)315 ObjectTypeDerived::event(Event* event)
316 {
317     return true;
318 }
319