def hCopy(self): """ # slot for action copy_to_clipboard """ sel = self.listWdg.selectedItems() #################### # test code l = [] for item in sel: # get url from path l.append(QUrl.fromLocalFile(item.data(Qt.UserRole)[0])) # init clipboard data q = QMimeData() # set some Windows magic values for copying files from system clipboard : Don't modify # 1 : copy; 2 : move q.setData("Preferred DropEffect", QByteArray(1, "2")) q.setUrls(l) # end of test code ##################### # copy image to clipboard item = sel[0] filename = item.data(Qt.UserRole)[0] if filename.endswith(IMAGE_FILE_EXTENSIONS): q.setImageData(QImage(sel[0].data(Qt.UserRole)[0])) QApplication.clipboard().clear() QApplication.clipboard().setMimeData(q)
class SnapWindow(QMainWindow): def __init__(self, app, flags): self.flag_snapped = False # Picture snapped or loaded at start. self.flag_frame = True # Window frame toggling. self.app = app self.winsize = None # None-Conditional toggle on resize. # Also the size the window should be. QMainWindow.__init__(self) self.dragicon = QIcon.fromTheme("folder-new").pixmap(QSize(24,24)) self.imgdata = None self.setWindowTitle("Snap") self.cliplabel = QtWidgets.QLabel(self) self.cliplabel.show() self.cliplabel.setScaledContents(True) self.clip_pix = self.cliplabel.pixmap() self.clipboard = app.clipboard() self.clipboard.dataChanged.connect(self.clipboardChanged) self.clipboard.clear(mode=self.clipboard.Clipboard) p = self.palette() p.setColor(self.backgroundRole(), QtCore.Qt.white) self.setPalette(p) self.hide() def load_from_image(self): self.flag_snapped = True im = QtGui.QImage() im.load(shared['inputfp']) pm = QtGui.QPixmap().fromImage(im) self.original_snap = pm.copy() self.original_size = pm.width(), pm.height() mpos = shared['curpos'] self.setGeometry(mpos.x(),mpos.y(), pm.width(), pm.height()) self.cliplabel.resize(pm.width(),pm.height()) self.cliplabel.setPixmap(pm) self.show() def clipboardChanged(self): if self.flag_snapped == False: self.clipboard = self.app.clipboard() pm = self.clipboard.pixmap() self.original_snap = pm.copy() self.original_size = pm.width(), pm.height() if pm.isNull(): pass else: self.flag_snapped = True mpos = shared['curpos'] self.setGeometry(mpos.x()-pm.width(),mpos.y()-pm.height(), pm.width(), pm.height()) self.cliplabel.resize(pm.width(),pm.height()) self.cliplabel.setPixmap(pm) self.show() def resizeEvent(self, event): super(SnapWindow, self).resizeEvent(event) if self.winsize: self.cliplabel.resize(self.savesize.width(),self.savesize.height()) self.resize(self.savesize.width(),self.savesize.height()) self.winsize = None else: self.cliplabel.resize(self.width(),self.height()) def scale_ratio(self): winsize = QSize(self.width(),self.height()) new_size = QSize(self.original_size[0],self.original_size[1]) new_size.scale(winsize, QtCore.Qt.KeepAspectRatio) self.cliplabel.resize(new_size) self.resize(new_size) def reset_size(self): origsize = QSize(self.original_size[0],self.original_size[1]) self.cliplabel.resize(origsize) self.cliplabel.setPixmap(self.original_snap) self.resize(origsize) def mousePressEvent(self, event): super(SnapWindow, self).mousePressEvent(event) if event.button() == QtCore.Qt.MouseButton.LeftButton: self.scale_ratio() self.QDrag = QDrag(self) self.QMimeData = QMimeData() self.imgdata = QByteArray() imgbuffer = QBuffer(self.imgdata) imgbuffer.open(QIODevice.WriteOnly) self.original_snap.save(imgbuffer, "PNG") self.QMimeData.setImageData(self.imgdata) self.QMimeData.setData("image/png", self.imgdata) self.QDrag.setMimeData(self.QMimeData) self.QDrag.setPixmap(self.dragicon) dropaction = self.QDrag.exec_(Qt.CopyAction) def contextMenuEvent(self, event): menu = QtWidgets.QMenu(self) action_rename = menu.addAction("Rename") action_clipboard = menu.addAction("Copy to Clipboard (Again)") action_save = menu.addAction("Save Original") action_reset = menu.addAction("Reset to Original") action_frame = menu.addAction("Toggle Frame") action_close = menu.addAction("Close") action = menu.exec_(self.mapToGlobal(event.pos())) if action == action_save: self.save_copy() elif action == action_clipboard: self.clipboard.setPixmap(self.original_snap) elif action == action_reset: self.reset_size() elif action == action_frame: if self.flag_frame == True: self.flag_frame = False self.setWindowFlag(QtCore.Qt.FramelessWindowHint, True) self.hide() self.show() self.winsize = self.size() else: self.flag_frame = True self.setWindowFlag(QtCore.Qt.FramelessWindowHint, False) self.hide() self.show() self.winsize = self.size() self.reset_size() elif action == action_close: self.close() elif action == action_rename: name, tmp = QInputDialog.getText(self, "", "Name this window:") self.setWindowTitle(name) def save_copy(self): fd = QtWidgets.QFileDialog() fd.setDirectory(QtCore.QDir('~/')) savefn = fd.getSaveFileName(self, 'Save File')[0] pixmap = self.original_snap barray = QtCore.QByteArray() qbuffer = QtCore.QBuffer(barray) qbuffer.open(QtCore.QIODevice.WriteOnly) pixmap.save(qbuffer, "PNG") bytesio = BytesIO(barray.data()) bytesio.seek(0) with open(savefn, 'wb') as savefile: savefile.write(bytesio.read())
def copyImageFile(self, filename): clipboard = QGuiApplication.clipboard() image = QImage(filename) data = QMimeData() data.setImageData(image) clipboard.setMimeData(data)