def __init__(self, engine, dictionary=None): super(AddTranslation, self).__init__() self.setupUi(self) self._engine = engine dictionaries = [d.get_path() for d in engine.dictionaries.dicts] self.dictionary.addItems(shorten_path(d) for d in dictionaries) if dictionary is None: self.dictionary.setCurrentIndex(0) else: assert dictionary in dictionaries self.dictionary.setCurrentIndex(dictionaries.index(dictionary)) engine.signal_connect('config_changed', self.on_config_changed) self.on_config_changed(engine.config) self.installEventFilter(self) self.strokes.installEventFilter(self) self.translation.installEventFilter(self) with engine: self._original_state = self.EngineState(None, engine.translator_state, engine.starting_stroke_state) engine.clear_translator_state() self._strokes_state = self.EngineState(self._dictionary_filter, engine.translator_state, StartingStrokeState(True, False)) engine.clear_translator_state() self._translations_state = self.EngineState(None, engine.translator_state, StartingStrokeState(True, False)) self._engine_state = self._original_state self._focus = None self.restore_state() self.finished.connect(self.save_state)
def _update_items(self, dictionaries=None, reverse_order=None): if dictionaries is not None: self._dictionaries = dictionaries if reverse_order is not None: self._reverse_order = reverse_order iterable = self._dictionaries if self._reverse_order: iterable = reversed(iterable) self.dictionary.clear() for d in iterable: item = shorten_path(d.path) if not d.enabled: item += ' [' + _('disabled') + ']' self.dictionary.addItem(item) selected_index = 0 if self._selected_dictionary is None: # No user selection, select first enabled dictionary. for n, d in enumerate(self._dictionaries): if d.enabled: selected_index = n break else: # Keep user selection. for n, d in enumerate(self._dictionaries): if d.path == self._selected_dictionary: selected_index = n break if self._reverse_order: selected_index = self.dictionary.count() - selected_index - 1 self.dictionary.setCurrentIndex(selected_index)
def __init__(self, engine, dictionary=None): super(AddTranslation, self).__init__() self.setupUi(self) self._engine = engine dictionaries = [d.get_path() for d in engine.dictionaries.dicts] self.dictionary.addItems(shorten_path(d) for d in dictionaries) if dictionary is None: self.dictionary.setCurrentIndex(0) else: assert dictionary in dictionaries self.dictionary.setCurrentIndex(dictionaries.index(dictionary)) engine.signal_connect('config_changed', self.on_config_changed) self.on_config_changed(engine.config) self.installEventFilter(self) self.strokes.installEventFilter(self) self.translation.installEventFilter(self) with engine: self._original_state = self.EngineState( None, engine.translator_state, engine.starting_stroke_state) engine.clear_translator_state() self._strokes_state = self.EngineState( self._dictionary_filter, engine.translator_state, StartingStrokeState(True, False)) engine.clear_translator_state() self._translations_state = self.EngineState( None, engine.translator_state, StartingStrokeState(True, False)) self._engine_state = self._original_state self._focus = None self.restore_state() self.finished.connect(self.save_state)
def _update_items(self, dictionaries=None, reverse_order=None): if dictionaries is not None: self._dictionaries = dictionaries if reverse_order is not None: self._reverse_order = reverse_order iterable = self._dictionaries if self._reverse_order: iterable = reversed(iterable) self.dictionary.clear() for d in iterable: item = shorten_path(d.path) if not d.enabled: # i18n: Widget: “AddTranslationWidget”. item = _('{dictionary} (disabled)').format(dictionary=item) self.dictionary.addItem(item) selected_index = 0 if self._selected_dictionary is None: # No user selection, select first enabled dictionary. for n, d in enumerate(self._dictionaries): if d.enabled: selected_index = n break else: # Keep user selection. for n, d in enumerate(self._dictionaries): if d.path == self._selected_dictionary: selected_index = n break if self._reverse_order: selected_index = self.dictionary.count() - selected_index - 1 self.dictionary.setCurrentIndex(selected_index)
def createEditor(self, parent, option, index): if index.column() == 2: dictionary_paths = [ shorten_path(dictionary.get_path()) for dictionary in self._dictionary_list ] combo = QComboBox(parent) combo.addItems(dictionary_paths) return combo return super(DictionaryItemDelegate, self).createEditor(parent, option, index)
def set_dictionary_file_names(self, filenames): system_name = self.get_system_name() section = SYSTEM_CONFIG_SECTION % system_name option = SYSTEM_DICTIONARIES_OPTION if filenames is None: self._config.remove_option(section, option) else: self._set(section, option, json.dumps( list(shorten_path(path) for path in filenames) ))
def set_dictionary_file_names(self, filenames): system_name = self.get_system_name() section = SYSTEM_CONFIG_SECTION % system_name option = SYSTEM_DICTIONARIES_OPTION if filenames is None: self._config.remove_option(section, option) else: self._set( section, option, json.dumps(list(shorten_path(path) for path in filenames)))
def on_browse(self): filename_suggestion = self.path.text() filename = QFileDialog.getSaveFileName( self, self._dialog_title, filename_suggestion, self._dialog_filter, )[0] if not filename: return self.path.setText(shorten_path(filename)) self.valueChanged.emit(filename)
def createEditor(self, parent, option, index): if index.column() == _COL_DICT: dictionary_paths = [ shorten_path(dictionary.get_path()) for dictionary in self._dictionary_list if not dictionary.readonly ] combo = QComboBox(parent) combo.addItems(dictionary_paths) return combo return super(DictionaryItemDelegate, self).createEditor(parent, option, index)
def data(self, index, role): if not index.isValid() or role not in (Qt.EditRole, Qt.DisplayRole): return None item = self._entries[index.row()] column = index.column() if column == _COL_STENO: return '/'.join(item.strokes) if column == _COL_TRANS: return escape_translation(item.translation) if column == _COL_DICT: return shorten_path(item.dictionary.get_path())
def __init__(self, engine, dictionary=None): super(AddTranslation, self).__init__() self.setupUi(self) self._engine = engine dictionaries = [d.get_path() for d in engine.dictionaries.dicts] self.dictionary.addItems(shorten_path(d) for d in dictionaries) if dictionary is None: self.dictionary.setCurrentIndex(0) else: assert dictionary in dictionaries self.dictionary.setCurrentIndex(dictionaries.index(dictionary)) engine.signal_connect('config_changed', self.on_config_changed) self.on_config_changed(engine.config) self.installEventFilter(self) self.strokes.installEventFilter(self) self.translation.installEventFilter(self) # Pre-populate the strokes or translations with last stroke/word. last_translation = None for t in reversed(engine.translator_state.translations): # Find the last undoable stroke. if t.has_undo(): last_translation = t break if last_translation: # Grab the last-formatted word last_word = last_translation.formatting[-1].word if last_word: # If the last translation was created with the dictionary... if last_translation.english: self.translation.setText(last_word.strip()) self.on_translation_edited() # Otherwise, it's just raw steno else: self.strokes.setText(last_word.strip()) self.on_strokes_edited() self.strokes.selectAll() with engine: self._original_state = self.EngineState(None, engine.translator_state, engine.starting_stroke_state) engine.clear_translator_state() self._strokes_state = self.EngineState(self._dictionary_filter, engine.translator_state, StartingStrokeState(True, False)) engine.clear_translator_state() self._translations_state = self.EngineState(None, engine.translator_state, StartingStrokeState(True, False)) self._engine_state = self._original_state self._focus = None self.restore_state() self.finished.connect(self.save_state)
def createEditor(self, parent, option, index): if index.column() == _COL_DICT: dictionary_paths = [ shorten_path(dictionary.path) for dictionary in self._dictionary_list if not dictionary.readonly ] combo = QComboBox(parent) combo.addItems(dictionary_paths) return combo return super().createEditor(parent, option, index)
def __init__(self, engine, dictionary=None): super(AddTranslation, self).__init__() self.setupUi(self) self._engine = engine dictionaries = [d.get_path() for d in engine.dictionaries.dicts] self.dictionary.addItems(shorten_path(d) for d in dictionaries) if dictionary is None: self.dictionary.setCurrentIndex(0) else: assert dictionary in dictionaries self.dictionary.setCurrentIndex(dictionaries.index(dictionary)) engine.signal_connect('config_changed', self.on_config_changed) self.on_config_changed(engine.config) self.installEventFilter(self) self.strokes.installEventFilter(self) self.translation.installEventFilter(self) # Pre-populate the strokes or translations with last stroke/word. last_translation = None for t in reversed(engine.translator_state.translations): # Find the last undoable stroke. if t.has_undo(): last_translation = t break if last_translation: # Grab the last-formatted word last_word = last_translation.formatting[-1].word if last_word: # If the last translation was created with the dictionary... if last_translation.english: self.translation.setText(last_word.strip()) self.on_translation_edited() # Otherwise, it's just raw steno else: self.strokes.setText(last_word.strip()) self.on_strokes_edited() self.strokes.selectAll() with engine: self._original_state = self.EngineState( None, engine.translator_state, engine.starting_stroke_state) engine.clear_translator_state() self._strokes_state = self.EngineState( self._dictionary_filter, engine.translator_state, StartingStrokeState(True, False)) engine.clear_translator_state() self._translations_state = self.EngineState( None, engine.translator_state, StartingStrokeState(True, False)) self._engine_state = self._original_state self._focus = None self.restore_state() self.finished.connect(self.save_state)
def _results_to_string(self, results, translation_first = False): current_dictionary_index = None display_strings = [] for (steno, translation, num_stroke, dictionary, dictionary_index) in results: if current_dictionary_index != dictionary_index: current_dictionary_index = dictionary_index display_strings.append(shorten_path(dictionary.path)) if translation_first: display_strings.append(' ' + translation + ': ' + steno) else: display_strings.append(' ' + steno + ': ' + translation) return '\n'.join(display_strings)
def createEditor(self, parent, option, index): if index.column() == _COL_DICT: dictionary_paths = [ shorten_path(dictionary.path) for dictionary in self._dictionary_list if not dictionary.readonly ] combo = QComboBox(parent) combo.addItems(dictionary_paths) return combo widget = super().createEditor(parent, option, index) if index.column() == _COL_STENO: widget.setValidator(StenoValidator()) return widget
def _update_dictionaries(self, dictionaries, record=True, save=True, scroll=False): if dictionaries == self._dictionaries: return False if save: self._engine.config = { 'dictionary_file_names': dictionaries } if record: self._states.append(self._dictionaries) self.action_Undo.setEnabled(True) self._dictionaries = dictionaries self.table.setRowCount(0) item = None for row, filename in enumerate(dictionaries): self.table.insertRow(row) item = QTableWidgetItem(shorten_path(filename)) item.setFlags(item.flags() & ~Qt.ItemIsEditable) self.table.setItem(row, 0, item) if scroll and item is not None: self.table.setCurrentItem(item) return True
def _update(self, config_update=None, full=False, reset_machine=False): original_config = self._config.as_dict() # Update configuration. if config_update is not None: self._config.update(**config_update) config = self._config.as_dict() else: config = original_config # Create configuration update. if full: config_update = config else: config_update = { option: value for option, value in config.items() if value != original_config[option] } if 'machine_type' in config_update: for opt in ( 'machine_specific_options', 'system_keymap', ): config_update[opt] = config[opt] # Update logging. log.set_stroke_filename(config['log_file_name']) log.enable_stroke_logging(config['enable_stroke_logging']) log.enable_translation_logging(config['enable_translation_logging']) # Update output. self._formatter.set_space_placement(config['space_placement']) self._formatter.start_attached = config['start_attached'] self._formatter.start_capitalized = config['start_capitalized'] self._translator.set_min_undo_length(config['undo_levels']) # Update system. system_name = config['system_name'] if system.NAME != system_name: log.info('loading system: %s', system_name) system.setup(system_name) # Update machine. update_keymap = False start_machine = False machine_params = MachineParams(config['machine_type'], config['machine_specific_options'], config['system_keymap']) # Do not reset if only the keymap changed. if self._machine_params is None or \ self._machine_params.type != machine_params.type or \ self._machine_params.options != machine_params.options: reset_machine = True if reset_machine: if self._machine is not None: self._machine.stop_capture() self._machine = None machine_type = config['machine_type'] machine_options = config['machine_specific_options'] try: machine_class = registry.get_plugin('machine', machine_type).obj except Exception as e: raise InvalidConfigurationError(str(e)) log.info('setting machine: %s', machine_type) self._machine = machine_class(machine_options) self._machine.set_suppression(self._is_running) self._machine.add_state_callback(self._machine_state_callback) self._machine.add_stroke_callback(self._machine_stroke_callback) self._machine_params = machine_params update_keymap = True start_machine = True elif self._machine is not None: update_keymap = 'system_keymap' in config_update if update_keymap: machine_keymap = config['system_keymap'] if machine_keymap is not None: self._machine.set_keymap(machine_keymap) if start_machine: self._machine.start_capture() # Update running extensions. enabled_extensions = config['enabled_extensions'] running_extensions = set(self._running_extensions) self._stop_extensions(running_extensions - enabled_extensions) self._start_extensions(enabled_extensions - running_extensions) # Trigger `config_changed` hook. if config_update: self._trigger_hook('config_changed', config_update) # Update dictionaries. config_dictionaries = OrderedDict( (d.path, d) for d in config['dictionaries'] ) copy_default_dictionaries(config_dictionaries.keys()) # Start by unloading outdated dictionaries. self._dictionaries_manager.unload_outdated() self._set_dictionaries([ d for d in self._dictionaries.dicts if d.path in config_dictionaries and \ d.path in self._dictionaries_manager ]) # And then (re)load all dictionaries. dictionaries = [] for result in self._dictionaries_manager.load(config_dictionaries.keys()): if isinstance(result, DictionaryLoaderException): d = ErroredDictionary(result.path, result.exception) # Only show an error if it's new. if d != self._dictionaries.get(result.path): log.error('loading dictionary `%s` failed: %s', shorten_path(result.path), str(result.exception)) else: d = result d.enabled = config_dictionaries[d.path].enabled dictionaries.append(d) self._set_dictionaries(dictionaries)
def set_log_file_name(self, filename): filename = shorten_path(filename) self._set(LOGGING_CONFIG_SECTION, LOG_FILE_OPTION, filename)
def short_path(self): return shorten_path(self.path)
def _update(self, config_update=None, full=False, reset_machine=False): original_config = self._config.as_dict() # Update configuration. if config_update is not None: self._config.update(**config_update) config = self._config.as_dict() else: config = original_config # Create configuration update. if full: config_update = config else: config_update = { option: value for option, value in config.items() if value != original_config[option] } # Update logging. log.set_stroke_filename(config['log_file_name']) log.enable_stroke_logging(config['enable_stroke_logging']) log.enable_translation_logging(config['enable_translation_logging']) # Update output. self._formatter.set_space_placement(config['space_placement']) self._formatter.start_attached = config['start_attached'] self._formatter.start_capitalized = config['start_capitalized'] self._translator.set_min_undo_length(config['undo_levels']) # Update system. system_name = config['system_name'] if system.NAME != system_name: log.info('loading system: %s', system_name) system.setup(system_name) # Update machine. update_keymap = False start_machine = False machine_params = MachineParams(config['machine_type'], config['machine_specific_options'], config['system_keymap']) # Do not reset if only the keymap changed. if self._machine_params is None or \ self._machine_params.type != machine_params.type or \ self._machine_params.options != machine_params.options: reset_machine = True if reset_machine: if self._machine is not None: self._machine.stop_capture() self._machine = None machine_type = config['machine_type'] machine_options = config['machine_specific_options'] machine_class = registry.get_plugin('machine', machine_type).obj log.info('setting machine: %s', machine_type) self._machine = machine_class(machine_options) self._machine.set_suppression(self._is_running) self._machine.add_state_callback(self._machine_state_callback) self._machine.add_stroke_callback(self._machine_stroke_callback) self._machine_params = machine_params update_keymap = True start_machine = True elif self._machine is not None: update_keymap = 'system_keymap' in config_update if update_keymap: machine_keymap = config['system_keymap'] if machine_keymap is not None: self._machine.set_keymap(machine_keymap) if start_machine: self._machine.start_capture() # Update running extensions. enabled_extensions = config['enabled_extensions'] running_extensions = set(self._running_extensions) self._stop_extensions(running_extensions - enabled_extensions) self._start_extensions(enabled_extensions - running_extensions) # Trigger `config_changed` hook. if config_update: self._trigger_hook('config_changed', config_update) # Update dictionaries. config_dictionaries = OrderedDict( (d.path, d) for d in config['dictionaries']) copy_default_dictionaries(config_dictionaries.keys()) # Start by unloading outdated dictionaries. self._dictionaries_manager.unload_outdated() self._set_dictionaries([ d for d in self._dictionaries.dicts if d.path in config_dictionaries and \ d.path in self._dictionaries_manager ]) # And then (re)load all dictionaries. dictionaries = [] for result in self._dictionaries_manager.load( config_dictionaries.keys()): if isinstance(result, DictionaryLoaderException): d = ErroredDictionary(result.path, result.exception) # Only show an error if it's new. if d != self._dictionaries.get(result.path): log.error('loading dictionary `%s` failed: %s', shorten_path(result.path), str(result.exception)) else: d = result d.enabled = config_dictionaries[d.path].enabled dictionaries.append(d) self._set_dictionaries(dictionaries)
def setter(config, key, value): config._set(section, option, shorten_path(value))
def set_ime_exe_file(self, filename): filename = shorten_path(filename) self._set(IME_CONFIG_SECTION, IME_EXE_FILE_OPTION, filename)
def set_ime_words_csv_file(self, filename): filename = shorten_path(filename) self._set(IME_CONFIG_SECTION, IME_WORDS_CSV_FILE_OPTION, filename)
def set_dictionary_file_names(self, filenames): self._update(DICTIONARY_CONFIG_SECTION, ((DICTIONARY_FILE_OPTION + str(n), shorten_path(path)) for n, path in enumerate(filenames, start=1)))
def setValue(self, value): self.path.setText(shorten_path(value))
def set_dictionary_file_names(self, filenames): self._update(DICTIONARY_CONFIG_SECTION, ( (DICTIONARY_FILE_OPTION + str(n), shorten_path(path)) for n, path in enumerate(filenames, start=1) ))