1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2013 Riverbank Computing Limited 7## Copyright (C) 2010 Hans-Peter Jansen <hpj@urpla.net>. 8## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 9## All rights reserved. 10## 11## This file is part of the examples of PyQt. 12## 13## $QT_BEGIN_LICENSE:BSD$ 14## You may use this file under the terms of the BSD license as follows: 15## 16## "Redistribution and use in source and binary forms, with or without 17## modification, are permitted provided that the following conditions are 18## met: 19## * Redistributions of source code must retain the above copyright 20## notice, this list of conditions and the following disclaimer. 21## * Redistributions in binary form must reproduce the above copyright 22## notice, this list of conditions and the following disclaimer in 23## the documentation and/or other materials provided with the 24## distribution. 25## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 26## the names of its contributors may be used to endorse or promote 27## products derived from this software without specific prior written 28## permission. 29## 30## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 41## $QT_END_LICENSE$ 42## 43############################################################################# 44 45 46from PyQt5.QtCore import (pyqtSignal, QBuffer, QByteArray, QFile, QIODevice, 47 QMimeData, Qt) 48from PyQt5.QtGui import QDrag, QIcon, QImage, QPainter, QPixmap 49from PyQt5.QtWidgets import (QApplication, QGridLayout, QLabel, QPushButton, 50 QScrollArea, QWidget) 51from PyQt5.QtSvg import QSvgWidget 52 53import delayedencoding_rc 54 55 56class MimeData(QMimeData): 57 58 dataRequested = pyqtSignal(str) 59 60 def formats(self): 61 formats = QMimeData.formats(self) 62 formats.append('image/png') 63 64 return formats 65 66 def retrieveData(self, mimeType, qvtype): 67 self.dataRequested.emit(mimeType) 68 69 return QMimeData.retrieveData(self, mimeType, qvtype) 70 71 72class SourceWidget(QWidget): 73 def __init__(self, parent=None): 74 super(SourceWidget, self).__init__(parent) 75 76 self.mimeData = None 77 78 imageFile = QFile(':/images/example.svg') 79 imageFile.open(QIODevice.ReadOnly) 80 self.imageData = imageFile.readAll() 81 imageFile.close() 82 83 imageArea = QScrollArea() 84 self.imageLabel = QSvgWidget() 85 self.imageLabel.renderer().load(self.imageData) 86 imageArea.setWidget(self.imageLabel) 87 88 instructTopLabel = QLabel("This is an SVG drawing:") 89 instructBottomLabel = QLabel("Drag the icon to copy the drawing as a PNG file:") 90 dragIcon = QPushButton("Export") 91 dragIcon.setIcon(QIcon(':/images/drag.png')) 92 dragIcon.pressed.connect(self.startDrag) 93 94 layout = QGridLayout() 95 layout.addWidget(instructTopLabel, 0, 0, 1, 2) 96 layout.addWidget(imageArea, 1, 0, 2, 2) 97 layout.addWidget(instructBottomLabel, 3, 0) 98 layout.addWidget(dragIcon, 3, 1) 99 self.setLayout(layout) 100 self.setWindowTitle("Delayed Encoding") 101 102 def createData(self, mimeType): 103 if mimeType != 'image/png': 104 return 105 106 image = QImage(self.imageLabel.size(), QImage.Format_RGB32) 107 painter = QPainter() 108 painter.begin(image) 109 self.imageLabel.renderer().render(painter) 110 painter.end() 111 112 data = QByteArray() 113 buffer = QBuffer(data) 114 buffer.open(QIODevice.WriteOnly) 115 image.save(buffer, 'PNG') 116 buffer.close() 117 self.mimeData.setData('image/png', data) 118 119 def startDrag(self): 120 self.mimeData = MimeData() 121 self.mimeData.dataRequested.connect(self.createData, Qt.DirectConnection) 122 123 drag = QDrag(self) 124 drag.setMimeData(self.mimeData) 125 drag.setPixmap(QPixmap(':/images/drag.png')) 126 drag.exec_(Qt.CopyAction) 127 128 129if __name__ == '__main__': 130 131 import sys 132 133 app = QApplication(sys.argv) 134 window = SourceWidget() 135 window.show() 136 sys.exit(app.exec_()) 137