~toolpart/+junk/pythoncard

« back to all changes in this revision

Viewing changes to samples/spirographInteractive/spirographInteractive.py

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2006-11-12 17:52:13 UTC
  • mfrom: (2.1.5 feisty)
  • Revision ID: james.westby@ubuntu.com-20061112175213-tv8bnl6rtpa2qw1o
Tags: 0.8.1-8.1
* Non-maintainer upload.
* Fix path to findfiles, codeEditor and resourceEditor:
   + patch from Ernest ter Kuile <ernestjw@xs4all.nl>. (Closes: #397018)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
"""
 
4
__version__ = "$Revision: 1.3 $"
 
5
__date__ = "$Date: 2004/08/12 19:19:02 $"
 
6
"""
 
7
 
 
8
"""
 
9
this is a direct port of the Java applet at
 
10
http://www.wordsmith.org/anu/java/spirograph.html
 
11
"""
 
12
 
 
13
from PythonCard import clipboard, dialog, graphic, model
 
14
import wx
 
15
import os
 
16
import random
 
17
import math
 
18
 
 
19
class Spirograph(model.Background):
 
20
 
 
21
    def on_initialize(self, event):
 
22
        self.filename = None
 
23
        comp = self.components
 
24
        self.sliderLabels = {
 
25
        'stcFixedCircleRadius':comp.stcFixedCircleRadius.text,
 
26
        'stcMovingCircleRadius':comp.stcMovingCircleRadius.text,
 
27
        'stcMovingCircleOffset':comp.stcMovingCircleOffset.text,
 
28
        'stcRevolutionsInRadians':comp.stcRevolutionsInRadians.text,
 
29
        }
 
30
        self.setSliderLabels()
 
31
        if self.components.chkDarkCanvas.checked:
 
32
            self.components.bufOff.backgroundColor = 'black'
 
33
            self.components.bufOff.clear()
 
34
        self.doSpirograph()
 
35
 
 
36
    def doSpirograph(self):
 
37
        comp = self.components
 
38
        
 
39
        canvas = comp.bufOff
 
40
        width, height = canvas.size
 
41
        xOffset = width / 2
 
42
        yOffset = height / 2
 
43
 
 
44
        R = comp.sldFixedCircleRadius.value
 
45
        r = comp.sldMovingCircleRadius.value
 
46
        O = comp.sldMovingCircleOffset.value
 
47
        revolutions = comp.sldRevolutionsInRadians.value
 
48
        
 
49
        color = comp.btnColor.backgroundColor
 
50
        canvas.foregroundColor = color
 
51
 
 
52
        canvas.autoRefresh = 0
 
53
        canvas.clear()
 
54
        
 
55
        if comp.radDrawingStyle.stringSelection == 'Lines':
 
56
            drawLines = 1
 
57
        else:
 
58
            drawLines = 0
 
59
 
 
60
        t = 0.0
 
61
 
 
62
        if R+r+O == 0:
 
63
            # avoid divide by zero errors
 
64
            s = 5.0/0.0000001
 
65
        else:
 
66
            s = 5.0/(R+r+O)
 
67
        rSum = R + r
 
68
        # avoid divide by zero errors
 
69
        if r == 0:
 
70
            r = 0.0000001
 
71
        exprResult = (rSum * t) / r
 
72
        lastX = rSum*math.cos(t) - O*math.cos(exprResult) + xOffset
 
73
        lastY = rSum*math.sin(t) - O*math.sin(exprResult) + yOffset
 
74
        self.keepDrawing = 1
 
75
        points = []
 
76
        while abs(t) <= revolutions:
 
77
            exprResult = (rSum * t) / r
 
78
            x = rSum*math.cos(t) - O*math.cos(exprResult) + xOffset
 
79
            y = rSum*math.sin(t) - O*math.sin(exprResult) + yOffset
 
80
            if drawLines:
 
81
                points.append((lastX, lastY, x, y))
 
82
                lastX = x
 
83
                lastY = y
 
84
            else:
 
85
                points.append((x, y))
 
86
            t += s
 
87
 
 
88
        if drawLines:
 
89
            canvas.drawLineList(points)
 
90
        else:
 
91
            canvas.drawPointList(points)
 
92
        canvas.autoRefresh = 1
 
93
        canvas.refresh()
 
94
 
 
95
    def on_btnColor_mouseClick(self, event):
 
96
        result = dialog.colorDialog(self)
 
97
        if result.accepted:
 
98
            self.components.bufOff.foregroundColor = result.color
 
99
            event.target.backgroundColor = result.color
 
100
            self.doSpirograph()
 
101
 
 
102
    def on_select(self, event):
 
103
        name = event.target.name
 
104
        # only process Sliders
 
105
        if name.startswith('sld'):
 
106
            labelName = 'stc' + name[3:]
 
107
            self.components[labelName].text = self.sliderLabels[labelName] + \
 
108
                ' ' + str(event.target.value)
 
109
        self.doSpirograph()
 
110
 
 
111
    def on_chkDarkCanvas_mouseClick(self, event):
 
112
        if event.target.checked:
 
113
            self.components.bufOff.backgroundColor = 'black'
 
114
        else:
 
115
            self.components.bufOff.backgroundColor = 'white'
 
116
        self.doSpirograph()
 
117
 
 
118
    def setSliderLabels(self):
 
119
        comp = self.components
 
120
        for key in self.sliderLabels:
 
121
            sliderName = 'sld' + key[3:]
 
122
            comp[key].text = self.sliderLabels[key] + ' ' + str(comp[sliderName].value)
 
123
 
 
124
    def on_btnRandom_mouseClick(self, event):
 
125
        comp = self.components
 
126
        comp.sldFixedCircleRadius.value = random.randint(1, 100)
 
127
        comp.sldMovingCircleRadius.value = random.randint(-50, 50)
 
128
        comp.sldMovingCircleOffset.value = random.randint(1, 100)
 
129
        self.setSliderLabels()
 
130
        self.doSpirograph()
 
131
 
 
132
    def openFile(self):
 
133
        wildcard = "All files (*.*)|*.*"
 
134
        result = dialog.openFileDialog(None, "Import which file?", '', '', wildcard)
 
135
        if result.accepted:
 
136
            path = result.paths[0]
 
137
            os.chdir(os.path.dirname(path))
 
138
            try:
 
139
                self.filename = path
 
140
                
 
141
                filename = os.path.splitext(os.path.basename(path))[0]
 
142
                if filename.startswith('spiro'):
 
143
                    items = filename[5:].split('_')
 
144
                    comp = self.components
 
145
                    comp.sldFixedCircleRadius.value = int(items[0])
 
146
                    comp.sldMovingCircleRadius.value = int(items[1])
 
147
                    comp.sldMovingCircleOffset.value = int(items[2])
 
148
                    comp.btnColor.backgroundColor = eval(items[3])
 
149
                    comp.chkDarkCanvas.checked = int(items[4])
 
150
                    if items[5] == 'L':
 
151
                        comp.radDrawingStyle.stringSelection = 'Lines'
 
152
                    else:
 
153
                        comp.radDrawingStyle.stringSelection = 'Points'
 
154
                    comp.sldRevolutionsInRadians.value = int(items[6])
 
155
                    self.setSliderLabels()
 
156
    
 
157
                    bmp = graphic.Bitmap(self.filename)
 
158
                    self.components.bufOff.drawBitmap(bmp, (0, 0))
 
159
            except:
 
160
                pass
 
161
 
 
162
    def on_menuFileOpen_select(self, event):
 
163
        self.openFile()
 
164
 
 
165
    def on_menuFileSaveAs_select(self, event):
 
166
        if self.filename is None:
 
167
            path = ''
 
168
            filename = ''
 
169
        else:
 
170
            path, filename = os.path.split(self.filename)
 
171
            
 
172
        comp = self.components
 
173
        style = comp.radDrawingStyle.stringSelection[0]
 
174
        filename = 'spiro' + str(comp.sldFixedCircleRadius.value) + '_' + \
 
175
            str(comp.sldMovingCircleRadius.value) + '_' + \
 
176
            str(comp.sldMovingCircleOffset.value) + '_' + \
 
177
            str(comp.btnColor.backgroundColor) + '_' + \
 
178
            str(comp.chkDarkCanvas.checked) + '_' + \
 
179
            style + '_' + \
 
180
            str(comp.sldRevolutionsInRadians.value) + \
 
181
            '.png'
 
182
 
 
183
        wildcard = "All files (*.*)|*.*"
 
184
        result = dialog.saveFileDialog(None, "Save As", path, filename, wildcard)
 
185
        if result.accepted:
 
186
            path = result.paths[0]
 
187
            fileType = graphic.bitmapType(path)
 
188
            try:
 
189
                bmp = self.components.bufOff.getBitmap()
 
190
                bmp.SaveFile(path, fileType)
 
191
                return True
 
192
            except:
 
193
                return False
 
194
        else:
 
195
            return False
 
196
 
 
197
    def on_menuEditCopy_select(self, event):
 
198
        clipboard.setClipboard(self.components.bufOff.getBitmap())
 
199
 
 
200
    def on_menuEditPaste_select(self, event):
 
201
        bmp = clipboard.getClipboard()
 
202
        if isinstance(bmp, wx.Bitmap):
 
203
            self.components.bufOff.drawBitmap(bmp)
 
204
 
 
205
    def on_editClear_command(self, event):
 
206
        self.components.bufOff.clear()
 
207
 
 
208
 
 
209
if __name__ == '__main__':
 
210
    app = model.Application(Spirograph)
 
211
    app.MainLoop()