Skip to content

Commit

Permalink
Fix custom bar style, show only available styles (#184)
Browse files Browse the repository at this point in the history
* Fix custom bar style causing a crash

Plus some ruff fixes

* Update tooltip (all shown styles should work)
  • Loading branch information
Yutsuten authored Dec 2, 2024
1 parent c3001b5 commit ac8f484
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
1 change: 0 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ lint.ignore = [
"D107", # Missing docstring in `__init__`
"ANN002", # Missing type annotation for `*args`
"ANN003", # Missing type annotation for `**kwargs`
"ANN101", # Missing type annotation for `self` in method
"ANN204", # Missing return type annotation for special method
"ANN401", # Disallow typing.Any (qt modules aren't discovered by pyright)
"UP007", # Use X | Y for type annotations (only available in python 3.10+)
Expand Down
8 changes: 4 additions & 4 deletions src/deck_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ def set_deck_conf(self, conf: dict[str, Any], *, update_life: bool) -> None:
bar_info['damageLearning'] = conf['damageLearning']

if update_life:
current_value = conf.get('currentValue', conf['maxLife'])
if current_value > conf['maxLife']:
current_value = conf['maxLife']
bar_info['currentValue'] = current_value
bar_info['currentValue'] = min(
conf.get('currentValue', conf['maxLife']),
conf['maxLife'],
)

@must_have_active_deck
def life_timer(self, bar_info: dict[str, Any]) -> None:
Expand Down
6 changes: 1 addition & 5 deletions src/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

BEHAVIORS = ['Drain life', 'Do nothing', 'Recover life']
POSITION_OPTIONS = ['Top', 'Bottom']
STYLE_OPTIONS = [
'Default', 'Cde', 'Cleanlooks', 'Fusion', 'Gtk', 'Macintosh', 'Motif',
'Plastique', 'Windows', 'Windows Vista', 'Windows XP',
]
TEXT_FORMAT = [{
'text': 'None',
}, {
Expand Down Expand Up @@ -44,7 +40,7 @@
'barBorderRadius': 0,
'barText': 0,
'barTextColor': '#000',
'barStyle': STYLE_OPTIONS.index('Default'),
'barStyle': 0,
'stopOnAnswer': False,
'stopOnLostFocus': True,
'startEmpty': False,
Expand Down
16 changes: 9 additions & 7 deletions src/progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import math
from typing import TYPE_CHECKING, Any, Literal

from .defaults import POSITION_OPTIONS, STYLE_OPTIONS, TEXT_FORMAT
from .defaults import POSITION_OPTIONS, TEXT_FORMAT

if TYPE_CHECKING:
from aqt.main import AnkiQt
Expand Down Expand Up @@ -185,20 +185,21 @@ def _update_bar_color(self) -> None:
return

self._current_bar_color = bar_color
custom_style = STYLE_OPTIONS[options['customStyle']] \
.replace(' ', '').lower()
if custom_style != 'default':
if options['customStyle']:
available_styles = ['default', *self._qt.QStyleFactory.keys()]
custom_style = available_styles[options['customStyle']]

qstyle = self._qt.QStyleFactory.create(custom_style)
self._qprogressbar.setStyle(qstyle)

palette = self._qt.QPalette()
fg_color = self._qt.QColor(bar_color)
palette.setColor(self._qt.QPalette.Highlight, fg_color)
palette.setColor(self._qt.QPalette.ColorRole.Highlight, fg_color)

if 'bgColor' in options:
bg_color = self._qt.QColor(options['bgColor'])
palette.setColor(self._qt.QPalette.Base, bg_color)
palette.setColor(self._qt.QPalette.Window, bg_color)
palette.setColor(self._qt.QPalette.ColorRole.Base, bg_color)
palette.setColor(self._qt.QPalette.ColorRole.Window, bg_color)

self._qprogressbar.setPalette(palette)

Expand All @@ -207,6 +208,7 @@ def _update_bar_color(self) -> None:
self._qprogressbar.setStyleSheet(
f'QProgressBar {{ {bar_elem} }}')
else:
# Default style
bar_elem_dict = {
'text-align': 'center',
'border-radius': f'{options["borderRadius"]}px',
Expand Down
6 changes: 3 additions & 3 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from operator import itemgetter
from typing import TYPE_CHECKING, Any, Iterator, Optional, Union

from .defaults import BEHAVIORS, DEFAULTS, POSITION_OPTIONS, STYLE_OPTIONS, TEXT_FORMAT
from .defaults import BEHAVIORS, DEFAULTS, POSITION_OPTIONS, TEXT_FORMAT
from .version import VERSION

if TYPE_CHECKING:
Expand Down Expand Up @@ -380,8 +380,8 @@ def generate_form() -> Any:
'Height of the life bar.')
tab.spin_box('borderRadiusInput', 'Border radius', [0, 20],
'Add a rounded border to the life bar.')
tab.combo_box('styleList', 'Style', STYLE_OPTIONS, '''Style of the \
life bar (not all options may work on your platform).''')
tab.combo_box('styleList', 'Style', ['Default', *aqt.QStyleFactory.keys()], '''Style of \
the life bar.''')
tab.color_select('fgColor', 'Bar color (default)',
"Color of the life bar's foreground.")
tab.spin_box('thresholdWarn', 'Warn threshold (%)', [0, 99],
Expand Down

0 comments on commit ac8f484

Please sign in to comment.