1
# -*- coding: utf-8 -*-
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
4
###############################################################################
5
# OpenLP - Open Source Lyrics Projection #
6
# --------------------------------------------------------------------------- #
7
# Copyright (c) 2008-2016 OpenLP Developers #
8
# --------------------------------------------------------------------------- #
9
# This program is free software; you can redistribute it and/or modify it #
10
# under the terms of the GNU General Public License as published by the Free #
11
# Software Foundation; version 2 of the License. #
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
18
# You should have received a copy of the GNU General Public License along #
19
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
21
###############################################################################
23
Extend QListWidget to handle drag and drop functionality
27
from PyQt5 import QtCore, QtGui, QtWidgets
29
from openlp.core.common import Registry
32
class ListWidgetWithDnD(QtWidgets.QListWidget):
34
Provide a list widget to store objects and handle drag and drop events
36
def __init__(self, parent=None, name=''):
38
Initialise the list widget
40
super(ListWidgetWithDnD, self).__init__(parent)
41
self.mime_data_text = name
43
def activateDnD(self):
45
Activate DnD of widget
47
self.setAcceptDrops(True)
48
self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
49
Registry().register_function(('%s_dnd' % self.mime_data_text), self.parent().load_file)
51
def mouseMoveEvent(self, event):
53
Drag and drop event does not care what data is selected as the recipient will use events to request the data
54
move just tell it what plugin to call
56
if event.buttons() != QtCore.Qt.LeftButton:
59
if not self.selectedItems():
62
drag = QtGui.QDrag(self)
63
mime_data = QtCore.QMimeData()
64
drag.setMimeData(mime_data)
65
mime_data.setText(self.mime_data_text)
66
drag.exec(QtCore.Qt.CopyAction)
68
def dragEnterEvent(self, event):
70
When something is dragged into this object, check if you should be able to drop it in here.
72
if event.mimeData().hasUrls():
77
def dragMoveEvent(self, event):
79
Make an object droppable, and set it to copy the contents of the object, not move it.
81
if event.mimeData().hasUrls():
82
event.setDropAction(QtCore.Qt.CopyAction)
87
def dropEvent(self, event):
89
Receive drop event check if it is a file and process it if it is.
91
:param event: Handle of the event pint passed
93
if event.mimeData().hasUrls():
94
event.setDropAction(QtCore.Qt.CopyAction)
97
for url in event.mimeData().urls():
98
local_file = os.path.normpath(url.toLocalFile())
99
if os.path.isfile(local_file):
100
files.append(local_file)
101
elif os.path.isdir(local_file):
102
listing = os.listdir(local_file)
104
files.append(os.path.join(local_file, file))
105
Registry().execute('%s_dnd' % self.mime_data_text, {'files': files, 'target': self.itemAt(event.pos())})