-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindPathWidget.py
122 lines (93 loc) · 3.55 KB
/
findPathWidget.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
import subprocess
from PyQt5.QtCore import Qt
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QPushButton, QFileDialog, QHBoxLayout, QLabel, QWidget, QAction, \
QMenu, QLineEdit
class FindPathLineEdit(QLineEdit):
def __init__(self):
super().__init__()
self.__initUi()
def __initUi(self):
self.setMouseTracking(True)
self.setReadOnly(True)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.__prepareMenu)
def mouseMoveEvent(self, e):
self.__showToolTip()
return super().mouseMoveEvent(e)
def __showToolTip(self):
text = self.text()
text_width = self.fontMetrics().boundingRect(text).width()
if text_width > self.width():
self.setToolTip(text)
else:
self.setToolTip('')
def __prepareMenu(self, pos):
menu = QMenu(self)
openDirAction = QAction('Open Path')
openDirAction.setEnabled(self.text().strip() != '')
openDirAction.triggered.connect(self.__openPath)
menu.addAction(openDirAction)
menu.exec(self.mapToGlobal(pos))
def __openPath(self):
filename = self.text()
path = filename.replace('/', '\\')
subprocess.Popen(r'explorer /select,"' + path + '"')
class FindPathWidget(QWidget):
findClicked = pyqtSignal()
added = pyqtSignal(str)
def __init__(self, default_filename: str = ''):
super().__init__()
self.__initVal()
self.__initUi(default_filename)
def __initVal(self):
self.__ext_of_files = ''
self.__directory = False
def __initUi(self, default_filename: str = ''):
self.__pathLineEdit = FindPathLineEdit()
if default_filename:
self.__pathLineEdit.setText(default_filename)
self.__pathFindBtn = QPushButton('Find')
self.__pathFindBtn.clicked.connect(self.__find)
self.__pathLineEdit.setMaximumHeight(self.__pathFindBtn.sizeHint().height())
lay = QHBoxLayout()
lay.addWidget(self.__pathLineEdit)
lay.addWidget(self.__pathFindBtn)
lay.setContentsMargins(0, 0, 0, 0)
self.setLayout(lay)
def setLabel(self, text):
self.layout().insertWidget(0, QLabel(text))
def setExtOfFiles(self, ext_of_files):
self.__ext_of_files = ext_of_files
def getLineEdit(self):
return self.__pathLineEdit
def getButton(self):
return self.__pathFindBtn
def getFileName(self):
return self.__pathLineEdit.text()
def setCustomFind(self, f: bool):
if f:
self.__pathFindBtn.clicked.disconnect(self.__find)
self.__pathFindBtn.clicked.connect(self.__customFind)
def __customFind(self):
self.findClicked.emit()
def __find(self):
if self.isForDirectory():
filename = QFileDialog.getExistingDirectory(self, 'Open Directory', '', QFileDialog.ShowDirsOnly)
if filename:
pass
else:
return
else:
str_exp_files_to_open = self.__ext_of_files if self.__ext_of_files else 'All Files (*.*)'
filename = QFileDialog.getOpenFileName(self, 'Find', '', str_exp_files_to_open)
if filename[0]:
filename = filename[0]
else:
return
self.__pathLineEdit.setText(filename)
self.added.emit(filename)
def setAsDirectory(self, f: bool):
self.__directory = f
def isForDirectory(self) -> bool:
return self.__directory