def _create_pixmap(self, path, color): """ Internal function that creates a new item pixmap from the given path :param path: str :param color: str or QColor :return: QPixmap """ if not path: return QPixmap() dpi = self.treeWidget().dpi() key = path + color + 'DPI-' + str(dpi) item_pixmap = self._PIXMAP_CACHE.get(key) if not item_pixmap: width = 20 * dpi height = 18 * dpi if '/' not in path and '\\' not in path: path = resources.get('icons', path) if not path or not os.path.exists(path): path = self.default_icon_path() pixmap2 = pixmap.Pixmap(path) pixmap2.set_color(color) pixmap2 = pixmap2.scaled(16 * dpi, 16 * dpi, Qt.KeepAspectRatio, Qt.SmoothTransformation) x = (width - pixmap2.width()) / 2 y = (height - pixmap2.height()) / 2 item_pixmap = QPixmap(QSize(width, height)) item_pixmap.fill(Qt.transparent) painter = QPainter(item_pixmap) painter.drawPixmap(x, y, pixmap2) painter.end() self._PIXMAP_CACHE[key] = item_pixmap return item_pixmap
def overlay_pixmap(pixmap, over_pixmap, overlay_color, align=Qt.AlignCenter): """ Overlays one pixmap over the other :param pixmap: :param over_pixmap: :param overlay_color: :param align: :return: """ if isinstance(overlay_color, str): overlay_color = color.Color.from_string(overlay_color) if overlay_color is not None: over_pixmap = colorize_pixmap(over_pixmap, overlay_color) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceOver) x = 0 y = 0 if align is Qt.AlignCenter: x = pixmap.width() / 2 - over_pixmap.width() / 2 y = pixmap.height() / 2 - over_pixmap.height() / 2 elif align is None: x = 0 y = 0 painter.drawPixmap(x, y, over_pixmap.width(), over_pixmap.height(), over_pixmap) painter.end()
def mousePressEvent(self, event): """ Override the behaviour when the mouse is pressed """ self._drag_initiated = False self._drag_dropped_pos = QPoint() if event.button() == Qt.LeftButton and event.modifiers( ) == Qt.ControlModifier: self._drag_start_pos = event.pos() # Get mouse position and store the area rectangle of the selected tab pos = event.pos() self._select_tab_index = self.tabAt(pos) rect = self.tabRect(self._select_tab_index) # Store a pixmap of the selected tab and a pixmap of the close hand cursor image # After that we draw the tab and also the hand cursor in the middle of the tab pixmap = QPixmap.grabWidget(self, rect) painter = QPainter(pixmap) cursor_pm = QPixmap(':/icons/close_hand_cursor.png') cursor_pos = QPoint(*map(lambda x, y: (x - y) * 0.5, rect.size().toTuple(), cursor_pm.size().toTuple())) painter.drawPixmap(cursor_pos, cursor_pm) painter.end() # Update cursor cursor = QCursor(pixmap) self.setCursor(cursor) super(TearOffTabBar, self).mousePressEvent(event)
def render(self, painter: QPainter, rect: QRect) -> None: for x in range(self.m_tilesRect.width() + 1): for y in range(self.m_tilesRect.height() + 1): tp = QPoint(x + self.m_tilesRect.left(), y + self.m_tilesRect.top()) box = self.tileRect(tp) if rect.intersects(box): painter.drawPixmap( box, self.m_tilePixmaps.get(QPointH(tp), self.m_emptyTile))
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.SmoothPixmapTransform) painter.translate(self._loading_pixmap.width() / 2, self._loading_pixmap.height() / 2) painter.rotate(self._rotation) painter.drawPixmap(-self._loading_pixmap.width() / 2, -self._loading_pixmap.height() / 2, self._loading_pixmap.width(), self._loading_pixmap.height(), self._loading_pixmap) painter.end() return super(CircleLoading, self).paintEvent(event)
def tint_pixmap(pixmap, tint_color=(255, 255, 255, 100), composition_mode=QPainter.CompositionMode_Plus): """ Composite one pixmap on top of another :param pixmap: :param tint_color: :param composition_mode: :return: """ tint_color = QColor(*tint_color) over_pixmap = QPixmap(pixmap.width(), pixmap.height()) over_pixmap.fill(tint_color) over_pixmap.setMask(pixmap.mask()) painter = QPainter(pixmap) painter.setCompositionMode(composition_mode) painter.drawPixmap(0, 0, over_pixmap.width(), over_pixmap.height(), over_pixmap) painter.end()
def thumbnail_icon(self): """ Returns the thumbnail icon :return: QIcon """ # custom_path = self.custom_icon_path() # if custom_path and '/' not in custom_path and '\\' not in custom_path: # custom_path = resources.icon('icons/{}'.format(self.theme().style()), custom_path) # if not custom_path or not os.path.isfile(custom_path): # return super(FolderItemView, self).thumbnail_icon() return super(FolderItemView, self).thumbnail_icon() color = self.icon_color() if not color: color = consts.DEFAULT_FOLDER_ICON_COLOR icon_key = custom_path + color icon = self._THUMBNAIL_ICON_CACHE.get(icon_key) if not icon: color1 = qt_color.Color.from_string(color) color2 = qt_color.Color.from_string('rgb(255, 255, 255, 150)') pixmap1 = qt_pixmap.Pixmap(self.THUMBNAIL_PATH) pixmap2 = qt_pixmap.Pixmap(custom_path) pixmap1.set_color(color1) pixmap2.set_color(color2) pixmap1 = pixmap1.scaled(128, 128, Qt.KeepAspectRatio, Qt.SmoothTransformation) pixmap2 = pixmap2.scaled(64, 64, Qt.KeepAspectRatio, Qt.SmoothTransformation) x = (128 - pixmap2.width()) / 2 y = (128 - pixmap2.width()) / 2 painter = QPainter(pixmap1) painter.drawPixmap(x, y + 5, pixmap2) painter.end() icon = qt_icon.Icon(pixmap1) self._THUMBNAIL_ICON_CACHE[icon_key] = icon return self._THUMBNAIL_ICON_CACHE.get(icon_key)
def _setup_general_tab(self): general_widget = QWidget() general_layout = layouts.VerticalLayout(spacing=2, margins=(2, 2, 2, 2)) general_widget.setLayout(general_layout) self._themes_combobox = combobox.BaseComboBox(parent=self) all_themes = resources.get_all_resources_of_type( resources.ResourceTypes.THEME) for i, theme in enumerate(all_themes): accent_color_hex = theme.accent_color accent_color = color.Color.hex_to_qcolor( accent_color_hex[1:] if accent_color_hex. startswith('#') else accent_color_hex) background_color_hex = theme.background_color background_color = color.Color.hex_to_qcolor( background_color_hex[1:] if accent_color_hex. startswith('#') else background_color_hex) accent_color_pixmap = QPixmap(25, 25) background_color_pixmap = QPixmap(25, 25) accent_color_pixmap.fill(accent_color) background_color_pixmap.fill(background_color) color_pixmap = QPixmap(50, 25) painter = QPainter(color_pixmap) painter.drawPixmap(0, 0, 25, 25, accent_color_pixmap) painter.drawPixmap(25, 0, 25, 25, background_color_pixmap) painter.end() color_icon = QIcon(color_pixmap) self._themes_combobox.addItem(color_icon, theme.name()) general_layout.addWidget(self._themes_combobox) general_layout.addStretch() return general_widget
def paintEvent(self, event: QPaintEvent) -> None: p = QPainter(self) self.m_normalMap.render(p, event.rect()) p.setPen(Qt.black) p.drawText( self.rect(), Qt.AlignBottom | Qt.TextWordWrap, "Map data CCBYSA 2009 OpenStreetMap.org contributors", ) p.end() if self.zoomed: dim = min(self.width(), self.height()) magnifierSize = min(MAX_MAGNIFIER, dim * 2 / 3) radius = magnifierSize / 2 ring = radius - 15 box = QSize(magnifierSize, magnifierSize) if self.maskPixmap.size() != box: self.maskPixmap = QPixmap(box) self.maskPixmap.fill(Qt.transparent) g = QRadialGradient() g.setCenter(radius, radius) g.setFocalPoint(radius, radius) g.setRadius(radius) g.setColorAt(1.0, QColor(255, 255, 255, 0)) g.setColorAt(0.5, QColor(128, 128, 128, 255)) mask = QPainter(self.maskPixmap) mask.setRenderHint(QPainter.Antialiasing) mask.setCompositionMode(QPainter.CompositionMode_Source) mask.setBrush(g) mask.setPen(Qt.NoPen) mask.drawRect(self.maskPixmap.rect()) mask.setBrush(QColor(Qt.transparent)) mask.drawEllipse(g.center(), ring, ring) mask.end() center = self.dragPos - QPoint(0, radius) center = center + QPoint(0, radius / 2) corner = center - QPoint(radius, radius) xy = center * 2 - QPoint(radius, radius) # only set the dimension to the magnified portion if self.zoomPixmap.size() != box: zoomPixmap = QPixmap(box) zoomPixmap.fill(Qt.lightGray) if True: p = QPainter(zoomPixmap) p.translate(-xy) self.m_largeMap.render(p, QRect(xy, box)) p.end() clipPath = QPainterPath() clipPath.addEllipse(center, ring, ring) p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) p.setClipPath(clipPath) p.drawPixmap(corner, zoomPixmap) p.setClipping(False) p.drawPixmap(corner, self.maskPixmap) p.setPen(Qt.gray) p.drawPath(clipPath) if self.invert: p = QPainter(self) p.setCompositionMode(QPainter.CompositionMode_Difference) p.fillRect(event.rect(), Qt.white) p.end()