File: drawer.py

package info (click to toggle)
songwrite 3-0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,584 kB
  • sloc: python: 9,162; xml: 1,369; makefile: 9
file content (228 lines) | stat: -rw-r--r-- 9,131 bytes parent folder | download
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
# -*- coding: utf-8 -*-

# Songwrite 3
# Copyright (C) 2008 Jean-Baptiste LAMY -- jibalamy@free.fr
#
# 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 version.
#
# 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/>.

import PyQt5.QtCore    as qtcore
import PyQt5.QtWidgets as qtwidgets
import PyQt5.QtGui     as qtgui

import editobj3

import songwrite3.canvas as canvas
from songwrite3.canvas import ctx_drawText, ctx_drawText2, ctx_drawLine, ctx_drawRect, ctx_drawEllipse, ctx_drawPixmap


class FingeringDrawer(canvas.PartitionDrawer):
  def __init__(self, canvas_, partition, compact, String):
    canvas.PartitionDrawer.__init__(self, canvas_, partition, compact)
    self.height = 300
    self.strings = [String(self)]
    self.string_height = self.canvas.default_line_height * 0.3
    for hole in next(iter(partition.view.fingerings.values())):
      if hole is None: self.string_height += self.canvas.default_line_height * 0.3
      else:            self.string_height += self.canvas.default_line_height * 0.6
    self.stem_extra_height = self.canvas.default_line_height
    
    self.lh2  = self.canvas.default_line_height / 2
    self.lh3  = self.canvas.default_line_height / 3
    self.lh4  = self.canvas.default_line_height / 4.5
    self.lh7  = self.canvas.default_line_height / 7
    self.lh_6 = self.canvas.default_line_height * 0.6

  def partition_listener(self, partition, type, new, old):
    if type is object:
      if (new.get("instrument_tonality") != old.get("instrument_tonality")):
        self.canvas.render_all()
        
  def note_width(self, note): return 9.0 * self.scale
  
  def note_string_id(self, note): return 0 # No "string" notion in fingering notation!
  def y_2_string_id(self, y, force = 0):
    if force:
      if   self.y + self.start_y > y: return -1
      elif y > self.y + self.height:  return  1
      return 0
    else:
      if self.y + self.start_y <= y <= self.y + self.height: return 0
      return None
  def string_id_2_y(self, string_id = 0):
    return self.y + self.start_y + self.stem_extra_height + self.string_height // 2
  def draw_mesure(self, ctx, time, mesure, y, height):
    canvas.PartitionDrawer.draw_mesure(self, ctx, time, mesure, self.y + self.start_y + self.stem_extra_height, self.string_height)
  def draw_strings(self, ctx, x, y, width, height):
    self.height = int(self.start_y + self.stem_extra_height + self.string_height + self.stem_extra_height_bottom + 1)
    
  def note_text(self, note): return "_"
  
  def note_dimensions(self, note):
    x = self.canvas.time_2_x(note.time)
    y = self.string_id_2_y()
    return (
      x + 3.0 * self.scale,
      y - self.string_height // 2,
      x + max(note.duration * self.canvas.zoom, self.canvas.char_h_size * 1.9),
      y + self.string_height // 2,
      )
    
  def draw_stem_and_beam(self, ctx, x, note, y1, y2, mesure = None, previous_time = -32000, next_time = 32000):
    x = x + self.canvas.default_line_height // 2
    canvas.PartitionDrawer.draw_stem_and_beam(self, ctx, x, note, y1, y2, mesure, previous_time, next_time)
    
  def draw_note(self, ctx, note, string_id, y):
    color = self.note_color(note)
    ctx.setPen(color)
    ctx.setBrush(color)
    
    x = self.canvas.time_2_x(note.time)
    y -= self.string_height // 2
    
    if note.is_dotted():
      ctx_drawEllipse(ctx,
        x + self.canvas.default_line_height // 2 + 2.5 * self.scale,
        y - 4.5 * self.scale,
        1.5 * self.scale,
        1.5 * self.scale,
      )
      
    fingering = self.partition.view.note_2_fingering(note)
    if fingering:
      holes  = fingering[:-1]
      breath = fingering[-1]
    else:
      holes = []
      breath = "?"

    for hole in holes:
      if   hole is None:
        y += self.lh3
        continue
      elif hole == 1:
        ctx_drawEllipse(ctx, x + self.lh2 - self.lh4, y + self.lh3 - self.lh4, self.lh4 * 2.0 + 0.5, self.lh4 * 2.0 + 0.5)
      elif hole == 0:
        ctx.setBrush(qtcore.Qt.transparent)
        ctx_drawEllipse(ctx, x + self.lh2 - self.lh4, y + self.lh3 - self.lh4, self.lh4 * 2.0 + 0.5, self.lh4 * 2.0 + 0.5)
        ctx.setBrush(color)
      elif hole == 0.5:
        ctx.setBrush(qtcore.Qt.transparent)
        ctx_drawEllipse(ctx, x + self.lh2 - self.lh4, y + self.lh3 - self.lh4, self.lh4 * 2.0 + 0.5, self.lh4 * 2.0 + 0.5)
        ctx.setBrush(color)
        ctx.drawChord  (qtcore.QRectF(x + self.lh2 - self.lh4, y + self.lh3 - self.lh4, self.lh4 * 2.0 + 0.5, self.lh4 * 2.0 + 0.5), 0, 180 * 16)
        
      else:
        x2 = x + self.lh2 - self.lh3 + self.lh7
        for sub_hole in hole:
          if   sub_hole == 1:
            ctx_drawEllipse(ctx, x2 - self.lh7, y + self.lh3 - self.lh7, self.lh7 * 2.0, self.lh7 * 2.0)
          elif sub_hole == 0:
            ctx.setBrush(qtcore.Qt.transparent)
            ctx_drawEllipse(ctx, x2 - self.lh7, y + self.lh3 - self.lh7, self.lh7 * 2.0, self.lh7 * 2.0)
            ctx.setBrush(color)
          x2 += self.lh3
      y += self.lh_6


    if breath:
      ctx_drawText(ctx,
                   x + self.canvas.default_line_height * 0.2,
                   y + self.canvas.default_line_height * 0.8,
                   breath)
      
    if note.fx     : getattr(self, "draw_note_fx_%s" % note.fx     )(ctx, note, string_id)
    if note.link_fx: getattr(self, "draw_note_fx_%s" % note.link_fx)(ctx, note, string_id)
    ctx.setPen(qtcore.Qt.black)
    ctx.setBrush(qtcore.Qt.black)
    

  def on_touchscreen_new_note(self, event):
    y = event.y() - self.y - self.start_y - self.stem_extra_height
    hole_clicked = -1
    for hole in next(iter(self.partition.view.fingerings.values())):
      if y < 0: break
      hole_clicked += 1
      if hole is None: y -= self.canvas.default_line_height * 0.3
      else:            y -= self.canvas.default_line_height * 0.6
    new_value = self.strings[0].on_click_at(hole_clicked)
    self.canvas.on_number_typed(new_value)
    return 1 # OK to change note by dragging up or down

    
class SingleString(object):
  def __init__(self, drawer, notation_pos = -1):
    self.drawer       = drawer
    self.notation_pos = notation_pos
    
  def get_base_note(self): return self.drawer.partition.view.get_bell_note()
  base_note = property(get_base_note)
  
  def text_2_value(self, note, text): return self.base_note
  def value_2_text(self, note): return "o"
  
  def __str__(self): return "Single string"
  
  def width(self): return -1
  
  def on_click_at(self, hole): return "0"
  

class TinWhistleSingleString(SingleString):
  def text_2_value(self, note, text):
    if   text ==  "0": return self.base_note + 11
    elif text ==  "9": return self.base_note + 10
    elif text ==  "1": return self.base_note + 9
    elif text ==  "2": return self.base_note + 7
    elif text ==  "3": return self.base_note + 5
    elif text ==  "4": return self.base_note + 4
    elif text ==  "5": return self.base_note + 2
    elif text ==  "6": return self.base_note
    elif text == "00": return self.base_note + 23
    elif text == "11": return self.base_note + 21
    elif text == "22": return self.base_note + 19
    elif text == "33": return self.base_note + 17
    elif text == "44": return self.base_note + 16
    elif text == "55": return self.base_note + 14
    elif text == "66": return self.base_note + 12
    if len(text) > 2: return self.text_2_value(note, text[-2:])
    if len(text) > 1: return self.text_2_value(note, text[-1])
    return self.base_note
  
  def on_click_at(self, note, hole):
    current_fingering = self.drawer.partition.view.note_2_fingering(note)
    g8 =   0
    if current_fingering and (current_fingering[-1] == "+"): breath = 12
    else:                                                     breath = 0
    if   hole <  0: return self.base_note + g8 + breath + 11
    elif hole == 0: return self.base_note + g8 + breath + 9
    elif hole == 1: return self.base_note + g8 + breath + 7
    elif hole == 2: return self.base_note + g8 + breath + 5
    #    hole == 3 is the "None" separator
    elif hole == 4: return self.base_note + g8 + breath + 4
    elif hole == 5: return self.base_note + g8 + breath + 2
    elif hole == 6: return self.base_note + g8 + breath
    elif hole == 7: # Toggle breath
      if breath: return note.value - 12
      else:      return note.value + 12
    

  def on_click_at(self, hole):
    if   hole <  0: return "0"
    elif hole == 0: return "1"
    elif hole == 1: return "2"
    elif hole == 2: return "3"
    #    hole == 3 is the "None" separator
    elif hole == 4: return "4"
    elif hole == 5: return "5"
    elif hole == 6: return "6"