-
Notifications
You must be signed in to change notification settings - Fork 7
/
odvrt.py
227 lines (195 loc) · 7.8 KB
/
odvrt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding: utf-8 -*-
import pathlib
import re
import sys
import os
from PyQt6 import QtWidgets, QtCore, QtGui
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QTableWidgetItem, QFileDialog, QDialog, QPushButton, QLabel, QProgressBar
from RenameTool import RenameTool
from qt_view import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
fileList = []
settings = {0:'downloadImg=False', 1:'formatStr=%code %title %actor'}
fileNames = None
downloadImage = False
configPath = pathlib.Path('config.ini')
progressbar = None
_func = None
def __init__(self):
super(MainWindow, self).__init__()
if sys.version_info < (3, 9):
print("Python版本錯誤,需要使用3.9以上版本,目前為"+str(sys.version_info[0])+"."+str(sys.version_info[1]))
print()
os.system("pause")
sys.exit(1)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setAcceptDrops(True)
# Read settings
try:
if not self.configPath.exists():
self.configPath.touch()
text = self.configPath.read_text()
textSet = text.split('\n')
for t in textSet:
if "downloadImg" in t:
result = re.findall('\w+$', t)
if result[0] == "True":
self.ui.downloadImgCheckBox.setChecked(True)
self.downloadImage = True
else:
self.ui.downloadImgCheckBox.setChecked(False)
self.downloadImage = False
if "formatStr" in t:
result = re.findall('=(.+)$', t)
self.ui.formatStr.setText(result[0])
except:
raise
# 讀取設定
# 開啟
self.ui.openButton.clicked.connect(self.openBtnClicked)
self.ui.action1.triggered.connect(self.openBtnClicked)
# 執行
self.ui.runButton.clicked.connect(self.runBtnClicked)
self.ui.action2.triggered.connect(self.runBtnClicked)
# 刪除
self.ui.delButton.clicked.connect(self.delBtnClicked)
self.ui.action5.triggered.connect(self.delBtnClicked)
# 下載縮圖
self.ui.downloadImgCheckBox.stateChanged.connect(self.downloadImgChecked)
self.fileNames = []
# 設定作者link
self.ui.action3.triggered.connect(self.openAuthorUrl)
self.ui.action4.triggered.connect(self.openProductUrl)
self.ui.setDefaultBtn.clicked.connect(self.setDefaultFormat)
def setDefaultFormat(self):
self.ui.formatStr.setText('%code %title %actor')
# drag and drop
def dragEnterEvent(self, event):
event.acceptProposedAction()
def dropEvent(self, event):
try:
file_path = str(event.mimeData().text())
file_path = file_path.replace('file:///', '').strip()
file_path = file_path.split('\n')
# print(fileNames)
# Count
for i in range(len(file_path)):
self.processDrop(self.fileNames, file_path[i])
self.fileNames = list(dict.fromkeys(self.fileNames))
self.updateFileList()
except:
# print("Unexpected error:", sys.exc_info()[0])
raise
def processDrop(self, obj, filePath):
if os.path.isdir(filePath):
for child in os.listdir():
self.processDrop(obj, child)
elif os.path.isfile(filePath):
if not filePath in obj:
return obj.append(filePath)
def openBtnClicked(self):
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.FileMode.ExistingFile)
dialog.setOption(QFileDialog.Option.ReadOnly)
if dialog.exec():
self.fileNames = dialog.selectedFiles() + self.fileNames
self.fileNames = list(dict.fromkeys(self.fileNames))
self.updateFileList()
def runBtnClicked(self):
self.confirmDialog = QDialog()
self.confirmDialog.setFixedWidth(480)
self.confirmDialog.setFixedHeight(100)
self.confirmDialog.setWindowTitle("上車?")
label = QLabel(self.confirmDialog)
label.setText("確定要更改列表中的檔案名稱?\n\n按確定後代表同意自負任何檔案毀損責任,與本產品無關")
label.move(10, 10)
btn = QPushButton(self.confirmDialog)
btn.setText("確定")
btn.move(380, 70)
btn.clicked.connect(self.showProgressAndRun)
btn = QPushButton(self.confirmDialog)
btn.setText("取消")
btn.move(300, 70)
btn.clicked.connect(lambda: self.confirmDialog.close())
self.confirmDialog.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
self.confirmDialog.exec()
def showProgressAndRun(self):
try:
# Save format
self.configPath.write_text("\n".join(['formatStr='+self.ui.formatStr.text() , 'downloadImg='+str(self.downloadImage)]))
self.confirmDialog.close()
self.progressbar = self.CustomProgressBar()
self.progressbar.move(-500, -500)
self.progressbar.setGeometry(0, 0, 300, 25)
self.progressbar.setMaximum(100)
self.progressbar.move(self.window().x() + 50,
self.window().y() + 150)
self.progressbar.setWindowTitle("執行中...")
self.progressbar.show()
self._func = RenameTool(self.fileNames, self.downloadImage, self.ui.formatStr.text())
self._func.countChanged.connect(self.onCountChanged)
self._func.finished.connect(self.threadFinished)
self._func.start()
self.progressbar.setFunc(self._func)
except:
print("Unexpected error:", sys.exc_info())
raise
def onCountChanged(self, value):
try:
self.progressbar.setValue(value)
except:
print("Unexpected error:", sys.exc_info())
raise
def threadFinished(self):
self.progressbar.close()
self._func.exit(0)
def openAuthorUrl(self):
try:
url = QtCore.QUrl('https://github.com/asadman1523')
QtGui.QDesktopServices.openUrl(url)
except:
# print("Unexpected error:", sys.exc_info())
raise
def openProductUrl(self) -> object:
url = QtCore.QUrl('https://github.com/asadman1523/odvrt')
QtGui.QDesktopServices.openUrl(url)
def delBtnClicked(self):
self.fileNames = []
self.ui.label.setText("共0個檔案")
self.ui.tableWidget.setRowCount(0)
self.setLayout(self.layout())
def downloadImgChecked(self):
try:
if self.ui.downloadImgCheckBox.isChecked():
self.downloadImage = True
self.settings[0] = True
else:
self.downloadImage = False
self.settings[0] = False
except:
pass
def updateFileList(self):
self.ui.tableWidget.setRowCount(len(self.fileNames))
self.ui.label.setText("共" + str(len(self.fileNames)) + "個檔案")
i = 0
while i < len(self.fileNames):
item = QTableWidgetItem()
# item.setFlags(item.flags() ^ 2)
item.setText(self.fileNames[i])
self.ui.tableWidget.setItem(i, 0, item)
i = i + 1
self.setLayout(self.layout())
class CustomProgressBar(QProgressBar):
func = None
def setFunc(self, func : RenameTool):
self.func = func
def closeEvent(self, a0: QtGui.QCloseEvent):
self.func.stopProc = True
a0.accept()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
sys.exit(app.exec())