-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.py
3744 lines (3210 loc) · 156 KB
/
canvas.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-0
# Songwrite 2
# Copyright (C) 2007-2010 Jean-Baptiste LAMY -- [email protected]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later verson.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division
import os, os.path, sys, math, bisect, locale, codecs
from cStringIO import StringIO
import gobject, gtk, cairo, pango, pangocairo
import gtk.keysyms as keysyms
if gtk.pygtk_version < (2, 7, 0): import cairo.gtk
from editobj2.introsp import *
from editobj2.observe import *
from editobj2.undoredo import *
import editobj2.editor_gtk as editor_gtk
import songwrite2.globdef as globdef
import songwrite2.model as model
import songwrite2.stemml as stemml
import songwrite2.player as player
import songwrite2.__editobj2__
zoom_levels = [0.25, 0.35, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 4.0]
ZOOM_2_CLIP_DURATION = {
0.25 : 96,
0.35 : 96,
0.5 : 96,
0.75 : 48,
1.0 : 48,
1.5 : 24,
2.0 : 24,
3.0 : 12,
4.0 : 12,
}
SELECTION_COLOR = None
SELECTION_COLOR2 = None
CLIPBOARD = None
CLIPBOARD_SOURCE = None
CLIPBOARD_X1 = 0
CLIPBOARD_Y1 = 0
CLIPBOARD_X2 = 0
CLIPBOARD_Y2 = 0
CLIPBOARD2_NAME = "_SONGWRITE2_NOTES"
CLIPBOARD2 = gtk.Clipboard(selection = CLIPBOARD2_NAME)
def find_view_children(o):
if isinstance(o, model.View) and hasattr(o, "strings"):
return o.strings
return []
class BaseCanvas(object):
def __init__(self, song, zoom = 1.0):
self.x = 0
self.y = 0
self.width = 0
self.height = 0
self.song = song
self.zoom = zoom
self.selection_x1 = 1000000
self.selection_y1 = 1000000
self.selection_x2 = -1
self.selection_y2 = -1
self.cursor = None
self.cursor_drawer = None
self.canvas = None
self.drawers = None
self.partition_2_drawer = {}
self.selections = set()
self.scale_pango_2_cairo = 72.0 / pangocairo.cairo_font_map_get_default().get_resolution()
self.set_default_font_size(16.0)
def set_default_font_size(self, size):
self.default_font_size = size
self.scale = self.default_font_size / 14.6666666667
self.default_line_height = 0
self.start_x = int((editor_gtk.SMALL_ICON_SIZE + 42) * self.scale)
def reset(self):
for drawer in self.drawers: drawer.destroy()
self.drawers = []
self.update_mesure_size()
def text_extents_default(self, text):
self.default_layout.set_text(text)
return self.default_layout.get_pixel_size()
def draw_text_default(self, ctx, text, x, y):
ctx.move_to(x, y)
self.default_layout.set_text(text)
ctx.show_layout(self.default_layout)
def draw_text_at_size(self, ctx, text, x, y, font_size_factor):
self.default_layout.set_font_description(pango.FontDescription(u"Sans %s" % (self.default_font_size * self.scale_pango_2_cairo * font_size_factor)))
ctx.move_to(x, y)
self.default_layout.set_text(text)
ctx.show_layout(self.default_layout)
self.default_layout.set_font_description(self.default_pango_font)
def draw_text_max_width(self, ctx, text, x, y, max_width):
ctx.move_to(x, y)
self.default_layout.set_text(text)
width0, height0 = self.default_layout.get_pixel_size()
if width0 < max_width:
ctx.show_layout(self.default_layout)
else:
font_size = self.default_font_size * self.scale_pango_2_cairo
while font_size:
font_size -= 1
self.default_layout.set_font_description(pango.FontDescription(u"Sans %s" % font_size))
width, height = self.default_layout.get_pixel_size()
if width < max_width: break
ctx.rel_move_to(0.0, 0.7 * (height0 - height))
ctx.show_layout(self.default_layout)
self.default_layout.set_font_description(self.default_pango_font)
def note_string_id(self, note):
return self.partition_2_drawer[note.partition].note_string_id(note)
def y_2_drawer(self, y):
for drawer in self.drawers:
if y < drawer.y + drawer.height: return drawer
return None
def x_2_time(self, x):
x0 = x - self.start_x + self.x
if x0 <= 0: return 0.0
mesure = self.song.mesure_at(x0 / self.zoom)
if mesure is None:
mesure = self.song.mesures[-1]
if not self.mesure_2_extra_x.has_key(mesure): self.update_mesure_size()
mesure_extra_x = self.mesure_2_extra_x[mesure]
return (x0 - mesure_extra_x) / self.zoom
while 1:
if not self.mesure_2_extra_x.has_key(mesure): self.update_mesure_size()
mesure_extra_x = self.mesure_2_extra_x[mesure]
time = (x0 - mesure_extra_x) / self.zoom
mesure2 = self.song.mesure_at(time)
if mesure2.time >= mesure.time: return time
mesure = mesure2
def time_2_x(self, time):
mesure = self.song.mesure_at(time)
if not self.mesure_2_extra_x.has_key(mesure): self.update_mesure_size()
return self.start_x - self.x + self.mesure_2_extra_x[mesure] + time * self.zoom
def update_mesure_size(self):
self.mesure_2_extra_x = {}
extra_x = self.scale * 3.0
for mesure in self.song.mesures + [None]:
symbols = self.song.playlist.symbols.get(mesure)
if symbols:
for symbol in symbols:
if symbol.startswith(r"\repeat"): extra_x += self.scale * 15.0
elif symbol == r"} % end alternative": extra_x += self.scale * 15.0
elif symbol == r"} % end repeat": extra_x += self.scale * 15.0
self.mesure_2_extra_x[mesure] = extra_x
extra_x += self.scale * 5.0
self.need_update_selection = 1
class Canvas(gtk.Fixed, BaseCanvas):
is_gui_interface = 1
def __init__(self, main, song, scrolled, zoom = 1.0):
BaseCanvas.__init__(self, song, zoom)
gtk.Fixed.__init__(self)
self.set_has_window(1)
global SELECTION_COLOR
if SELECTION_COLOR is None:
global SELECTION_COLOR2
color = gtk.Entry().rc_get_style().bg[gtk.STATE_SELECTED]
SELECTION_COLOR2 = color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0
SELECTION_COLOR = [1.0 - 0.35 * (1.0 - x) for x in SELECTION_COLOR2]
color = gtk.Entry().rc_get_style().bg[gtk.STATE_PRELIGHT]
self.canvas = gtk.DrawingArea()
self.put(self.canvas, 0, 0)
self.main = main
self.song = song
self.hadjustment = scrolled.get_hadjustment()
self.vadjustment = scrolled.get_vadjustment()
self.last_selections = set()
self.drag_selections = set()
self.click_x = 0
self.click_y = 0
self.drag_start_x = 0
self.drag_start_y = 0
self.last_selection_x1 = 1000000
self.last_selection_y1 = 1000000
self.last_selection_x2 = -1
self.last_selection_y2 = -1
self.need_update_selection = 0
self.current_duration = 48
self.previous_text = "" # Previously typed text
self.edit_timeout = 0
self.first_time = 1
self.x_before_tracking = -1
self.touchscreen_new_note = None
self.show_render_zone = 0
if globdef.config.FONTSIZE:
self.set_default_font_size(globdef.config.FONTSIZE)
else:
self.set_default_font_size(self.get_style().font_desc.get_size() / pango.SCALE * pangocairo.cairo_font_map_get_default().get_resolution() / 72.0)
#self.set_default_font_size(32.0)
#print self.default_font_size
self.canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.KEY_PRESS_MASK | gtk.gdk.BUTTON1_MOTION_MASK)
self.canvas.set_flags(gtk.CAN_FOCUS)
self.canvas.connect('expose_event' , self.on_expose)
self.canvas.connect('key_press_event' , self.on_key_press)
self.canvas.connect('motion_notify_event' , self.on_mouse_motion)
self.canvas.connect('button_press_event' , self.on_button_press)
self.canvas.connect('button_release_event' , self.on_button_release)
self.connect('drag-drop' , self.on_drag_drop)
self.connect('drag-data-received' , self.on_drag_data_received)
self.connect('drag-data-get' , self.on_drag_data_get)
self.connect('drag-data-delete' , self.on_drag_data_delete)
self.connect('size-allocate' , self.on_resized)
self.connect('scroll-event' , self.on_before_scroll)
self.vadjustment.connect("value-changed", self.on_scroll)
self.hadjustment.connect("value-changed", self.on_scroll)
self.vadjustment.step_increment = self.hadjustment.step_increment = 10
observe(song , self.song_listener)
observe(song.playlist , self.playlist_listener)
observe(song.playlist.playlist_items, self.playlist_listener)
observe(song.mesures , self.mesures_listener)
for mesure in song.mesures:
observe(mesure, self.mesure_listener)
observe(song.partitions, self.partitions_listener)
for partition in song.partitions:
observe(partition , self.partition_listener)
if isinstance(partition, model.Partition):
observe_tree(partition.view , self.view_listener, find_view_children)
observe(partition.notes, self.notes_listener)
for note in partition.notes: observe(note, self.note_listener)
self.drag_source_set(gtk.gdk.BUTTON1_MASK, [(CLIPBOARD2_NAME, 0, 0)], gtk.gdk.ACTION_MOVE | gtk.gdk.ACTION_COPY)
self.drag_source_targets = self.drag_source_get_target_list()
self.drag_source_unset()
self.drag_dest_set(gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP, [(CLIPBOARD2_NAME, 0, 0)], gtk.gdk.ACTION_MOVE | gtk.gdk.ACTION_COPY)
self.connect("drag-motion", self.on_drag_motion)
self.context_menu_song = gtk.Menu()
menu_item = gtk.MenuItem(_(u"Song and instruments...")); menu_item.connect("activate", self.main.on_song_prop ); self.context_menu_song.append(menu_item)
self.context_menu_song.show_all()
self.context_menu_note = gtk.Menu()
menu_item = gtk.MenuItem(_(u"Edit note..." )); menu_item.connect("activate", self.main.on_note_prop ); self.context_menu_note.append(menu_item)
menu_item = gtk.MenuItem(_(u"Edit bar..." )); menu_item.connect("activate", self.main.on_bars_prop ); self.context_menu_note.append(menu_item)
menu_item = gtk.MenuItem(_(u"Edit instrument...")); menu_item.connect("activate", self.main.on_instrument_prop); self.context_menu_note.append(menu_item)
self.context_menu_note.show_all()
self.update_mesure_size()
def config_listener(self, obj, type, new, old):
for drawer in self.drawers:
drawer.config_listener(obj, type, new, old)
if type is object:
if (new.get("FONTSIZE") != old.get("FONTSIZE")):
if globdef.config.FONTSIZE:
self.set_default_font_size(globdef.config.FONTSIZE)
else:
self.set_default_font_size(self.get_style().font_desc.get_size() / pango.SCALE * pangocairo.cairo_font_map_get_default().get_resolution() / 72.0)
self.reset()
self.need_update_selection = 1
self.render_all()
def on_drag_motion(self, widget, drag, x, y, timestamp):
if drag.actions & gtk.gdk.ACTION_MOVE: drag.drag_status(gtk.gdk.ACTION_MOVE, timestamp)
else: drag.drag_status(gtk.gdk.ACTION_COPY, timestamp)
return 1
def play_tracker(self, time):
if time == -1:
self.render_selection()
self.selection_x1 = 1000000
self.selection_y1 = 1000000
self.selection_x2 = -1
self.selection_y2 = -1
for note in self.selections:
drawer = self.partition_2_drawer[note.partition]
x1, y1, x2, y2 = drawer.note_dimensions(note)
x1 += self.x
y1 += self.y
x2 += self.x
y2 += self.y
if x1 < self.selection_x1: self.selection_x1 = x1
if y1 < self.selection_y1: self.selection_y1 = y1
if x2 > self.selection_x2: self.selection_x2 = x2
if y2 > self.selection_y2: self.selection_y2 = y2
self.render_selection()
self.x_before_tracking = -1
else:
if self.x_before_tracking == -1: self.x_before_tracking = max(0, self.x)
self.render_selection()
self.selection_x1 = self.time_2_x(time) + self.x
self.selection_y1 = self.drawers[1].y + self.y
self.selection_x2 = self.selection_x1 + 2 * self.char_h_size
self.selection_y2 = self.drawers[-1].y + self.drawers[-1].height + self.y
self.render_selection()
if self.hadjustment.upper - self.hadjustment.lower > self.hadjustment.page_size:
if self.selection_x2 > self.x + self.width * 0.75:
self.hadjustment.value = min(self.hadjustment.upper, self.selection_x2 - self.width * 0.1)
elif self.selection_x1 < self.x:
self.hadjustment.value = max(self.hadjustment.lower, self.selection_x2 - self.width * 0.1)
def destroy(self):
if self.drawers:
for drawer in self.drawers: drawer.destroy()
unobserve(self.cursor)
unobserve(self.song , self.song_listener)
unobserve(self.song.playlist , self.playlist_listener)
unobserve(self.song.playlist.playlist_items, self.playlist_listener)
unobserve(self.song.mesures , self.mesures_listener)
unobserve(self.song.partitions , self.partitions_listener)
for mesure in self.song.mesures:
unobserve(mesure, self.mesure_listener)
for partition in self.song.partitions:
unobserve(partition, self.partition_listener)
if isinstance(partition, model.Partition):
unobserve_tree(partition.view, self.view_listener, find_view_children)
unobserve(partition.notes, self.notes_listener)
for note in partition.notes:
unobserve(note, self.note_listener)
gtk.Fixed.destroy(self)
def update_time(self):
for drawer in self.drawers:
if isinstance(drawer, LyricsDrawer): drawer.update_melody()
def get_selected_time(self):
if self.selections : return list(self.selections )[0].time
if self.last_selections: return list(self.last_selections)[0].time
return 0
def get_selected_mesures(self):
mesures = list(set([self.song.mesure_at(note.time) for note in self.selections]))
mesures.sort() # Sort and reverse => if the user reduces the length of several mesures,
mesures.reverse() # the change is first applied at the last mesure;
# because if new mesures are created, there are created by cloning the last one.
return mesures
return list(set([self.song.mesure_at(note.time) for note in self.selections]))
def get_selected_partitions(self):
return list(set([note.partition for note in self.selections]))
def song_listener(self, song, type, new, old):
self.main.set_title(u"Songwrite2 -- %s" % song.title)
self.render_all()
def playlist_listener(self, playlist, type, new, old):
self.update_mesure_size()
self.render_all()
def mesures_listener(self, obj, type, new, old):
new = set(new)
old = set(old)
for mesure in new - old: observe (mesure, self.mesure_listener)
for mesure in old - new: unobserve(mesure, self.mesure_listener)
self.update_mesure_size()
def mesure_listener(self, mesure, type, new, old):
self.update_mesure_size()
self.render_all()
def partitions_listener(self, obj, type, new, old):
new = set(new)
old = set(old)
for partition in new - old:
observe (partition, self.partition_listener)
if isinstance(partition, model.Partition):
observe_tree(partition.view , self.view_listener, find_view_children)
observe(partition.notes, self.notes_listener)
for note in partition.notes: observe(note, self.note_listener)
for partition in old - new:
unobserve(partition, self.partition_listener)
if isinstance(partition, model.Partition):
unobserve_tree(partition.view , self.view_listener, find_view_children)
unobserve(partition.notes, self.notes_listener)
for note in partition.notes: unobserve(note, self.note_listener)
self.update_melody_lyrics()
for drawer in self.drawers: drawer.drawers_changed()
self.deselect_all()
self.reset()
self.render_all()
def update_melody_lyrics(self):
current_melody = None
for drawer in self.drawers:
if isinstance(drawer, LyricsDrawer):
if current_melody: current_melody.associated_lyrics.append(drawer)
elif isinstance(drawer, PartitionDrawer):
drawer.associated_lyrics = []
if drawer.partition.view.ok_for_lyrics:
current_melody = drawer
def partition_listener(self, partition, type, new, old):
if type is object:
if (new.get("view") != old.get("view")):
unobserve_tree(old.get("view"), self.view_listener, find_view_children)
observe_tree (new.get("view"), self.view_listener, find_view_children)
self.deselect_all()
self.reset()
self.render_all()
if (new.get("g8") != old.get("g8")) or (new.get("tonality") != old.get("tonality")):
self.deselect_all()
self.reset()
self.render_all()
elif (new.get("instrument") != old.get("instrument")) or (new.get("header") != old.get("header")) or (new.get("visible") != old.get("visible")):
self.render_all()
self.partition_2_drawer[partition].partition_listener(partition, type, new, old)
def view_listener(self, view, type, new, old):
self.render_all()
def strings_listener(self, view, type, new, old):
self.render_all()
def notes_listener(self, obj, type, new, old):
new = set(new)
old = set(old)
for note in new - old:
if not isobserved(note, self.note_listener): # Can be already observed if note was the cursor before
observe (note, self.note_listener)
for note in old - new: unobserve(note, self.note_listener)
drawer = self.partition_2_drawer.get(tuple(new or old)[0].partition)
if drawer:
drawer.notes_listener(obj, type, new, old)
def note_listener(self, note, type, new, old):
#print note, type, new, old
if note in self.selections:
self.need_update_selection = 1
if (note is self.cursor) and (note.value > 0):
self.cursor = None
self.render_selection()
self.main.set_selected_note(self.main.selected_note)
drawer = self.partition_2_drawer.get(note.partition)
if drawer:
drawer.note_listener(note, type, new, old)
drawer.render_note(note)
def set_cursor(self, cursor, cursor_drawer):
self.cursor = cursor
self.cursor_drawer = cursor_drawer
def set_zoom(self, zoom):
self.selection_x1 = (self.selection_x1 - self.start_x) * zoom / self.zoom + self.start_x
self.selection_x2 = (self.selection_x2 - self.start_x) * zoom / self.zoom + self.start_x
self.hadjustment.value = self.hadjustment.value / self.zoom * zoom
self.need_update_selection = 1
self.zoom = zoom
self.render_pixel(0, 0, self.width, self.height)
self.update_time()
self.main.zoom_menu.get_child().set_text(u"%s%%" % int(zoom * 100.0))
def delayed_edit(self, note):
if self.edit_timeout: gobject.source_remove(self.edit_timeout)
self.edit_timeout = gobject.timeout_add(40, self.edit, note)
def edit(self, note):
mesures = self.get_selected_mesures()
if len(mesures) == 1: mesure = mesures[0]
else: mesure = ObjectPack(mesures)
partitions = self.get_selected_partitions()
if partitions:
if len(partitions) == 1:
partition = partitions[0]
else:
partition = ObjectPack(partitions)
partition.song = ObjectPack([p.song for p in partitions])
self.main.set_selected_partition(partition)
self.main.set_selected_mesure(mesure)
self.main.set_selected_note(note)
if getattr(self.main, "notebook", None):
if partitions: self.main.notebook.edit(-3, partition)
self.main.notebook.edit(-2, mesure)
self.main.notebook.edit(-1, note)
# else:
# self.main.set_title(u"Songwrite2 -- %s -- %s -- %s" % (self.song.title, self.song.mesure_at(note), description(note.__class__).label_for(note)))
def arrange_selection_at_fret(self, fret):
old_string_ids = dict([(note, note.string_id) for note in self.selections if hasattr(note, "string_id")])
def do_it():
for note, string_id in old_string_ids.iteritems():
if isinstance(note.partition.view, model.TablatureView):
strings = note.partition.view.strings
if note.value - strings[string_id].base_note - note.partition.capo < fret:
while (string_id < len(strings) - 1) and (note.value - strings[string_id].base_note - note.partition.capo < fret):
string_id += 1
else:
while (string_id > 0) and (note.value - strings[string_id - 1].base_note - note.partition.capo >= fret):
string_id -= 1
note.string_id = string_id
self.render_all()
def undo_it():
for note, string_id in old_string_ids.iteritems():
note.string_id = string_id
self.render_all()
UndoableOperation(do_it, undo_it, _(u"arrange at fret #%s") % fret, self.main.undo_stack)
def select_all(self):
self.deselect_all()
for partition in self.song.partitions:
if isinstance(partition, model.Partition):
drawer = self.partition_2_drawer[partition]
for note in partition.notes:
self.add_selection(note, render = 0, edit = 1, *drawer.note_dimensions(note))
self.render_selection()
def deselect_all(self):
if self.cursor: self.cursor_drawer.destroy_cursor()
if self.selection_x2 != -1:
self.render_selection()
self.selection_x1 = 1000000
self.selection_y1 = 1000000
self.selection_x2 = -1
self.selection_y2 = -1
self.selections = set()
self.cursor = None
self.previous_text = ""
self.touchscreen_new_note = None
def extend_selection(self, x1, y1, x2, y2, note = None):
if self.selection_x1 > x1:
self.selection_x1 = x1
if note and (note.time == 0):
self.hadjustment.value = 0
else:
if x1 < self.x + self.start_x: self.hadjustment.value = x1 - self.start_x
if self.selection_y1 > y1:
self.selection_y1 = y1
if y1 < self.y: self.vadjustment.value = y1
if self.selection_x2 < x2:
self.selection_x2 = x2
if x2 > self.x + self.width: self.hadjustment.value = x2 - self.width
if self.selection_y2 < y2:
self.selection_y2 = y2
if y2 > self.y + self.height: self.vadjustment.value = y2 - self.height
def add_selection(self, note, x1, y1, x2, y2, render = 1, edit = 1):
self.selections.add(note)
self.extend_selection(x1 + self.x, y1 + self.y, x2 + self.x, y2 + self.y, note)
if not note is self.cursor:
global CLIPBOARD, CLIPBOARD_SOURCE, CLIPBOARD_X1, CLIPBOARD_Y1, CLIPBOARD_X2, CLIPBOARD_Y2
CLIPBOARD_SOURCE = self
CLIPBOARD = self.last_selections = self.selections
CLIPBOARD_X1 = self.last_selection_x1 = self.selection_x1
CLIPBOARD_Y1 = self.last_selection_y1 = self.selection_y1
CLIPBOARD_X2 = self.last_selection_x2 = self.selection_x2
CLIPBOARD_Y2 = self.last_selection_y2 = self.selection_y2
CLIPBOARD2.set_with_data([(CLIPBOARD2_NAME, 0, 0)], self.clipboard_get, self.clipboard_clear, None)
if render: self.render_selection()
if edit : self.delayed_edit(note)
def clipboard_clear(self, clipboard, userdata): pass
def clipboard_get(self, clipboard, selection_data, info, userdata):
_xml = StringIO()
xml = codecs.lookup("utf8")[3](_xml)
context = model._XMLContext()
xml.write("""<?xml version="1.0" encoding="utf-8"?>\n<notes click_x="0">""")
min_y = 10000.0
for note in CLIPBOARD:
drawer = self.partition_2_drawer[note.partition]
x1, y1, x2, y2 = drawer.note_dimensions(note)
y = (y1 + y2) // 2
if y < min_y: min_y = y
for note in CLIPBOARD:
drawer = self.partition_2_drawer[note.partition]
x1, y1, x2, y2 = drawer.note_dimensions(note)
nb_char = len(drawer.note_text(note))
x = x1 + (self.char_h_size * nb_char) // 2 - CLIPBOARD_X1
y = (y1 + y2) // 2 - min_y
label = drawer.strings[note.partition.view.note_string_id(note)].value_2_text(note)
note.__xml__(xml, context, ' x="%r" y="%r" label="%s" view_type="%s"' % (x, y, label, note.partition.view.__class__.__name__[:-4].lower()))
xml.write("</notes>")
selection_data.set(selection_data.target, 8, xml.getvalue())
def on_button_press(self, obj, event):
self.canvas.grab_focus()
self.click_x = event.x + self.x
self.click_y = event.y + self.y
if event.button == 1:
if self.selections and (self.selection_x1 <= self.click_x <= self.selection_x2) and (self.selection_y1 <= self.click_y <= self.selection_y2):
if self.cursor in self.selections:
drawer = self.partition_2_drawer[list(self.selections)[0].partition]
if drawer.on_touchscreen_new_note(event):
self.touchscreen_new_note = tuple(self.selections)[0]
self.touchscreen_new_note_y0 = event.y
self.touchscreen_new_note_value0 = self.touchscreen_new_note.value
else:
self.deselect_all()
if event.x > self.start_x:
for drawer in self.drawers:
if drawer.y <= event.y <= drawer.y + drawer.height:
drawer.on_button_press(event)
break
elif event.type == gtk.gdk._2BUTTON_PRESS:
for drawer in self.drawers:
if drawer.y <= event.y <= drawer.y + drawer.height:
self.main.set_selected_partition(getattr(drawer, "partition", None) or getattr(drawer, "lyrics"))
self.main.on_instrument_prop()
break
elif event.button == 2:
if CLIPBOARD:
drag = CLIPBOARD_SOURCE.drag_begin(self.drag_source_targets, gtk.gdk.ACTION_COPY, 2, event)
CLIPBOARD_SOURCE.drag_start_x = self.drag_start_x = CLIPBOARD_X1 + 20
CLIPBOARD_SOURCE.drag_start_y = self.drag_start_y = CLIPBOARD_Y1 + 20
CLIPBOARD_SOURCE.drag_selections = set(CLIPBOARD)
width = int(CLIPBOARD_X2 - CLIPBOARD_X1) + 2
height = int(CLIPBOARD_Y2 - CLIPBOARD_Y1) + 2
pixmap = gtk.gdk.Pixmap(self.window, width, height)
ctx = pixmap.cairo_create()
CLIPBOARD_SOURCE.draw_drag_icon(ctx, CLIPBOARD_X1 - 1, CLIPBOARD_Y1 - 1, width, height)
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 1, 8, max(width, 100), height) # It seems that GTK doesn't like small drag icons !
pixbuf.fill(0x00000000)
pixbuf.get_from_drawable(pixmap, self.window.get_colormap(), 0, 0, 0, 0, width, height)
pixbuf = pixbuf.add_alpha(1, chr(255), chr(255), chr(255))
drag.set_icon_pixbuf(pixbuf, 20, 20)
elif event.button == 3:
#self.deselect_all()
if event.x > self.start_x:
for drawer in self.drawers:
if drawer.y <= event.y <= drawer.y + drawer.height: drawer.on_button_press(event)
def on_button_release(self, obj, event):
if (not self.selections) and (self.selection_x2 != -1):
time1 = self.x_2_time(self.selection_x1 - self.x)
time2 = self.x_2_time(self.selection_x2 - self.x)
y1 = self.selection_y1 - self.y
y2 = self.selection_y2 - self.y
self.deselect_all()
for partition in self.song.partitions:
if not isinstance(partition, model.Partition): continue
drawer = self.partition_2_drawer[partition]
string_id1 = drawer.y_2_string_id(y1 + drawer.string_height // 2, 1)
string_id2 = drawer.y_2_string_id(y2 - drawer.string_height // 2, 1)
for note in partition.notes_at(time1, time2):
if string_id1 <= drawer.note_string_id(note) <= string_id2:
self.add_selection(note, render = 0, edit = 0, *drawer.note_dimensions(note))
if self.selections:
self.render_selection()
if len(self.selections) == 1:
self.delayed_edit(tuple(self.selections)[0])
else:
pack = ObjectPack(list(self.selections))
self.delayed_edit(pack)
def on_mouse_motion(self, obj, event):
if self.touchscreen_new_note:
drawer = self.partition_2_drawer[self.touchscreen_new_note.partition]
value = self.touchscreen_new_note_value0
delta = int((self.touchscreen_new_note_y0 - event.y) // (20.0 * self.scale))
value = drawer.mouse_motion_note(value, delta)
while not drawer.note_value_possible(self.touchscreen_new_note, value):
if delta >= 0: value += 1
else: value -= 1
if self.touchscreen_new_note.value != value:
self.touchscreen_new_note.value = value
scan()
elif self.drag_check_threshold(int(self.click_x - self.x), int(self.click_y - self.y), int(event.x), int(event.y)):
if self.selections and (self.selection_x1 <= self.click_x <= self.selection_x2) and (self.selection_y1 <= self.click_y <= self.selection_y2) and not self.cursor:
drag = self.drag_begin(self.drag_source_targets, gtk.gdk.ACTION_MOVE | gtk.gdk.ACTION_COPY, 1, event)
self.drag_start_x = self.click_x
self.drag_start_y = self.click_y
self.drag_selections = set(self.last_selections)
width = int(self.selection_x2 - self.selection_x1) + 2
height = int(self.selection_y2 - self.selection_y1) + 2
pixmap = gtk.gdk.Pixmap(self.window, width, height)
ctx = pixmap.cairo_create()
self.draw_drag_icon(ctx, self.selection_x1 - 1, self.selection_y1 - 1, width, height)
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 1, 8, max(width, 100), height) # It seems that GTK doesn't like small drag icons !
pixbuf.fill(0x00000000)
pixbuf.get_from_drawable(pixmap, self.window.get_colormap(), 0, 0, 0, 0, width, height)
pixbuf = pixbuf.add_alpha(1, chr(255), chr(255), chr(255))
drag.set_icon_pixbuf(pixbuf, int(self.click_x - self.selection_x1), int(self.click_y - self.selection_y1))
else:
if self.selections: self.deselect_all()
old_sel_x1 = self.selection_x1
old_sel_y1 = self.selection_y1
old_sel_x2 = self.selection_x2
old_sel_y2 = self.selection_y2
if event.x <= self.start_x: self.hadjustment.value = max(0, self.hadjustment.value - 10)
elif event.x >= self.width : self.hadjustment.value = min(max(self.hadjustment.upper - self.width, 0), self.hadjustment.value + 10)
if event.y <= 0 : self.vadjustment.value = max(0, self.vadjustment.value - 10)
elif event.y >= self.height : self.vadjustment.value = min(max(self.vadjustment.upper - self.height, 0), self.vadjustment.value + 10)
self.selection_x1 = max(min(self.click_x, event.x + self.x), self.start_x)
self.selection_y1 = min(self.click_y, event.y + self.y)
self.selection_x2 = max(self.click_x, event.x + self.x)
self.selection_y2 = max(self.click_y, event.y + self.y)
self.render(min(old_sel_x1, self.selection_x1) - 1,
min(old_sel_y1, self.selection_y1) - 1,
max(old_sel_x2, self.selection_x2) - min(old_sel_x1, self.selection_x1) + 2,
max(old_sel_y2, self.selection_y2) - min(old_sel_y1, self.selection_y1) + 2)
def on_drag_drop(self, widget, drag_context, x, y, timestamp): return True
def on_drag_data_get(self, widget, drag_context, selection_data, info, timestamp):
_xml = StringIO()
xml = codecs.lookup("utf8")[3](_xml)
context = model._XMLContext()
xml.write("""<?xml version="1.0" encoding="utf-8"?>\n<notes click_x="%s">\n""" % (self.drag_start_x - self.selection_x1))
for note in sorted(self.drag_selections):
drawer = self.partition_2_drawer[note.partition]
x1, y1, x2, y2 = drawer.note_dimensions(note)
nb_char = len(drawer.note_text(note))
x = x1 + (self.char_h_size * nb_char) // 2 - self.drag_start_x
y = (y1 + y2) // 2 - self.drag_start_y + self.y
label = drawer.strings[note.partition.view.note_string_id(note)].value_2_text(note)
note.__xml__(xml, context, ' x="%r" y="%r" label="%s" view_type="%s"' % (x, y, label, note.partition.view.__class__.__name__[:-4].lower()))
xml.write("</notes>")
selection_data.set(selection_data.target, 8, xml.getvalue())
def on_drag_data_received(self, widget, drag_context, x0, y0, selection_data, info, timestamp):
xml = selection_data.data
print xml
from cStringIO import StringIO
click_x, notes = stemml.parse(StringIO(xml))
orig_time = click_x / self.zoom
dest_time = self.x_2_time(x0)
self.paste_notes(notes, dest_time, orig_time, y0)
def on_note_paste(self):
xml = CLIPBOARD2.wait_for_contents(CLIPBOARD2_NAME).data
from cStringIO import StringIO
click_x, notes = stemml.parse(StringIO(xml))
if not self.selections: return
note = tuple(self.selections)[0]
drawer = self.partition_2_drawer[note.partition]
y0 = drawer.string_id_2_y(drawer.note_string_id(note))
dest_time = note.time
orig_time = 0
self.paste_notes(notes, dest_time, orig_time, y0)
def paste_notes(self, notes, dest_time, orig_time, y0):
orig_time += min([note.time for note in notes])
clip_duration = ZOOM_2_CLIP_DURATION[self.zoom]
if clip_duration == 96:
if (self.song.mesure_at(dest_time) or self.song.mesures[-1]).rythm2 == 8: clip_duration /= 2
if clip_duration == 144:
if (self.song.mesure_at(dest_time) or self.song.mesures[-1]).rythm2 == 4: clip_duration /= 3
else:
if clip_duration / 1.5 in model.DURATIONS.keys(): clip_duration /= 3
dt = int(0.4 + float(abs(dest_time - orig_time)) / clip_duration) * clip_duration
if dest_time < orig_time: dt = -dt
notes_data = []
previous_notes = {}
for note in notes:
y = y0 + note.y
drawer = self.y_2_drawer(y)
if not isinstance(drawer, PartitionDrawer): continue
time = note.time + dt
if time < 0: continue
if (note.view_type == drawer.partition.view.__class__.__name__[:-4].lower()) and drawer.partition.view.can_paste_note_by_string:
# Paste by string
string_id = drawer.y_2_string_id(y)
if string_id is None: continue
paste_by_string = 1
else:
# Paste by note
string_id = drawer.partition.view.note_string_id(note)
paste_by_string = 0
notes_data.append((note, drawer, time, string_id, paste_by_string))
for previous_note in drawer.partition.notes_at(time):
if string_id == drawer.note_string_id(previous_note):
drawer_previous_notes = previous_notes.get(drawer)
if not drawer_previous_notes: drawer_previous_notes = previous_notes[drawer] = []
drawer_previous_notes.append(previous_note)
break
saved_duration = {}
def do_it(notes_data = notes_data):
for drawer, notes in previous_notes.items():
drawer.partition.remove(*notes)
new_notes = {}
for note, drawer, time, string_id, paste_by_string in notes_data:
new_note = model.Note(drawer.partition, time, note.duration, note.value, note.volume)
new_note.fx = note.fx
new_note.link_fx = note.link_fx
new_note.duration_fx = note.duration_fx
new_note.strum_dir_fx = note.strum_dir_fx
if note.bend_pitch: new_note.bend_pitch = note.bend_pitch
for attr in model.NOTE_ATTRS:
value = getattr(note, attr)
if not value is None: setattr(new_note, attr, value)
if not drawer.partition.view.automatic_string_id:
new_note.string_id = string_id
if paste_by_string:
#new_note.value = note.value - note.string_pitch + drawer.strings[string_id].base_note
note.partition = drawer.partition # required for text_2_value
new_note.value = drawer.strings[string_id].text_2_value(new_note, note.label)
drawer_new_notes = new_notes.get(drawer)
if not drawer_new_notes: drawer_new_notes = new_notes[drawer] = []
drawer_new_notes.append(new_note)
self.deselect_all()
for drawer, notes in new_notes.items():
drawer.partition.add_note(*notes)
for note in notes:
self.add_selection(note, render = 0, edit = 0, *drawer.note_dimensions(note))
self.auto_update_duration(min(notes), saved_duration)
self.auto_update_duration(drawer.partition.note_after(max(notes)))
self.render_all()
def undo_it(notes = notes):
new_notes = {}
for note, drawer, time, string_id, paste_by_string in notes_data:
drawer_new_notes = new_notes.get(drawer)
if not drawer_new_notes: drawer_new_notes = new_notes[drawer] = []
for new_note in drawer.partition.notes_at(time):
if paste_by_string:
if string_id == drawer.note_string_id(new_note):
drawer_new_notes.append(new_note)
break
else:
if new_note.value == note.value:
drawer_new_notes.append(new_note)
break
for drawer, notes in new_notes.items():
drawer.partition.remove(*notes)
for drawer, notes in previous_notes.items():
drawer.partition.add_note(*notes)
self.restore_saved_duration(saved_duration)
self.render_all()
UndoableOperation(do_it, undo_it, _(u"add %s note(s)") % len(notes), self.main.undo_stack)
def on_drag_data_delete(self, widget, drag_context):
# Some notes int the selection may have been already deleted, if the selection was pasted partly
# over the selection itself.
notes = [note for note in self.drag_selections if note.partition and note in note.partition.notes]
self.delete_notes(notes)
def round_time_to_current_duration(self, time, delta = 0):
#duration = min(96, self.current_duration)
duration = min(ZOOM_2_CLIP_DURATION[self.zoom], self.current_duration)
if duration == 96:
if (self.song.mesure_at(time) or self.song.mesures[-1]).rythm2 == 8: duration /= 2
if duration == 144:
if (self.song.mesure_at(time) or self.song.mesures[-1]).rythm2 == 4: duration /= 3
else:
if duration / 1.5 in model.DURATIONS.keys(): duration /= 3
return max(0, int((delta + (time // duration)) * duration))
def on_key_press(self, obj, event):
#print dir(event), event.keyval
keyval = event.keyval
if keyval == keysyms.Left:
if self.selections:
sel = tuple(self.selections)[0]
time = sel.time
string_id = self.note_string_id(sel)
note = sel.partition.note_before_pred(sel, lambda a: self.note_string_id(a) == string_id)
time0 = self.round_time_to_current_duration(time, -1)
#time0 = ((time - 1) // self.current_duration) * self.current_duration
if note and note.time >= time0:
self.deselect_all()
self.partition_2_drawer[sel.partition].select_note(note)
elif time > 0:
self.deselect_all()
self.partition_2_drawer[sel.partition].select_at(time0, string_id)
elif keyval == keysyms.Right:
if self.selections:
sel = tuple(self.selections)[0]
time = sel.time
string_id = self.note_string_id(sel)
note = sel.partition.note_after_pred(sel, lambda a: self.note_string_id(a) == string_id)
time0 = self.round_time_to_current_duration(time, 1)
if note and note.time <= time0:
self.deselect_all()
self.partition_2_drawer[sel.partition].select_note(note)
else:
self.deselect_all()
self.partition_2_drawer[sel.partition].select_at(time0, string_id)
elif keyval == keysyms.Up:
if self.selections:
sel = tuple(self.selections)[0]
string_id = self.note_string_id(sel)
if string_id > 0:
self.deselect_all()
self.partition_2_drawer[sel.partition].select_at(sel.time, string_id - 1)
else:
i = self.drawers.index(self.partition_2_drawer[sel.partition])
if i > 0:
drawer = self.drawers[i - 1]
if isinstance(drawer, PartitionDrawer):
self.deselect_all()
drawer.select_at(sel.time, len(drawer.strings) - 1)
elif keyval == keysyms.Down:
if self.selections:
sel = tuple(self.selections)[0]
string_id = self.note_string_id(sel)
if string_id < len(self.partition_2_drawer[sel.partition].strings) - 1:
self.deselect_all()
self.partition_2_drawer[sel.partition].select_at(sel.time, string_id + 1)
else:
i = self.drawers.index(self.partition_2_drawer[sel.partition])
if i < len(self.drawers) - 1:
drawer = self.drawers[i + 1]
if isinstance(drawer, PartitionDrawer):
self.deselect_all()
drawer.select_at(sel.time, 0)