def set_clipboard_image(self): """Export the formatted output to an image and store it in the clipboard. The image stored in the clipboard is a PNG file with alpha transparency. """ div = self.view.page().mainFrame().findFirstElement('.output') images = {} for background in (Qt.transparent, Qt.white): image = QImage(div.geometry().size(), QImage.Format_ARGB32_Premultiplied) image.fill(background) painter = QPainter(image) div.render(painter) painter.end() images[background] = image # Windows needs this buffer hack to get alpha transparency in # the copied PNG. buffer_ = QBuffer() buffer_.open(QIODevice.WriteOnly) images[Qt.transparent].save(buffer_, 'PNG') buffer_.close() data = QMimeData() data.setData('PNG', buffer_.data()) data.setImageData(images[Qt.white]) QApplication.clipboard().setMimeData(data)
def copy(self): """Copy to the clipboard""" data = QMimeData() text = '\n'.join([cursor.selectedText() \ for cursor in self.cursors()]) data.setText(text) data.setData(self.MIME_TYPE, text.encode('utf8')) QApplication.clipboard().setMimeData(data)
def mimeData(self, indexes): """Encodes ``indexes`` as mime data for dragging.""" # serialize data as xml root = etree.Element('stringlist') for index in indexes: element = self._get_element_for_index(index) root.append(deepcopy(element)) serialized = etree.tostring(root, xml_declaration=True) mimedata = QMimeData() mimedata.setData('text/xml', serialized) return mimedata