From 29cb1bc03a5b6082279a7d4abfabc7fa1f43613b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Wed, 18 Sep 2024 15:55:26 +0800 Subject: [PATCH] ... --- .../LunaTranslator/gui/inputdialog.py | 36 ++++--- .../LunaTranslator/gui/usefulwidget.py | 102 ++++++++++-------- .../LunaTranslator/metadata/abstract.py | 2 +- .../LunaTranslator/metadata/vndb.py | 2 +- plugins/CMakeLists.txt | 4 +- 5 files changed, 80 insertions(+), 66 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/inputdialog.py b/LunaTranslator/LunaTranslator/gui/inputdialog.py index e3c9e474..88ad1130 100644 --- a/LunaTranslator/LunaTranslator/gui/inputdialog.py +++ b/LunaTranslator/LunaTranslator/gui/inputdialog.py @@ -62,7 +62,7 @@ class noundictconfigdialog1(LDialog): self.model = LStandardItemModel() self.model.setHorizontalHeaderLabels(label) - table = TableViewW(self) + table = TableViewW(self, copypaste=True, updown=True) table.setModel(self.model) table.horizontalHeader().setSectionResizeMode(3, QHeaderView.ResizeMode.Stretch) table.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.Stretch) @@ -72,7 +72,6 @@ class noundictconfigdialog1(LDialog): table.horizontalHeader().setSectionResizeMode( 0, QHeaderView.ResizeMode.ResizeToContents ) - table.setsimplemenu() self.table = table for row, item in enumerate(reflist): @@ -122,19 +121,24 @@ class noundictconfigdialog1(LDialog): def __setindexwidget(self, index: QModelIndex, data): if index.column() == 0: + data = {"regex": data.lower() == "true"} self.table.setIndexWidget(index, getsimpleswitch(data, "regex")) - if index.column() == 1: + elif index.column() == 1: + data = {"escape": data.lower() == "true"} self.table.setIndexWidget(index, getsimpleswitch(data, "escape")) + else: + self.table.model().setItem(index.row(), index.column(), QStandardItem(data)) def __getindexwidgetdata(self, index: QModelIndex): if index.column() == 0: - return {"regex": self.table.indexWidgetX(index).isChecked()} + return self.table.indexWidgetX(index).isChecked() if index.column() == 1: - return {"escape": self.table.indexWidgetX(index).isChecked()} + return self.table.indexWidgetX(index).isChecked() + return self.table.safetext(index) def apply(self): def __check(row): - k = self.model.item(row, 2).text() + k = self.table.safetext(row, 2) if k == "": return "" switch = self.table.indexWidgetX(row, 0).isChecked() @@ -144,8 +148,8 @@ class noundictconfigdialog1(LDialog): self.table.dedumpmodel(__check) self.reflist.clear() for row in range(self.model.rowCount()): - k = self.model.item(row, 2).text() - v = self.model.item(row, 3).text() + k = self.table.safetext(row, 2) + v = self.table.safetext(row, 3) switch = self.table.indexWidgetX(row, 0) es = self.table.indexWidgetX(row, 1) self.reflist.append( @@ -321,7 +325,7 @@ class yuyinzhidingsetting(LDialog): self.model = LStandardItemModel() self.model.setHorizontalHeaderLabels(["正则", "条件", "目标", "指定为"]) - table = TableViewW(self) + table = TableViewW(self, updown=True, copypaste=False) table.setModel(self.model) table.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.Stretch) table.horizontalHeader().setSectionResizeMode( @@ -333,7 +337,6 @@ class yuyinzhidingsetting(LDialog): table.horizontalHeader().setSectionResizeMode( 0, QHeaderView.ResizeMode.ResizeToContents ) - table.setsimplemenu({"copypaste": False}) self.table = table for row, item in enumerate(reflist): @@ -386,7 +389,7 @@ class yuyinzhidingsetting(LDialog): rows = self.model.rowCount() self.reflist.clear() for row in range(rows): - k = self.model.item(row, 2).text() + k = self.table.safetext(row, 2) switch = self.table.indexWidgetX(row, 0) con = self.table.indexWidgetX(row, 1) con2 = self.table.indexWidgetX(row, 3) @@ -732,14 +735,14 @@ class postconfigdialog_(LDialog): if isinstance(self.configdict, dict): for row in range(rows): - text = self.model.item(row, 0).text() - self.configdict[text] = self.model.item(row, 1).text() + text = self.table.safetext(row, 0) + self.configdict[text] = self.table.safetext(row, 1) elif isinstance(self.configdict, list): for row in range(rows): - text = self.model.item(row, 0).text() + text = self.table.safetext(row, 0) item = {} for _i, key in enumerate(self.dictkeys): - item[key] = self.model.item(row, _i).text() + item[key] = self.table.safetext(row, _i) self.configdict.append(item) else: raise @@ -769,11 +772,10 @@ class postconfigdialog_(LDialog): else: raise model.setHorizontalHeaderLabels(headers) - table = TableViewW(self) + table = TableViewW(self, copypaste=True, updown=True) table.setModel(model) table.setWordWrap(False) table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) - table.setsimplemenu() button = threebuttons(texts=["添加行", "删除行", "上移", "下移", "立即应用"]) self.table = table button.btn1clicked.connect(table.insertplainrow) diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index d38459ea..0e716dd9 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -1,5 +1,5 @@ from qtsymbols import * -import os, platform, functools, uuid, json, math +import os, platform, functools, uuid, json, math, csv, io from traceback import print_exc import windows, qtawesome, winsharedutils, gobject from webviewpy import webview_native_handle_kind_t, Webview, declare_library_path @@ -87,11 +87,16 @@ class FocusDoubleSpin(QDoubleSpinBox): class TableViewW(QTableView): - def __init__(self, *argc) -> None: + def __init__(self, *argc, updown=False, copypaste=False) -> None: super().__init__(*argc) self.setSelectionMode(QAbstractItemView.SelectionMode.ContiguousSelection) + self.copypaste = copypaste + self.updown = updown + if updown or copypaste: + self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.customContextMenuRequested.connect(self.showmenu) - def showmenu(self, info, pos): + def showmenu(self, pos): r = self.currentIndex().row() if r < 0: return @@ -100,9 +105,10 @@ class TableViewW(QTableView): down = LAction("下移") copy = LAction("复制") paste = LAction("粘贴") - menu.addAction(up) - menu.addAction(down) - if info.get("copypaste", True): + if self.updown: + menu.addAction(up) + menu.addAction(down) + if self.copypaste: menu.addAction(copy) menu.addAction(paste) action = menu.exec(self.cursor().pos()) @@ -115,11 +121,19 @@ class TableViewW(QTableView): elif action == paste: self.pastetable() - def setsimplemenu(self, info=None): - self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) - if info is None: - info = {} - self.customContextMenuRequested.connect(functools.partial(self.showmenu, info)) + def keyPressEvent(self, e): + if self.copypaste: + if e.modifiers() == Qt.KeyboardModifier.ControlModifier: + if e.key() == Qt.Key.Key_C: + self.copytable() + elif e.key() == Qt.Key.Key_V: + self.pastetable() + else: + super().keyPressEvent(e) + else: + super().keyPressEvent(e) + else: + super().keyPressEvent(e) def insertplainrow(self, row=0): self.model().insertRow( @@ -211,7 +225,8 @@ class TableViewW(QTableView): def getindexwidgetdata(self, index: QModelIndex): ... - def setindexwidget(self, index, data): ... + def setindexwidget(self, index, data): + self.model().setItem(index.row(), index.column(), QStandardItem(data)) def safetext(self, row_or_index, col=None, mybewidget=False): if col is None: @@ -229,50 +244,47 @@ class TableViewW(QTableView): return _1 def copytable(self) -> str: - if len(self.selectedIndexes()) <= 1: - return winsharedutils.clipboard_set(self.safetext(self.currentIndex())) _data = [] - minr = minc = 999999999 - maxr = maxc = 0 + lastrow = -1 for index in self.selectedIndexes(): - minr = min(minr, index.row()) - minc = min(minc, index.column()) - maxr = max(maxr, index.row()) - maxc = max(maxc, index.column()) - _data.append(self.safetext(index, mybewidget=True)) - data = { - "data": _data, - "row": maxr - minr + 1, - "col": maxc - minc + 1, - } - winsharedutils.clipboard_set(json.dumps(data, ensure_ascii=False)) + if index.row() != lastrow: + _data.append([]) + lastrow = index.row() + _data[-1].append(self.safetext(index, mybewidget=True)) + output = io.StringIO() + + csv_writer = csv.writer(output, delimiter="\t") + for row in _data: + csv_writer.writerow(row) + csv_str = output.getvalue() + output.close() + winsharedutils.clipboard_set(csv_str) def pastetable(self): string = winsharedutils.clipboard_get() + current = self.currentIndex() try: - js = json.loads(string) - current = self.currentIndex() - for _ in range(js["row"]): + csv_file = io.StringIO(string) + csv_reader = csv.reader(csv_file, delimiter="\t") + my_list = list(csv_reader) + csv_file.close() + if len(my_list) == 1 and len(my_list[0]) == 1: + self.model().itemFromIndex(current).setText(my_list[0][0]) + return + for j, line in enumerate(my_list): self.insertplainrow(current.row() + 1) - for i, data in enumerate(js.get("data", [])): - c = current.column() + i % js["col"] - if c >= self.model().columnCount(): - continue - if isinstance(data, str): - self.model().setItem( - current.row() + 1 + i // js["col"], c, QStandardItem(data) - ) - else: - self.model().setItem( - current.row() + 1 + i // js["col"], c, QStandardItem("") - ) + for j, line in enumerate(my_list): + for i in range(len(line)): + data = line[i] + c = current.column() + i + if c >= self.model().columnCount(): + continue self.setindexwidget( - self.model().index(current.row() + 1 + i // js["col"], c), data + self.model().index(current.row() + 1 + j, c), data ) - except: print_exc() - self.model().itemFromIndex(self.currentIndex()).setText(string) + self.model().itemFromIndex(current).setText(string) def getQMessageBox( diff --git a/LunaTranslator/LunaTranslator/metadata/abstract.py b/LunaTranslator/LunaTranslator/metadata/abstract.py index 355aef0a..f300ff52 100644 --- a/LunaTranslator/LunaTranslator/metadata/abstract.py +++ b/LunaTranslator/LunaTranslator/metadata/abstract.py @@ -130,7 +130,7 @@ class common: ff.write(_content) def dispatchdownloadtask(self, url): - if url is None: + if not url: return None __routine = f"cache/metadata/{self.typename}" if self.typename == "vndb": diff --git a/LunaTranslator/LunaTranslator/metadata/vndb.py b/LunaTranslator/LunaTranslator/metadata/vndb.py index fa107232..e8f5d962 100644 --- a/LunaTranslator/LunaTranslator/metadata/vndb.py +++ b/LunaTranslator/LunaTranslator/metadata/vndb.py @@ -144,7 +144,7 @@ def getinfosbyvid(proxy, vid): return dict( title=gettitlefromjs(js["results"][0]), - img=js["results"][0]["image"]["url"], + img=js["results"][0]["image"]["url"] if js["results"][0]["image"] else None, sc=imgs, dev=dev, tags=sorted(tags, key=lambda x: -rates[tags.index(x)]), diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 587a963f..15a452d6 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -28,8 +28,8 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/version) include(generate_product_version) set(VERSION_MAJOR 5) -set(VERSION_MINOR 37) -set(VERSION_PATCH 2) +set(VERSION_MINOR 38) +set(VERSION_PATCH 0) add_library(pch pch.cpp) target_precompile_headers(pch PUBLIC pch.h)