Commit 85a825e5 authored by dmMaze's avatar dmMaze
Browse files

restore editing focus after saving, fix redo/undo stack states #504

parent 91f2ee40
Loading
Loading
Loading
Loading
+59 −18
Original line number Diff line number Diff line
@@ -243,9 +243,7 @@ class Canvas(QGraphicsScene):
        self.rubber_band_origin = None

        self.draw_undo_stack = QUndoStack(self)
        self.draw_undo_stack.indexChanged.connect(self.on_drawstack_changed)
        self.text_undo_stack = QUndoStack(self)
        self.text_undo_stack.indexChanged.connect(self.on_textstack_changed)
        self.saved_drawundo_step = 0
        self.saved_textundo_step = 0

@@ -292,6 +290,8 @@ class Canvas(QGraphicsScene):

        self.saved_textundo_step = 0
        self.saved_drawundo_step = 0
        self.num_pushed_textstep = 0
        self.num_pushed_drawstep = 0

        self.clipboard_blks: List[TextBlock] = []

@@ -863,39 +863,76 @@ class Canvas(QGraphicsScene):
            return self.draw_undo_stack
        return None

    def push_undo_command(self, command: QUndoCommand):
        undo_stack = self.get_active_undostack()
        if undo_stack is not None:
            undo_stack.push(command)
    def push_undo_command(self, command: QUndoCommand, update_pushed_step=True):
        if self.textEditMode():
            self.push_text_command(command, update_pushed_step)
        elif self.drawMode():
            self.push_draw_command(command, update_pushed_step)
        else:
            return

    def on_drawstack_changed(self, index: int):
        if index != self.saved_drawundo_step:
    def push_draw_command(self, command: QUndoCommand, update_pushed_step=True):
        if command is not None:
            self.draw_undo_stack.push(command)
        if update_pushed_step:
            self.num_pushed_drawstep += 1
            self.on_drawstack_changed()

    def push_text_command(self, command: QUndoCommand, update_pushed_step=True):
        if command is not None:
            self.text_undo_stack.push(command)
        if update_pushed_step:
            self.num_pushed_textstep += 1
            self.on_textstack_changed()

    def on_drawstack_changed(self):
        if self.num_pushed_drawstep != self.saved_drawundo_step:
            self.setProjSaveState(True)
        elif self.text_undo_stack.index() == self.saved_textundo_step:
        elif self.num_pushed_textstep == self.saved_textundo_step:
            self.setProjSaveState(False)

    def on_textstack_changed(self, index: int):
        if index != self.saved_textundo_step:
    def on_textstack_changed(self):
        if self.num_pushed_textstep != self.saved_textundo_step:
            self.setProjSaveState(True)
        elif self.draw_undo_stack.index() == self.saved_drawundo_step:
        elif self.num_pushed_drawstep == self.saved_drawundo_step:
            self.setProjSaveState(False)
        self.textstack_changed.emit()

    def redo_textedit(self):
        self.num_pushed_textstep += 1
        self.text_undo_stack.redo()

    def undo_textedit(self):
        self.num_pushed_textstep -= 1
        self.text_undo_stack.undo()

    def redo(self):
        undo_stack = self.get_active_undostack()
        if self.textEditMode():
            undo_stack = self.text_undo_stack
            self.num_pushed_textstep += 1
            self.on_textstack_changed()
        elif self.drawMode():
            undo_stack = self.draw_undo_stack
            self.num_pushed_drawstep += 1
            self.on_drawstack_changed()
        else:
            return
        if undo_stack is not None:
            undo_stack.redo()
            if undo_stack == self.text_undo_stack:
                self.txtblkShapeControl.updateBoundingRect()

    def undo(self):
        undo_stack = self.get_active_undostack()
        if self.textEditMode():
            undo_stack = self.text_undo_stack
            self.num_pushed_textstep -= 1
            self.on_textstack_changed()
        elif self.drawMode():
            undo_stack = self.draw_undo_stack
            self.num_pushed_drawstep -= 1
            self.on_drawstack_changed()
        else:
            return
        if undo_stack is not None:
            undo_stack.undo()
            if undo_stack == self.text_undo_stack:
@@ -905,24 +942,28 @@ class Canvas(QGraphicsScene):
        if update_saved_step:
            self.saved_drawundo_step = 0
            self.saved_textundo_step = 0
            self.num_pushed_textstep = 0
            self.num_pushed_drawstep = 0
        self.draw_undo_stack.clear()
        self.text_undo_stack.clear()

    def clear_text_stack(self):
        self.num_pushed_textstep = 0
        self.text_undo_stack.clear()

    def clear_draw_stack(self):
        self.num_pushed_drawstep = 0
        self.draw_undo_stack.clear()

    def update_saved_undostep(self):
        self.saved_drawundo_step = self.draw_undo_stack.index()
        self.saved_textundo_step = self.text_undo_stack.index()
        self.saved_drawundo_step = self.num_pushed_drawstep
        self.saved_textundo_step = self.num_pushed_textstep

    def text_change_unsaved(self) -> bool:
        return self.saved_textundo_step != self.text_undo_stack.index()
        return self.saved_textundo_step != self.num_pushed_textstep

    def draw_change_unsaved(self) -> bool:
        return self.saved_drawundo_step != self.draw_undo_stack.index()
        return self.saved_drawundo_step != self.num_pushed_drawstep

    def prepareClose(self):
        self.blockSignals(True)
+1 −1
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ class RunBlkTransCommand(QUndoCommand):
        self.empty_command = None
        if mode > 1:
            self.empty_command = EmptyCommand()
            canvas.draw_undo_stack.push(self.empty_command)
            canvas.push_draw_command(self.empty_command)

        self.op_counter = -1
        self.blkitems = blkitems
+19 −3
Original line number Diff line number Diff line
@@ -641,7 +641,6 @@ class MainWindow(mainwindow_cls):
        self.ocrSubWidget.show()

    def on_req_update_pagetext(self):
        self.global_search_widget.searched_textstack_step = self.canvas.text_undo_stack.index()
        if self.canvas.text_change_unsaved():
            self.st_manager.updateTextBlkList()

@@ -714,7 +713,6 @@ class MainWindow(mainwindow_cls):
    def save_proj(self):
        if self.leftBar.imgTransChecker.isChecked()\
            and self.imgtrans_proj.directory is not None:
            
            self.conditional_manual_save()

    def saveCurrentPage(self, update_scene_text=True, save_proj=True, restore_interface=False, save_rst_only=False):
@@ -722,6 +720,14 @@ class MainWindow(mainwindow_cls):
        if not self.imgtrans_proj.img_valid:
            return
        
        if restore_interface:
            set_canvas_focus = self.canvas.hasFocus()
            sel_textitem = self.canvas.selected_text_items()
            n_sel_textitems = len(sel_textitem)
            editing_textitem = None
            if n_sel_textitems == 1 and sel_textitem[0].isEditing():
                editing_textitem = sel_textitem[0]
        
        if update_scene_text:
            self.st_manager.updateTextBlkList()
        
@@ -769,6 +775,16 @@ class MainWindow(mainwindow_cls):
                self.bottomBar.textblockChecker.click()
            if hide_tsc:
                self.st_manager.txtblkShapeControl.show()
            if set_canvas_focus:
                self.canvas.setFocus()
            if n_sel_textitems > 0:
                self.canvas.block_selection_signal = True
                for blk in sel_textitem:
                    blk.setSelected(True)
                self.st_manager.on_incanvas_selection_changed()
                self.canvas.block_selection_signal = False
            if editing_textitem is not None:
                editing_textitem.startEdit()
        
    def translatorStatusBtnPressed(self):
        self.leftBar.configChecker.setChecked(True)
@@ -1139,7 +1155,7 @@ class MainWindow(mainwindow_cls):

    def on_global_replace_finished(self):
        rt = self.global_search_widget.replace_thread
        self.canvas.text_undo_stack.push(
        self.canvas.push_text_command(
            GlobalRepalceAllCommand(rt.sceneitem_list, rt.background_list, rt.target_text, self.imgtrans_proj)
        )
        rt.sceneitem_list = None
+5 −3
Original line number Diff line number Diff line
@@ -299,6 +299,7 @@ class RearrangeBlksCommand(QUndoCommand):
            pw_ct.show()
            self.ctrl.textEditList.ensureWidgetVisible(pw_ct, yMargin=pw.height())


class TextPanel(Widget):
    def __init__(self, app: QApplication, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
@@ -926,25 +927,26 @@ class SceneTextManager(QObject):
    def on_push_textitem_undostack(self, num_steps: int, is_formatting: bool):
        blkitem: TextBlkItem = self.sender()
        e_trans = self.pairwidget_list[blkitem.idx].e_trans if not is_formatting else None
        self.canvas.push_undo_command(TextItemEditCommand(blkitem, e_trans, num_steps))
        self.canvas.push_undo_command(TextItemEditCommand(blkitem, e_trans, num_steps), update_pushed_step=False)

    def on_push_edit_stack(self, num_steps: int):
        edit: Union[TransTextEdit, SourceTextEdit] = self.sender()
        blkitem = self.textblk_item_list[edit.idx] if type(edit) == TransTextEdit else None
        self.canvas.push_undo_command(TextEditCommand(edit, num_steps, blkitem))
        self.canvas.push_undo_command(TextEditCommand(edit, num_steps, blkitem), update_pushed_step=False)

    def on_propagate_textitem_edit(self, pos: int, added_text: str, input_method_used: bool):
        blk_item: TextBlkItem = self.sender()
        edit = self.pairwidget_list[blk_item.idx].e_trans
        propagate_user_edit(blk_item, edit, pos, added_text, input_method_used)
        self.canvas.push_text_command(command=None, update_pushed_step=True)

    def on_propagate_transwidget_edit(self, pos: int, added_text: str, input_method_used: bool):
        edit: TransTextEdit = self.sender()
        blk_item = self.textblk_item_list[edit.idx]
        if blk_item.isEditing():
            blk_item.setTextInteractionFlags(Qt.TextInteractionFlag.NoTextInteraction)
        
        propagate_user_edit(edit, blk_item, pos, added_text, input_method_used)
        self.canvas.push_text_command(command=None, update_pushed_step=True)

    def apply_fontformat(self, fontformat: FontFormat):
        selected_blks = self.canvas.selected_text_items()