Project

Profile

Help

How to connect?
Download (9.94 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / ui / dialogs / preferences.py @ 9083fa1b

1
# Copyright (c) 2016, LE GOFF Vincent
2
# All rights reserved.
3

    
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are met:
6

    
7
# * Redistributions of source code must retain the above copyright notice, this
8
#   list of conditions and the following disclaimer.
9

    
10
# * Redistributions in binary form must reproduce the above copyright notice,
11
#   this list of conditions and the following disclaimer in the documentation
12
#   and/or other materials provided with the distribution.
13

    
14
# * Neither the name of ytranslate nor the names of its
15
#   contributors may be used to endorse or promote products derived from
16
#   this software without specific prior written permission.
17

    
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28

    
29
"""Module containing the preferences dialog."""
30

    
31
import wx
32

    
33
from ytranslate import t
34

    
35
class GeneralTab(wx.Panel):
36

    
37
    """General tab."""
38

    
39
    def __init__(self, parent, engine):
40
        super(GeneralTab, self).__init__(parent)
41
        self.engine = engine
42

    
43
        self.InitUI()
44
        self.Fit()
45

    
46
    def InitUI(self):
47
        sizer = wx.BoxSizer(wx.VERTICAL)
48
        self.SetSizer(sizer)
49

    
50
        # Language selection
51
        l_languages = wx.StaticText(self, label=t("ui.dialog.general"))
52
        languages = wx.ListCtrl(self, style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
53
        languages.InsertColumn(0, "Name")
54
        self.languages = languages
55
        self.PopulateList()
56

    
57
        # Append to the sizer
58
        sizer.Add(l_languages)
59
        sizer.Add(languages, proportion=4)
60

    
61
    def PopulateList(self):
62
        """Add the different languages in the list."""
63
        supported = self.engine.settings.LANGUAGES
64
        codes = [lang[0] for lang in supported]
65
        languages = [lang[1] for lang in supported]
66
        for language in languages:
67
            self.languages.Append((language, ))
68

    
69
        default = self.engine.settings.get_language()
70
        index = codes.index(default)
71
        self.languages.Select(index)
72
        self.languages.Focus(index)
73

    
74
    def get_selected_language(self):
75
        """Return the selected language's code."""
76
        supported = self.engine.settings.LANGUAGES
77
        codes = [lang[0] for lang in supported]
78
        languages = [lang[1] for lang in supported]
79
        index = self.languages.GetFirstSelected()
80
        return codes[index]
81

    
82

    
83
class DisplayTab(wx.Panel):
84

    
85
    """Display tab."""
86

    
87
    def __init__(self, parent, engine):
88
        super(DisplayTab, self).__init__(parent)
89
        self.engine = engine
90
        self.supported = [
91
            "ascii",
92
            "big5",
93
            "big5hkscs",
94
            "cp037",
95
            "cp424",
96
            "cp437",
97
            "cp500",
98
            "cp720",
99
            "cp737",
100
            "cp775",
101
            "cp850",
102
            "cp852",
103
            "cp855",
104
            "cp856",
105
            "cp857",
106
            "cp858",
107
            "cp860",
108
            "cp861",
109
            "cp862",
110
            "cp863",
111
            "cp864",
112
            "cp865",
113
            "cp866",
114
            "cp869",
115
            "cp874",
116
            "cp875",
117
            "cp932",
118
            "cp949",
119
            "cp950",
120
            "cp1006",
121
            "cp1026",
122
            "cp1140",
123
            "cp1250",
124
            "cp1251",
125
            "cp1252",
126
            "cp1253",
127
            "cp1254",
128
            "cp1255",
129
            "cp1256",
130
            "cp1257",
131
            "cp1258",
132
            "euc_jp",
133
            "euc_jis_2004",
134
            "euc_jisx0213",
135
            "euc_kr",
136
            "gb2312",
137
            "gbk",
138
            "gb18030",
139
            "hz",
140
            "iso2022_jp",
141
            "iso2022_jp_1",
142
            "iso2022_jp_2",
143
            "iso2022_jp_2004",
144
            "iso2022_jp_3",
145
            "iso2022_jp_ext",
146
            "iso2022_kr",
147
            "latin_1",
148
            "iso8859_2",
149
            "iso8859_3",
150
            "iso8859_4",
151
            "iso8859_5",
152
            "iso8859_6",
153
            "iso8859_7",
154
            "iso8859_8",
155
            "iso8859_9",
156
            "iso8859_10",
157
            "iso8859_13",
158
            "iso8859_14",
159
            "iso8859_15",
160
            "iso8859_16",
161
            "johab",
162
            "koi8_r",
163
            "koi8_u",
164
            "mac_cyrillic",
165
            "mac_greek",
166
            "mac_iceland",
167
            "mac_latin2",
168
            "mac_roman",
169
            "mac_turkish",
170
            "ptcp154",
171
            "shift_jis",
172
            "shift_jis_2004",
173
            "shift_jisx0213",
174
            "utf_32",
175
            "utf_32_be",
176
            "utf_32_le",
177
            "utf_16",
178
            "utf_16_be",
179
            "utf_16_le",
180
            "utf_7",
181
            "utf_8",
182
            "utf_8_sig"
183
        ]
184

    
185
        self.InitUI()
186
        self.Fit()
187

    
188
    def InitUI(self):
189
        sizer = wx.BoxSizer(wx.VERTICAL)
190
        self.SetSizer(sizer)
191

    
192
        # Encoding selection
193
        l_encodings = wx.StaticText(self, label=t("ui.dialog.encodings"))
194
        encodings = wx.ListCtrl(self, style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
195
        encodings.InsertColumn(0, "Name")
196
        self.encodings = encodings
197
        self.PopulateList()
198

    
199
        # Append to the sizer
200
        sizer.Add(l_encodings)
201
        sizer.Add(encodings, proportion=4)
202

    
203
    def PopulateList(self):
204
        """Add the different encodings in the list."""
205
        supported = self.supported
206
        for encoding in supported:
207
            self.encodings.Append((encoding, ))
208

    
209
        default = self.engine.settings["options.general.encoding"]
210
        try:
211
            index = supported.index(default)
212
        except IndexError:
213
            pass
214
        else:
215
            self.encodings.Select(index)
216
            self.encodings.Focus(index)
217

    
218
    def get_selected_encoding(self):
219
        """Return the selected encoding."""
220
        supported = self.supported
221
        index = self.encodings.GetFirstSelected()
222
        return supported[index]
223

    
224

    
225
class AccessibilityTab(wx.Panel):
226

    
227
    """Accessibility tab."""
228

    
229
    def __init__(self, parent, engine):
230
        super(AccessibilityTab, self).__init__(parent)
231
        self.engine = engine
232

    
233
        self.InitUI()
234
        self.Fit()
235

    
236
    def InitUI(self):
237
        settings = self.engine.settings
238
        sizer = wx.GridBagSizer(15, 15)
239
        self.SetSizer(sizer)
240

    
241
        # Tabbing
242
        self.tab_end = wx.CheckBox(self, label=t("ui.dialog.tab_end"))
243
        self.tab_end.SetValue(settings["options.accessibility.tab_end"])
244

    
245
        # TTS preferendces
246
        self.TTS_on = wx.CheckBox(self, label=t("ui.dialog.TTS.on"))
247
        self.TTS_on.SetValue(settings["options.TTS.on"])
248
        self.TTS_outside = wx.CheckBox(self, label=t("ui.dialog.TTS.outside"))
249
        self.TTS_outside.SetValue(settings["options.TTS.outside"])
250

    
251
        # Append to the sizer
252
        sizer.Add(self.tab_end, pos=(0, 0))
253
        sizer.Add(self.TTS_on, pos=(0, 1))
254
        sizer.Add(self.TTS_outside, pos=(0, 2))
255

    
256

    
257
class PreferencesTabs(wx.Notebook):
258

    
259
    """Preference tabs."""
260

    
261
    def __init__(self, parent, engine):
262
        wx.Notebook.__init__(self, parent)
263

    
264
        general_tab = GeneralTab(self, engine)
265
        display_tab = DisplayTab(self, engine)
266
        accessibility_tab = AccessibilityTab(self, engine)
267
        self.AddPage(general_tab, t("ui.dialog.general"))
268
        self.AddPage(display_tab, t("ui.dialog.display"))
269
        self.AddPage(accessibility_tab, t("ui.dialog.accessibility"))
270
        self.general = general_tab
271
        self.display = display_tab
272
        self.accessibility = accessibility_tab
273

    
274
class PreferencesDialog(wx.Dialog):
275

    
276
    """Preferences dialog."""
277

    
278
    def __init__(self, engine):
279
        super(PreferencesDialog, self).__init__(None, title="Preferences")
280
        self.engine = engine
281

    
282
        self.InitUI()
283
        self.Maximize()
284

    
285
    def InitUI(self):
286
        sizer = wx.GridBagSizer(15, 15)
287
        self.SetSizer(sizer)
288

    
289
        # Add the tabs
290
        self.tabs = PreferencesTabs(self, self.engine)
291
        buttons = self.CreateButtonSizer(wx.OK | wx.CANCEL)
292

    
293
        # Append to the sizer
294
        sizer.Add(self.tabs, pos=(1, 0), span=( 5, 5))
295
        sizer.Add(buttons, pos=(8, 0), span=(1, 2))
296

    
297
        # Event binding
298
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=wx.ID_OK)
299
        self.Bind(wx.EVT_BUTTON, self.OnCancel, id=wx.ID_CANCEL)
300

    
301
        # Displaying
302
        self.tabs.general.SetFocus()
303

    
304
    def OnOK(self, e):
305
        """Save the preferences."""
306
        settings = self.engine.settings
307
        general = self.tabs.general
308
        display = self.tabs.display
309
        accessibility = self.tabs.accessibility
310
        new_language = general.get_selected_language()
311
        encoding = display.get_selected_encoding()
312
        old_language = settings["options.general.language"]
313
        settings["options.general.language"] = new_language
314
        settings["options.general.encoding"] = encoding
315
        tab_end = accessibility.tab_end.GetValue()
316
        settings["options.accessibility.tab_end"] = tab_end
317
        settings["options.TTS.on"] = accessibility.TTS_on.GetValue()
318
        settings["options.TTS.outside"] = accessibility.TTS_outside.GetValue()
319
        settings["options"].write()
320
        if old_language != new_language:
321
            wx.MessageBox(t("ui.dialog.message.update_language"),
322
                    t("ui.dialog.restart"), wx.OK | wx.ICON_INFORMATION)
323
        self.Destroy()
324

    
325
    def OnCancel(self, e):
326
        """Simply exit the dialog."""
327
        self.Destroy()
(4-4/4)