Skip to content

Commit

Permalink
Corpus - Open file dialogue in previous location
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Aug 24, 2023
1 parent 9e2828c commit b306874
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
45 changes: 45 additions & 0 deletions orangecontrib/text/widgets/utils/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import unittest
from unittest.mock import patch, ANY

from orangecontrib.text.corpus import get_sample_corpora_dir
from orangecontrib.text.widgets.utils import FileWidget, QFileDialog
from orangewidget.tests.base import GuiTest


class TestFileWidget(GuiTest):
@patch.object(QFileDialog, "getOpenFileName", return_value=("path", ""))
def test_start_dir(self, mock):
file_widget = FileWidget(
recent_files=[],
icon_size=(16, 16),
dialog_title="Open Orange Document Corpus",
reload_label="Reload",
browse_label="Browse",
allow_empty=False,
minimal_width=250,
)
file_widget.browse()
mock.assert_called_with(ANY, ANY, "~/", ANY)

file_widget.recent_files = ["book-excerpts.tab"]
file_widget.browse()
mock.assert_called_with(ANY, ANY, get_sample_corpora_dir(), ANY)

cur_dir = os.path.dirname(__file__)
file_widget.recent_files.insert(0, os.path.join(cur_dir, "file.tab"))
file_widget.browse()
mock.assert_called_with(ANY, ANY, cur_dir, ANY)

# dir doesn't exit case
file_widget.recent_files.insert(0, "/non/exiting/dir/file.tab")
file_widget.browse()
mock.assert_called_with(ANY, ANY, "~/", ANY)

# if browse have start_dir argument use this path
file_widget.browse("/sample/path")
mock.assert_called_with(ANY, ANY, "/sample/path", ANY)


if __name__ == "__main__":
unittest.main()
46 changes: 34 additions & 12 deletions orangecontrib/text/widgets/utils/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,27 @@ class FileWidget(QWidget):
on_open = pyqtSignal(str)

# TODO consider removing directory_aliases since it is not used any more
def __init__(self, dialog_title='', dialog_format='',
start_dir=os.path.expanduser('~/'),
icon_size=(12, 20), minimal_width=200,
browse_label='Browse', on_open=None,
reload_button=True, reload_label='Reload',
recent_files=None, directory_aliases=None,
allow_empty=True, empty_file_label='(none)'):
""" Creates a widget with a button for file loading and
def __init__(
self,
dialog_title="",
dialog_format="",
icon_size=(12, 20),
minimal_width=200,
browse_label="Browse",
on_open=None,
reload_button=True,
reload_label="Reload",
recent_files=None,
directory_aliases=None,
allow_empty=True,
empty_file_label="(none)",
):
"""Creates a widget with a button for file loading and
an optional combo box for recent files and reload buttons.
Args:
dialog_title (str): The title of the dialog.
dialog_format (str): Formats for the dialog.
start_dir (str): A directory to start from.
icon_size (int, int): The size of buttons' icons.
on_open (callable): A callback function that accepts filepath as the only argument.
reload_button (bool): Whether to show reload button.
Expand All @@ -246,7 +253,6 @@ def __init__(self, dialog_title='', dialog_format='',
super().__init__()
self.dialog_title = dialog_title
self.dialog_format = dialog_format
self.start_dir = start_dir

# Recent files should also contain `empty_file_label` so
# when (none) is selected this is stored in settings.
Expand All @@ -259,7 +265,8 @@ def __init__(self, dialog_title='', dialog_format='',
self.recent_files.append(self.empty_file_label)

self.check_existence()
self.on_open.connect(on_open)
if on_open:
self.on_open.connect(on_open)

layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
Expand Down Expand Up @@ -290,8 +297,23 @@ def __init__(self, dialog_title='', dialog_format='',
self.reload_button.setIconSize(QSize(*icon_size))
layout.addWidget(self.reload_button)

def __start_dir(self):
"""Extract start dir form recent path or return home"""
if self.recent_files and self.recent_files[-1] != self.empty_file_label:
recent = self.recent_files[0] # latest path
if not os.path.isabs(recent):
# if not absolute path it is just filename of file in sample
# corpora dir - attach the path
recent = os.path.join(get_sample_corpora_dir(), recent)
recent = os.path.dirname(recent)
if os.path.exists(recent):
return recent

return "~/"

def browse(self, start_dir=None):
start_dir = start_dir or self.start_dir
if not start_dir:
start_dir = self.__start_dir()
path, _ = QFileDialog().getOpenFileName(self, self.dialog_title,
start_dir, self.dialog_format)

Expand Down

0 comments on commit b306874

Please sign in to comment.