Add drag & drop support on binary widget
issue7722 review54381002
This commit is contained in:
1 parent
1f2167a7ce
commit
11838f1dce
4 files changed
+52
-19
No files matched your search
@@ -1,3 +1,5 @@
|
||||
* Add drag & drop support on binary widget
|
||||
|
||||
Version 5.0.0 - 2018-10-01
|
||||
* Bug fixes (see mercurial logs for details)
|
||||
* Use dropdown for attachment
|
||||
|
||||
@@ -41,12 +41,24 @@ class Attachment(WinForm):
|
||||
|
||||
def add_uri(self, uri):
|
||||
data_field = self.screen.group.fields['data']
|
||||
link_field = self.screen.group.fields['link']
|
||||
type_field = self.screen.group.fields['type']
|
||||
name_field = self.screen.group.fields[data_field.attrs['filename']]
|
||||
description_field = self.screen.group.fields['description']
|
||||
new_record = self.screen.new()
|
||||
uri = unquote(uri)
|
||||
file_name = os.path.basename(urlparse(uri).path)
|
||||
name_field.set_client(new_record, file_name)
|
||||
data_field.set_client(new_record, urlopen(uri).read())
|
||||
parse = urlparse(uri)
|
||||
if not parse.scheme:
|
||||
description_field.set_client(new_record, uri)
|
||||
else:
|
||||
uri = unquote(uri)
|
||||
file_name = os.path.basename(parse.path)
|
||||
name_field.set_client(new_record, file_name)
|
||||
if parse.scheme == 'file':
|
||||
data_field.set_client(new_record, urlopen(uri).read())
|
||||
type_field.set_client(new_record, 'data')
|
||||
else:
|
||||
link_field.set_client(new_record, uri)
|
||||
type_field.set_client(new_record, 'link')
|
||||
self.screen.display()
|
||||
|
||||
def add_file(self, filename):
|
||||
|
||||
@@ -542,9 +542,9 @@ class Form(SignalEvent, TabContent):
|
||||
gtktoolbar = super(Form, self).create_toolbar(toolbars)
|
||||
|
||||
attach_btn = self.buttons['attach']
|
||||
target_entry = gtk.TargetEntry.new('text/uri-list', 0, 0)
|
||||
attach_btn.drag_dest_set(gtk.DEST_DEFAULT_ALL, [
|
||||
target_entry,
|
||||
gtk.TargetEntry.new('text/uri-list', 0, 0),
|
||||
gtk.TargetEntry.new('text/plain', 0, 0),
|
||||
], gtk.gdk.ACTION_MOVE | gtk.gdk.ACTION_COPY)
|
||||
attach_btn.connect('drag_data_received',
|
||||
self.attach_drag_data_received)
|
||||
@@ -712,8 +712,11 @@ class Form(SignalEvent, TabContent):
|
||||
win_attach = Attachment(record,
|
||||
lambda: self.update_attachment_count(reload=True))
|
||||
if info == 0:
|
||||
for uri in selection.get_uris():
|
||||
# Win32 cut&paste terminates the list with a NULL character
|
||||
if not uri or uri == '\0':
|
||||
continue
|
||||
win_attach.add_uri(uri)
|
||||
if selection.get_uris():
|
||||
for uri in selection.get_uris():
|
||||
# Win32 cut&paste terminates the list with a NULL character
|
||||
if not uri or uri == '\0':
|
||||
continue
|
||||
win_attach.add_uri(uri)
|
||||
else:
|
||||
win_attach.add_uri(selection.get_text())
|
||||
@@ -3,6 +3,8 @@
|
||||
import gtk
|
||||
import gettext
|
||||
import os
|
||||
from urllib.request import urlopen
|
||||
from urllib.parse import urlparse, unquote
|
||||
from tryton.common import common
|
||||
from tryton.common import file_selection, Tooltips, file_open, file_write
|
||||
from tryton.common.entry_position import reset_position
|
||||
@@ -36,6 +38,12 @@ class BinaryMixin(Widget):
|
||||
'tryton-search', gtk.ICON_SIZE_SMALL_TOOLBAR))
|
||||
self.but_select.set_relief(gtk.RELIEF_NONE)
|
||||
self.but_select.connect('clicked', self.select)
|
||||
target_entry = gtk.TargetEntry.new('text/uri-list', 0, 0)
|
||||
self.but_select.drag_dest_set(gtk.DEST_DEFAULT_ALL, [
|
||||
target_entry,
|
||||
], gtk.gdk.ACTION_MOVE | gtk.gdk.ACTION_COPY)
|
||||
self.but_select.connect(
|
||||
'drag-data-received', self.select_drag_data_received)
|
||||
tooltips.set_tip(self.but_select, _('Select...'))
|
||||
hbox.pack_start(self.but_select, expand=False, fill=False)
|
||||
|
||||
@@ -78,15 +86,23 @@ class BinaryMixin(Widget):
|
||||
def select(self, widget=None):
|
||||
if not self.field:
|
||||
return
|
||||
filters = self.filters
|
||||
self._set_uri(
|
||||
'file:///' + file_selection(
|
||||
_('Select'), preview=self.preview, filters=self.filters))
|
||||
|
||||
filename = file_selection(_('Select'), preview=self.preview,
|
||||
filters=filters)
|
||||
if filename:
|
||||
self.field.set_client(self.record, open(filename, 'rb').read())
|
||||
if self.filename_field:
|
||||
self.filename_field.set_client(self.record,
|
||||
os.path.basename(filename))
|
||||
def select_drag_data_received(
|
||||
self, widget, context, x, y, selection, info, timestamp):
|
||||
if not self.field:
|
||||
return
|
||||
for uri in selection.get_uris():
|
||||
self._set_uri(uri)
|
||||
|
||||
def _set_uri(self, uri):
|
||||
uri = unquote(uri)
|
||||
self.field.set_client(self.record, urlopen(uri).read())
|
||||
if self.filename_field:
|
||||
self.filename_field.set_client(self.record,
|
||||
os.path.basename(urlparse(uri).path))
|
||||
|
||||
def get_data(self):
|
||||
if hasattr(self.field, 'get_data'):
|
||||
|
||||
Reference in new issue
Block a user