提交 1794ae68 编写于 作者: 之一Yo's avatar 之一Yo

重载构造函数,适配 QtDesigner

上级 55068da8
......@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: GPLv3, see LICENSE for more details.
"""
__version__ = "0.7.2"
__version__ = "0.7.3"
from .components import *
from .common import *
......
......@@ -155,14 +155,17 @@ RadioButton::indicator:disabled {
TransparentToolButton {
background-color: transparent;
border: none;
border-radius: 4px;
margin: 0;
}
TransparentToolButton:hover {
background-color: rgba(255, 255, 255, 9);
border: none;
}
TransparentToolButton:pressed {
background-color: rgba(255, 255, 255, 6);
border: none;
}
\ No newline at end of file
......@@ -155,14 +155,17 @@ RadioButton::indicator:disabled {
TransparentToolButton {
background-color: transparent;
border: none;
border-radius: 4px;
margin: 0;
}
TransparentToolButton:hover {
background-color: rgba(0, 0, 0, 9);
border: none;
}
TransparentToolButton:pressed {
background-color: rgba(0, 0, 0, 6);
border: none;
}
\ No newline at end of file
此差异已折叠。
# coding: utf-8
from functools import singledispatch, update_wrapper
class singledispatchmethod:
"""Single-dispatch generic method descriptor.
Supports wrapping existing descriptors and handles non-descriptor
callables as instance methods.
"""
def __init__(self, func):
if not callable(func) and not hasattr(func, "__get__"):
raise TypeError(f"{func!r} is not callable or a descriptor")
self.dispatcher = singledispatch(func)
self.func = func
def register(self, cls, method=None):
"""generic_method.register(cls, func) -> func
Registers a new implementation for the given *cls* on a *generic_method*.
"""
return self.dispatcher.register(cls, func=method)
def __get__(self, obj, cls=None):
def _method(*args, **kwargs):
if args:
method = self.dispatcher.dispatch(args[0].__class__)
else:
method = self.func
for v in kwargs.values():
if v.__class__ in self.dispatcher.registry:
method = self.dispatcher.dispatch(v.__class__)
if method is not self.func:
break
return method.__get__(obj, cls)(*args, **kwargs)
_method.__isabstractmethod__ = self.__isabstractmethod__
_method.register = self.register
update_wrapper(_method, self.func)
return _method
@property
def __isabstractmethod__(self):
return getattr(self.func, '__isabstractmethod__', False)
......@@ -372,6 +372,16 @@ class PickerBase(QPushButton):
pass
class PickerToolButton(TransparentToolButton):
""" Picker tool button """
def _drawIcon(self, icon, painter, rect):
if self.isPressed:
painter.setOpacity(1)
super()._drawIcon(icon, painter, rect)
class PickerPanel(QWidget):
""" picker panel """
......@@ -386,8 +396,8 @@ class PickerPanel(QWidget):
self.view = QFrame(self)
self.itemMaskWidget = ItemMaskWidget(self.listWidgets, self)
self.hSeparatorWidget = SeparatorWidget(Qt.Horizontal, self.view)
self.yesButton = TransparentToolButton(FluentIcon.ACCEPT, self.view)
self.cancelButton = TransparentToolButton(FluentIcon.CLOSE, self.view)
self.yesButton = PickerToolButton(FluentIcon.ACCEPT, self.view)
self.cancelButton = PickerToolButton(FluentIcon.CLOSE, self.view)
self.hBoxLayout = QHBoxLayout(self)
self.listLayout = QHBoxLayout()
......
......@@ -3,22 +3,29 @@ from typing import Union
from PyQt5.QtCore import QUrl, Qt, QRectF, QSize
from PyQt5.QtGui import QDesktopServices, QIcon, QPainter
from PyQt5.QtWidgets import QPushButton, QRadioButton, QToolButton, QApplication
from PyQt5.QtWidgets import QPushButton, QRadioButton, QToolButton, QApplication, QWidget
from ...common.icon import FluentIconBase, drawIcon, isDarkTheme, Theme
from ...common.style_sheet import FluentStyleSheet
from ...common.overload import singledispatchmethod
class PushButton(QPushButton):
""" push button """
def __init__(self, text: str, parent=None, icon: Union[QIcon, str, FluentIconBase] = None):
super().__init__(text=text, parent=parent)
@singledispatchmethod
def __init__(self, parent: QWidget = None):
super().__init__(parent)
FluentStyleSheet.BUTTON.apply(self)
self._icon = icon
self.isPressed = False
self.setProperty('hasIcon', icon is not None)
self.setIconSize(QSize(16, 16))
self.setIcon(None)
@__init__.register
def _(self, text: str, parent: QWidget = None, icon: Union[QIcon, str, FluentIconBase] = None):
self.__init__(parent=parent)
self.setText(text)
self.setIcon(icon)
def setIcon(self, icon: Union[QIcon, str, FluentIconBase]):
self.setProperty('hasIcon', icon is not None)
......@@ -86,30 +93,59 @@ class PrimaryPushButton(PushButton):
class HyperlinkButton(QPushButton):
""" Hyperlink button """
def __init__(self, url: str, text: str, parent=None):
super().__init__(text, parent)
self.url = QUrl(url)
self.clicked.connect(lambda i: QDesktopServices.openUrl(self.url))
@singledispatchmethod
def __init__(self, parent: QWidget = None):
super().__init__(parent)
self.url = QUrl()
FluentStyleSheet.BUTTON.apply(self)
self.setCursor(Qt.PointingHandCursor)
self.clicked.connect(lambda i: QDesktopServices.openUrl(self.url))
@__init__.register
def _(self, url: str, text: str, parent: QWidget = None):
self.__init__(parent)
self.setText(text)
self.url.setUrl(url)
class RadioButton(QRadioButton):
""" Radio button """
def __init__(self, text: str, parent=None):
super().__init__(text, parent)
@singledispatchmethod
def __init__(self, parent: QWidget = None):
super().__init__(parent)
FluentStyleSheet.BUTTON.apply(self)
@__init__.register
def _(self, text: str, parent: QWidget = None):
self.__init__(parent)
self.setText(text)
class ToolButton(QToolButton):
""" Tool button """
def __init__(self, icon: Union[QIcon, str, FluentIconBase], parent=None):
@singledispatchmethod
def __init__(self, parent: QWidget = None):
super().__init__(parent)
self._icon = icon
self.isPressed = False
FluentStyleSheet.BUTTON.apply(self)
self.isPressed = False
self.setIcon(QIcon())
@__init__.register
def _(self, icon: FluentIconBase, parent: QWidget = None):
self.__init__(parent)
self.setIcon(icon)
@__init__.register
def _(self, icon: QIcon, parent: QWidget = None):
self.__init__(parent)
self.setIcon(icon)
@__init__.register
def _(self, icon: str, parent: QWidget = None):
self.__init__(parent)
self.setIcon(icon)
def setIcon(self, icon: Union[QIcon, str, FluentIconBase]):
self._icon = icon
......@@ -151,26 +187,9 @@ class ToolButton(QToolButton):
w, h = self.iconSize().width(), self.iconSize().height()
y = (self.height() - h) / 2
x = (self.width() - w) / 2
x = (self.width() - w) / 2
self._drawIcon(self._icon, painter, QRectF(x, y, w, h))
class TransparentToolButton(QToolButton):
class TransparentToolButton(ToolButton):
""" Transparent background tool button """
def __init__(self, icon: Union[QIcon, str, FluentIconBase], parent=None):
super().__init__(parent)
self._icon = icon
self.setCursor(Qt.PointingHandCursor)
FluentStyleSheet.BUTTON.apply(self)
def paintEvent(self, e):
super().paintEvent(e)
painter = QPainter(self)
painter.setRenderHints(QPainter.Antialiasing |
QPainter.SmoothPixmapTransform)
iw, ih = self.iconSize().width(), self.iconSize().height()
w, h = self.width(), self.height()
rect = QRectF((w - iw)/2, (h - ih)/2, iw, ih)
drawIcon(self._icon, painter, rect)
\ No newline at end of file
# coding: utf-8
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtWidgets import QCheckBox, QWidget
from ...common.style_sheet import FluentStyleSheet
from ...common.overload import singledispatchmethod
class CheckBox(QCheckBox):
""" Check box """
def __init__(self, text: str, parent=None):
@singledispatchmethod
def __init__(self, parent: QWidget = None):
super().__init__(parent)
FluentStyleSheet.CHECK_BOX.apply(self)
@__init__.register
def _(self, text: str, parent: QWidget = None):
super().__init__(text, parent)
FluentStyleSheet.CHECK_BOX.apply(self)
......@@ -5,14 +5,31 @@ from PyQt5.QtGui import QIcon, QPainter
from PyQt5.QtWidgets import QWidget
from ...common.icon import FluentIconBase, drawIcon
from ...common.overload import singledispatchmethod
class IconWidget(QWidget):
""" Icon widget """
def __init__(self, icon: Union[str, QIcon, FluentIconBase], parent=None):
super().__init__(parent=parent)
self.icon = icon
@singledispatchmethod
def __init__(self, parent=None):
super().__init__(parent)
self.setIcon(QIcon())
@__init__.register
def _(self, icon: FluentIconBase, parent: QWidget = None):
self.__init__(parent)
self.setIcon(icon)
@__init__.register
def _(self, icon: QIcon, parent: QWidget = None):
self.__init__(parent)
self.setIcon(icon)
@__init__.register
def _(self, icon: str, parent: QWidget = None):
self.__init__(parent)
self.setIcon(icon)
def setIcon(self, icon: Union[str, QIcon, FluentIconBase]):
self.icon = icon
......
......@@ -17,12 +17,21 @@ class LineEditButton(QToolButton):
def __init__(self, icon: Union[str, QIcon, FluentIconBase], parent=None):
super().__init__(parent=parent)
self._icon = icon
self.isPressed = False
self.setFixedSize(31, 23)
self.setIconSize(QSize(10, 10))
self.setCursor(Qt.PointingHandCursor)
self.setObjectName('lineEditButton')
FluentStyleSheet.LINE_EDIT.apply(self)
def mousePressEvent(self, e):
self.isPressed = True
super().mousePressEvent(e)
def mouseReleaseEvent(self, e):
self.isPressed = False
super().mouseReleaseEvent(e)
def paintEvent(self, e):
super().paintEvent(e)
painter = QPainter(self)
......@@ -33,6 +42,9 @@ class LineEditButton(QToolButton):
w, h = self.width(), self.height()
rect = QRectF((w - iw)/2, (h - ih)/2, iw, ih)
if self.isPressed:
painter.setOpacity(0.7)
if isDarkTheme():
drawIcon(self._icon, painter, rect)
else:
......
......@@ -5,6 +5,7 @@ from PyQt5.QtWidgets import (QProxyStyle, QSlider, QStyle, QStyleOptionSlider,
QWidget)
from ...common.style_sheet import FluentStyleSheet
from ...common.overload import singledispatchmethod
class Slider(QSlider):
......@@ -12,7 +13,13 @@ class Slider(QSlider):
clicked = pyqtSignal(int)
def __init__(self, orientation, parent=None):
@singledispatchmethod
def __init__(self, parent: QWidget = None):
super().__init__(parent)
FluentStyleSheet.SLIDER.apply(self)
@__init__.register
def _(self, orientation: Qt.Orientation, parent: QWidget = None):
super().__init__(orientation, parent=parent)
FluentStyleSheet.SLIDER.apply(self)
......
......@@ -31,17 +31,29 @@ class SpinButton(QToolButton):
def __init__(self, icon: SpinIcon, parent=None):
super().__init__(parent=parent)
self.isPressed = False
self._icon = icon
self.setFixedSize(31, 23)
self.setIconSize(QSize(10, 10))
FluentStyleSheet.SPIN_BOX.apply(self)
def mousePressEvent(self, e):
self.isPressed = True
super().mousePressEvent(e)
def mouseReleaseEvent(self, e):
self.isPressed = False
super().mouseReleaseEvent(e)
def paintEvent(self, e):
super().paintEvent(e)
painter = QPainter(self)
painter.setRenderHints(QPainter.Antialiasing |
QPainter.SmoothPixmapTransform)
if self.isPressed:
painter.setOpacity(0.7)
self._icon.render(painter, QRectF(10, 9, 11, 11))
......
......@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup(
name="PyQt-Fluent-Widgets",
version="0.7.2",
version="0.7.3",
keywords="pyqt fluent widgets",
author="zhiyiYo",
author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册