-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFileLine.py
71 lines (54 loc) · 1.95 KB
/
FileLine.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
from PySide2 import QtCore, QtWidgets
import os
class FileLine(QtWidgets.QWidget):
pathChanged = QtCore.Signal()
def __init__(self, parent=None):
super(FileLine, self).__init__(parent)
self.__path = None
# Layout
main_layout = QtWidgets.QHBoxLayout(self)
self.setLayout(main_layout)
main_layout.setContentsMargins(0, 0, 0, 0)
# Widgets
self.line = QtWidgets.QLineEdit()
self.line.setReadOnly(True)
self.btn = QtWidgets.QPushButton()
self.btn.setFixedHeight(20)
self.btn.setIcon(self.btn.style().standardIcon(
QtWidgets.QStyle.SP_DirIcon))
# Add Widgets
main_layout.addWidget(self.line)
main_layout.addWidget(self.btn)
# Logic
self.btn.clicked.connect(self.btn_clicked)
@property
def path(self):
return self.get_path()
@path.setter
def path(self, path):
self.set_path(path)
def btn_clicked(self):
""" The functionality of the button being clicked.
We launch a file dialog and get the result. We send the path to the
method that sets the rest if the widget and emits an update
"""
# Get the starting directory. If the path already exists, we use that
# if it doesn't exist we open in the home folder of the user
if os.path.exists(self.line.text()):
start_dir = self.line.text()
else:
start_dir = os.path.expanduser("~")
path = QtWidgets.QFileDialog.getExistingDirectory(
self, 'Set a Directory', start_dir, QtWidgets.QFileDialog.ShowDirsOnly)
self.set_path(path)
def get_path(self):
""" Get the path
"""
return self.line.text()
def set_path(self, path):
""" Set the path
"""
path = os.path.abspath(path)
if os.path.exists(path):
self.line.setText(path)
self.pathChanged.emit()