Skip to content

Commit 55963a2

Browse files
committed
Code style improvement and documentation
1 parent 892a7a4 commit 55963a2

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

qtapputils/widgets/path.py

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,41 @@
2323
# ---- Local imports
2424
from qtapputils.qthelpers import create_toolbutton
2525

26+
2627
class PathBoxWidget(QFrame):
2728
"""
28-
A widget to display and select a directory or file location.
29+
a widget to display and select a directory or file location.
30+
31+
Features
32+
--------
33+
- Read-only line edit showing the current path.
34+
- Browse button to open a QFileDialog for selecting a path.
35+
- Optionally, uses a custom icon for the browse button.
36+
- Emits `sig_path_changed` when the path changes.
37+
38+
Parameters
39+
----------
40+
parent : QWidget, optional
41+
Parent widget.
42+
path_type : str, optional
43+
Type of path dialog: 'getExistingDirectory', 'getOpenFileName', or
44+
'getSaveFileName'.
45+
filters : str, optional
46+
Filter string for file dialogs.
47+
gettext : Callable, optional
48+
Translation function for GUI strings.
49+
browse_icon : QIcon, optional
50+
Custom icon for the browse button.
2951
"""
3052
sig_path_changed = Signal(str)
3153

32-
def __init__(self, parent: QWidget = None,
33-
path_type: str = 'getExistingDirectory',
34-
filters: str = None, gettext: Callable = None,
35-
browse_icon: QIcon = None):
54+
def __init__(
55+
self,
56+
parent: QWidget = None,
57+
path_type: str = 'getExistingDirectory',
58+
filters: str = None,
59+
gettext: Callable = None,
60+
browse_icon: QIcon = None):
3661
super().__init__(parent)
3762

3863
_ = gettext if gettext else lambda x: x
@@ -54,9 +79,7 @@ def __init__(self, parent: QWidget = None,
5479
self.browse_btn.setDefault(False)
5580
self.browse_btn.setAutoDefault(False)
5681
self.browse_btn.clicked.connect(self.browse_path)
57-
58-
# We need to do this to align vertically the height of the
59-
# browse button with the line edit.
82+
# Align line edit height with button.
6083
self.path_lineedit.setFixedHeight(
6184
self.browse_btn.sizeHint().height() - 2)
6285
else:
@@ -73,20 +96,27 @@ def __init__(self, parent: QWidget = None,
7396
layout.addWidget(self.path_lineedit, 0, 0)
7497
layout.addWidget(self.browse_btn, 0, 1)
7598

76-
def is_valid(self):
77-
"""Return whether path is valid."""
99+
def is_valid(self) -> bool:
100+
"""Return True if the current path exists on disk."""
78101
return osp.exists(self.path())
79102

80-
def is_empty(self):
81-
"""Return whether the path is empty."""
82-
return self.path_lineedit.text() == ''
103+
def is_empty(self) -> bool:
104+
"""Return True if the current path is empty."""
105+
return not self.path_lineedit.text().strip()
83106

84-
def path(self):
85-
"""Return the path of this pathbox widget."""
107+
def path(self) -> str:
108+
"""Return the currently displayed path."""
86109
return self.path_lineedit.text()
87110

88111
def set_path(self, path: str):
89-
"""Set the path to the specified value."""
112+
"""
113+
Set the path to the specified value.
114+
115+
Parameters
116+
----------
117+
path : str
118+
The new path to display and set as default directory.
119+
"""
90120
if path == self.path():
91121
return
92122

@@ -102,22 +132,29 @@ def browse_path(self):
102132
self, self._caption, self.directory(),
103133
options=QFileDialog.ShowDirsOnly)
104134
elif self._path_type == 'getOpenFileName':
105-
path, ext = QFileDialog.getOpenFileName(
135+
path, _ = QFileDialog.getOpenFileName(
106136
self, self._caption, self.directory(), self.filters)
107137
elif self._path_type == 'getSaveFileName':
108-
path, ext = QFileDialog.getSaveFileName(
138+
path, _ = QFileDialog.getSaveFileName(
109139
self, self._caption, self.directory(), self.filters)
110140

111141
if path:
112142
self.set_path(path)
113143

114-
def directory(self):
144+
def directory(self) -> str:
115145
"""Return the directory that is used by the QFileDialog."""
116146
return (self._directory if osp.exists(self._directory) else
117147
osp.expanduser('~'))
118148

119149
def set_directory(self, directory: str = path):
120-
"""Set the default directory that will be used by the QFileDialog."""
150+
"""
151+
Set the default directory for file dialogs.
152+
153+
Parameters
154+
----------
155+
directory : str or None
156+
Directory path to set as default.
157+
"""
121158
if directory is not None and osp.exists(directory):
122159
self._directory = directory
123160

0 commit comments

Comments
 (0)