1#!/usr/bin/env python
2
3
4#############################################################################
5##
6## Copyright (C) 2013 Riverbank Computing Limited.
7## Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
8## All rights reserved.
9##
10## This file is part of the examples of PyQt.
11##
12## $QT_BEGIN_LICENSE:BSD$
13## You may use this file under the terms of the BSD license as follows:
14##
15## "Redistribution and use in source and binary forms, with or without
16## modification, are permitted provided that the following conditions are
17## met:
18##   * Redistributions of source code must retain the above copyright
19##     notice, this list of conditions and the following disclaimer.
20##   * Redistributions in binary form must reproduce the above copyright
21##     notice, this list of conditions and the following disclaimer in
22##     the documentation and/or other materials provided with the
23##     distribution.
24##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
25##     the names of its contributors may be used to endorse or promote
26##     products derived from this software without specific prior written
27##     permission.
28##
29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
40## $QT_END_LICENSE$
41##
42#############################################################################
43
44
45import sys
46
47from PyQt5.QtCore import (pyqtProperty, Q_CLASSINFO, QCoreApplication, QObject,
48        QUrl)
49from PyQt5.QtGui import QColor
50from PyQt5.QtQml import (qmlRegisterType, QQmlComponent, QQmlEngine,
51        QQmlListProperty)
52
53
54QML = b'''
55import People 1.0
56import QtQuick 2.0
57
58BirthdayParty {
59    host: Boy {
60        name: "Bob Jones"
61        shoe { size: 12; color: "white"; brand: "Bikey"; price: 90.0 }
62    }
63
64    Boy {
65        name: "Leo Hodges"
66        shoe { size: 10; color: "black"; brand: "Thebok"; price: 59.95 }
67    }
68
69    Boy {
70        name: "Jack Smith"
71        shoe {
72            size: 8
73            color: "blue"
74            brand: "Luma"
75            price: 19.95
76        }
77    }
78
79    Girl {
80        name: "Anne Brown"
81        shoe.size: 7
82        shoe.color: "red"
83        shoe.brand: "Job Macobs"
84        shoe.price: 699.99
85    }
86}
87'''
88
89
90class ShoeDescription(QObject):
91    def __init__(self, parent=None):
92        super(ShoeDescription, self).__init__(parent)
93
94        self._size = 0
95        self._color = QColor()
96        self._brand = ''
97        self._price = 0.0
98
99    @pyqtProperty(int)
100    def size(self):
101        return self._size
102
103    @size.setter
104    def size(self, size):
105        self._size = size
106
107    @pyqtProperty(QColor)
108    def color(self):
109        return self._color
110
111    @color.setter
112    def color(self, color):
113        self._color = color
114
115    @pyqtProperty(str)
116    def brand(self):
117        return self._brand
118
119    @brand.setter
120    def brand(self, brand):
121        self._brand = brand
122
123    @pyqtProperty(float)
124    def price(self):
125        return self._price
126
127    @price.setter
128    def price(self, price):
129        self._price = price
130
131
132class Person(QObject):
133    def __init__(self, parent=None):
134        super(Person, self).__init__(parent)
135
136        self._name = ''
137        self._shoe = ShoeDescription()
138
139    @pyqtProperty(str)
140    def name(self):
141        return self._name
142
143    @name.setter
144    def name(self, name):
145        self._name = name
146
147    @pyqtProperty(ShoeDescription)
148    def shoe(self):
149        return self._shoe
150
151
152class Boy(Person):
153    pass
154
155
156class Girl(Person):
157    pass
158
159
160class BirthdayParty(QObject):
161    Q_CLASSINFO('DefaultProperty', 'guests')
162
163    def __init__(self, parent=None):
164        super(BirthdayParty, self).__init__(parent)
165
166        self._host = None
167        self._guests = []
168
169    @pyqtProperty(Person)
170    def host(self):
171        return self._host
172
173    @host.setter
174    def host(self, host):
175        self._host = host
176
177    @pyqtProperty(QQmlListProperty)
178    def guests(self):
179        return QQmlListProperty(Person, self, self._guests)
180
181
182app = QCoreApplication(sys.argv)
183
184qmlRegisterType(BirthdayParty, "People", 1, 0, "BirthdayParty")
185qmlRegisterType(ShoeDescription)
186qmlRegisterType(Person)
187qmlRegisterType(Boy, "People", 1, 0, "Boy")
188qmlRegisterType(Girl, "People", 1, 0, "Girl")
189
190engine = QQmlEngine()
191
192component = QQmlComponent(engine)
193component.setData(QML, QUrl())
194
195party = component.create()
196
197if party is not None and party.host is not None:
198    print("\"%s\" is having a birthday!" % party.host.name)
199
200    if isinstance(party.host, Boy):
201        print("He is inviting:")
202    else:
203        print("She is inviting:")
204
205    bestShoe = None
206
207    for guest in party.guests:
208        print("    \"%s\"" % guest.name)
209
210        if bestShoe is None or bestShoe.shoe.price < guest.shoe.price:
211            bestShoe = guest
212
213    if bestShoe is not None:
214        print("\"%s\" is wearing the best shoes!" % bestShoe.name)
215else:
216    for e in component.errors():
217        print("Error:", e.toString());
218