diff --git a/LunaTranslator/LunaTranslator/gui/codeacceptdialog.py b/LunaTranslator/LunaTranslator/gui/codeacceptdialog.py index 6b3b151a..f271e205 100644 --- a/LunaTranslator/LunaTranslator/gui/codeacceptdialog.py +++ b/LunaTranslator/LunaTranslator/gui/codeacceptdialog.py @@ -83,7 +83,7 @@ class codeacceptdialog(LDialog): row += 1 button = threebuttons(texts=["添加行", "删除行", "立即应用"]) button.btn1clicked.connect(self.clicked1) - button.btn2clicked.connect(self.clicked2) + button.btn2clicked.connect(self.table.removeselectedrows) button.btn3clicked.connect(self.apply) self.button = button formLayout.addWidget(self.table) @@ -126,17 +126,6 @@ class codeacceptdialog(LDialog): index = self.model.index(0, 0) self.table.setIndexWidget(index, codecombox) - def clicked2(self): - skip = [] - for index in self.table.selectedIndexes(): - if index.row() in skip: - continue - skip.append(index.row()) - skip = reversed(sorted(skip)) - - for row in skip: - self.model.removeRow(row) - def apply(self): rows = self.model.rowCount() diff --git a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py index ce53896e..6c31a971 100644 --- a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py +++ b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py @@ -1050,8 +1050,10 @@ class dialog_setting_game_internal(QWidget): formLayout2 = self.createfollowdefault( savehook_new_data[gameuid], "tts_follow_default", formLayout ) - if 'tts_repair_use_at_translate' not in savehook_new_data[gameuid]: - savehook_new_data[gameuid]['tts_repair_use_at_translate']=globalconfig["ttscommon"]["tts_repair"] + if "tts_repair_use_at_translate" not in savehook_new_data[gameuid]: + savehook_new_data[gameuid]["tts_repair_use_at_translate"] = globalconfig[ + "ttscommon" + ]["tts_repair"] formLayout2.addRow( "语音指定", getboxlayout( @@ -1084,8 +1086,10 @@ class dialog_setting_game_internal(QWidget): icon="fa.gear", ), QLabel(), - getsimpleswitch(savehook_new_data[gameuid], "tts_repair_use_at_translate"), - LLabel('作用于翻译'), + getsimpleswitch( + savehook_new_data[gameuid], "tts_repair_use_at_translate" + ), + LLabel("作用于翻译"), ], margin0=True, makewidget=True, @@ -1227,8 +1231,8 @@ class dialog_setting_game_internal(QWidget): return menu = QMenu(self.__textprocinternaltable) remove = LAction(("删除")) - up = LAction(("上移")) - down = LAction(("下移")) + up = LAction("上移") + down = LAction("下移") menu.addAction(remove) menu.addAction(up) menu.addAction(down) @@ -3298,8 +3302,8 @@ class dialog_savedgame_v3(QWidget): editname = LAction(("修改列表名称")) addlist = LAction(("创建列表")) dellist = LAction(("删除列表")) - Upaction = LAction(("上移")) - Downaction = LAction(("下移")) + Upaction = LAction("上移") + Downaction = LAction("下移") addgame = LAction(("添加游戏")) batchadd = LAction(("批量添加")) menu.addAction(Upaction) diff --git a/LunaTranslator/LunaTranslator/gui/inputdialog.py b/LunaTranslator/LunaTranslator/gui/inputdialog.py index d1d3c9d7..c5e83a8a 100644 --- a/LunaTranslator/LunaTranslator/gui/inputdialog.py +++ b/LunaTranslator/LunaTranslator/gui/inputdialog.py @@ -50,35 +50,26 @@ class noundictconfigdialog1(LDialog): if r < 0: return menu = QMenu(table) - up = LAction(("上移")) - down = LAction(("下移")) + up = LAction("上移") + down = LAction("下移") + copy = LAction("复制") + paste = LAction("粘贴") menu.addAction(up) menu.addAction(down) + menu.addAction(copy) + menu.addAction(paste) action = menu.exec(table.cursor().pos()) if action == up: - - self.moverank(table, -1) + table.moverank(-1) elif action == down: - self.moverank(table, 1) + table.moverank(1) + elif action == copy: + table.copytable() - def moverank(self, table: TableViewW, dy): - curr = table.currentIndex() - model = table.model() - target = (curr.row() + dy) % model.rowCount() - texts = [model.item(curr.row(), i).text() for i in range(model.columnCount())] - - item = self.reflist.pop(curr.row()) - self.reflist.insert( - target, {"key": texts[1], "value": [2], "regex": item["regex"]} - ) - model.removeRow(curr.row()) - model.insertRow(target, [QStandardItem(text) for text in texts]) - table.setCurrentIndex(model.index(target, curr.column())) - table.setIndexWidget( - model.index(target, 0), getsimpleswitch(self.reflist[target], "regex") - ) + elif action == paste: + table.pastetable() def __init__(self, parent, reflist, title, label) -> None: super().__init__(parent, Qt.WindowType.WindowCloseButtonHint) @@ -125,32 +116,20 @@ class noundictconfigdialog1(LDialog): button4.clicked.connect(clicked4) search.addWidget(button4) - + table.getindexwidgetdata = self.__getindexwidgetdata + table.setindexwidget = self.__setindexwidget + self.table = table button = threebuttons(texts=["添加行", "删除行", "上移", "下移", "立即应用"]) + table.insertplainrow = lambda row: self.newline( + row, {"key": "", "value": "", "regex": False} + ) - def clicked1(): - self.reflist.insert(0, {"key": "", "value": "", "regex": False}) + button.btn1clicked.connect(functools.partial(table.insertplainrow, 0)) - self.newline(0, self.reflist[0]) - - button.btn1clicked.connect(clicked1) - - def clicked2(): - skip = [] - for index in self.table.selectedIndexes(): - if index.row() in skip: - continue - skip.append(index.row()) - skip = reversed(sorted(skip)) - - for row in skip: - self.model.removeRow(row) - self.reflist.pop(row) - - button.btn2clicked.connect(clicked2) + button.btn2clicked.connect(self.table.removeselectedrows) button.btn5clicked.connect(self.apply) - button.btn3clicked.connect(functools.partial(self.moverank, table, -1)) - button.btn4clicked.connect(functools.partial(self.moverank, table, 1)) + button.btn3clicked.connect(functools.partial(table.moverank, -1)) + button.btn4clicked.connect(functools.partial(table.moverank, 1)) self.button = button formLayout.addWidget(table) formLayout.addLayout(search) @@ -159,21 +138,21 @@ class noundictconfigdialog1(LDialog): self.resize(QSize(600, 400)) self.show() + def __setindexwidget(self, index: QModelIndex, data): + if index.column() == 0: + self.table.setIndexWidget(index, getsimpleswitch(data, "regex")) + + def __getindexwidgetdata(self, index: QModelIndex): + return {"regex": self.table.indexWidgetX(index).isChecked()} + def apply(self): - rows = self.model.rowCount() - dedump = set() - needremoves = [] - for row in range(rows): + self.table.dedumpmodel(1) + self.reflist.clear() + for row in range(self.model.rowCount()): k = self.model.item(row, 1).text() v = self.model.item(row, 2).text() - if k == "" or k in dedump: - needremoves.append(row) - continue - self.reflist[row].update({"key": k, "value": v}) - dedump.add(k) - for row in reversed(needremoves): - self.model.removeRow(row) - self.reflist.pop(row) + switch = self.table.indexWidgetX(row, 0) + self.reflist.append({"key": k, "value": v, "regex": switch.isChecked()}) def closeEvent(self, a0: QCloseEvent) -> None: self.button.setFocus() @@ -287,50 +266,24 @@ class yuyinzhidingsetting(LDialog): if r < 0: return menu = QMenu(table) - up = LAction(("上移")) - down = LAction(("下移")) + up = LAction("上移") + down = LAction("下移") menu.addAction(up) menu.addAction(down) action = menu.exec(table.cursor().pos()) if action == up: - self.moverank(table, -1) + table.moverank(-1) elif action == down: - self.moverank(table, 1) - - def moverank(self, table: TableViewW, dy): - curr = table.currentIndex() - model = table.model() - target = (curr.row() + dy) % model.rowCount() - texts = [model.item(curr.row(), i).text() for i in range(model.columnCount())] - - item = self.reflist.pop(curr.row()) - self.reflist.insert( - target, - { - "key": texts[1], - "condition": item["condition"], - "regex": item["regex"], - "target": item["target"], - }, - ) - - model.removeRow(curr.row()) - model.insertRow(target, [QStandardItem(text) for text in texts]) - table.setCurrentIndex(model.index(target, curr.column())) - table.setIndexWidget( - model.index(target, 0), getsimpleswitch(self.reflist[target], "regex") - ) - com = getsimplecombobox(["首尾", "包含"], item, "condition") - table.setIndexWidget(self.model.index(target, 1), com) - table.setIndexWidget(self.model.index(target, 3), self.createacombox(item)) + table.moverank(1) def createacombox(self, config): com = LFocusCombo() com.addItems(["跳过", "默认", "选择声音"]) target = config.get("target", "skip") + com.target = target if target == "skip": com.setCurrentIndex(0) elif target == "default": @@ -346,11 +299,11 @@ class yuyinzhidingsetting(LDialog): def __comchange(self, com: LFocusCombo, config, idx): if idx == 0: - config["target"] = "skip" + com.target = "skip" if com.count() > 3: com.removeItem(com.count() - 1) elif idx == 1: - config["target"] = "default" + com.target = "default" if com.count() > 3: com.removeItem(com.count() - 1) elif idx == 2: @@ -359,7 +312,7 @@ class yuyinzhidingsetting(LDialog): if voice.datas["voice"] is None: com.setCurrentIndex(1) return - config["target"] = ( + com.target = ( voice.datas["engine"], voice.datas["voice"], voice.datas["vis"], @@ -428,30 +381,16 @@ class yuyinzhidingsetting(LDialog): button = threebuttons(texts=["添加行", "删除行", "上移", "下移", "立即应用"]) def clicked1(): - self.reflist.insert( + self.newline( 0, {"key": "", "condition": 0, "regex": False, "target": "skip"} ) - self.newline(0, self.reflist[0]) - button.btn1clicked.connect(clicked1) - def clicked2(): - skip = [] - for index in self.table.selectedIndexes(): - if index.row() in skip: - continue - skip.append(index.row()) - skip = reversed(sorted(skip)) - - for row in skip: - self.model.removeRow(row) - self.reflist.pop(row) - - button.btn2clicked.connect(clicked2) + button.btn2clicked.connect(table.removeselectedrows) button.btn5clicked.connect(self.apply) - button.btn3clicked.connect(functools.partial(self.moverank, table, -1)) - button.btn4clicked.connect(functools.partial(self.moverank, table, 1)) + button.btn3clicked.connect(functools.partial(table.moverank, -1)) + button.btn4clicked.connect(functools.partial(table.moverank, 1)) self.button = button formLayout.addWidget(table) formLayout.addLayout(search) @@ -461,20 +400,22 @@ class yuyinzhidingsetting(LDialog): self.show() def apply(self): + self.table.dedumpmodel(2) rows = self.model.rowCount() - dedump = set() - needremoves = [] + self.reflist.clear() for row in range(rows): k = self.model.item(row, 2).text() - - if k == "" or k in dedump: - needremoves.append(row) - continue - self.reflist[row].update({"key": k}) - dedump.add(k) - for row in reversed(needremoves): - self.model.removeRow(row) - self.reflist.pop(row) + switch = self.table.indexWidgetX(row, 0) + con = self.table.indexWidgetX(row, 1) + con2 = self.table.indexWidgetX(row, 3) + self.reflist.append( + { + "key": k, + "condition": con.currentIndex(), + "regex": switch.isChecked(), + "target": con2.target, + } + ) def closeEvent(self, a0: QCloseEvent) -> None: self.button.setFocus() @@ -777,47 +718,28 @@ class postconfigdialog_(LDialog): if action == up: - self.moverank(table, -1) + table.moverank(-1) elif action == down: - self.moverank(table, 1) + table.moverank(1) elif action == copy: table.copytable() elif action == paste: table.pastetable() - def moverank(self, table: TableViewW, dy): - curr = table.currentIndex() - target = (curr.row() + dy) % table.model().rowCount() - texts = [ - table.model().item(curr.row(), i).text() - for i in range(table.model().columnCount()) - ] - - table.model().removeRow(curr.row()) - table.model().insertRow(target, [QStandardItem(text) for text in texts]) - table.setCurrentIndex(table.model().index(target, curr.column())) - def apply(self): + self.table.dedumpmodel(0) rows = self.model.rowCount() self.configdict.clear() if isinstance(self.configdict, dict): for row in range(rows): text = self.model.item(row, 0).text() - if text == "": - continue self.configdict[text] = self.model.item(row, 1).text() elif isinstance(self.configdict, list): - dedump = set() for row in range(rows): text = self.model.item(row, 0).text() - if text == "": - continue - if text in dedump: - continue - dedump.add(text) item = {} for _i, key in enumerate(self.dictkeys): item[key] = self.model.item(row, _i).text() @@ -864,31 +786,12 @@ class postconfigdialog_(LDialog): functools.partial(self.showmenu, table) ) button = threebuttons(texts=["添加行", "删除行", "上移", "下移", "立即应用"]) + self.table = table + button.btn1clicked.connect(table.insertplainrow) + button.btn2clicked.connect(table.removeselectedrows) - def clicked1(): - if isinstance(configdict, dict): - model.insertRow(0, [QStandardItem(), QStandardItem()]) - elif isinstance(configdict, list): - model.insertRow(0, [QStandardItem() for _ in range(len(dictkeys))]) - else: - raise - - def clicked2(): - skip = [] - for index in table.selectedIndexes(): - if index.row() in skip: - continue - skip.append(index.row()) - skip = reversed(sorted(skip)) - - for row in skip: - model.removeRow(row) - - button.btn1clicked.connect(clicked1) - button.btn2clicked.connect(clicked2) - - button.btn3clicked.connect(functools.partial(self.moverank, table, -1)) - button.btn4clicked.connect(functools.partial(self.moverank, table, 1)) + button.btn3clicked.connect(functools.partial(table.moverank, -1)) + button.btn4clicked.connect(functools.partial(table.moverank, 1)) button.btn5clicked.connect(self.apply) self.button = button self.model = model diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 650abb8b..98908069 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -95,10 +95,82 @@ class TableViewW(QTableView): super().__init__(*argc) self.setSelectionMode(QAbstractItemView.SelectionMode.ContiguousSelection) + def insertplainrow(self, row=0): + self.model().insertRow( + row, [QStandardItem() for _ in range(self.model().columnCount())] + ) + + def dedumpmodel(self, col): + + rows = self.model().rowCount() + dedump = set() + needremoves = [] + for row in range(rows): + k = self.safetext(row, col) + if k == "" or k in dedump: + needremoves.append(row) + continue + dedump.add(k) + for row in reversed(needremoves): + self.model().removeRow(row) + + def removeselectedrows(self): + row = self.currentIndex().row() + col = self.currentIndex().column() + skip = [] + for index in self.selectedIndexes(): + if index.row() in skip: + continue + skip.append(index.row()) + skip = reversed(sorted(skip)) + + for row in skip: + self.model().removeRow(row) + row = min(row, self.model().rowCount() - 1) + self.setCurrentIndex(self.model().index(row, col)) + return skip + + def moverank(self, dy): + curr = self.currentIndex() + row, col = curr.row(), curr.column() + + model = self.model() + realws = [] + for _ in range(self.model().columnCount()): + w = self.indexWidget(self.model().index(row, _)) + if w is None: + realws.append(None) + continue + l: QHBoxLayout = w.layout() + w = l.takeAt(0).widget() + realws.append(w) + target = (row + dy) % model.rowCount() + model.insertRow(target, model.takeRow(row)) + self.setCurrentIndex(model.index(target, col)) + for _ in range(self.model().columnCount()): + self.setIndexWidget(self.model().index(target, _), realws[_]) + return row, target + + def indexWidgetX(self, row_or_index, col=None): + if col is None: + index: QModelIndex = row_or_index + else: + index = self.model().index(row_or_index, col) + w = self.indexWidget(index) + if w is None: + return w + l: QHBoxLayout = w.layout() + return l.itemAt(0).widget() + def setIndexWidget(self, index: QModelIndex, w: QWidget): - super().setIndexWidget(index, w) if w is None: return + __w = QWidget() + __l = QHBoxLayout() + __w.setLayout(__l) + __l.setContentsMargins(0, 0, 0, 0) + __l.addWidget(w) + super().setIndexWidget(index, __w) if self.rowHeight(index.row()) < w.height(): self.setRowHeight(index.row(), w.height()) @@ -107,13 +179,23 @@ class TableViewW(QTableView): if isinstance(m, LStandardItemModel): m.updatelangtext() - def safetext(self, row, col=None): + def getindexwidgetdata(self, index: QModelIndex): ... + + def setindexwidget(self, index, data): ... + + def safetext(self, row_or_index, col=None, mybewidget=False): if col is None: - index = row + index: QModelIndex = row_or_index else: - index = self.model().index(row, col) + index = self.model().index(row_or_index, col) + if mybewidget: + w = self.indexWidget(index) + if w is not None: + return self.getindexwidgetdata(index) + _1 = self.model().itemFromIndex(index) _1 = _1.text() if _1 else "" + return _1 def copytable(self) -> str: @@ -127,7 +209,7 @@ class TableViewW(QTableView): minc = min(minc, index.column()) maxr = max(maxr, index.row()) maxc = max(maxc, index.column()) - _data.append(self.safetext(index)) + _data.append(self.safetext(index, mybewidget=True)) data = { "data": _data, "row": maxr - minr + 1, @@ -141,17 +223,25 @@ class TableViewW(QTableView): js = json.loads(string) current = self.currentIndex() for _ in range(js["row"]): - self.model().insertRow(current.row() + 1, []) - + 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 - self.model().setItem( - current.row() + 1 + i // js["col"], c, QStandardItem(data) - ) + 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("") + ) + self.setindexwidget( + self.model().index(current.row() + 1 + i // js["col"], c), data + ) except: + print_exc() self.model().itemFromIndex(self.currentIndex()).setText(string) @@ -1694,16 +1784,8 @@ class listediter(LDialog): self.moverank(1) def moverank(self, dy): - curr = self.hctable.currentIndex() - target = (curr.row() + dy) % self.hcmodel.rowCount() - text = self.internalrealname[curr.row()] - self.internalrealname.pop(curr.row()) - self.hcmodel.removeRow(curr.row()) - self.internalrealname.insert(target, text) - if self.namemapfunction: - text = self.namemapfunction(text) - self.hcmodel.insertRow(target, [QStandardItem(text)]) - self.hctable.setCurrentIndex(self.hcmodel.index(target, 0)) + src, tgt = self.hctable.moverank(dy) + self.internalrealname.insert(tgt, self.internalrealname.pop(src)) def __init__( self, @@ -1793,15 +1875,8 @@ class listediter(LDialog): print_exc() def clicked2(self): - skip = [] - for index in self.hctable.selectedIndexes(): - if index.row() in skip: - continue - skip.append(index.row()) - skip = reversed(sorted(skip)) - + skip = self.hctable.removeselectedrows() for row in skip: - self.hcmodel.removeRow(row) self.internalrealname.pop(row) def closeEvent(self, a0: QCloseEvent) -> None: diff --git a/LunaTranslator/LunaTranslator/transoptimi/noundict.py b/LunaTranslator/LunaTranslator/transoptimi/noundict.py index 6cf17a32..0b2d0aff 100644 --- a/LunaTranslator/LunaTranslator/transoptimi/noundict.py +++ b/LunaTranslator/LunaTranslator/transoptimi/noundict.py @@ -33,28 +33,16 @@ class noundictconfigdialog(LDialog): if action == up: - self.moverank(table, -1) + table.moverank(-1) elif action == down: - self.moverank(table, 1) + table.moverank(1) elif action == copy: table.copytable() elif action == paste: table.pastetable() - def moverank(self, table: TableViewW, dy): - curr = table.currentIndex() - target = (curr.row() + dy) % table.model().rowCount() - texts = [ - table.model().item(curr.row(), i).text() - for i in range(table.model().columnCount()) - ] - - table.model().removeRow(curr.row()) - table.model().insertRow(target, [QStandardItem(text) for text in texts]) - table.setCurrentIndex(table.model().index(target, curr.column())) - def apply(self): rows = self.model.rowCount() self.configdict.clear() @@ -116,22 +104,9 @@ class noundictconfigdialog(LDialog): ) button.btn1clicked.connect(clicked1) - - def clicked2(): - - skip = [] - for index in self.table.selectedIndexes(): - if index.row() in skip: - continue - skip.append(index.row()) - skip = reversed(sorted(skip)) - - for row in skip: - model.removeRow(row) - - button.btn2clicked.connect(clicked2) - button.btn3clicked.connect(functools.partial(self.moverank, table, -1)) - button.btn4clicked.connect(functools.partial(self.moverank, table, 1)) + button.btn2clicked.connect(self.table.removeselectedrows) + button.btn3clicked.connect(functools.partial(table.moverank, -1)) + button.btn4clicked.connect(functools.partial(table.moverank, 1)) button.btn5clicked.connect(self.apply) @@ -196,10 +171,10 @@ class noundictconfigdialog_private(LDialog): if action == up: - self.moverank(table, -1) + table.moverank(-1) elif action == down: - self.moverank(table, 1) + table.moverank(1) elif action == copy: table.copytable() @@ -207,30 +182,15 @@ class noundictconfigdialog_private(LDialog): elif action == paste: table.pastetable() - def moverank(self, table: TableViewW, dy): - curr = table.currentIndex() - target = (curr.row() + dy) % table.model().rowCount() - texts = [ - table.model().item(curr.row(), i).text() - for i in range(table.model().columnCount()) - ] - - table.model().removeRow(curr.row()) - table.model().insertRow(target, [QStandardItem(text) for text in texts]) - table.setCurrentIndex(table.model().index(target, curr.column())) - def apply(self): + self.table.dedumpmodel(0) rows = self.model.rowCount() self.configdict.clear() - _dedump = set() + for row in range(rows): k, v = self.model.item(row, 0).text(), self.model.item(row, 1).text() - if k == "": - continue - if k in _dedump: - continue + self.configdict.append([k, v]) - _dedump.add(k) def __init__( self, parent, configdict, title, label=["原文", "翻译"], _=None @@ -260,29 +220,10 @@ class noundictconfigdialog_private(LDialog): ) button = threebuttons(texts=["添加行", "删除行", "上移", "下移", "立即应用"]) self.table = table - - def clicked1(): - - model.insertRow(0, [QStandardItem(), QStandardItem()]) - - button.btn1clicked.connect(clicked1) - - def clicked2(): - - skip = [] - for index in self.table.selectedIndexes(): - if index.row() in skip: - continue - skip.append(index.row()) - skip = reversed(sorted(skip)) - - for row in skip: - model.removeRow(row) - - button.btn2clicked.connect(clicked2) - button.btn3clicked.connect(functools.partial(self.moverank, table, -1)) - button.btn4clicked.connect(functools.partial(self.moverank, table, 1)) - + button.btn1clicked.connect(table.insertplainrow) + button.btn2clicked.connect(table.removeselectedrows) + button.btn3clicked.connect(functools.partial(table.moverank, -1)) + button.btn4clicked.connect(functools.partial(table.moverank, 1)) button.btn5clicked.connect(self.apply) search = QHBoxLayout()