Implemented drag&drop between image groups

This commit is contained in:
Arjan Schrijver 2013-01-29 13:02:20 +01:00
parent d09b9edcc9
commit 34455239ef
3 changed files with 48 additions and 1 deletions

View File

@ -356,6 +356,12 @@ class MediaManagerItem(QtGui.QWidget):
if new_files:
self.validateAndLoad(new_files)
def dndMoveInternal(self, target):
"""
Handle internal moving of media manager items
"""
pass
def validateAndLoad(self, files):
"""
Process a list for files either from the File Dialog or from Drag and

View File

@ -45,6 +45,7 @@ class TreeWidgetWithDnD(QtGui.QTreeWidget):
"""
QtGui.QTreeWidget.__init__(self, parent)
self.mimeDataText = name
self.allowInternalDnD = False
self.header().close()
self.defaultIndentation = self.indentation()
self.setIndentation(0)
@ -58,6 +59,11 @@ class TreeWidgetWithDnD(QtGui.QTreeWidget):
self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'%s_dnd' % self.mimeDataText),
self.parent().loadFile)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'%s_dnd_internal' % self.mimeDataText),
self.parent().dndMoveInternal)
def doInternalDnD(self, accept):
self.allowInternalDnD = accept
def mouseMoveEvent(self, event):
"""
@ -80,6 +86,8 @@ class TreeWidgetWithDnD(QtGui.QTreeWidget):
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
elif self.allowInternalDnD:
event.accept()
else:
event.ignore()
@ -87,12 +95,15 @@ class TreeWidgetWithDnD(QtGui.QTreeWidget):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
elif self.allowInternalDnD:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
"""
Receive drop event check if it is a file and process it if it is.
Receive drop event, check if it is a file or internal object and process it if it is.
``event``
Handle of the event pint passed
@ -110,5 +121,9 @@ class TreeWidgetWithDnD(QtGui.QTreeWidget):
for file in listing:
files.append(os.path.join(localFile, file))
Receiver.send_message(u'%s_dnd' % self.mimeDataText, files)
elif self.allowInternalDnD:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
Receiver.send_message(u'%s_dnd_internal' % self.mimeDataText, self.itemAt(event.pos()))
else:
event.ignore()

View File

@ -85,6 +85,7 @@ class ImageMediaItem(MediaManagerItem):
self.listView.clear()
self.listView.setIconSize(QtCore.QSize(88, 50))
self.listView.setIndentation(self.listView.defaultIndentation)
self.listView.doInternalDnD(True)
self.servicePath = os.path.join(AppLocation.get_section_data_path(self.settingsSection), u'thumbnails')
check_directory_exists(self.servicePath)
# Import old images list
@ -259,6 +260,31 @@ class ImageMediaItem(MediaManagerItem):
self.loadFullList(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename),
initialLoad)
def dndMoveInternal(self, target):
"""
Handle drag-and-drop moving of images within the media manager
"""
items_to_move = self.listView.selectedItems()
# Determine group to move images to
target_group = target
if isinstance(target_group.data(0, QtCore.Qt.UserRole), ImageFilenames):
target_group = target.parent()
# Don't allow moving to the Imported group
if target_group.data(0, QtCore.Qt.UserRole) is None:
return
# Move images in the treeview
items_to_save = []
for item in items_to_move:
if isinstance(item.data(0, QtCore.Qt.UserRole), ImageFilenames):
item.parent().removeChild(item)
target_group.addChild(item)
item_data = item.data(0, QtCore.Qt.UserRole)
item_data.group_id = target_group.data(0, QtCore.Qt.UserRole).id
items_to_save.append(item_data)
target_group.sortChildren(0, QtCore.Qt.AscendingOrder)
# Update the group ID's of the images in the database
self.manager.save_objects(items_to_save)
def generateSlideData(self, service_item, item=None, xmlVersion=False,
remote=False, context=ServiceItemContext.Service):
background = QtGui.QColor(Settings().value(self.settingsSection + u'/background color'))