Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: impl the code outline draft #159

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "A powerful and artistic UI library based on PyQt5 / PySide6"
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.8"
dependencies = ["PyQt5>=5.15.10"]
dependencies = ["PyQt5>=5.15.10", "typing-extensions>=4.12.2"]

[project.urls]
Repository = "https://github.com/ChinaIceF/PyQt-SiliconUI"
Expand Down
86 changes: 86 additions & 0 deletions siui/components/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# NOTE This is the refactor of button component. It's working in progress. It will
# replace button once it's done. Now it's draft, code may be ugly and verbose temporarily.

from PyQt5.QtCore import QRect, QRectF, Qt
from PyQt5.QtGui import QColor, QIcon, QPainter, QPainterPath, QPaintEvent
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget


class SiPushButton(QPushButton):
def __init__(self, parent: QWidget | None = None) -> None:
super().__init__(parent)

@classmethod
def withText(cls, text: str, parent: QWidget | None = None) -> "SiPushButton":
cls = cls(parent)
cls.setText(text)
return cls

@classmethod
def withIcon(cls, icon: QIcon, parent: QWidget | None = None) -> "SiPushButton":
cls = cls(parent)
cls.setIcon(icon)
return cls

def withTextAndIcon(cls, text: str, icon: str, parent: QWidget | None = None) -> "SiPushButton":
cls = cls(parent)
cls.setText(text)
cls.setIcon(icon)
return cls

@property
def bottomBorderHeight(self) -> int:
return round(self.rect().height() * 0.125)

@staticmethod
def _drawBackgroundPath(rect: QRect) -> QPainterPath:
path = QPainterPath()
path.addRoundedRect(QRectF(0, 0, rect.width(), rect.height()), 40, 40)
return path

def _drawBackgroundRect(self, painter: QPainter, rect: QRect) -> None:
painter.setBrush(QColor("#2D2932"))
painter.drawPath(self._drawBackgroundPath(rect))

def _drawButtonPath(self, rect: QRect) -> QPainterPath:
path = QPainterPath()
path.addRoundedRect(QRectF(0, 0, rect.width(), rect.height() - self.bottomBorderHeight), 40, 40)
return path

def _drawBbuttonRect(self, painter: QPainter, rect: QRect) -> None:
painter.setBrush(QColor("#4C4554"))
painter.drawPath(self._drawButtonPath(rect))

def enterEvent(self, event) -> None:
super().enterEvent(event)

def leaveEvent(self, event) -> None:
super().leaveEvent(event)

def resizeEvent(self, event) -> None:
super().resizeEvent(event)

def paintEvent(self, event: QPaintEvent) -> None:
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
painter.setPen(Qt.PenStyle.NoPen)
rect = self.rect()
self._drawBackgroundRect(painter, rect)
self._drawBbuttonRect(painter, rect)


class Window(QWidget):
def __init__(self) -> None:
super().__init__()
self.resize(600, 800)
self.btn = SiPushButton(self)
self.btn.setFixedSize(2400, 320)
self.main_layout = QVBoxLayout(self)
self.main_layout.addWidget(self.btn, alignment=Qt.AlignmentFlag.AlignCenter)


if __name__ == "__main__":
app = QApplication([])
window = Window()
window.show()
app.exec()
10 changes: 10 additions & 0 deletions siui/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
## This module defines global shared types

Use Python's Type Hint syntax, reference:
- [`typing`](https://docs.python.org/3/library/typing.html)
- [`PEP 484`](https://www.python.org/dev/peps/pep-0484/)
- [`PEP 526`](https://www.python.org/dev/peps/pep-0526/)
"""

from typing_extensions import TypeAlias
Loading