Debian Codegraph @

examples/mandelbrot.qml

raw
1
2 /**
3 * PyOtherSide: Asynchronous Python 3 Bindings for Qt 5 and Qt 6
4 * Copyright (c) 2011, 2013, Thomas Perl <[email protected]>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12 * FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
17 **/
18
19 import QtQuick 2.0
20 import io.thp.pyotherside 1.0
21
22 Image {
23 id: image
24 width: 300
25 height: 300
26
27 property real zoomFactor: 1.2
28 property real zoom: 2.5
29 property real xCenter: -0.5
30 property real yCenter: 0.0
31 property real xRange: zoom
32 property real yRange: zoom * height / width
33 property real mLeft: xCenter - xRange / 2
34 property real mRight: xCenter + xRange / 2
35 property real mTop: yCenter - yRange / 2
36 property real mBottom: yCenter + yRange / 2
37
38 onXRangeChanged: updateImageTimer.start()
39 onYRangeChanged: updateImageTimer.start()
40 onXCenterChanged: updateImageTimer.start()
41 onYCenterChanged: updateImageTimer.start()
42
43 sourceSize {
44 width: image.width / 4
45 height: image.height / 4
46 }
47
48 MouseArea {
49 anchors.fill: parent
50 property real lastPosX
51 property real lastPosY
52 onPressed: {
53 lastPosX = mouse.x;
54 lastPosY = mouse.y;
55 }
56
57 onPositionChanged: {
58 var diffX = (mouse.x - lastPosX);
59 var diffY = (mouse.y - lastPosY);
60
61 image.xCenter -= diffX / image.width * image.xRange;
62 image.yCenter -= diffY / image.height * image.yRange;
63
64 lastPosX = mouse.x;
65 lastPosY = mouse.y;
66 }
67 }
68
69 Column {
70 spacing: 10
71 anchors {
72 right: parent.right
73 top: parent.top
74 margins: 10
75 }
76
77 Rectangle {
78 width: 30; height: 30
79 color: '#aaffffff'
80 Text {
81 anchors.centerIn: parent
82 text: '-'
83 }
84 MouseArea {
85 anchors.fill: parent
86 onClicked: image.zoom *= image.zoomFactor
87 }
88 }
89
90 Rectangle {
91 width: 30; height: 30
92 color: '#aaffffff'
93 Text {
94 anchors.centerIn: parent
95 text: '+'
96 }
97 MouseArea {
98 anchors.fill: parent
99 onClicked: image.zoom /= image.zoomFactor
100 }
101 }
102 }
103
104 Python {
105 id: py
106
107 Component.onCompleted: {
108 // Add the directory of this .qml file to the search path
109 py.addImportPath(Qt.resolvedUrl('.'));
110
111 py.importModule('mandelbrot', function () {
112 // Do the first image update once the module is loaded
113 updateImageTimer.start();
114 });
115 }
116
117 onError: console.log('Python error: ' + traceback)
118 }
119
120 Timer {
121 id: updateImageTimer
122 interval: 100
123 onTriggered: image.source = 'image://python/' + image.mLeft + '/' + image.mRight + '/' + image.mTop + '/' + image.mBottom
124 }
125 }