def __initCursors(self): """ Private method to initialize the various cursors. """ self.__normalCursor = QCursor(Qt.ArrowCursor) pix = QPixmap(":colorpicker-cursor.xpm") mask = pix.createHeuristicMask() pix.setMask(mask) self.__colorPickerCursor = QCursor(pix, 1, 21) pix = QPixmap(":paintbrush-cursor.xpm") mask = pix.createHeuristicMask() pix.setMask(mask) self.__paintCursor = QCursor(pix, 0, 19) pix = QPixmap(":fill-cursor.xpm") mask = pix.createHeuristicMask() pix.setMask(mask) self.__fillCursor = QCursor(pix, 3, 20) pix = QPixmap(":aim-cursor.xpm") mask = pix.createHeuristicMask() pix.setMask(mask) self.__aimCursor = QCursor(pix, 10, 10) pix = QPixmap(":eraser-cursor.xpm") mask = pix.createHeuristicMask() pix.setMask(mask) self.__rubberCursor = QCursor(pix, 1, 16)
def mouseMoveEvent(self, event): if QLineF(QPointF(event.screenPos()), QPointF(event.buttonDownScreenPos(Qt.LeftButton))).length() < QApplication.startDragDistance(): return drag = QDrag(event.widget()) mime = QMimeData() drag.setMimeData(mime) ColorItem.n += 1 if ColorItem.n > 2 and qrand() % 3 == 0: image = QImage(':/images/head.png') mime.setImageData(image) drag.setPixmap(QPixmap.fromImage(image).scaled(30,40)) drag.setHotSpot(QPoint(15, 30)) else: mime.setColorData(self.color) mime.setText("#%02x%02x%02x" % (self.color.red(), self.color.green(), self.color.blue())) pixmap = QPixmap(34, 34) pixmap.fill(Qt.white) painter = QPainter(pixmap) painter.translate(15, 15) painter.setRenderHint(QPainter.Antialiasing) self.paint(painter, None, None) painter.end() pixmap.setMask(pixmap.createHeuristicMask()) drag.setPixmap(pixmap) drag.setHotSpot(QPoint(15, 20)) drag.exec_() self.setCursor(Qt.OpenHandCursor)
async def loadImg(self, src, name): try: async with aiohttp.ClientSession() as session: async with session.get(src, headers=headers, timeout=60) as response: if response.status == 200: image_content = await response.read() else: raise aiohttp.ClientError() except Exception as e: print(e) return width = self.width height = self.height pic = QPixmap() pic.loadFromData(image_content) localSrc = cacheFolder + '/' + name pic.save(localSrc, 'jpg') pic = pic.scaled(width, height) self.src = localSrc # 上遮罩。 if self.pixMask: mask = QPixmap() mask.load(self.pixMask) mask = mask.scaled(width, height) pic.setMask(mask.createHeuristicMask()) self.setPixmap(pic)
def setSrc(self, src): src = str(src) if 'http' in src or 'https' in src: cacheList = os.listdir(cacheFolder) name = makeMd5(src) localSrc = cacheFolder + '/' + name if name in cacheList: self.setSrc(localSrc) self.src = localSrc return self.loadImg(src, name) else: self.src = src pix = QPixmap(src) pix.load(src) pix = pix.scaled(self.width, self.height) # mask需要与pix是相同大小。 if self.pixMask: mask = QPixmap(self.pixMask) mask = mask.scaled(self.width, self.height) pix.setMask(mask.createHeuristicMask()) self.setPixmap(pix)
def __load_cursors(cls): cursors = { (DrawingMode.NoDraw, DrawingShape.NoShape): QCursor(Qt.ArrowCursor) } for k in cls.key_to_drawstate: px = QPixmap(cursors_xpm[k]) mask = px.createHeuristicMask() px.setMask(mask) cursor = QCursor(px, 0, 0) # hot point is always top left (mode, shape) = (cls.key_to_drawstate[k]) cursors[(mode, shape)] = cursor return cursors
def mouseMoveEvent(self, event): if ( QLineF( QPointF(event.screenPos()), QPointF(event.buttonDownScreenPos(Qt.LeftButton)), ).length() < QApplication.startDragDistance() ): return drag = QDrag(event.widget()) mime = QMimeData() drag.setMimeData(mime) ColorItem.n += 1 if ColorItem.n > 2 and qrand() % 3 == 0: root = QFileInfo(__file__).absolutePath() image = QImage(root + "/images/head.png") mime.setImageData(image) drag.setPixmap(QPixmap.fromImage(image).scaled(30, 40)) drag.setHotSpot(QPoint(15, 30)) else: mime.setColorData(self.color) mime.setText( "#%02x%02x%02x" % (self.color.red(), self.color.green(), self.color.blue()) ) pixmap = QPixmap(34, 34) pixmap.fill(Qt.white) painter = QPainter(pixmap) painter.translate(15, 15) painter.setRenderHint(QPainter.Antialiasing) self.paint(painter, None, None) painter.end() pixmap.setMask(pixmap.createHeuristicMask()) drag.setPixmap(pixmap) drag.setHotSpot(QPoint(15, 20)) drag.exec_() self.setCursor(Qt.OpenHandCursor)
def main(): app = QApplication(sys.argv) filename = os.path.join(os.path.dirname(__file__), "assets.db") create = not QFile.exists(filename) db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(filename) if not db.open(): QMessageBox.warning( None, "Asset Manager", ("Database Error: {0}".format(db.lastError().text()))) sys.exit(1) splash = None if create: app.setOverrideCursor(QCursor(Qt.WaitCursor)) splash = QLabel() pixmap = QPixmap(":/assetmanagersplash.png") splash.setPixmap(pixmap) splash.setMask(pixmap.createHeuristicMask()) splash.setWindowFlags(Qt.SplashScreen) rect = app.desktop().availableGeometry() splash.move((rect.width() - pixmap.width()) / 2, (rect.height() - pixmap.height()) / 2) splash.show() app.processEvents() createFakeData() form = MainForm() form.show() if create: splash.close() app.processEvents() app.restoreOverrideCursor() app.exec_() del form del db
class KEyesWidget(QWidget): update_interval = 50 # ms faces = { "Aaron": ("keyes-aaron.png", (49, 63, 12, 8), (79, 63, 12, 8)), "Adrian": ("keyes-adrian.png", (46, 67, 11, 6), (74, 68, 11, 6)), "Cornelius": ("keyes-cornelius.png", (49, 68, 11, 6), (79, 68, 11, 6)), "Eva": ("keyes-eva.png", (51, 63, 12, 6), (83, 63, 12, 6)), "Sebastian": ("keyes-sebastian.png", (50, 58, 14, 7), (83, 58, 14, 7)), } def __init__(self): QLabel.__init__(self) self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint) self.setAttribute(Qt.WA_NoSystemBackground) self.setMouseTracking(True) self.dragPosition = QPoint(0, 0) self.mousePosition = QCursor.pos() self.actionFaces = QActionGroup(self) allFaceActions = [] for name in sorted(self.faces): action = QAction(name, self.actionFaces) action.setCheckable(True) allFaceActions.append(action) self.actionFaces.triggered.connect(self.actionUpdateFace) startAction = random.choice(allFaceActions) startAction.setChecked(True) self.actionUpdateFace(startAction) self.actionQuit = QAction("Quit", self) self.actionQuit.triggered.connect(QApplication.instance().quit) self.timer = QTimer() self.timer.timeout.connect(self.updateFromMousePosition) self.timer.start(self.update_interval) self.painter = None def actionUpdateFace(self, action): self.setFace(action.text()) def setFace(self, name): self.setWindowTitle(name) self.pixmap = QPixmap(normalize_path(self.faces[name][0])) self.setWindowIcon(QIcon(self.pixmap)) self.eyes = [Eye(*self.faces[name][1]), Eye(*self.faces[name][2])] self.setMask(self.pixmap.createHeuristicMask()) if self.isVisible(): self.update() def updateFromMousePosition(self): newPosition = QCursor.pos() if newPosition == self.mousePosition: return self.mousePosition = newPosition if self.isVisible(): self.update() def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.dragPosition = event.globalPos() - self.frameGeometry().topLeft() event.accept() def mouseMoveEvent(self, event): if event.buttons() == Qt.LeftButton: self.move(event.globalPos() - self.dragPosition) event.accept() def contextMenuEvent(self, event): menu = QMenu(self) menu.addActions(self.actionFaces.actions()) menu.addSeparator() menu.addAction(self.actionQuit) menu.exec_(event.globalPos()) def paintEvent(self, event): painter = QPainter(self) self.painter = painter painter.drawPixmap(QPoint(0, 0), self.pixmap) for eye in self.eyes: eye.render(self) self.painter = None def sizeHint(self): return self.pixmap.size()
def drawPixmap(self): pixmap = QPixmap(self.diameter, self.diameter) pixmap.fill(Qt.white) self.paintOnPixmap(pixmap) pixmap.setMask(pixmap.createHeuristicMask()) return pixmap