Project

Profile

Help

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

github / src / ui / dialogs / preferences.py @ b54abeec

1
"""Module containing the preferences dialog."""
2

    
3
import wx
4

    
5
from ytranslate import t
6

    
7
class GeneralTab(wx.Panel):
8

    
9
    """General tab."""
10

    
11
    def __init__(self, parent, engine):
12
        super(GeneralTab, self).__init__(parent)
13
        self.engine = engine
14

    
15
        self.InitUI()
16
        self.Fit()
17

    
18
    def InitUI(self):
19
        sizer = wx.BoxSizer(wx.VERTICAL)
20
        self.SetSizer(sizer)
21

    
22
        # Language selection
23
        l_languages = wx.StaticText(self, label=t("ui.dialog.general"))
24
        languages = wx.ListCtrl(self, style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
25
        languages.InsertColumn(0, "Name")
26
        self.languages = languages
27
        self.PopulateList()
28

    
29
        # Append to the sizer
30
        sizer.Add(l_languages)
31
        sizer.Add(languages, proportion=4)
32

    
33
    def PopulateList(self):
34
        """Add the different languages in the list."""
35
        supported = self.engine.settings.LANGUAGES
36
        codes = [lang[0] for lang in supported]
37
        languages = [lang[1] for lang in supported]
38
        for language in languages:
39
            self.languages.Append((language, ))
40

    
41
        default = self.engine.settings.get_language()
42
        index = codes.index(default)
43
        self.languages.Select(index)
44
        self.languages.Focus(index)
45

    
46
    def get_selected_language(self):
47
        """Return the selected language's code."""
48
        supported = self.engine.settings.LANGUAGES
49
        codes = [lang[0] for lang in supported]
50
        languages = [lang[1] for lang in supported]
51
        index = self.languages.GetFirstSelected()
52
        return codes[index]
53

    
54

    
55
class AccessibilityTab(wx.Panel):
56

    
57
    """Accessibility tab."""
58

    
59
    def __init__(self, parent, engine):
60
        super(AccessibilityTab, self).__init__(parent)
61
        self.engine = engine
62

    
63
        self.InitUI()
64
        self.Fit()
65

    
66
    def InitUI(self):
67
        settings = self.engine.settings
68
        sizer = wx.GridBagSizer(15, 15)
69
        self.SetSizer(sizer)
70

    
71
        # TTS preferendces
72
        self.TTS_on = wx.CheckBox(self, label=t("ui.dialog.TTS.on"))
73
        self.TTS_on.SetValue(settings["options.TTS.on"])
74
        self.TTS_outside = wx.CheckBox(self, label=t("ui.dialog.TTS.outside"))
75
        self.TTS_outside.SetValue(settings["options.TTS.outside"])
76

    
77
        # Append to the sizer
78
        sizer.Add(self.TTS_on, pos=(0, 0))
79
        sizer.Add(self.TTS_outside, pos=(0, 1))
80

    
81

    
82
class PreferencesTabs(wx.Notebook):
83

    
84
    """Preference tabs."""
85

    
86
    def __init__(self, parent, engine):
87
        wx.Notebook.__init__(self, parent)
88

    
89
        general_tab = GeneralTab(self, engine)
90
        accessibility_tab = AccessibilityTab(self, engine)
91
        self.AddPage(general_tab, t("ui.dialog.general"))
92
        self.AddPage(accessibility_tab, t("ui.dialog.accessibility"))
93
        self.general = general_tab
94
        self.accessibility = accessibility_tab
95

    
96
class PreferencesDialog(wx.Dialog):
97

    
98
    """Preferences dialog."""
99

    
100
    def __init__(self, engine):
101
        super(PreferencesDialog, self).__init__(None, title="Preferences")
102
        self.engine = engine
103

    
104
        self.InitUI()
105
        self.Maximize()
106

    
107
    def InitUI(self):
108
        sizer = wx.GridBagSizer(15, 15)
109
        self.SetSizer(sizer)
110

    
111
        # Add the tabs
112
        self.tabs = PreferencesTabs(self, self.engine)
113
        buttons = self.CreateButtonSizer(wx.OK | wx.CANCEL)
114

    
115
        # Append to the sizer
116
        sizer.Add(self.tabs, pos=(1, 0), span=( 5, 5))
117
        sizer.Add(buttons, pos=(8, 0), span=(1, 2))
118

    
119
        # Event binding
120
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=wx.ID_OK)
121
        self.Bind(wx.EVT_BUTTON, self.OnCancel, id=wx.ID_CANCEL)
122

    
123
        # Displaying
124
        self.tabs.general.SetFocus()
125

    
126
    def OnOK(self, e):
127
        """Save the preferences."""
128
        settings = self.engine.settings
129
        general = self.tabs.general
130
        accessibility = self.tabs.accessibility
131
        new_language = general.get_selected_language()
132
        old_language = settings["options.general.language"]
133
        settings["options.general.language"] = new_language
134
        settings["options.TTS.on"] = accessibility.TTS_on.GetValue()
135
        settings["options.TTS.outside"] = accessibility.TTS_outside.GetValue()
136
        settings["options"].write()
137
        if old_language != new_language:
138
            wx.MessageBox(t("ui.dialog.message.update_language"),
139
                    t("ui.dialog.restart"), wx.OK | wx.ICON_INFORMATION)
140
        self.Destroy()
141

    
142
    def OnCancel(self, e):
143
        """Simply exit the dialog."""
144
        self.Destroy()
(3-3/3)