-
Notifications
You must be signed in to change notification settings - Fork 38
/
music_score_panel.py
298 lines (273 loc) · 11.8 KB
/
music_score_panel.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import wx
import traceback
import sys
from wxhelper import wx_cursor, wx_colour
PY3 = sys.version_info.major > 2
WX4 = wx.version().startswith('4')
class MusicScorePanel(wx.ScrolledWindow):
def __init__(self, parent, renderer):
wx.ScrolledWindow.__init__(self, parent, -1)#, style=wx.CLIP_CHILDREN )
self.renderer = renderer
self.draw_ops = []
#self.SetVirtualSize(wx.Size(1450, 1001))
self.svg = None
self.fill_cache = {}
self.stroke_cache = {}
self.path_cache = {}
self.note_paths = []
self.current_page = renderer.empty_page
##self.selected_note_path_indices = set()
##self.selected_note_descriptions = set()
if wx.Platform == "__WXMSW__":
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
self.buffer_width = 400
self.buffer_height = 800
for i in range(wx.Display.GetCount()):
r = wx.Display(i).GetGeometry()
self.buffer_width = max(self.buffer_width, r.GetWidth())
self.buffer_height = max(self.buffer_height, r.GetHeight())
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftButtonDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftButtonUp)
self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
self.OnNoteSelectionChangedDesc = None
self.cross_cursor = wx_cursor(wx.CURSOR_CROSS)
self.pointer_cursor = wx_cursor(wx.CURSOR_ARROW)
self.drag_start_x = None
self.drag_start_y = None
self.drag_rect = None
self.SetVirtualSize((self.buffer_width, self.buffer_height))
self.SetScrollbars(20, 20, 50, 50)
# 1.3.6.2 [JWdJ] 2015-02-14 hook events after initializing to prevent unnecessary redraws
self.need_redraw = True
self.redrawing = False
# self.redraw_counter = 0
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.highlighted_notes = None
def reset_scrolling(self):
self.SetVirtualSize((self.buffer_width, self.buffer_height))
self.SetScrollbars(20, 20, 50, 50)
def get_xy_of_mouse_event(self, event):
x, y = self.CalcUnscrolledPosition(event.GetX(), event.GetY())
return x, y
# def get_path_under_mouse(self, event):
# x, y = self.get_xy_of_mouse_event(event)
# pt = wx.Point2D(x, y)
# for i, path in enumerate(self.note_paths):
# path_box = path.GetBox()
# path_box.Inset(-2.0, -2.0)
# if path_box.Contains(pt):
# self.SetCursor(self.pointer_cursor)
# return (i, path)
# return (-1, None)
# def get_note_desc_under_mouse(self, event):
# x, y = self.get_xy_of_mouse_event(event)
# pt = wx.Point2D(x, y)
# for i, path in enumerate(self.note_paths):
# path_box = path.GetBox()
# path_box.Inset(-2.0, -2.0)
# if path_box.Contains(pt):
# self.SetCursor(self.pointer_cursor)
# return (i, path)
# return (-1, None)
# def move_selection(self, direction):
# if not self.renderer.notes or not self.renderer.selected_indices:
# return
# index = min(self.renderer.selected_indices) + direction
# if index < 0:
# index = 0
# elif index >= len(self.renderer.notes):
# index = len(self.renderer.notes)-1
# self.renderer.clear_note_selection()
# self.renderer.add_note_to_selection(index)
# self.redraw()
# if self.OnNoteSelectionChangedDesc:
# self.OnNoteSelectionChangedDesc(self.renderer.selected_indices)
def OnLeftButtonDown(self, event):
if event.LeftDown():
self.SetFocus()
page = self.current_page
old_selection = page.selected_indices.copy()
page.clear_note_selection()
x, y = self.get_xy_of_mouse_event(event)
note_index = page.hit_test(x, y)
if note_index is None:
close_note_index = page.hit_test(x, y, return_closest_hit=True)
else:
close_note_index = None
if note_index is not None:
page.add_note_to_selection(note_index)
else:
self.drag_start_x, self.drag_start_y = self.get_xy_of_mouse_event(event)
self.CaptureMouse()
self.OnMouseMotion(event)
if old_selection != page.selected_indices:
self.redraw()
#if old_selection != self.renderer.selected_indices or note_index is not None:
self.OnNoteSelectionChangedDesc(page.selected_indices, close_note_index=close_note_index)
## if event.LeftDown():
## self.SetFocus()
## old_selection = self.selected_note_path_indices.copy()
## self.selected_note_path_indices = set()
## self.selected_note_descriptions = set()
## i, path = self.get_path_under_mouse(event)
## if path:
## self.selected_note_path_indices.add(i)
## else:
## self.drag_start_x, self.drag_start_y = self.get_xy_of_mouse_event(event)
## self.CaptureMouse()
## self.OnMouseMotion(event)
## if old_selection != self.selected_note_path_indices:
## self.redraw()
## if self.OnNoteSelectionChanged:
## self.OnNoteSelectionChanged(sorted(self.selected_note_path_indices))
def OnLeftButtonUp(self, event):
if self.HasCapture():
try:
self.ReleaseMouse()
except:
pass
self.drag_start_x = None
self.drag_start_y = None
self.drag_rect = None
self.OnMouseMotion(event)
self.redraw()
def OnMouseMotion(self, event):
page = self.current_page
if self.HasCapture():
if self.drag_start_x is not None and self.drag_start_y is not None:
x, y = self.get_xy_of_mouse_event(event)
self.drag_rect = (min(self.drag_start_x, x), min(self.drag_start_y, y), abs(self.drag_start_x-x), abs(self.drag_start_y-y))
rect = wx.Rect(*map(int, self.drag_rect))
old_selection = page.selected_indices.copy()
page.select_notes(rect)
if old_selection != page.selected_indices and self.OnNoteSelectionChangedDesc:
self.OnNoteSelectionChangedDesc(page.selected_indices)
self.redraw()
else:
x, y = self.get_xy_of_mouse_event(event)
if page.hit_test(x, y) is not None:
self.SetCursor(self.pointer_cursor)
else:
self.SetCursor(self.cross_cursor)
## if self.HasCapture():
## x, y = self.get_xy_of_mouse_event(event)
## self.drag_rect = (min(self.drag_start_x, x), min(self.drag_start_y, y), abs(self.drag_start_x-x), abs(self.drag_start_y-y))
## #rect = wx.Rect2D(*self.drag_rect)
## #rect = wx.Rect(*map(lambda x: int(x*self.renderer.zoom), self.drag_rect))
## rect = wx.Rect(*map(lambda x: int(x), self.drag_rect))
## self.renderer.select_notes(rect)
## old_selection = self.selected_note_path_indices.copy()
## self.selected_note_path_indices = set()
## for i, path in enumerate(self.note_paths):
## path_box = path.GetBox()
## if rect.Intersects(path_box):
## self.selected_note_path_indices.add(i)
## self.redraw()
## if old_selection != self.selected_note_path_indices and self.OnNoteSelectionChanged:
## self.OnNoteSelectionChanged(sorted(self.selected_note_path_indices))
## else:
## i, path = self.get_path_under_mouse(event)
## if path:
## self.SetCursor(self.pointer_cursor)
## else:
## self.SetCursor(self.cross_cursor)
def OnSize(self, evt):
w, h = self.GetClientSize()
# 1.3.6.2 [JWdJ] 2015-02-14 prevent unneeded redraws
if w != self.renderer.min_width or h != self.renderer.min_height:
self.renderer.min_width = w
self.renderer.min_height = h
if self.current_page != self.renderer.empty_page:
self.renderer.update_buffer(self.current_page)
self.redraw()
def OnPaint(self, evt):
# The buffer already contains our drawing, so no need to
# do anything else but create the buffered DC. When this
# method exits and dc is collected then the buffer will be
# blitted to the paint DC automagically
##if wx.Platform == "__WXMSW__":
## dc = wx.BufferedPaintDC(self, self.renderer.buffer, wx.BUFFER_VIRTUAL_AREA)
##else:
dc = wx.PaintDC(self)
self.PrepareDC(dc)
if self.current_page != self.renderer.empty_page:
if self.need_redraw:
self.Draw()
self.need_redraw = False
dc.DrawBitmap(self.renderer.buffer, 0, 0)
if self.highlighted_notes:
self.renderer.draw_notes(page=self.current_page, note_indices=self.highlighted_notes, highlight=True, dc=dc)
else:
dc.SetBackground(wx.WHITE_BRUSH)
dc.Clear()
def set_page(self, page):
is_other_page = self.current_page and page and self.current_page.index != page.index
self.current_page = page
self.redraw()
if is_other_page:
self.Scroll(0, 0)
def clear(self):
self.current_page = self.renderer.empty_page
self.renderer.clear()
self.Refresh()
def redraw(self):
if self.redrawing:
return
page = self.current_page
if page is None:
return
z = self.renderer.zoom
w, h = int(page.svg_width * z), int(page.svg_height * z)
self.redrawing = True
try:
self.SetVirtualSize((w, h)) # triggers redraw again
self.note_paths = []
self.need_redraw = True
self.renderer.update_buffer(page)
# self.redraw_counter += 1
# print 'need_redraw %d' % self.redraw_counter
self.Refresh()
self.Update()
finally:
self.redrawing = False
def draw_drag_rect(self, dc):
if self.drag_rect:
dc = wx.GraphicsContext.Create(dc)
#z = self.renderer.zoom
#dc.Scale(z, z)
x, y, width, height = self.drag_rect
dc.SetPen( dc.CreatePen(wx.Pen(wx_colour('black'), 1.0, style=wx.DOT )) )
dc.SetBrush(dc.CreateBrush(wx.Brush(wx_colour('#fffbc6'), wx.SOLID)))
path = dc.CreatePath()
path.MoveToPoint(x, y)
path.AddLineToPoint(x+width, y)
path.AddLineToPoint(x+width, y+height)
path.AddLineToPoint(x, y+height)
path.AddLineToPoint(x, y)
dc.DrawPath(path)
def draw_notes_highlighted(self, note_indices):
self.highlighted_notes = note_indices
self.redrawing = True
try:
self.Refresh()
self.Update()
finally:
self.redrawing = False
self.highlighted_notes = None
def Draw(self):
dc = wx.BufferedDC(None, self.renderer.buffer)
if not WX4:
dc.BeginDrawing()
try:
dc.SetBackground(wx.WHITE_BRUSH)
dc.Clear()
self.draw_drag_rect(dc)
if self.current_page != self.renderer.empty_page:
self.renderer.draw(page=self.current_page, clear_background=False, dc=dc)
except Exception as e:
error_msg = traceback.format_exc()
print('Warning: ' + error_msg)
finally:
if not WX4:
dc.EndDrawing()