Skip to content

path_widget

frog.gui.path_widget ¤

Provides a widget for choosing the path to a measure script.

Classes¤

OpenDirectoryWidget(initial_dir_path=None, **file_dialog_kwargs) ¤

Bases: PathWidget

A widget that lets the user choose the path to an existing directory.

Create a new OpenDirectoryWidget.

Parameters:

Name Type Description Default
initial_dir_path Path | None

The initial file path to display

None
file_dialog_kwargs Any

Arguments to pass to QFileDialog.getOpenFileName

{}
Source code in frog/gui/path_widget.py
111
112
113
114
115
116
117
118
119
120
121
122
123
def __init__(
    self,
    initial_dir_path: Path | None = None,
    **file_dialog_kwargs: Any,
) -> None:
    """Create a new OpenDirectoryWidget.

    Args:
        initial_dir_path: The initial file path to display
        file_dialog_kwargs: Arguments to pass to QFileDialog.getOpenFileName
    """
    super().__init__(initial_dir_path)
    self.file_dialog_kwargs = file_dialog_kwargs
Functions¤
try_get_path_from_dialog() ¤

Try to get the path of the dir to open by raising a dialog.

Source code in frog/gui/path_widget.py
125
126
127
128
129
def try_get_path_from_dialog(self) -> Path | None:
    """Try to get the path of the dir to open by raising a dialog."""
    dir_path = QFileDialog.getExistingDirectory(**self.file_dialog_kwargs)

    return Path(dir_path) if dir_path else None

OpenFileWidget(initial_file_path=None, extension=None, **file_dialog_kwargs) ¤

Bases: PathWidget

A widget that lets the user choose the path to an existing file.

Create a new OpenFileWidget.

Parameters:

Name Type Description Default
initial_file_path Path | None

The initial file path to display

None
extension str | None

The extension of the file to open

None
file_dialog_kwargs Any

Arguments to pass to QFileDialog.getOpenFileName

{}
Source code in frog/gui/path_widget.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def __init__(
    self,
    initial_file_path: Path | None = None,
    extension: str | None = None,
    **file_dialog_kwargs: Any,
) -> None:
    """Create a new OpenFileWidget.

    Args:
        initial_file_path: The initial file path to display
        extension: The extension of the file to open
        file_dialog_kwargs: Arguments to pass to QFileDialog.getOpenFileName
    """
    super().__init__(initial_file_path)
    if extension:
        file_dialog_kwargs["filter"] = f"*.{extension}"
    self.file_dialog_kwargs = file_dialog_kwargs
Functions¤
try_get_path_from_dialog() ¤

Try to get the path of the file to open by raising a dialog.

Source code in frog/gui/path_widget.py
101
102
103
104
105
def try_get_path_from_dialog(self) -> Path | None:
    """Try to get the path of the file to open by raising a dialog."""
    filename, _ = QFileDialog.getOpenFileName(**self.file_dialog_kwargs)

    return Path(filename) if filename else None

PathWidget(initial_file_path=None) ¤

Bases: QWidget

A widget containing a text box with a file path and a browse button.

Create a new PathWidget.

Parameters:

Name Type Description Default
initial_file_path Path | None

The initial file path to display

None
Source code in frog/gui/path_widget.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(self, initial_file_path: Path | None = None) -> None:
    """Create a new PathWidget.

    Args:
        initial_file_path: The initial file path to display
    """
    super().__init__()

    self.line_edit = QLineEdit()
    """Indicates the current selected path."""

    if initial_file_path:
        self.line_edit.setText(str(initial_file_path))

    browse = QPushButton("&Browse...")
    browse.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Minimum)
    browse.clicked.connect(self._browse_clicked)

    layout = QHBoxLayout()
    layout.setContentsMargins(0, 0, 0, 0)
    layout.addWidget(self.line_edit)
    layout.addWidget(browse)
    self.setLayout(layout)
Attributes¤
line_edit = QLineEdit() instance-attribute ¤

Indicates the current selected path.

Functions¤
set_path(path) ¤

Set the path of this widget.

Source code in frog/gui/path_widget.py
75
76
77
def set_path(self, path: Path) -> None:
    """Set the path of this widget."""
    self.line_edit.setText(str(path))
try_get_path() ¤

Try to get the path of the chosen file for this widget.

If the path hasn't yet been set, a QFileDialog is opened to let the user choose one.

Returns:

Type Description
Path | None

The selected file path or None if the user has cancelled

Source code in frog/gui/path_widget.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def try_get_path(self) -> Path | None:
    """Try to get the path of the chosen file for this widget.

    If the path hasn't yet been set, a QFileDialog is opened to let the user choose
    one.

    Returns:
        The selected file path or None if the user has cancelled
    """
    txt = self.line_edit.text()
    if txt:
        return Path(txt)

    # ...otherwise the user hasn't chosen a path. Let them do it now.
    filename = self.try_get_path_from_dialog()
    if filename:
        self.set_path(filename)
        return Path(filename)

    # No path selected
    return None
try_get_path_from_dialog() abstractmethod ¤

Try to get the file name by raising a dialog.

Source code in frog/gui/path_widget.py
49
50
51
@abstractmethod
def try_get_path_from_dialog(self) -> Path | None:
    """Try to get the file name by raising a dialog."""

SaveFileWidget(initial_file_path=None, extension=None, **file_dialog_kwargs) ¤

Bases: PathWidget

A widget that lets the user choose the path to save a file.

Create a new SaveFileWidget.

Parameters:

Name Type Description Default
initial_file_path Path | None

The initial file path to display

None
extension str | None

The extension of the file to save

None
file_dialog_kwargs Any

Arguments to pass to QFileDialog.getSaveFileName

{}
Source code in frog/gui/path_widget.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def __init__(
    self,
    initial_file_path: Path | None = None,
    extension: str | None = None,
    **file_dialog_kwargs: Any,
) -> None:
    """Create a new SaveFileWidget.

    Args:
        initial_file_path: The initial file path to display
        extension: The extension of the file to save
        file_dialog_kwargs: Arguments to pass to QFileDialog.getSaveFileName
    """
    super().__init__(initial_file_path)
    if extension:
        file_dialog_kwargs["filter"] = f"*.{extension}"
    self.extension = extension
    self.file_dialog_kwargs = file_dialog_kwargs
Functions¤
try_get_path_from_dialog() ¤

Try to get the path to save the file to by opening a dialog.

Source code in frog/gui/path_widget.py
154
155
156
157
158
159
160
161
162
163
164
165
166
def try_get_path_from_dialog(self) -> Path | None:
    """Try to get the path to save the file to by opening a dialog."""
    filename, _ = QFileDialog.getSaveFileName(**self.file_dialog_kwargs)

    # User cancelled
    if not filename:
        return None

    # Ensure it has the right extension if required
    if self.extension and not filename.lower().endswith(f".{self.extension}"):
        filename += f".{self.extension}"

    return Path(filename)