File: Preset.py

package info (click to toggle)
cecilia 5.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,124 kB
  • sloc: python: 17,246; sh: 42; makefile: 11
file content (144 lines) | stat: -rw-r--r-- 6,156 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
# encoding: utf-8
"""
Copyright 2011 iACT, Universite de Montreal, Jean Piche, Olivier Belanger, Jean-Michel Dumas

This file is part of Cecilia 5.

Cecilia 5 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.

Cecilia 5 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 Cecilia 5.  If not, see <http://www.gnu.org/licenses/>.
"""

import wx
from cecilia.Resources import CeciliaLib
from .constants import *
from .Widgets import *


class CECPreset(wx.Panel):
    def __init__(self, parent, id=-1, size=(-1, -1), style=wx.BORDER_SIMPLE):
        wx.Panel.__init__(self, parent, id, size=size, style=style)
        self.SetBackgroundColour(BACKGROUND_COLOUR)
        self.parent = parent

        self.currentPreset = 'init'

        mainSizer = wx.FlexGridSizer(0, 1, 0, 0)
        mainSizer.Add(10, 1, 0)

        presetTextPanel = wx.Panel(self, -1, style=wx.BORDER_NONE)
        presetTextPanel.SetBackgroundColour(TITLE_BACK_COLOUR)
        presetTextSizer = wx.FlexGridSizer(1, 1, 0, 0)
        presetText = wx.StaticText(presetTextPanel, -1, 'PRESETS')
        presetText.SetFont(wx.Font(SECTION_TITLE_FONT, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        presetText.SetBackgroundColour(TITLE_BACK_COLOUR)
        presetText.SetForegroundColour(SECTION_TITLE_COLOUR)
        presetTextSizer.Add(presetText, 0, wx.ALIGN_RIGHT | wx.ALL, 3)
        presetTextSizer.AddGrowableCol(0)
        presetTextPanel.SetSizer(presetTextSizer)
        mainSizer.Add(presetTextPanel, 1, wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, 0)

        lineSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.presetChoice = CustomMenu(self, choice=self.orderingPresetNames(),
                                       size=(150, 20), init=self.currentPreset,
                                       outFunction=self.onPresetSelect, colour=TR_BACK_COLOUR)
        CeciliaLib.setToolTip(self.presetChoice, TT_PRESET)
        lineSizer.Add(self.presetChoice, 0, wx.ALIGN_LEFT, 1)

        lineSizer.Add(10, 1, 0)

        saveTool = ToolBox(self, tools=['save', 'delete'], outFunction=[self.onSavePreset, self.onDeletePreset])
        CeciliaLib.setToolTip(saveTool, TT_PRESET_TOOLS)
        lineSizer.Add(saveTool, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 2)

        mainSizer.Add(lineSizer, 0, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 7)

        mainSizer.AddGrowableCol(0)
        self.SetSizer(mainSizer)

    def getPresets(self):
        return self.presetChoice.getChoice()

    def setLabel(self, label):
        if label in CeciliaLib.getVar("presets").keys():
            self.presetChoice.setLabel(label, False)

    def loadPresets(self):
        presets = self.orderingPresetNames()
        self.presetChoice.setChoice(presets, False)

    def orderingPresetNames(self):
        presets = list(CeciliaLib.getVar("presets").keys())
        presets.sort()
        presets.insert(0, 'init')
        return presets

    def onPresetSelect(self, idxPreset, newPreset):
        if newPreset in CeciliaLib.getVar("presets"):
            CeciliaLib.loadPresetFromDict(newPreset)
            for preset in CeciliaLib.getVar("presets"):
                if preset != newPreset:
                    CeciliaLib.getVar("presets")[preset]['active'] = False
            CeciliaLib.getVar("presets")[newPreset]['active'] = True
            self.currentPreset = newPreset
        elif newPreset == 'init':
            CeciliaLib.loadPresetFromDict("init")
            for preset in CeciliaLib.getVar("presets"):
                CeciliaLib.getVar("presets")[preset]['active'] = False
            self.currentPreset = "init"

    def onDeletePreset(self):
        if self.currentPreset in CeciliaLib.getVar("presets"):
            dlg = wx.MessageDialog(self,
                                    'Preset %s will be deleted. Are you sure?' % self.currentPreset,
                                    'Warning!', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_INFORMATION)
            if dlg.ShowModal() == wx.ID_NO:
                ok = False
            else:
                ok = True
            dlg.Destroy()

            if ok:
                CeciliaLib.deletePreset(self.currentPreset)
                self.presetChoice.setChoice(self.orderingPresetNames(), False)
                self.presetChoice.setStringSelection("")
                CeciliaLib.saveCeciliaFile(self, showDialog=False)

    def onSavePreset(self):
        dlg = wx.TextEntryDialog(self, 'Enter preset name:', 'Saving Preset', self.currentPreset)

        if dlg.ShowModal() == wx.ID_OK:
            newPreset = CeciliaLib.ensureNFD(dlg.GetValue())
        else:
            newPreset = ''
        dlg.Destroy()

        if newPreset == '':
            CeciliaLib.showErrorDialog('Failed saving preset', 'You must give a name to your preset!')
            return
        if newPreset == 'init':
            CeciliaLib.showErrorDialog('Failed saving preset', '"init" is reserved. You must give another name to your preset!')
            return
        ok = True
        if newPreset in CeciliaLib.getVar("presets").keys():
            dlg2 = wx.MessageDialog(self, 'The preset you entered already exists. Are you sure you want to overwrite it?',
                                    'Existing preset!', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_INFORMATION)
            if dlg2.ShowModal() == wx.ID_NO:
                ok = False
            dlg2.Destroy()

        if ok:
            self.currentPreset = newPreset
            CeciliaLib.savePresetToDict(self.currentPreset)
            self.presetChoice.setChoice(self.orderingPresetNames(), False)
            self.presetChoice.setStringSelection(self.currentPreset)
            CeciliaLib.saveCeciliaFile(self, showDialog=False)