-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassandysFileListWidget.py
More file actions
184 lines (148 loc) · 7.21 KB
/
classandysFileListWidget.py
File metadata and controls
184 lines (148 loc) · 7.21 KB
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
# encoding:utf-8
# https://github.com/wangandi520
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QListWidget, QPushButton, QFileDialog, QAbstractItemView
from PyQt5.QtCore import Qt, QObject, pyqtSignal
from pathlib import Path
# pip install pyqt5
class andysFileListWidget(QWidget):
#列表内容数量发生变化时
fileListChangedSignal = pyqtSignal()
#被选中项目变化时
currentRowChangedSignal = pyqtSignal()
def __init__(self, parent=None):
# 拖拽文件加入列表。如果拖拽文件夹,里面的所有文件都加入。不重复加入
super().__init__(parent)
self.setAcceptDrops(True)
self.allFileListArray = []
self.currentSelectedFileListArray = []
self.fileListWidget = QListWidget()
self.ifFirstDrop = False
self.firstDropDir = False
self.openFileButton = QPushButton('添加')
self.deleteFileButton = QPushButton('删除')
self.clearListButton = QPushButton('清空')
# mode = 1,仅加载文件。mode = 2,仅加载文件夹
#self.mode = 1
self.openFileButton.clicked.connect(self.doOpenFileButton)
self.deleteFileButton.clicked.connect(self.doDeleteFileButton)
self.clearListButton.clicked.connect(self.doClearListButton)
self.fileListWidget.currentRowChanged.connect(self.doCurrentRowChanged)
self.fileListWidget.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.fileListWidget.selectionModel().selectionChanged.connect(self.doSelectionChanged)
topLayout = QHBoxLayout()
topLayout.addWidget(self.openFileButton)
topLayout.addWidget(self.deleteFileButton)
topLayout.addWidget(self.clearListButton)
bottomLayout = QHBoxLayout()
bottomLayout.addWidget(self.fileListWidget)
mainLayout = QVBoxLayout()
mainLayout.addLayout(topLayout)
mainLayout.addLayout(bottomLayout)
self.setLayout(mainLayout)
def dragEnterEvent(self, event):
event.accept()
def dropEvent(self, event):
for eachFile in event.mimeData().urls():
filePath = eachFile.toString()
if (filePath[0:8] == 'file:///' and Path(filePath[8:]) not in self.allFileListArray):
if Path.is_file(Path(filePath[8:])):
self.ifFirstDrop = False
self.allFileListArray.append(Path(filePath[8:]))
self.fileListWidget.addItem(Path(filePath[8:]).name)
if Path.is_dir(Path(filePath[8:])):
if(self.ifFirstDrop == False and len(self.allFileListArray) == 0):
self.ifFirstDrop = True
self.firstDropDir = Path(filePath[8:])
else:
self.ifFirstDrop = False
for file in Path(filePath[8:]).glob('**/*'):
if Path.is_file(file):
self.allFileListArray.append(file)
self.fileListWidget.addItem(file.name)
self.fileListChangedSignal.emit()
def dragMoveEvent(self, event):
event.accept()
def updateAllFileListArray(self, newArray):
self.allFileListArray = newArray
def reloadFileListWidget(self):
self.fileListWidget.clear()
for eachFile in self.allFileListArray:
self.fileListWidget.addItem(eachFile.name)
# def getMode(self):
# return self.mode
def getIfFirstDrop(self):
return self.ifFirstDrop
def getFirstDropDir(self):
return self.firstDropDir
def getCurrentRowFilePath(self):
if len(self.allFileListArray) > 0:
return self.allFileListArray[self.fileListWidget.currentRow()]
def getAllFileListArray(self):
return self.allFileListArray
def getLastRowFilePath(self):
if len(self.allFileListArray) > 0:
return self.allFileListArray[-1]
def getThisRowFilePath(self, rowNumber):
# index start = 0
if (rowNumber < len(self.allFileListArray) and len(self.allFileListArray) > 0):
return self.allFileListArray[rowNumber]
else:
return ''
def getCurrentRowFilePath(self):
if len(self.allFileListArray) > 0:
return self.allFileListArray[self.fileListWidget.currentRow()]
def getCurrentRow(self):
return self.fileListWidget.currentRow()
def setOpenFileButtonDisabled(self):
self.openFileButton.setDisabled(True)
def setDeleteFileButtonDisabled(self):
self.deleteFileButton.setDisabled(True)
def setClearListDisabled(self):
self.clearListButton.setDisabled(True)
def setCurrentRowFilePath(self, newPath):
if len(self.allFileListArray) > 0:
self.allFileListArray[self.fileListWidget.currentRow()] = newPath
# def setMode(self, value):
# self.mode = value
# return self.mode
def doSelectionChanged(self):
# 被选中的文件的路径
selectedListWidgetItemInOtherArray = [self.allFileListArray[getIndex.row()] for getIndex in self.fileListWidget.selectedIndexes()]
self.currentSelectedFileListArray = selectedListWidgetItemInOtherArray
def doCurrentRowChanged(self):
self.currentRowChangedSignal.emit()
# for i in range(self.fileListWidget.count()):
# print(self.fileListWidget.item(i).text())
#for eachItem in self.fileListWidget.selectedItems():
# print(self.fileListWidget.currentRow())
def doOpenFileButton(self):
# 打开文件。如果是文件夹请拖拽。
tempFileName = QFileDialog.getOpenFileNames(self, "打开文件", ".", "*.*")[0]
for eachFilePath in tempFileName:
if (Path(eachFilePath) not in self.allFileListArray):
if Path.is_file(Path(eachFilePath)):
self.allFileListArray.append(Path(eachFilePath))
self.fileListWidget.addItem(Path(eachFilePath).name)
if Path.is_dir(Path(eachFilePath)):
for eacheachFilePath in Path(eachFilePath).glob('**/*'):
if Path.is_file(eacheachFilePath):
self.allFileListArray.append(eacheachFilePath)
self.fileListWidget.addItem(eacheachFilePath.name)
self.fileListChangedSignal.emit()
def doDeleteFileButton(self):
if (self.fileListWidget.currentRow() > -1):
self.fileListWidget.takeItem(self.fileListWidget.row(self.fileListWidget.currentItem()))
self.allFileListArray.remove(self.allFileListArray[self.fileListWidget.currentRow()])
self.fileListChangedSignal.emit()
def doClearListButton(self):
self.fileListWidget.clear()
self.allFileListArray = []
self.fileListChangedSignal.emit()
self.ifFirstDrop = False
self.firstDropDir = False
if __name__ == '__main__':
app = QApplication(sys.argv)
mywidget = andysFileListWidget()
mywidget.show()
sys.exit(app.exec_())