提交 6e031d35 编写于 作者: 之一Yo's avatar 之一Yo

添加 `FlipView` 设置圆角的功能

上级 bcbf3051
...@@ -54,6 +54,11 @@ class Demo(QWidget): ...@@ -54,6 +54,11 @@ class Demo(QWidget):
self.flipView.addImages([str(i) for i in Path('./resource').glob('*')]) self.flipView.addImages([str(i) for i in Path('./resource').glob('*')])
self.pager.setPageNumber(self.flipView.count()) self.pager.setPageNumber(self.flipView.count())
# adjust border radius
# self.flipView.setBorderRadius(15)
# self.flipView.setFixedSize(QSize(710, 270))
# self.flipView.setSpacing(15)
self.pager.currentIndexChanged.connect(self.flipView.setCurrentIndex) self.pager.currentIndexChanged.connect(self.flipView.setCurrentIndex)
self.flipView.currentIndexChanged.connect(self.pager.setCurrentIndex) self.flipView.currentIndexChanged.connect(self.pager.setCurrentIndex)
......
...@@ -148,7 +148,7 @@ class HyperlinkButton(PushButton): ...@@ -148,7 +148,7 @@ class HyperlinkButton(PushButton):
FluentStyleSheet.BUTTON.apply(self) FluentStyleSheet.BUTTON.apply(self)
self.setCursor(Qt.PointingHandCursor) self.setCursor(Qt.PointingHandCursor)
setFont(self) setFont(self)
self.clicked.connect(lambda i: QDesktopServices.openUrl(self.getUrl())) self.clicked.connect(self._onClicked)
@__init__.register @__init__.register
def _(self, url: str, text: str, parent: QWidget = None, icon: Union[QIcon, FluentIconBase, str] = None): def _(self, url: str, text: str, parent: QWidget = None, icon: Union[QIcon, FluentIconBase, str] = None):
...@@ -163,6 +163,10 @@ class HyperlinkButton(PushButton): ...@@ -163,6 +163,10 @@ class HyperlinkButton(PushButton):
def setUrl(self, url: Union[str, QUrl]): def setUrl(self, url: Union[str, QUrl]):
self._url = QUrl(url) self._url = QUrl(url)
def _onClicked(self):
if self.getUrl().isValid():
QDesktopServices.openUrl(self.getUrl())
def _drawIcon(self, icon, painter, rect, state=QIcon.Off): def _drawIcon(self, icon, painter, rect, state=QIcon.Off):
if isinstance(icon, FluentIconBase) and self.isEnabled(): if isinstance(icon, FluentIconBase) and self.isEnabled():
icon = icon.icon(color=themeColor()) icon = icon.icon(color=themeColor())
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
from typing import List, Union from typing import List, Union
from PyQt5.QtCore import Qt, pyqtSignal, QModelIndex, QSize, pyqtProperty, QRectF, QPropertyAnimation from PyQt5.QtCore import Qt, pyqtSignal, QModelIndex, QSize, pyqtProperty, QRectF, QPropertyAnimation
from PyQt5.QtGui import QPixmap, QPainter, QColor, QImage, QWheelEvent from PyQt5.QtGui import QPixmap, QPainter, QColor, QImage, QWheelEvent, QPainterPath
from PyQt5.QtWidgets import QStyleOptionViewItem, QWidget, QListWidget, QStyledItemDelegate, QListWidgetItem from PyQt5.QtWidgets import QStyleOptionViewItem, QListWidget, QStyledItemDelegate, QListWidgetItem
from ...common.overload import singledispatchmethod from ...common.overload import singledispatchmethod
from ...common.style_sheet import isDarkTheme, FluentStyleSheet from ...common.style_sheet import isDarkTheme, FluentStyleSheet
...@@ -77,10 +77,15 @@ class FlipImageDelegate(QStyledItemDelegate): ...@@ -77,10 +77,15 @@ class FlipImageDelegate(QStyledItemDelegate):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.borderRadius = 0
def itemSize(self): def itemSize(self):
return self.parent().itemSize return self.parent().itemSize
def setBorderRadius(self, radius: int):
self.borderRadius = radius
self.parent().viewport().update()
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex): def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
painter.save() painter.save()
painter.setRenderHints(QPainter.Antialiasing) painter.setRenderHints(QPainter.Antialiasing)
...@@ -94,12 +99,17 @@ class FlipImageDelegate(QStyledItemDelegate): ...@@ -94,12 +99,17 @@ class FlipImageDelegate(QStyledItemDelegate):
if image is None: if image is None:
return painter.restore() return painter.restore()
image = image.scaled(size * r, Qt.KeepAspectRatio, Qt.SmoothTransformation)
x = option.rect.x() + int((option.rect.width() - size.width()) / 2) x = option.rect.x() + int((option.rect.width() - size.width()) / 2)
y = option.rect.y() + int((option.rect.height() - size.height()) / 2) y = option.rect.y() + int((option.rect.height() - size.height()) / 2)
painter.drawImage(QRectF(x, y, size.width(), size.height()), image) rect = QRectF(x, y, size.width(), size.height())
# clipped path
path = QPainterPath()
path.addRoundedRect(rect, self.borderRadius, self.borderRadius)
image = image.scaled(size * r, Qt.KeepAspectRatio, Qt.SmoothTransformation)
painter.setClipPath(path)
painter.drawImage(rect, image)
painter.restore() painter.restore()
...@@ -132,7 +142,6 @@ class FlipView(QListWidget): ...@@ -132,7 +142,6 @@ class FlipView(QListWidget):
self.scrollBar.setForceHidden(True) self.scrollBar.setForceHidden(True)
self.setUniformItemSizes(True) self.setUniformItemSizes(True)
self.setGridSize(self.itemSize)
self.setFixedSize(self.itemSize) self.setFixedSize(self.itemSize)
self.setItemDelegate(self.delegate) self.setItemDelegate(self.delegate)
self.setMovement(QListWidget.Static) self.setMovement(QListWidget.Static)
...@@ -167,7 +176,6 @@ class FlipView(QListWidget): ...@@ -167,7 +176,6 @@ class FlipView(QListWidget):
return return
self._itemSize = size self._itemSize = size
self.setGridSize(size)
for i in range(self.count()): for i in range(self.count()):
item = self.item(i) item = self.item(i)
...@@ -179,6 +187,13 @@ class FlipView(QListWidget): ...@@ -179,6 +187,13 @@ class FlipView(QListWidget):
""" get the size of item """ """ get the size of item """
return self._itemSize return self._itemSize
def setBorderRadius(self, radius: int):
""" set the border radius of item """
self.delegate.setBorderRadius(radius)
def getBorderRadius(self):
return self.delegate.borderRadius
def scrollPrevious(self): def scrollPrevious(self):
""" scroll to previous item """ """ scroll to previous item """
self.setCurrentIndex(self.currentIndex() - 1) self.setCurrentIndex(self.currentIndex() - 1)
...@@ -315,6 +330,7 @@ class FlipView(QListWidget): ...@@ -315,6 +330,7 @@ class FlipView(QListWidget):
self.scrollPrevious() self.scrollPrevious()
itemSize = pyqtProperty(QSize, getItemSize, setItemSize) itemSize = pyqtProperty(QSize, getItemSize, setItemSize)
borderRadius = pyqtProperty(int, getBorderRadius, setBorderRadius)
class HorizontalFlipView(FlipView): class HorizontalFlipView(FlipView):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册