~suutari-olli/openlp/click-slide-to-go-live-from-blank

« back to all changes in this revision

Viewing changes to openlp/core/lib/listwidgetwithdnd.py

  • Committer: Simon Hanna
  • Date: 2016-05-17 08:48:19 UTC
  • mfrom: (2625.1.36 openlp)
  • mto: (2625.1.37 openlp)
  • mto: This revision was merged to the branch mainline in revision 2649.
  • Revision ID: simon.hanna@serve-me.info-20160517084819-lgup78nzyzjympuu
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
3
 
 
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.                              #
12
 
#                                                                             #
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    #
16
 
# more details.                                                               #
17
 
#                                                                             #
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
 
###############################################################################
22
 
"""
23
 
Extend QListWidget to handle drag and drop functionality
24
 
"""
25
 
import os
26
 
 
27
 
from PyQt5 import QtCore, QtGui, QtWidgets
28
 
 
29
 
from openlp.core.common import Registry
30
 
 
31
 
 
32
 
class ListWidgetWithDnD(QtWidgets.QListWidget):
33
 
    """
34
 
    Provide a list widget to store objects and handle drag and drop events
35
 
    """
36
 
    def __init__(self, parent=None, name=''):
37
 
        """
38
 
        Initialise the list widget
39
 
        """
40
 
        super(ListWidgetWithDnD, self).__init__(parent)
41
 
        self.mime_data_text = name
42
 
 
43
 
    def activateDnD(self):
44
 
        """
45
 
        Activate DnD of widget
46
 
        """
47
 
        self.setAcceptDrops(True)
48
 
        self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
49
 
        Registry().register_function(('%s_dnd' % self.mime_data_text), self.parent().load_file)
50
 
 
51
 
    def mouseMoveEvent(self, event):
52
 
        """
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
55
 
        """
56
 
        if event.buttons() != QtCore.Qt.LeftButton:
57
 
            event.ignore()
58
 
            return
59
 
        if not self.selectedItems():
60
 
            event.ignore()
61
 
            return
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)
67
 
 
68
 
    def dragEnterEvent(self, event):
69
 
        """
70
 
        When something is dragged into this object, check if you should be able to drop it in here.
71
 
        """
72
 
        if event.mimeData().hasUrls():
73
 
            event.accept()
74
 
        else:
75
 
            event.ignore()
76
 
 
77
 
    def dragMoveEvent(self, event):
78
 
        """
79
 
        Make an object droppable, and set it to copy the contents of the object, not move it.
80
 
        """
81
 
        if event.mimeData().hasUrls():
82
 
            event.setDropAction(QtCore.Qt.CopyAction)
83
 
            event.accept()
84
 
        else:
85
 
            event.ignore()
86
 
 
87
 
    def dropEvent(self, event):
88
 
        """
89
 
        Receive drop event check if it is a file and process it if it is.
90
 
 
91
 
        :param event:  Handle of the event pint passed
92
 
        """
93
 
        if event.mimeData().hasUrls():
94
 
            event.setDropAction(QtCore.Qt.CopyAction)
95
 
            event.accept()
96
 
            files = []
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)
103
 
                    for file in listing:
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())})
106
 
        else:
107
 
            event.ignore()