fenrir/src/fenrirscreenreader/core/quickMenuManager.py

132 lines
4.9 KiB
Python
Raw Normal View History

2019-02-13 16:14:23 -05:00
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from fenrirscreenreader.core import debug
2019-02-13 17:42:51 -05:00
from fenrirscreenreader.core.settingsData import settingsData
2019-02-13 16:14:23 -05:00
class quickMenuManager():
def __init__(self):
2019-02-13 17:14:05 -05:00
self.position = 0
self.quickMenu = []
2019-02-13 17:42:51 -05:00
self.settings = settingsData
2019-02-13 16:14:23 -05:00
def initialize(self, environment):
2019-02-13 17:14:05 -05:00
self.env = environment
self.loadMenu(self.env['runtime']['settingsManager'].getSetting('menu', 'quickMenu'))
2019-02-13 16:14:23 -05:00
def shutdown(self):
2019-02-13 17:14:05 -05:00
pass
def loadMenu(self, menuString):
self.position = 0
self.quickMenu = []
if menuString == '':
return
entrys = menuString.split(';')
for e in entrys:
entry = e.split('#')
if len(entry) != 2:
continue
2019-02-13 17:42:51 -05:00
try:
t = self.settings[entry[0]][entry[1]]
except:
print(entry[0],entry[1], 'not found')
continue
2019-02-13 17:14:05 -05:00
self.quickMenu.append({'section': entry[0], 'setting': entry[1]})
def nextEntry(self):
if len(self.quickMenu) == 0:
return False
self.position += 1
if self.position >= len(self.quickMenu):
self.position = 0
return True
def prevEntry(self):
if len(self.quickMenu) == 0:
return False
self.position -= 1
if self.position < 0:
self.position = len(self.quickMenu) - 1
return True
def nextValue(self):
if len(self.quickMenu) == 0:
return False
2019-02-13 17:42:51 -05:00
section = self.quickMenu[self.position]['section']
setting = self.quickMenu[self.position]['setting']
valueString = ''
2019-02-13 17:14:05 -05:00
try:
2019-02-13 18:02:31 -05:00
valueString = self.env['runtime']['settingsManager'].getSetting(section, setting)
2019-02-13 17:14:05 -05:00
except:
return False
2019-02-13 17:42:51 -05:00
2019-02-13 17:14:05 -05:00
try:
2019-02-13 17:42:51 -05:00
if isinstance(self.settings[section][setting], str):
value = str(valueString)
return False
elif isinstance(self.settings[section][setting], bool):
if not valueString in ['True','False']:
return False
value = not value
2019-02-13 18:12:29 -05:00
self.env['runtime']['settingsManager'].setSetting(section, setting, str(value))
2019-02-13 17:42:51 -05:00
elif isinstance(self.settings[section][setting], int):
value = int(valueString)
value += 1
2019-02-13 18:12:29 -05:00
self.env['runtime']['settingsManager'].setSetting(section, setting, str(value))
2019-02-13 17:42:51 -05:00
elif isinstance(self.settings[section][setting], float):
value = float(valueString)
value += 0.05
if value > 1.0:
value = 1.0
2019-02-13 18:15:04 -05:00
self.env['runtime']['settingsManager'].setSetting(section, setting, str(value)[:4])
2019-02-13 18:12:29 -05:00
except Exception as e:
2019-02-13 17:27:20 -05:00
return False
2019-02-13 17:14:05 -05:00
return True
def prevValue(self):
if len(self.quickMenu) == 0:
return False
2019-02-13 17:42:51 -05:00
section = self.quickMenu[self.position]['section']
setting = self.quickMenu[self.position]['setting']
valueString = ''
2019-02-13 17:14:05 -05:00
try:
2019-02-13 17:42:51 -05:00
valueString = self.env['runtime']['settingsManager'].getSetting(section, setting)
2019-02-13 17:14:05 -05:00
except:
return False
try:
2019-02-13 17:42:51 -05:00
if isinstance(self.settings[section][setting], str):
value = str(valueString)
return False
elif isinstance(self.settings[section][setting], bool):
if not valueString in ['True','False']:
return False
value = not value
2019-02-13 18:12:29 -05:00
self.env['runtime']['settingsManager'].setSetting(section, setting, str(value))
2019-02-13 17:42:51 -05:00
elif isinstance(self.settings[section][setting], int):
value = int(valueString)
value -= 1
if value < 0:
value = 0
2019-02-13 18:12:29 -05:00
self.env['runtime']['settingsManager'].setSetting(section, setting, str(value))
2019-02-13 17:42:51 -05:00
elif isinstance(self.settings[section][setting], float):
value = float(valueString)
value -= 0.05
if value < 0.0:
value = 0.0
2019-02-13 18:15:04 -05:00
self.env['runtime']['settingsManager'].setSetting(section, setting, str(value)[:4])
2019-02-13 18:12:29 -05:00
except Exception as e:
2019-02-13 17:27:20 -05:00
return False
2019-02-13 17:14:05 -05:00
return True
def getCurrentEntry(self):
if len(self.quickMenu) == 0:
return ''
try:
return _(self.quickMenu[self.position]['section']) + ' ' + _(self.quickMenu[self.position]['setting'])
except:
return _('setting invalid')
def getCurrentValue(self):
if len(self.quickMenu) == 0:
return ''
try:
return self.env['runtime']['settingsManager'].getSetting(self.quickMenu[self.position]['section'], self.quickMenu[self.position]['setting'])
except:
return _('setting value invalid')