File: widget.py

package info (click to toggle)
frescobaldi 3.0.0~git20161001.0.eec60717%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,792 kB
  • ctags: 5,843
  • sloc: python: 37,853; sh: 180; makefile: 69
file content (169 lines) | stat: -rw-r--r-- 6,634 bytes parent folder | download | duplicates (2)
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
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
The Layout Control options widget.
"""


import sys

from PyQt5.QtCore import pyqtSignal, QSettings
from PyQt5.QtWidgets import (QCheckBox, QHBoxLayout, QLineEdit, QToolButton,
                             QVBoxLayout, QWidget)

import app
import icons
import layoutcontrol
import engrave
import userguide


class Widget(QWidget):
    
    optionsChanged = pyqtSignal()
    
    def __init__(self, tool):
        super(Widget, self).__init__(tool)
        
        layout = QVBoxLayout(spacing=1)
        self.setLayout(layout)
        
        # manual mode UI elements that need special treatment
        self.CBverbose = QCheckBox(clicked=self.optionsChanged)
        self.CBpointandclick = QCheckBox(clicked=self.optionsChanged)
        self.CBcustomfile = QCheckBox(clicked=self.optionsChanged)
        self.LEcustomfile = QLineEdit(enabled=False)
        
        # run Lily button
        self.engraveButton = QToolButton()
        self.engraveButton.setDefaultAction(
            engrave.engraver(tool.mainwindow()).actionCollection.engrave_debug)
        
        # help button
        self.helpButton = QToolButton(clicked=self.helpButtonClicked)
        self.helpButton.setIcon(icons.get('help-contents'))
        
        # add manual widgets
        hbox = QHBoxLayout()
        hbox.setContentsMargins(0, 0, 0, 0)
        hbox.addWidget(self.engraveButton)
        hbox.addWidget(self.helpButton)
        hbox.addStretch(1)
        layout.addLayout(hbox)
        layout.addWidget(self.CBverbose)
        layout.addWidget(self.CBpointandclick)
        
        # automatically processed modes
        self.checkboxes = {}
        for mode in layoutcontrol.modelist():
            self.checkboxes[mode] = cb = QCheckBox(clicked=self.optionsChanged)
            layout.addWidget(cb)
        
        # add manual widgets
        layout.addWidget(self.CBcustomfile)
        layout.addWidget(self.LEcustomfile)
        layout.addStretch(1)
        
        # connect manual widgets
        self.CBcustomfile.toggled.connect(self.LEcustomfile.setEnabled)
        self.LEcustomfile.textEdited.connect(self.optionsChanged)
        
        app.translateUI(self)
        self.loadSettings()
        tool.mainwindow().aboutToClose.connect(self.saveSettings)
    
    def translateUI(self):
        for mode in layoutcontrol.modelist():
            label = layoutcontrol.label(mode)
            tooltip = layoutcontrol.tooltip(mode)
            self.checkboxes[mode].setText(label)
            self.checkboxes[mode].setToolTip(tooltip)
        self.helpButton.setText(_("Help"))
        self.helpButton.setToolTip(_("Help"))
        self.CBverbose.setText(_("Verbose output"))
        self.CBverbose.setToolTip(_("Run LilyPond with verbose output"))
        self.CBpointandclick.setText(_("Point-and-Click"))
        self.CBpointandclick.setToolTip(_("Run LilyPond in preview mode (with Point and Click)"))
        self.CBcustomfile.setText(_("Include Custom File:"))
        self.CBcustomfile.setToolTip(_("Include a custom file with definitions\n"
                      "for additional Layout Control Modes"))
        self.LEcustomfile.setToolTip(_("Filename to be included"))
    
    def loadSettings(self):
        """Called on construction. Load settings and set checkboxes state."""
        s = QSettings()
        s.beginGroup('lilypond_settings')
        for mode in layoutcontrol.modelist():
            self.checkboxes[mode].setChecked(s.value(mode, False, bool))
        self.CBverbose.setChecked(s.value('verbose', False, bool))
        self.CBpointandclick.setChecked(s.value('point-and-click', True, bool))
        self.CBcustomfile.setChecked(s.value('custom-file', False, bool))
        self.LEcustomfile.setText(s.value('custom-filename', '', str))
        
    def saveSettings(self):
        """Called on close. Save settings and checkboxes state."""
        s = QSettings()
        s.beginGroup('lilypond_settings')
        for mode in layoutcontrol.modelist():
            s.setValue(mode, self.checkboxes[mode].isChecked())
        s.setValue('verbose', self.CBverbose.isChecked())
        s.setValue('point-and-click', self.CBpointandclick.isChecked())
        s.setValue('custom-file', self.CBcustomfile.isChecked())
        s.setValue('custom-filename', self.LEcustomfile.text())

    def helpButtonClicked(self):
        userguide.show("engrave_layout")
        
    def preview_options(self):
        """Return a list of Debug Mode command line options for LilyPond."""
        args = []
        
        # 'automatic' widgets
        for mode in layoutcontrol.modelist():
            if self.checkboxes[mode].isChecked():
                args.append(layoutcontrol.option(mode))
        
        # manual widgets
        if self.CBcustomfile.isChecked():
            file_to_include = self.LEcustomfile.text()
            args.append('-ddebug-custom-file=' + file_to_include)
        
        # if at least one debug mode is used, add the directory with the
        # preview-mode files to the search path
        if args:
            args.insert(0, '-I' + layoutcontrol.__path__[0])
            # File that conditionally includes different formatters
            args.insert(1, '-dinclude-settings=debug-layout-options.ly') 
        
        if self.CBpointandclick.isChecked():
            args.insert(0, '-dpoint-and-click')
        else:
            args.insert(0, '-dno-point-and-click')
        
        if self.CBverbose.isChecked():
            args.insert(0, '--verbose')
        
        if self.CBcustomfile.isChecked():
            file_to_include = self.LEcustomfile.text()
            if file_to_include:
                args.append('-ddebug-custom-file=' + file_to_include)
        return args