|
| 1 | +import os |
| 2 | +import typing |
| 3 | +from PySide6.QtWidgets import * |
| 4 | +from PySide6.QtCore import * |
| 5 | +from PySide6.QtGui import * |
| 6 | +from ..forms.generated.ui_filelistinputwidget import Ui_FileListInputWidget |
| 7 | +from preppipe.language import * |
| 8 | +from ..translatablewidgetinterface import * |
| 9 | + |
| 10 | +class FileListInputWidget(QWidget, TranslatableWidgetInterface): |
| 11 | + listChanged = Signal() |
| 12 | + |
| 13 | + ui : Ui_FileListInputWidget |
| 14 | + isDirectoryMode : bool |
| 15 | + isExistingOnly : bool |
| 16 | + verifyCB : typing.Callable[[str], bool] | None |
| 17 | + fieldName : Translatable | str |
| 18 | + filter : Translatable | str |
| 19 | + lastAddedPath : str |
| 20 | + |
| 21 | + TR_gui_filelistinputwidget = TranslationDomain("gui_filelistinputwidget") |
| 22 | + _tr_add = TR_gui_filelistinputwidget.tr("add", |
| 23 | + en="Add", |
| 24 | + zh_cn="添加", |
| 25 | + zh_hk="添加", |
| 26 | + ) |
| 27 | + _tr_remove = TR_gui_filelistinputwidget.tr("remove", |
| 28 | + en="Remove", |
| 29 | + zh_cn="删除", |
| 30 | + zh_hk="刪除", |
| 31 | + ) |
| 32 | + _tr_move_up = TR_gui_filelistinputwidget.tr("move_up", |
| 33 | + en="Move Up", |
| 34 | + zh_cn="上移", |
| 35 | + zh_hk="上移", |
| 36 | + ) |
| 37 | + _tr_move_down = TR_gui_filelistinputwidget.tr("move_down", |
| 38 | + en="Move Down", |
| 39 | + zh_cn="下移", |
| 40 | + zh_hk="下移", |
| 41 | + ) |
| 42 | + |
| 43 | + def __init__(self, parent : QWidget | None = None): |
| 44 | + super(FileListInputWidget, self).__init__(parent) |
| 45 | + self.ui = Ui_FileListInputWidget() |
| 46 | + self.ui.setupUi(self) |
| 47 | + self.isDirectoryMode = False |
| 48 | + self.isExistingOnly = False |
| 49 | + self.verifyCB = None |
| 50 | + self.fieldName = "" |
| 51 | + self.filter = "" |
| 52 | + self.lastAddedPath = "" |
| 53 | + |
| 54 | + self.bind_text(self.ui.addButton.setText, self._tr_add) |
| 55 | + self.bind_text(self.ui.removeButton.setText, self._tr_remove) |
| 56 | + self.bind_text(self.ui.moveUpButton.setText, self._tr_move_up) |
| 57 | + self.bind_text(self.ui.moveDownButton.setText, self._tr_move_down) |
| 58 | + |
| 59 | + self.ui.listWidget.itemChanged.connect(lambda: self.listChanged.emit()) |
| 60 | + self.ui.addButton.clicked.connect(self.itemAdd) |
| 61 | + self.ui.removeButton.clicked.connect(self.itemRemove) |
| 62 | + self.ui.moveUpButton.clicked.connect(self.itemMoveUp) |
| 63 | + self.ui.moveDownButton.clicked.connect(self.itemMoveDown) |
| 64 | + self.setAcceptDrops(True) |
| 65 | + |
| 66 | + def setDirectoryMode(self, v: bool): |
| 67 | + self.isDirectoryMode = v |
| 68 | + if self.verifyCB is None: |
| 69 | + self.verifyCB = (lambda path: os.path.isdir(path)) if v else (lambda path: os.path.isfile(path)) |
| 70 | + |
| 71 | + def setVerifyCallBack(self, cb): |
| 72 | + """Set a callback function that verifies a path. The function should accept a string and return a boolean.""" |
| 73 | + self.verifyCB = cb |
| 74 | + |
| 75 | + def setExistingOnly(self, v: bool): |
| 76 | + self.isExistingOnly = v |
| 77 | + |
| 78 | + def setFieldName(self, name: Translatable | str): |
| 79 | + self.fieldName = name |
| 80 | + if isinstance(name, Translatable): |
| 81 | + self.bind_text(self.ui.label.setText, name) |
| 82 | + else: |
| 83 | + self.ui.label.setText(name) |
| 84 | + |
| 85 | + def setFilter(self, filter_str: Translatable | str): |
| 86 | + self.filter = filter_str |
| 87 | + |
| 88 | + def getCurrentList(self): |
| 89 | + results = [] |
| 90 | + for i in range(self.ui.listWidget.count()): |
| 91 | + item = self.ui.listWidget.item(i) |
| 92 | + results.append(item.text()) |
| 93 | + return results |
| 94 | + |
| 95 | + def dragEnterEvent(self, e: QDragEnterEvent): |
| 96 | + if e.mimeData().hasUrls(): |
| 97 | + path = e.mimeData().urls()[0].toLocalFile() |
| 98 | + if not self.verifyCB or self.verifyCB(path): |
| 99 | + e.acceptProposedAction() |
| 100 | + return |
| 101 | + super().dragEnterEvent(e) |
| 102 | + |
| 103 | + def dropEvent(self, event: QDropEvent): |
| 104 | + for url in event.mimeData().urls(): |
| 105 | + path = url.toLocalFile() |
| 106 | + if not self.verifyCB or self.verifyCB(path): |
| 107 | + self.addPath(path) |
| 108 | + event.acceptProposedAction() |
| 109 | + |
| 110 | + @Slot(str) |
| 111 | + def addPath(self, path: str): |
| 112 | + # Prevent duplicates |
| 113 | + for i in range(self.ui.listWidget.count()): |
| 114 | + if self.ui.listWidget.item(i).text() == path: |
| 115 | + return |
| 116 | + newItem = QListWidgetItem(path) |
| 117 | + newItem.setToolTip(path) |
| 118 | + self.ui.listWidget.addItem(newItem) |
| 119 | + self.lastAddedPath = path |
| 120 | + self.listChanged.emit() |
| 121 | + |
| 122 | + @Slot() |
| 123 | + def itemMoveUp(self): |
| 124 | + curRow = self.ui.listWidget.currentRow() |
| 125 | + if curRow > 0: |
| 126 | + item = self.ui.listWidget.takeItem(curRow) |
| 127 | + self.ui.listWidget.insertItem(curRow - 1, item) |
| 128 | + self.ui.listWidget.setCurrentRow(curRow - 1) |
| 129 | + self.listChanged.emit() |
| 130 | + |
| 131 | + @Slot() |
| 132 | + def itemMoveDown(self): |
| 133 | + curRow = self.ui.listWidget.currentRow() |
| 134 | + if curRow >= 0 and curRow + 1 < self.ui.listWidget.count(): |
| 135 | + item = self.ui.listWidget.takeItem(curRow) |
| 136 | + self.ui.listWidget.insertItem(curRow + 1, item) |
| 137 | + self.ui.listWidget.setCurrentRow(curRow + 1) |
| 138 | + self.listChanged.emit() |
| 139 | + |
| 140 | + _tr_select_dialog_title = TR_gui_filelistinputwidget.tr("select_dialog_title", |
| 141 | + en="Please select {field}", |
| 142 | + zh_cn="请选择{field}", |
| 143 | + zh_hk="請選擇{field}", |
| 144 | + ) |
| 145 | + |
| 146 | + @Slot() |
| 147 | + def itemAdd(self): |
| 148 | + dialogTitle = self._tr_select_dialog_title.format(field=str(self.fieldName)) |
| 149 | + initialDir = self.lastAddedPath if self.lastAddedPath else "" |
| 150 | + dialog = QFileDialog(self, dialogTitle, initialDir, str(self.filter)) |
| 151 | + if self.isDirectoryMode: |
| 152 | + dialog.setFileMode(QFileDialog.Directory) |
| 153 | + dialog.setOption(QFileDialog.ShowDirsOnly, True) |
| 154 | + else: |
| 155 | + dialog.setFileMode(QFileDialog.ExistingFile if self.isExistingOnly else QFileDialog.AnyFile) |
| 156 | + dialog.fileSelected.connect(self.addPath) |
| 157 | + dialog.show() |
| 158 | + |
| 159 | + @Slot() |
| 160 | + def itemRemove(self): |
| 161 | + curRow = self.ui.listWidget.currentRow() |
| 162 | + if curRow >= 0: |
| 163 | + item = self.ui.listWidget.takeItem(curRow) |
| 164 | + del item |
| 165 | + self.listChanged.emit() |
0 commit comments