Commit 36d027d6 authored by dmMaze's avatar dmMaze
Browse files

impl open *.json

parent 2bc7c6ef
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -44,9 +44,12 @@ class ProjImgTrans:
    def proj_name(self) -> str:
        return self.type+'_'+osp.basename(self.directory)

    def load(self, directory: str) -> bool:
    def load(self, directory: str, json_path: str = None) -> bool:
        self.directory = directory
        if json_path is None:
            self.proj_path = osp.join(self.directory, self.proj_name() + '.json')
        else:
            self.proj_path = json_path
        new_proj = False
        if not osp.exists(self.proj_path):
            new_proj = True
@@ -105,6 +108,15 @@ class ProjImgTrans:
            if len(self.pages) > 0:
                self.set_current_img_byidx(0)

    def load_from_json(self, json_path: str):
        old_dir = self.directory
        directory = osp.dirname(json_path)
        try:
            self.load(directory, json_path=json_path)
        except Exception as e:
            self.load(old_dir)
            raise ProjectLoadFailureException(e)

    def set_current_img(self, imgname: str):
        if imgname is not None:
            if imgname not in self.pages:
+31 −10
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ class PageListView(QListWidget):

class MainWindow(QMainWindow):

    proj_directory = None
    imgtrans_proj: ProjImgTrans = ProjImgTrans()
    save_on_page_changed = True
    opening_dir = False
@@ -56,12 +55,12 @@ class MainWindow(QMainWindow):
        self.showMaximized()

        if open_dir != '' and osp.exists(open_dir):
            self.openDir(open_dir)
            self.openProj(open_dir)
        elif self.config.open_recent_on_startup:
            if len(self.leftBar.recent_proj_list) > 0:
                proj_dir = self.leftBar.recent_proj_list[0]
                if osp.exists(proj_dir):
                    self.openDir(proj_dir)
                    self.OpenProj(proj_dir)

    def setupThread(self):
        self.imsave_thread = ImgSaveThread()
@@ -80,6 +79,7 @@ class MainWindow(QMainWindow):
        self.leftBar.configChecked.connect(self.setupConfigUI)
        
        self.leftBar.open_dir.connect(self.openDir)
        self.leftBar.open_json_proj.connect(self.openJsonProj)
        self.leftBar.save_proj.connect(self.save_proj)
        self.leftBar.export_doc.connect(self.on_export_doc)
        self.leftBar.import_doc.connect(self.on_import_doc)
@@ -225,21 +225,41 @@ class MainWindow(QMainWindow):
    def setupConfigUI(self):
        self.centralStackWidget.setCurrentIndex(1)

    def OpenProj(self, proj_path: str):
        if osp.isdir(proj_path):
            self.openDir(proj_path)
        else:
            self.openJsonProj(proj_path)

    def openDir(self, directory: str):
        self.opening_dir = True
        try:
            self.st_manager.clearSceneTextitems()
            self.opening_dir = True
            self.imgtrans_proj.load(directory)
            self.st_manager.clearSceneTextitems()
            self.titleBar.setTitleContent(osp.basename(directory))
            self.updatePageList()
            self.opening_dir = False
        except Exception as e:
            self.opening_dir = False
            LOGGER.exception(e)
            LOGGER.warning("Failed to load project from " + directory)
            self.dl_manager.handleRunTimeException(self.tr('Failed to load project ') + directory, '')
            return
        self.proj_directory = directory
        self.titleBar.setTitleContent(osp.basename(directory))

    def openJsonProj(self, json_path: str):
        try:
            self.opening_dir = True
            self.imgtrans_proj.load_from_json(json_path)
            self.st_manager.clearSceneTextitems()
            self.leftBar.updateRecentProjList(self.imgtrans_proj.proj_path)
            self.updatePageList()
            self.titleBar.setTitleContent(osp.basename(self.imgtrans_proj.proj_path))
            self.opening_dir = False
        except Exception as e:
            self.opening_dir = False
            LOGGER.exception(e)
            LOGGER.warning("Failed to load project from " + json_path)
            self.dl_manager.handleRunTimeException(self.tr('Failed to load project ') + json_path, '')
        
    def updatePageList(self):
        if self.pageList.count() != 0:
@@ -248,7 +268,7 @@ class MainWindow(QMainWindow):
            item_func = lambda imgname: QListWidgetItem(imgname)
        else:
            item_func = lambda imgname:\
                QListWidgetItem(QIcon(osp.join(self.proj_directory, imgname)), imgname)
                QListWidgetItem(QIcon(osp.join(self.imgtrans_proj.directory, imgname)), imgname)
        for imgname in self.imgtrans_proj.pages:
            lstitem =  item_func(imgname)
            self.pageList.addItem(lstitem)
@@ -651,3 +671,4 @@ class MainWindow(QMainWindow):
        self.st_manager.updateSceneTextitems()


+8 −7
Original line number Diff line number Diff line
import os.path as osp
from collections import OrderedDict
from typing import List
from typing import List, Union

from .stylewidgets import Widget, PaintQSlider
from .constants import TITLEBAR_HEIGHT, WINDOW_BORDER_WIDTH, BOTTOMBAR_HEIGHT, DRAG_DIR_NONE, DRAG_DIR_VER, DRAG_DIR_BDIAG, DRAG_DIR_FDIAG, LEFTBAR_WIDTH, LEFTBTN_WIDTH
@@ -123,6 +123,7 @@ class LeftBar(Widget):
    imgTransChecked = Signal()
    configChecked = Signal()
    open_dir = Signal(str)
    open_json_proj = Signal(str)
    save_proj = Signal()
    save_config = Signal()
    run_imgtrans = Signal()
@@ -210,7 +211,7 @@ class LeftBar(Widget):
            self.recentMenu.addAction(action)
            action.triggered.connect(self.recentActionTriggered)

    def updateRecentProjList(self, proj_list: List[str]):
    def updateRecentProjList(self, proj_list: Union[str, List[str]]):
        if len(proj_list) == 0:
            return
        if isinstance(proj_list, str):
@@ -268,17 +269,17 @@ class LeftBar(Widget):
            self.recentMenu.removeAction(self.sender())
        
    def onOpenFolder(self) -> None:
        # newdir = str(QFileDialog.getExistingDirectory(self, "Select Directory")).replace("/", "\\")
        dialog = QFileDialog()
        dialog.setDefaultSuffix('.jpg')
        folder_path = str(dialog.getExistingDirectory(self, "Select Directory"))
        folder_path = str(dialog.getExistingDirectory(self, self.tr("Select Directory")))
        if osp.exists(folder_path):
            self.updateRecentProjList(folder_path)
            self.open_dir.emit(folder_path)
        # self.open_dir.emit(folder_path)

    def onOpenProj(self):
        self.open_dir.emit()
        dialog = QFileDialog()
        json_path = str(dialog.getOpenFileUrl(self.parent(), self.tr('Import *.docx'), filter="*.json")[0].toLocalFile())
        if osp.exists(json_path):
            self.open_json_proj.emit(json_path)

    def onSaveProj(self):
        self.save_proj.emit()