|
| 1 | +import wx |
| 2 | +import MathCATgui |
| 3 | +import yaml |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import webbrowser |
| 7 | +from tendo import singleton |
| 8 | +import gettext |
| 9 | +_ = gettext.gettext |
| 10 | + |
| 11 | +#initialize the languages dictionary |
| 12 | +Speech_Language = { "en": "English : en"} |
| 13 | + |
| 14 | +# initialize the user preferences tuples |
| 15 | +user_preferences = dict([("", "")]) |
| 16 | +Speech_Impairment = ("LearningDisability", "Blindness", "LowVision") |
| 17 | +Speech_Verbosity = ("Terse", "Medium", "Verbose") |
| 18 | +Speech_SpeechStyle = ("ClearSpeak", "SimpleSpeak") |
| 19 | +Speech_SubjectArea = ("General") |
| 20 | +Speech_Chemistry = ("SpellOut", "AsCompound", "Off") |
| 21 | + |
| 22 | +Navigation_NavMode = ("Enhanced", "Simple", "Character") |
| 23 | +#Navigation_ResetNavMode is boolean |
| 24 | +#Navigation_OverView is boolean |
| 25 | +Navigation_NavVerbosity = ("Terse", "Medium", "Verbose") |
| 26 | +#Navigation_AutoZoomOut is boolean |
| 27 | + |
| 28 | +Braille_BrailleNavHighlight = ("Off", "FirstChar", "EndPoints", "All") |
| 29 | +Braille_BrailleCode = ("Nemeth", "UEB") |
| 30 | + |
| 31 | +def path_to_default_preferences(): |
| 32 | + #the default preferences file is: C:\Users\<user-name>AppData\Roaming\\nvda\\addons\\mathCAT\\globalPlugins\\MathCAT\\Rules\\prefs.yaml |
| 33 | + return os.path.expanduser('~')+"\\AppData\\Roaming\\nvda\\addons\\mathCAT\\globalPlugins\\MathCAT\\Rules\\prefs.yaml"; |
| 34 | + |
| 35 | +def path_to_user_preferences_folder(): |
| 36 | + #the user preferences file is stored at: C:\Users\<user-name>AppData\Roaming\MathCAT\prefs.yaml |
| 37 | + return os.path.expanduser('~')+"\\AppData\\Roaming\\MathCAT"; |
| 38 | + |
| 39 | +def path_to_user_preferences(): |
| 40 | + #the user preferences file is stored at: C:\Users\<user-name>AppData\Roaming\MathCAT\prefs.yaml |
| 41 | + return path_to_user_preferences_folder() + "\\prefs.yaml"; |
| 42 | + |
| 43 | +def load_default_preferences(): |
| 44 | + global user_preferences |
| 45 | + #load default preferences into the user preferences data structure (overwites existing) |
| 46 | + if os.path.exists(path_to_default_preferences()): |
| 47 | + with open(path_to_default_preferences()) as f: |
| 48 | + user_preferences = yaml.load(f, Loader=yaml.FullLoader) |
| 49 | + else: |
| 50 | + #default preferences file is NOT found |
| 51 | + wx.MessageBox(_(u"MathCat preferences file not found. The program will now exit."), "Error", wx.OK | wx.ICON_ERROR) |
| 52 | + os.sys.exit(-1) |
| 53 | + |
| 54 | +def load_user_preferences(): |
| 55 | + global user_preferences |
| 56 | + #merge user file values into the user preferences data structure |
| 57 | + if os.path.exists(path_to_user_preferences()): |
| 58 | + with open(path_to_user_preferences()) as f: |
| 59 | + # merge with the default preferences, overwriting with the user's values |
| 60 | + user_preferences |= yaml.load(f, Loader=yaml.FullLoader) |
| 61 | + |
| 62 | +def write_user_preferences(): |
| 63 | + if not os.path.exists(path_to_user_preferences_folder()): |
| 64 | + #create a folder for the user preferences |
| 65 | + os.mkdir(path_to_user_preferences_folder()) |
| 66 | + with open(path_to_user_preferences(), 'w') as f: |
| 67 | + #write values to the user preferences file, NOT the default |
| 68 | + yaml.dump(user_preferences, f) |
| 69 | + |
| 70 | +def GetKey(val): |
| 71 | + for key, value in Speech_Language.items(): |
| 72 | + if val == value: |
| 73 | + return key |
| 74 | + return "en" |
| 75 | + |
| 76 | +class UserInterface(MathCATgui.MathCATPreferencesDialog): |
| 77 | + |
| 78 | + def set_ui_values(self): |
| 79 | + #set the UI elements to the ones read from the preference file(s) |
| 80 | + try: |
| 81 | + self.m_choiceImpairment.SetSelection(Speech_Impairment.index(user_preferences["Speech"]["Impairment"])) |
| 82 | + self.m_choiceLanguage.SetStringSelection(Speech_Language[user_preferences["Speech"]["Language"]]) |
| 83 | + self.m_choiceSpeechStyle.SetSelection(Speech_SpeechStyle.index(user_preferences["Speech"]["SpeechStyle"])) |
| 84 | + self.m_choiceSpeechAmount.SetSelection(Speech_Verbosity.index(user_preferences["Speech"]["Verbosity"])) |
| 85 | + self.m_choiceSpeechForChemical.SetSelection(Speech_Chemistry.index(user_preferences["Speech"]["Chemistry"])) |
| 86 | + self.m_choiceNavigationMode.SetSelection(Navigation_NavMode.index(user_preferences["Navigation"]["NavMode"])) |
| 87 | + self.m_checkBoxResetNavigationMode.SetValue(user_preferences["Navigation"]["ResetNavMode"]) |
| 88 | + self.m_choiceSpeechAmountNavigation.SetSelection(Navigation_NavVerbosity.index(user_preferences["Navigation"]["NavVerbosity"])) |
| 89 | + if user_preferences["Navigation"]["Overview"]: |
| 90 | + self.m_choiceNavigationSpeech.SetSelection(0) |
| 91 | + else: |
| 92 | + self.m_choiceNavigationSpeech.SetSelection(1) |
| 93 | + self.m_checkBoxResetNavigationSpeech.SetValue(user_preferences["Navigation"]["ResetOverview"]) |
| 94 | + self.m_checkBoxAutomaticZoom.SetValue(user_preferences["Navigation"]["AutoZoomOut"]) |
| 95 | + self.m_choiceBrailleHighlights.SetSelection(Braille_BrailleNavHighlight.index(user_preferences["Braille"]["BrailleNavHighlight"])) |
| 96 | + self.m_choiceBrailleMathCode.SetSelection(Braille_BrailleCode.index(user_preferences["Braille"]["BrailleCode"])) |
| 97 | + except KeyError as err: |
| 98 | + print('Key not found') |
| 99 | + |
| 100 | + def get_ui_values(self): |
| 101 | + global user_preferences |
| 102 | + # read the values from the UI and update the user preferences dictionary |
| 103 | + user_preferences["Speech"]["Impairment"] = Speech_Impairment[self.m_choiceImpairment.GetSelection()] |
| 104 | + user_preferences["Speech"]["Language"] = GetKey( self.m_choiceLanguage.GetStringSelection()) |
| 105 | + user_preferences["Speech"]["SpeechStyle"] = Speech_SpeechStyle[self.m_choiceSpeechStyle.GetSelection()] |
| 106 | + user_preferences["Speech"]["Verbosity"] = Speech_Verbosity[self.m_choiceSpeechAmount.GetSelection()] |
| 107 | + user_preferences["Speech"]["Chemistry"] = Speech_Chemistry[self.m_choiceSpeechForChemical.GetSelection()] |
| 108 | + user_preferences["Navigation"]["NavMode"] = Navigation_NavMode[self.m_choiceNavigationMode.GetSelection()] |
| 109 | + user_preferences["Navigation"]["ResetNavMode"] = self.m_checkBoxResetNavigationMode.GetValue() |
| 110 | + user_preferences["Navigation"]["NavVerbosity"] = Navigation_NavVerbosity[self.m_choiceSpeechAmountNavigation.GetSelection()] |
| 111 | + if self.m_choiceNavigationSpeech.GetSelection() == 0: |
| 112 | + user_preferences["Navigation"]["Overview"] = True |
| 113 | + else: |
| 114 | + user_preferences["Navigation"]["Overview"] = False |
| 115 | + user_preferences["Navigation"]["ResetOverview"] = self.m_checkBoxResetNavigationSpeech.GetValue() |
| 116 | + user_preferences["Navigation"]["AutoZoomOut"] = self.m_checkBoxAutomaticZoom.GetValue() |
| 117 | + user_preferences["Braille"]["BrailleNavHighlight"] = Braille_BrailleNavHighlight[self.m_choiceBrailleHighlights.GetSelection()] |
| 118 | + user_preferences["Braille"]["BrailleCode"] = Braille_BrailleCode[self.m_choiceBrailleMathCode.GetSelection()] |
| 119 | + user_preferences["MathCATPreferencesLastCategory"] = self.m_listBoxPreferencesTopic.GetSelection() |
| 120 | + |
| 121 | + def __init__(self,parent): |
| 122 | + #initialize parent class |
| 123 | + MathCATgui.MathCATPreferencesDialog.__init__(self,parent) |
| 124 | + if "MathCATPreferencesLastCategory" in user_preferences: |
| 125 | + #set the categories selection to what we used on last run |
| 126 | + self.m_listBoxPreferencesTopic.SetSelection(user_preferences["MathCATPreferencesLastCategory"]) |
| 127 | + #show the appropriate dialogue page |
| 128 | + self.m_simplebookPanelsCategories.SetSelection(self.m_listBoxPreferencesTopic.GetSelection()) |
| 129 | + else: |
| 130 | + #set the categories selection to the first item |
| 131 | + self.m_listBoxPreferencesTopic.SetSelection(0) |
| 132 | + user_preferences["MathCATPreferencesLastCategory"]="0" |
| 133 | + #populate the language choices |
| 134 | + self.m_choiceLanguage.Clear() |
| 135 | + for lang in Speech_Language.values(): |
| 136 | + self.m_choiceLanguage.Append(lang) |
| 137 | + #populate the SpeechStyle choices |
| 138 | + #this will need to be done on change of language |
| 139 | + self.m_choiceSpeechStyle.Clear() |
| 140 | + self.m_choiceSpeechStyle.Append("ClearSpeak") |
| 141 | + |
| 142 | + UserInterface.set_ui_values(self) |
| 143 | + |
| 144 | + def OnClickOK(self,event): |
| 145 | + UserInterface.get_ui_values(self) |
| 146 | + write_user_preferences() |
| 147 | + app.ExitMainLoop() |
| 148 | + |
| 149 | + def OnClickCancel(self,event): |
| 150 | + app.ExitMainLoop() |
| 151 | + |
| 152 | + def OnClickApply(self,event): |
| 153 | + UserInterface.get_ui_values(self) |
| 154 | + write_user_preferences() |
| 155 | + |
| 156 | + def OnClickReset(self,event): |
| 157 | + load_default_preferences() |
| 158 | + UserInterface.set_ui_values(self) |
| 159 | + |
| 160 | + def OnClickHelp(self,event): |
| 161 | + webbrowser.open('https://nsoiffer.github.io/MathCAT/users.html') |
| 162 | + |
| 163 | + def OnListBoxCategories(self,event): |
| 164 | + #show the appropriate dialogue page |
| 165 | + self.m_simplebookPanelsCategories.SetSelection(self.m_listBoxPreferencesTopic.GetSelection()) |
| 166 | + |
| 167 | + def MathCATPreferencesDialogOnCharHook(self,event): |
| 168 | + #designed choice is that Enter is the same as clicking OK, and Escape is the same as clicking Cancel |
| 169 | + keyCode = event.GetKeyCode() |
| 170 | + if keyCode == wx.WXK_ESCAPE: |
| 171 | + UserInterface.OnClickCancel(self,event) |
| 172 | + if keyCode == wx.WXK_RETURN: |
| 173 | + UserInterface.OnClickOK(self,event) |
| 174 | + event.Skip() |
| 175 | + |
| 176 | +#if there is already an instance of MathCATPreferences running then terminate |
| 177 | +try: |
| 178 | + me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running |
| 179 | +except: |
| 180 | + os.sys.exit(-2) |
| 181 | + |
| 182 | +app = wx.App(False) |
| 183 | +#get the default preferences (we don't write to this file) |
| 184 | +load_default_preferences() |
| 185 | +#having grabbed the default values, let's see if there is a preferences file for this user |
| 186 | +load_user_preferences() |
| 187 | +frame = UserInterface(None) |
| 188 | +frame.Show(True) |
| 189 | +app.MainLoop() |
0 commit comments