diff options
Diffstat (limited to 'examples/widgets/draganddrop/dropsite/droparea.py')
-rw-r--r-- | examples/widgets/draganddrop/dropsite/droparea.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/examples/widgets/draganddrop/dropsite/droparea.py b/examples/widgets/draganddrop/dropsite/droparea.py new file mode 100644 index 000000000..16416cef2 --- /dev/null +++ b/examples/widgets/draganddrop/dropsite/droparea.py @@ -0,0 +1,104 @@ +############################################################################# +## +## Copyright (C) 2022 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +from PySide6.QtCore import QMimeData, Qt, Slot, Signal +from PySide6.QtGui import QPalette, QPixmap +from PySide6.QtWidgets import QFrame, QLabel + + +class DropArea(QLabel): + + changed = Signal(QMimeData) + + def __init__(self, parent=None): + super().__init__(parent) + self.setMinimumSize(200, 200) + self.setFrameStyle(QFrame.Sunken | QFrame.StyledPanel) + self.setAlignment(Qt.AlignCenter) + self.setAcceptDrops(True) + self.setAutoFillBackground(True) + self.clear() + + def dragEnterEvent(self, event): + self.setText("<drop content>") + self.setBackgroundRole(QPalette.Highlight) + + event.acceptProposedAction() + self.changed.emit(event.mimeData()) + + def dragMoveEvent(self, event): + event.acceptProposedAction() + + def dropEvent(self, event): + mime_data = event.mimeData() + + if mime_data.hasImage(): + self.setPixmap(QPixmap(mime_data.imageData())) + elif mime_data.hasFormat("text/markdown"): + self.setText(mime_data.data("text/markdown")) + self.setTextFormat(Qt.MarkdownText) + elif mime_data.hasHtml(): + self.setText(mime_data.html()) + self.setTextFormat(Qt.RichText) + elif mime_data.hasText(): + self.setText(mime_data.text()) + self.setTextFormat(Qt.PlainText) + elif mime_data.hasUrls(): + url_list = mime_data.urls() + text = "" + for i in range(0, min(len(url_list), 32)): + text += url_list[i].path() + "\n" + self.setText(text) + else: + self.setText("Cannot display data") + + self.setBackgroundRole(QPalette.Dark) + event.acceptProposedAction() + + def dragLeaveEvent(self, event): + self.clear() + event.accept() + + @Slot() + def clear(self): + self.setText("<drop content>") + self.setBackgroundRole(QPalette.Dark) + + self.changed.emit(None) |