Exemplo n.º 1
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        engine = QApplication.instance().engine
        self._engine = engine
        self._dictionaries = []
        self._reverse_order = False
        self._selected_dictionary = None
        engine.signal_connect('config_changed', self.on_config_changed)
        self.on_config_changed(engine.config)
        engine.signal_connect('dictionaries_loaded', self.on_dictionaries_loaded)
        self.on_dictionaries_loaded(self._engine.dictionaries)

        self._special_fmt = (
            '<span style="' +
            'background-color:' + self.palette().base().color().name() +';' +
            'font-family:monospace;' +
            '">%s</span>'
        )

        self.strokes.installEventFilter(self)
        self.translation.installEventFilter(self)

        with engine:

            # Pre-populate the strokes or translations with last stroke/word.
            last_translations = engine.translator_state.translations
            translation = None
            for t in reversed(last_translations):
                # Find the last undoable stroke.
                if t.has_undo():
                    translation = t
                    break
            # Is it a raw stroke?
            if translation is not None and not translation.english:
                # Yes.
                self.strokes.setText(translation.formatting[0].text)
                self.on_strokes_edited()
                self.strokes.selectAll()
            else:
                # No, grab the last-formatted word.
                retro_formatter = RetroFormatter(last_translations)
                last_words = retro_formatter.last_words(strip=True)
                if last_words:
                    self.translation.setText(last_words[0])
                    self.on_translation_edited()

            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
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    def __init__(self, engine, dictionary_path=None):
        super(AddTranslationWidget, self).__init__()
        self.setupUi(self)
        self._engine = engine
        self._dictionaries = []
        self._reverse_order = False
        self._selected_dictionary = dictionary_path
        engine.signal_connect('config_changed', self.on_config_changed)
        self.on_config_changed(engine.config)
        engine.signal_connect('dictionaries_loaded',
                              self.on_dictionaries_loaded)
        self.on_dictionaries_loaded(self._engine.dictionaries)

        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
Exemplo n.º 5
0
 def _clear_state(self, undo=False):
     self._engine.clear_translator_state(undo)
     self._engine.starting_stroke_state = StartingStrokeState(False, False)