From 402f0b0c8ab51b9cedae214f7f9d2c333705c0e7 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Oct 2020 20:49:15 -0700 Subject: [PATCH] closes #645 - drag-and-drop doesn't honor multiFileChooser logic --- gooey/gui/application.py | 2 +- gooey/gui/components/mouse.py | 2 ++ gooey/gui/components/widgets/core/chooser.py | 29 ++++++++++++++----- .../gui/components/widgets/core/text_input.py | 4 ++- gooey/gui/util/filedrop.py | 21 +++++++++----- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/gooey/gui/application.py b/gooey/gui/application.py index 428bdf2..c1ed1db 100644 --- a/gooey/gui/application.py +++ b/gooey/gui/application.py @@ -30,7 +30,7 @@ def build_app(build_spec): i18n.load(build_spec['language_dir'], build_spec['language'], build_spec['encoding']) imagesPaths = image_repository.loadImages(build_spec['image_dir']) gapp = GooeyApplication(merge(build_spec, imagesPaths)) - # wx.lib.inspection.InspectionTool().Show() + wx.lib.inspection.InspectionTool().Show() gapp.Show() return (app, gapp) diff --git a/gooey/gui/components/mouse.py b/gooey/gui/components/mouse.py index ddbd2d7..0569f78 100644 --- a/gooey/gui/components/mouse.py +++ b/gooey/gui/components/mouse.py @@ -23,4 +23,6 @@ def notifyMouseEvent(event): """ Notify interested listeners of the LEFT_DOWN mouse event """ + # TODO: is there ever a situation where this wouldn't be skipped..? + event.Skip() pub.send_message_sync(events.LEFT_DOWN, wxEvent=event) \ No newline at end of file diff --git a/gooey/gui/components/widgets/core/chooser.py b/gooey/gui/components/widgets/core/chooser.py index 26151b1..baabf16 100644 --- a/gooey/gui/components/widgets/core/chooser.py +++ b/gooey/gui/components/widgets/core/chooser.py @@ -8,24 +8,44 @@ from gooey.gui.components.widgets.dialogs.calender_dialog import CalendarDlg from gooey.gui.components.widgets.dialogs.time_dialog import TimeDlg from gooey.gui.lang.i18n import _ from gooey.util.functional import merge +from gui.util.filedrop import FileDrop class Chooser(wx.Panel): """ + TODO: Tests! + TODO: Document GooeyOptions! Base 'Chooser' type. Launches a Dialog box that allows the user to pick files, directories, dates, etc.. and places the result into a TextInput in the UI - """ + TODO: oh, young me. DRY != Good Abstraction + TODO: this is another weird inheritance hierarchy that's hard + to follow. Why do subclasses rather into, not their parent + class, but their _physical_ UI parent to grab the Gooey Options? + All this could be simplified to make the data flow + more apparent. + """ + _gooey_options = { + 'pathsep': str + } def __init__(self, parent, *args, **kwargs): super(Chooser, self).__init__(parent) + self.options = parent._options buttonLabel = kwargs.pop('label', _('browse')) self.widget = TextInput(self, *args, **kwargs) self.button = wx.Button(self, label=buttonLabel) self.button.Bind(wx.EVT_BUTTON, self.spawnDialog) + self.dropTarget = FileDrop(self.widget, self.dropHandler) + self.SetDropTarget(self.dropTarget) + self.widget.SetDropTarget(self.dropTarget) self.layout() + def dropHandler(self, x, y, filenames): + sep = self.options.get('pathsep', os.pathsep) + self.widget.setValue(sep.join(filenames)) + return True def layout(self): layout = wx.BoxSizer(wx.HORIZONTAL) @@ -36,25 +56,21 @@ class Chooser(wx.Panel): v.Add(layout, 1, wx.EXPAND, wx.TOP, 1) self.SetSizer(v) - def spawnDialog(self, event): fd = self.getDialog() if fd.ShowModal() == wx.ID_CANCEL: return self.processResult(self.getResult(fd)) - def getDialog(self): return wx.FileDialog(self, _('open_file')) def getResult(self, dialog): return dialog.GetPath() - def processResult(self, result): self.setValue(result) - def setValue(self, value): self.widget.setValue(value) @@ -137,17 +153,16 @@ class DateChooser(Chooser): defaults = {'label': _('choose_date')} super(DateChooser, self).__init__(*args, **merge(kwargs, defaults)) - def getDialog(self): return CalendarDlg(self) + class TimeChooser(Chooser): """ Launches a time picker which returns and ISO Time """ def __init__(self, *args, **kwargs): defaults = {'label': _('choose_time')} super(TimeChooser, self).__init__(*args, **merge(kwargs, defaults)) - def getDialog(self): return TimeDlg(self) diff --git a/gooey/gui/components/widgets/core/text_input.py b/gooey/gui/components/widgets/core/text_input.py index 0b74084..b161f7f 100644 --- a/gooey/gui/components/widgets/core/text_input.py +++ b/gooey/gui/components/widgets/core/text_input.py @@ -29,10 +29,12 @@ class TextInput(wx.Panel): self.widget.AppendText(str(value)) self.widget.SetInsertionPoint(0) - def getValue(self): return self.widget.GetValue() + def SetDropTarget(self, target): + self.widget.SetDropTarget(target) + def PasswordInput(_, parent, *args, **kwargs): diff --git a/gooey/gui/util/filedrop.py b/gooey/gui/util/filedrop.py index abb64b3..7b9f355 100644 --- a/gooey/gui/util/filedrop.py +++ b/gooey/gui/util/filedrop.py @@ -1,11 +1,16 @@ import wx + class FileDrop(wx.FileDropTarget): - def __init__(self, window): - wx.FileDropTarget.__init__(self) - self.window = window - - def OnDropFiles(self, x, y, filenames): - for name in filenames: - self.window.WriteText(name) - return True + def __init__(self, window, dropStrategy=None): + wx.FileDropTarget.__init__(self) + self.window = window + self.dropHandler = dropStrategy or self._defaultStrategy + + def OnDropFiles(self, x, y, filenames): + return self.dropHandler(x, y, filenames) + + def _defaultStrategy(self, x, y, filenames): + for name in filenames: + self.window.WriteText(name) + return True