Skip to content

Commit 73ca002

Browse files
authored
Add option to sharp or rounded borders (#100)
1 parent f82fe02 commit 73ca002

File tree

12 files changed

+223
-150
lines changed

12 files changed

+223
-150
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ Further information can be found in our docs:
124124

125125
- [Usage Guide](https://pyqtdarktheme.readthedocs.io/en/latest/how_to_use.html)
126126

127+
### Sharp frame
128+
129+
You can change the border corner style.
130+
131+
```python
132+
# Default is "rounded".
133+
stylesheet = qdarktheme.load_stylesheet(border="sharp")
134+
```
135+
136+
![widget_gallery_dark_theme_sharp](https://raw.githubusercontent.com/5yutan5/PyQtDarkTheme/main/images/widget_gallery_dark_sharp.png)
137+
127138
## Example
128139

129140
To check example app, run:

images/widget_gallery_dark.png

59.2 KB
Loading
991 KB
Loading

images/widget_gallery_light.png

23 KB
Loading
1000 KB
Loading

qdarktheme/main.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
"Maybe you need to install qt-binding. Available Qt-binding packages: PySide6, PyQt6, PyQt5, PySide2."
3131
)
3232

33+
# Pattern
34+
_PATTERN_RADIUS = re.compile(r"\$radius\{[\s\S]*?\}")
35+
_PATTERN_ENV_PATCH = re.compile(r"\$env_patch\{[\s\S]*?\}")
36+
3337

3438
class _SvgFileNotFoundError(FileNotFoundError):
3539

@@ -47,8 +51,23 @@ def get_themes() -> tuple[str, ...]:
4751
return THEMES
4852

4953

54+
def _replace_rounded(match: re.Match) -> str:
55+
return match.group().replace("$radius{", "").replace("}", "")
56+
57+
58+
def _replace_sharp(match: re.Match) -> str:
59+
return _PATTERN_RADIUS.sub("0", match.group())
60+
61+
62+
def _parse_radius(stylesheet: str, border: str = "rounded") -> dict[str, str]:
63+
"""Parse `$radius{...}` placeholder in template stylesheet."""
64+
matches = _PATTERN_RADIUS.finditer(stylesheet)
65+
replace = _replace_rounded if border == "rounded" else _replace_sharp
66+
return {match.group(): replace(match) for match in matches}
67+
68+
5069
def _parse_env_patch(stylesheet: str) -> dict[str, str]:
51-
"""Parse `$env_patch{...}` symbol in template stylesheet.
70+
"""Parse `$env_patch{...}` placeholder in template stylesheet.
5271
5372
Template stylesheet has `$env_patch{...}` symbol.
5473
This symbol has json string and resolve the differences of the style between qt versions.
@@ -68,7 +87,7 @@ def _parse_env_patch(stylesheet: str) -> dict[str, str]:
6887
Value is the value of the `value` key in $env_patch.
6988
"""
7089
replacements: dict[str, str] = {}
71-
for match in re.finditer(r"\$env_patch\{[\s\S]*?\}", stylesheet):
90+
for match in re.finditer(_PATTERN_ENV_PATCH, stylesheet):
7291
match_text = match.group()
7392
json_text = match_text.replace("$env_patch", "")
7493
env_property: dict[str, str] = json.loads(json_text)
@@ -104,11 +123,12 @@ def _parse_env_patch(stylesheet: str) -> dict[str, str]:
104123
return replacements
105124

106125

107-
def load_stylesheet(theme: str = "dark") -> str:
126+
def load_stylesheet(theme: str = "dark", border: str = "rounded") -> str:
108127
"""Load the style sheet which looks like flat design. There are two themes, dark theme and light theme.
109128
110129
Args:
111-
theme: The name of the theme. Available theme are "dark" and "light".
130+
theme: The name of the theme. Available themes are "dark" and "light".
131+
border: The border style. Available styles are "rounded" and "sharp".
112132
113133
Raises:
114134
TypeError: If the arg of theme name is wrong.
@@ -130,10 +150,20 @@ def load_stylesheet(theme: str = "dark") -> str:
130150
131151
app = QApplication([])
132152
app.setStyleSheet(qdarktheme.load_stylesheet("light"))
153+
154+
Change sharp frame.
155+
156+
Sharp Frame::
157+
158+
app = QApplication([])
159+
app.setStyleSheet(qdarktheme.load_stylesheet(border="sharp"))
133160
"""
134161
if theme not in get_themes():
135162
raise TypeError("The argument [theme] can only be specified as 'dark' or 'light'.") from None
136163

164+
if border not in ("rounded", "sharp"):
165+
raise TypeError("The argument [border] can only be specified as 'rounded' or 'sharp'.")
166+
137167
try:
138168
# In mac os, if the qt version is 5.13 or lower, the svg icon of Qt resource file cannot be read correctly.
139169
if compare_v(_qt_version, "<", "5.13.0"):
@@ -157,12 +187,15 @@ def load_stylesheet(theme: str = "dark") -> str:
157187
else:
158188
from qdarktheme.themes.light.stylesheet import STYLE_SHEET
159189

160-
# Create Qt version patches
161-
replacements = _parse_env_patch(STYLE_SHEET)
162-
# Replace the ${path} variable by real path value
163-
replacements["${path}"] = icon_path
164190
# Build stylesheet
165-
return multi_replace(STYLE_SHEET, replacements)
191+
# Radius
192+
replacements_radius = _parse_radius(STYLE_SHEET, border)
193+
stylesheet = multi_replace(STYLE_SHEET, replacements_radius)
194+
# Env
195+
replacements_env = _parse_env_patch(stylesheet)
196+
# Path
197+
replacements_env["${path}"] = icon_path
198+
return multi_replace(stylesheet, replacements_env)
166199

167200

168201
def load_palette(theme: str = "dark"):

0 commit comments

Comments
 (0)