diff options
author | Yamakaja <dastw@gmx.net> | 2020-07-19 16:18:38 +0200 |
---|---|---|
committer | Sebastian Koslowski <sebastian.koslowski@gmail.com> | 2020-07-23 08:27:30 +0200 |
commit | b9eff3f5e61ae2443f85549bcf60f1fae4ff0447 (patch) | |
tree | b4413369331f18015366fac5c2514fdf7ad5b694 /grc/gui | |
parent | c2f42238032e2315371aaec8a64687d99028ccd2 (diff) |
grc: Add dir_select parameter type
This commit adds a new parameter type to GNURadio Companion that allows
you to select directories using a gui. This functionality is
very similar to that of the previously available "file_open" and
"file_save" parameter types, and mainly differs by calling GTKs file
chooser with FileChooserAction.SELECT_FOLDER.
Diffstat (limited to 'grc/gui')
-rw-r--r-- | grc/gui/ParamWidgets.py | 38 | ||||
-rw-r--r-- | grc/gui/canvas/param.py | 3 |
2 files changed, 41 insertions, 0 deletions
diff --git a/grc/gui/ParamWidgets.py b/grc/gui/ParamWidgets.py index feea8f8418..7a5ce419fb 100644 --- a/grc/gui/ParamWidgets.py +++ b/grc/gui/ParamWidgets.py @@ -334,3 +334,41 @@ class FileParam(EntryParam): self._editing_callback() self._apply_change() file_dialog.destroy() # destroy the dialog + +class DirectoryParam(FileParam): + """Provide an entry box for a directory and a button to browse for it.""" + + def _handle_clicked(self, widget=None): + """ + Open the directory selector, when the button is clicked. + On success, update the entry. + """ + dirname = self.param.get_evaluated() if self.param.is_valid() else '' + + if not os.path.isdir(dirname): # Check if directory exists, if not fall back to workdir + dirname = os.getcwd() + + if self.param.dtype == "dir_select": # Setup directory selection dialog, and fail for unexpected dtype + dir_dialog = Gtk.FileChooserDialog( + title = 'Select a Directory...', action = Gtk.FileChooserAction.SELECT_FOLDER, + transient_for = self._transient_for + ) + else: + raise ValueError("Can't open directory chooser dialog for type " + repr(self.param.dtype)) + + # Set dialog properties + dir_dialog.add_buttons( 'gtk-cancel', Gtk.ResponseType.CANCEL, 'gtk-open', Gtk.ResponseType.OK ) + dir_dialog.set_current_folder(dirname) + dir_dialog.set_local_only(True) + dir_dialog.set_select_multiple(False) + + # Show dialog and update paramter on success + if Gtk.ResponseType.OK == dir_dialog.run(): + path = dir_dialog.get_filename() + self._input.set_text(path) + self._editing_callback() + self._apply_change() + + # Cleanup dialog + dir_dialog.destroy() + diff --git a/grc/gui/canvas/param.py b/grc/gui/canvas/param.py index b612902928..462bcee86f 100644 --- a/grc/gui/canvas/param.py +++ b/grc/gui/canvas/param.py @@ -32,6 +32,9 @@ class Param(CoreParam): if dtype in ('file_open', 'file_save'): input_widget_cls = ParamWidgets.FileParam + elif dtype == 'dir_select': + input_widget_cls = ParamWidgets.DirectoryParam + elif dtype == 'enum': input_widget_cls = ParamWidgets.EnumParam |