~ubuntu-branches/ubuntu/vivid/frescobaldi/vivid

« back to all changes in this revision

Viewing changes to python/frescobaldi_app/scorewiz/preview.py

  • Committer: Package Import Robot
  • Author(s): Ryan Kavanagh
  • Date: 2012-01-03 16:20:11 UTC
  • mfrom: (1.4.1)
  • Revision ID: package-import@ubuntu.com-20120103162011-tsjkwl4sntwmprea
Tags: 2.0.0-1
* New upstream release 
* Drop the following uneeded patches:
  + 01_checkmodules_no_python-kde4_build-dep.diff
  + 02_no_pyc.diff
  + 04_no_binary_lilypond_upgrades.diff
* Needs new dependency python-poppler-qt4
* Update debian/watch for new download path
* Update copyright file with new holders and years

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
2
 
#
3
 
# Copyright (c) 2008, 2009, 2010 by Wilbert Berendsen
4
 
#
5
 
# This program is free software; you can redistribute it and/or
6
 
# modify it under the terms of the GNU General Public License
7
 
# as published by the Free Software Foundation; either version 2
8
 
# of the License, or (at your option) any later version.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
# See http://www.gnu.org/licenses/ for more information.
19
 
 
20
 
from __future__ import unicode_literals
21
 
 
22
 
"""
23
 
Preview dialog for the Score Wizard (scorewiz/__init__.py).
24
 
In separate file to ease maintenance.
25
 
"""
26
 
 
27
 
import math
28
 
import ly, ly.dom
29
 
 
30
 
from frescobaldi_app.runlily import LilyPreviewDialog
31
 
from frescobaldi_app.scorewiz import config
32
 
 
33
 
class PreviewDialog(LilyPreviewDialog):
34
 
    def __init__(self, scorewiz):
35
 
        self.scorewiz = scorewiz
36
 
        LilyPreviewDialog.__init__(self, scorewiz)
37
 
    
38
 
    def loadSettings(self):
39
 
        self.restoreDialogSize(config("preview"))
40
 
        
41
 
    def saveSettings(self):
42
 
        self.saveDialogSize(config("preview"))
43
 
        
44
 
    def showPreview(self):
45
 
        builder = self.scorewiz.builder()
46
 
        builder.midi = False # not needed
47
 
        doc = builder.document()
48
 
 
49
 
        keysig = doc.findChild(ly.dom.KeySignature) 
50
 
        timesig = doc.findChild(ly.dom.TimeSignature)
51
 
        partial = doc.findChild(ly.dom.Partial)
52
 
        # create a list of durations for the example notes.
53
 
        durs = []
54
 
        if partial:
55
 
            durs.append((partial.dur, partial.dots))
56
 
        if timesig:
57
 
            dur = int(math.log(int(timesig.beat), 2))
58
 
            num = min(int(timesig.num)*2, 10)
59
 
        else:
60
 
            dur, num = 2, 4
61
 
        durs += [(dur, 0)] * num
62
 
        
63
 
        def addItems(stub, generator):
64
 
            for dur, dots in durs:
65
 
                node = next(generator)
66
 
                node.append(ly.dom.Duration(dur, dots))
67
 
                stub.append(node)
68
 
            
69
 
        lyrics = lyricsGen(len(durs))
70
 
        # iter over all the Assignments to add example notes etc.
71
 
        for a in doc.findChildren(ly.dom.Assignment, 1):
72
 
            stub = a[-1]
73
 
            if isinstance(stub, ly.dom.LyricMode):
74
 
                stub.append(next(lyrics))
75
 
            elif isinstance(stub, ly.dom.Relative):
76
 
                addItems(stub[-1], pitchGen(keysig))
77
 
            elif isinstance(stub, ly.dom.ChordMode):
78
 
                addItems(stub, chordGen(keysig))
79
 
            elif isinstance(stub, ly.dom.FigureMode):
80
 
                addItems(stub, figureGen())
81
 
            elif isinstance(stub, ly.dom.DrumMode):
82
 
                addItems(stub, drumGen())
83
 
 
84
 
        LilyPreviewDialog.showPreview(self, builder.ly(doc))
85
 
 
86
 
 
87
 
# Generators for different kinds of example input
88
 
def pitchGen(startPitch):
89
 
    note = startPitch.note
90
 
    while True:
91
 
        for n in (note, note, (note + 9 ) % 7, (note + 8) % 7,
92
 
                  note, (note + 11) % 7, note):
93
 
            chord = ly.dom.Chord()
94
 
            ly.dom.Pitch(-1, n, startPitch.alter, parent=chord)
95
 
            yield chord
96
 
 
97
 
def chordGen(startPitch):
98
 
    for n in pitchGen(startPitch):
99
 
        yield n
100
 
        for i in 1, 2, 3:
101
 
            yield ly.dom.TextDur("\\skip")
102
 
        
103
 
def lyricsGen(length):
104
 
    while True:
105
 
        for i in "ha", "hi", "ho", "he", "hu":
106
 
            result = [i]*length
107
 
            result[0] = result[0].title()
108
 
            yield ly.dom.Text(' '.join(result))
109
 
 
110
 
def figureGen():
111
 
    while True:
112
 
        for i in 5, 6, 3, 8, 7:
113
 
            for s in "<{0}>".format(i), "\\skip", "\\skip":
114
 
                yield ly.dom.TextDur(s)
115
 
            
116
 
def drumGen():
117
 
    while True:
118
 
        for s in "bd", "hh", "sn", "hh":
119
 
            yield ly.dom.TextDur(s)
120
 
 
121
 
 
122
 
 
 
 
b'\\ No newline at end of file'