forked from wxWidgets/Phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicsGradient.py
281 lines (212 loc) · 8.57 KB
/
GraphicsGradient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
import wx
g = wx
import wx.lib.scrolledpanel as scrolled
# To test compatibility of gradients with the generic GraphicsContext classes
# uncomment this line
#import wx.lib.graphics as g
#----------------------------------------------------------------------
class GradientPanel(wx.Panel):
"""
This panel will be painted with the gradient brush created by the
rest of the sample.
"""
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
# Create a simple default brush we can use until a gradient
# brush is given to us
self.brush = wx.WHITE_BRUSH
self.SetBackgroundStyle(wx.BG_STYLE_PAINT)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.SetInitialSize((600,100))
def DrawWithBrush(self, brush):
self.brush = brush
self.Refresh()
def OnPaint(self, evt):
dc = wx.PaintDC(self)
gc = g.GraphicsContext.Create(dc)
gc.SetBrush(self.brush)
gc.SetPen(wx.Pen('black', 1))
w, h = gc.GetSize()
gc.DrawRectangle(0,0,w,h)
class GradientStopPanel(wx.Panel):
"""
Contains the controls for editing each gradient stop. (Colour,
alpha and relative position.)
"""
def __init__(self, parent, posVal, colour=wx.BLACK, alpha=wx.ALPHA_OPAQUE):
wx.Panel.__init__(self, parent)
# make some widgets
self.pos = wx.SpinCtrlDouble(self, value='%2f' % posVal, size=(65,-1),
min=0.0, max=1.0, initial=posVal, inc=0.01)
self.pos.SetToolTip(
"A value between 0 and 1 representing the distance between (x1,y1) "
"and (x2,y2) for this gradient stop.")
self.clrPicker = wx.ColourPickerCtrl(self, colour=colour)
self.clrPicker.SetToolTip("The colour for this gradient stop")
self.minusBtn = wx.Button(self, -1, " - ", style=wx.BU_EXACTFIT)
self.minusBtn.SetToolTip("Remove this gradient stop")
# put them in a sizer
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.pos, 0, wx.ALIGN_CENTER_VERTICAL)
sizer.Add(self.clrPicker, 0, wx.ALIGN_CENTER_VERTICAL|wx.LEFT, 10)
sizer.Add(self.minusBtn, 0, wx.ALIGN_CENTER_VERTICAL|wx.LEFT, 25)
border = wx.BoxSizer()
border.Add(sizer, 1, wx.EXPAND|wx.ALL, 4)
self.SetSizer(border)
self.Bind(wx.EVT_BUTTON, self.OnMinusButton, self.minusBtn)
@property
def colour(self):
return self.clrPicker.GetColour()
@property
def position(self):
return self.pos.GetValue()
def OnMinusButton(self, evt):
wx.CallAfter(self.Parent.RemoveStop, self)
class TestPanel(scrolled.ScrolledPanel):
"""
The main panel for this sample
"""
def __init__(self, parent, log):
self.log = log
scrolled.ScrolledPanel.__init__(self, parent, -1)
# make the panel that will display the gradient
self.gpanel = GradientPanel(self)
# and the other widgets for collecting data about the gradient
label1 = wx.StaticText(self, -1, "Geometry")
label1.SetFont(wx.FFont(15, wx.FONTFAMILY_SWISS, wx.FONTFLAG_BOLD))
x1 = 0
x2 = self.gpanel.Size.width
y0 = self.gpanel.Size.height / 2
self.x1 = wx.TextCtrl(self, value=str(x1), size=(50,-1))
self.y1 = wx.TextCtrl(self, value=str(y0), size=(50,-1))
self.x2 = wx.TextCtrl(self, value=str(x2), size=(50,-1))
self.y2 = wx.TextCtrl(self, value=str(y0), size=(50,-1))
label2 = wx.StaticText(self, -1, "Stops")
label2.SetFont(wx.FFont(15, wx.FONTFAMILY_SWISS, wx.FONTFLAG_BOLD))
firstStop = GradientStopPanel(self, 0.0)
lastStop = GradientStopPanel(self, 1.0, wx.WHITE)
self.stops = [firstStop, lastStop]
addStopBtn = wx.Button(self, -1, " + ", style=wx.BU_EXACTFIT)
# bind some events
self.Bind(wx.EVT_BUTTON, self.OnAddStop, addStopBtn)
self.Bind(wx.EVT_SPINCTRLDOUBLE, self.OnPositionUpdated)
self.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnColourChanged)
self.Bind(wx.EVT_TEXT, self.OnGeometryChanged)
# do the layout
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.gpanel)
sizer.Add((1,15))
sizer.Add(label1)
sizer.Add((1, 5))
fgs = wx.FlexGridSizer(cols=11, hgap=5, vgap=5)
fgs.AddMany([ (wx.StaticText(self, -1, "x1:"), 0, wx.ALIGN_CENTER_VERTICAL),
self.x1,
((5,1)),
(wx.StaticText(self, -1, "y1:"), 0, wx.ALIGN_CENTER_VERTICAL),
self.y1] )
fgs.Add((40,1))
fgs.AddMany([ (wx.StaticText(self, -1, "x2:"), 0, wx.ALIGN_CENTER_VERTICAL),
self.x2,
((5,1)),
(wx.StaticText(self, -1, "y2:"), 0, wx.ALIGN_CENTER_VERTICAL),
self.y2] )
sizer.Add(fgs, 0, wx.LEFT, 25)
sizer.Add((1,15))
sizer.Add(label2)
sizer.Add((1, 5))
self.stopSizer = wx.BoxSizer(wx.VERTICAL)
self.stopSizer.Add(firstStop)
self.stopSizer.Add(lastStop)
self.stopSizer.Add(addStopBtn, 0, wx.TOP, 10)
sizer.Add(self.stopSizer, 0, wx.LEFT, 25)
border = wx.BoxSizer()
border.Add(sizer, 1, wx.EXPAND|wx.ALL, 15)
self.SetSizer(border)
self.SetLimits()
self.UpdateBrush()
self.SetupScrolling()
def RemoveStop(self, stop):
self.stops.remove(stop)
stop.Destroy()
self.Layout()
self.SetLimits()
self.UpdateBrush()
self.SetupScrolling()
def OnAddStop(self, evt):
newstop = GradientStopPanel(self, 1.0)
self.stopSizer.Insert(len(self.stops), newstop)
self.stops.append(newstop)
self.Layout()
self.SetLimits()
self.UpdateBrush()
self.SetupScrolling()
def OnPositionUpdated(self, evt):
# called when any of the spinctrls in the nested panels are updated
self.SetLimits()
self.UpdateBrush()
def OnColourChanged(self, evt):
# called when any of the color pickers are updated
self.UpdateBrush()
def OnGeometryChanged(self, evt):
# called for changes to x1,y1 or x2,y2
self.UpdateBrush()
def SetLimits(self):
# Tweak the panels in self.stops to set limits on the allowed
# positions, and to disable those that should not be changed or
# removed.
first = self.stops[0]
last = self.stops[-1]
first.pos.Disable()
first.minusBtn.Disable()
last.pos.Disable()
last.minusBtn.Disable()
for idx in range(1, len(self.stops)-1):
prev = self.stops[idx-1]
next = self.stops[idx+1]
stop = self.stops[idx]
stop.pos.SetMin(prev.position)
stop.pos.SetMax(next.position)
stop.pos.Enable()
stop.minusBtn.Enable()
def UpdateBrush(self):
"""
This is where the magic happens. We convert all the values from the
widgets on this panel into a collection of gradient stops and then
create a brush from them, and finally, ask the display panel to
repaint itself with that new brush.
"""
def floatOrZero(value):
try:
return float(value)
except ValueError:
return 0.0
x1 = floatOrZero(self.x1.Value)
y1 = floatOrZero(self.y1.Value)
x2 = floatOrZero(self.x2.Value)
y2 = floatOrZero(self.y2.Value)
gstops = g.GraphicsGradientStops()
gstops.SetStartColour(self.stops[0].colour)
gstops.SetEndColour(self.stops[-1].colour)
for s in self.stops[1:-1]:
gs = g.GraphicsGradientStop(s.colour, s.position)
gstops.Add(gs)
ctx = g.GraphicsContext.Create()
brush = ctx.CreateLinearGradientBrush(x1,y1, x2,y2, gstops)
self.gpanel.DrawWithBrush(brush)
#----------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
#----------------------------------------------------------------------
overview = """<html><body>
<h2><center> Gradients on a GraphicsContext </center></h2>
Multi-stop gradients can be used as fills in a wx.GraphicsContext.
This sample shows how to use the GraphicsGradientStops class to
accomplish this and allows the user to experiment with gradients.
</body></html>
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])