diff --git a/bcloud/App.py b/bcloud/App.py index cb29a73..c23357a 100644 --- a/bcloud/App.py +++ b/bcloud/App.py @@ -17,6 +17,7 @@ Config.check_first() _ = Config._ from bcloud import const +from bcloud.const import TargetInfo, TargetType from bcloud import gutil from bcloud.log import logger from bcloud import util @@ -37,6 +38,12 @@ BLINK_DELTA = 250 # 字体闪烁间隔, 250 miliseconds BLINK_SUSTAINED = 3 # 字体闪烁持续时间, 5 seconds +# 用于处理拖放上传 +DROP_TARGETS = ( + (TargetType.URI_LIST, Gtk.TargetFlags.OTHER_APP, TargetInfo.URI_LIST), +) +DROP_TARGET_LIST = [Gtk.TargetEntry.new(*t) for t in DROP_TARGETS] + class App: @@ -71,7 +78,7 @@ def on_app_startup(self, app): self.window.connect('delete-event', self.on_main_window_deleted) app.add_window(self.window) - self.window.drag_dest_set(Gtk.DestDefaults.ALL, const.DnD_TARGET_LIST, + self.window.drag_dest_set(Gtk.DestDefaults.ALL, DROP_TARGET_LIST, Gdk.DragAction.COPY) self.window.connect('drag-data-received', self.on_main_window_drag_data_received) @@ -219,7 +226,7 @@ def on_main_window_drag_data_received(self, window, drag_context, x, y, ''' if not self.profile: return - if info == const.TARGET_TYPE_URI_LIST: + if info == TargetInfo.URI_LIST: uris = data.get_uris() source_paths = util.uris_to_paths(uris) if source_paths: diff --git a/bcloud/HomePage.py b/bcloud/HomePage.py index 83a7359..3d58086 100644 --- a/bcloud/HomePage.py +++ b/bcloud/HomePage.py @@ -11,6 +11,7 @@ from bcloud import Config _ = Config._ from bcloud import const +from bcloud.const import TargetInfo, TargetType from bcloud.IconWindow import IconWindow from bcloud.IconWindow import TreeWindow from bcloud import gutil @@ -18,6 +19,14 @@ from bcloud import pcs from bcloud import util + +# 用于处理拖放上传 +DROP_TARGETS = ( + (TargetType.URI_LIST, Gtk.TargetFlags.OTHER_APP, TargetInfo.URI_LIST), +) +DROP_TARGET_LIST = [Gtk.TargetEntry.new(*t) for t in DROP_TARGETS] + + class PathBox(Gtk.Box): '''路径栏''' @@ -169,7 +178,7 @@ def __init__(self, app): super().__init__(orientation=Gtk.Orientation.VERTICAL) self.app = app - self.drag_dest_set(Gtk.DestDefaults.ALL, const.DnD_TARGET_LIST, + self.drag_dest_set(Gtk.DestDefaults.ALL, DROP_TARGET_LIST, Gdk.DragAction.COPY) if Config.GTK_GE_312: @@ -394,7 +403,7 @@ def do_drag_data_received(self, drag_context, x, y, data, info, time): ''' if not self.app.profile: return - if info == const.TARGET_TYPE_URI_LIST: + if info == TargetInfo.URI_LIST: uris = data.get_uris() source_paths = util.uris_to_paths(uris) if source_paths: diff --git a/bcloud/IconWindow.py b/bcloud/IconWindow.py index 5fcd666..6be1996 100644 --- a/bcloud/IconWindow.py +++ b/bcloud/IconWindow.py @@ -19,6 +19,7 @@ from bcloud import Config _ = Config._ from bcloud import const +from bcloud.const import TargetInfo, TargetType from bcloud.FolderBrowserDialog import FolderBrowserDialog from bcloud.NewFolderDialog import NewFolderDialog from bcloud.PropertiesDialog import PropertiesDialog @@ -34,12 +35,18 @@ range(11)) TYPE_TORRENT = 'application/x-bittorrent' -DRAG_TEXT = 0 DRAG_TARGETS = ( - ('text/plain', Gtk.TargetFlags.SAME_WIDGET, DRAG_TEXT), + # 用于拖拽下载 + (TargetType.URI_LIST, Gtk.TargetFlags.OTHER_APP, TargetInfo.URI_LIST), + # 用于移动文件到子目录 + (TargetType.PLAIN_TEXT, Gtk.TargetFlags.SAME_WIDGET, TargetInfo.PLAIN_TEXT), ) -TARGET_LIST = [Gtk.TargetEntry.new(*t) for t in DRAG_TARGETS] -DRAG_ACTION = Gdk.DragAction.COPY +DROP_TARGETS = ( + (TargetType.PLAIN_TEXT, Gtk.TargetFlags.SAME_WIDGET, TargetInfo.PLAIN_TEXT), +) +DRAG_TARGET_LIST = [Gtk.TargetEntry.new(*t) for t in DRAG_TARGETS] +DROP_TARGET_LIST = [Gtk.TargetEntry.new(*t) for t in DROP_TARGETS] +DRAG_ACTION = Gdk.DragAction.MOVE class IconWindow(Gtk.ScrolledWindow): '''这个类用于获取文件, 并将它显示到IconView中去. @@ -72,9 +79,9 @@ def init_ui(self): self.iconview.set_selection_mode(Gtk.SelectionMode.MULTIPLE) self.iconview.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, - TARGET_LIST, DRAG_ACTION) + DRAG_TARGET_LIST, DRAG_ACTION) self.iconview.connect('drag-data-get', self.on_drag_data_get) - self.iconview.enable_model_drag_dest(TARGET_LIST, DRAG_ACTION) + self.iconview.enable_model_drag_dest(DROP_TARGET_LIST, DRAG_ACTION) self.iconview.connect('drag-data-received', self.on_drag_data_received) self.iconview.connect('item-activated', self.on_iconview_item_activated) self.iconview.connect('button-press-event', @@ -140,8 +147,11 @@ def on_drag_data_get(self, widget, context, data, info, time): 'newname': self.liststore[tree_path][NAME_COL], }) filelist_str = json.dumps(filelist) - if info == DRAG_TEXT: + if info == TargetInfo.PLAIN_TEXT: data.set_text(filelist_str, -1) + # 拖拽时无法获取目标路径, 所以不能实现拖拽下载功能 + elif info == TargetInfo.URI_LIST: + data.set_uris([]) def on_drag_data_received(self, widget, context, x, y, data, info, time): '''拖放结束''' @@ -152,9 +162,11 @@ def on_drag_data_received(self, widget, context, x, y, data, info, time): return target_path = self.liststore[tree_path][PATH_COL] is_dir = self.liststore[tree_path][ISDIR_COL] - if not is_dir or info != DRAG_TEXT: + if not is_dir or info != TargetInfo.PLAIN_TEXT: + return + filelist_str = data.get_text() + if not filelist_str: return - filelist_str = data.get_data().decode() filelist = json.loads(filelist_str) for file_item in filelist: if file_item['path'] == target_path: @@ -624,9 +636,9 @@ def init_ui(self): self.iconview = Gtk.TreeView(model=self.liststore) self.iconview.set_tooltip_column(TOOLTIP_COL) self.iconview.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, - TARGET_LIST, DRAG_ACTION) + DRAG_TARGET_LIST, DRAG_ACTION) self.iconview.connect('drag-data-get', self.on_drag_data_get) - self.iconview.enable_model_drag_dest(TARGET_LIST, DRAG_ACTION) + self.iconview.enable_model_drag_dest(DROP_TARGET_LIST, DRAG_ACTION) self.iconview.connect('drag-data-received', self.on_drag_data_received) self.iconview.connect('row-activated', lambda view, path, column: @@ -704,9 +716,9 @@ def on_drag_data_received(self, widget, context, x, y, data, info, time): return target_path = self.liststore[tree_path][PATH_COL] is_dir = self.liststore[tree_path][ISDIR_COL] - if not is_dir or info != DRAG_TEXT: + if not is_dir or info != TargetInfo.PLAIN_TEXT: return - filelist_str = data.get_data().decode() + filelist_str = data.get_text() filelist = json.loads(filelist_str) for file_item in filelist: if file_item['path'] == target_path: diff --git a/bcloud/const.py b/bcloud/const.py index 0e34331..83aee56 100644 --- a/bcloud/const.py +++ b/bcloud/const.py @@ -8,8 +8,6 @@ 与界面相关的常量, 都位于Config.py. ''' -from gi.repository import Gtk - from bcloud import Config _ = Config._ @@ -96,9 +94,19 @@ class ValidatePathState: ) -# 拖放目标, 接收从其它程序拖进来的目录 -TARGET_TYPE_URI_LIST = 0 -DnD_LIST = ( - ('text/uri-list', Gtk.TargetFlags.OTHER_APP, TARGET_TYPE_URI_LIST), -) -DnD_TARGET_LIST = [Gtk.TargetEntry.new(*t) for t in DnD_LIST] +class TargetInfo: + '''拖放类型编号''' + + URI_LIST = 0 + PLAIN_TEXT = 1 + RAW = 2 + TEXT_JSON = 3 + + +class TargetType: + '''拖放类型''' + + URI_LIST = 'text/uri-list' + PLAIN_TEXT = 'text/plain' + RAW = 'application/octet-stream' + TEXT_JSON = 'application/json'