openlp/openlp/core/lib/listwidgetwithdnd.py

108 lines
4.3 KiB
Python
Raw Normal View History

2009-06-23 20:59:38 +00:00
# -*- coding: utf-8 -*-
2012-12-28 22:06:43 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2009-06-23 20:59:38 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2015-12-31 22:46:06 +00:00
# Copyright (c) 2008-2016 OpenLP Developers #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2010-06-19 17:31:42 +00:00
"""
Extend QListWidget to handle drag and drop functionality
"""
2012-04-12 14:16:12 +00:00
import os
2011-07-27 18:28:35 +00:00
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtGui, QtWidgets
2013-12-13 17:44:05 +00:00
from openlp.core.common import Registry
2011-07-27 18:28:35 +00:00
2013-02-01 19:58:18 +00:00
2015-11-07 00:49:40 +00:00
class ListWidgetWithDnD(QtWidgets.QListWidget):
"""
2010-06-19 17:31:42 +00:00
Provide a list widget to store objects and handle drag and drop events
"""
2013-08-31 18:17:38 +00:00
def __init__(self, parent=None, name=''):
2010-06-19 17:31:42 +00:00
"""
Initialise the list widget
"""
2013-07-18 11:17:40 +00:00
super(ListWidgetWithDnD, self).__init__(parent)
2013-12-24 07:19:57 +00:00
self.mime_data_text = name
2011-07-27 18:28:35 +00:00
def activateDnD(self):
"""
Activate DnD of widget
"""
self.setAcceptDrops(True)
2015-11-07 00:49:40 +00:00
self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
2013-12-24 07:19:57 +00:00
Registry().register_function(('%s_dnd' % self.mime_data_text), self.parent().load_file)
2011-07-27 18:28:35 +00:00
2009-06-23 20:59:38 +00:00
def mouseMoveEvent(self, event):
"""
2013-02-01 19:58:18 +00:00
Drag and drop event does not care what data is selected as the recipient will use events to request the data
move just tell it what plugin to call
2009-06-23 20:59:38 +00:00
"""
if event.buttons() != QtCore.Qt.LeftButton:
2011-02-10 18:38:03 +00:00
event.ignore()
2009-06-23 20:59:38 +00:00
return
2011-05-12 15:50:20 +00:00
if not self.selectedItems():
event.ignore()
return
2009-06-23 20:59:38 +00:00
drag = QtGui.QDrag(self)
2013-12-24 07:19:57 +00:00
mime_data = QtCore.QMimeData()
drag.setMimeData(mime_data)
mime_data.setText(self.mime_data_text)
2015-11-07 00:49:40 +00:00
drag.exec(QtCore.Qt.CopyAction)
2011-07-27 18:28:35 +00:00
def dragEnterEvent(self, event):
2013-02-01 19:58:18 +00:00
"""
When something is dragged into this object, check if you should be able to drop it in here.
"""
2011-08-02 05:07:09 +00:00
if event.mimeData().hasUrls():
2011-07-27 18:28:35 +00:00
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
2013-02-01 19:58:18 +00:00
"""
Make an object droppable, and set it to copy the contents of the object, not move it.
"""
2011-08-03 15:52:29 +00:00
if event.mimeData().hasUrls():
2011-07-27 18:28:35 +00:00
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.
2014-03-17 19:05:55 +00:00
:param event: Handle of the event pint passed
2011-07-27 18:28:35 +00:00
"""
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
2011-08-02 05:07:09 +00:00
files = []
2011-07-27 18:28:35 +00:00
for url in event.mimeData().urls():
local_file = os.path.normpath(url.toLocalFile())
2013-12-24 07:19:57 +00:00
if os.path.isfile(local_file):
files.append(local_file)
elif os.path.isdir(local_file):
listing = os.listdir(local_file)
2011-08-02 05:07:09 +00:00
for file in listing:
2013-12-24 07:19:57 +00:00
files.append(os.path.join(local_file, file))
Registry().execute('%s_dnd' % self.mime_data_text, {'files': files, 'target': self.itemAt(event.pos())})
2011-07-27 18:28:35 +00:00
else:
event.ignore()