Skip to content

Commit

Permalink
Drag and Drop refined
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuLang committed Oct 27, 2014
1 parent b521b3d commit fac042f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
11 changes: 9 additions & 2 deletions bcloud/App.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 11 additions & 2 deletions bcloud/HomePage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@
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
from bcloud.log import logger
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):
'''路径栏'''

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
38 changes: 25 additions & 13 deletions bcloud/IconWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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中去.
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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):
'''拖放结束'''
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 16 additions & 8 deletions bcloud/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
与界面相关的常量, 都位于Config.py.
'''

from gi.repository import Gtk

from bcloud import Config
_ = Config._

Expand Down Expand Up @@ -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'

0 comments on commit fac042f

Please sign in to comment.