mirror of
https://github.com/ankitects/anki.git
synced 2026-01-07 02:53:54 -05:00
refactor
This commit is contained in:
parent
aefa53984e
commit
0b02be28ed
2 changed files with 18 additions and 25 deletions
|
|
@ -146,16 +146,17 @@ class FlexibleTimerLabel(QLabel):
|
|||
def __init__(self, parent=None) -> None:
|
||||
super().__init__(parent)
|
||||
self._time = 0 # current time (seconds)
|
||||
self._max_time = 0 # maximum time (seconds); 0 means hidden
|
||||
self._max_time = 0 # maximum time (seconds); 0 means unset
|
||||
self._qtimer = QTimer(self)
|
||||
self._qtimer.setInterval(1000)
|
||||
qconnect(self._qtimer.timeout, self._on_tick)
|
||||
self.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.setHidden(True)
|
||||
|
||||
def start(self, max_time: int) -> None:
|
||||
self._time = 0
|
||||
if max_time <= 0:
|
||||
raise ValueError("max time should be greater than 0")
|
||||
self._max_time = max_time
|
||||
self._time = 0
|
||||
self._update_display()
|
||||
if self._qtimer.isActive():
|
||||
self._qtimer.stop()
|
||||
|
|
@ -165,30 +166,21 @@ class FlexibleTimerLabel(QLabel):
|
|||
if self._qtimer.isActive():
|
||||
self._qtimer.stop()
|
||||
|
||||
# Internal tick handler
|
||||
def _on_tick(self) -> None:
|
||||
self._time += 1
|
||||
# clamp to max_time if set (mirrors TS: time = Math.min(maxTime, time))
|
||||
if self._time > self._max_time > 0:
|
||||
self._time = self._max_time
|
||||
self._time = min(self._time + 1, self._max_time)
|
||||
self._update_display()
|
||||
# if reached max, keep ticking but display in red (TS continues interval)
|
||||
|
||||
def _update_display(self) -> None:
|
||||
if self._max_time <= 0:
|
||||
super().setText("") # hide when max_time == 0
|
||||
self.setHidden(True)
|
||||
return
|
||||
raise ValueError("max time should be greater than 0")
|
||||
|
||||
self.setHidden(False)
|
||||
t = min(self._max_time, self._time) if self._max_time > 0 else self._time
|
||||
m = t // 60
|
||||
s = t % 60
|
||||
t = min(self._max_time, self._time)
|
||||
m, s = divmod(t, 60)
|
||||
s_str = f"{s:02d}"
|
||||
time_string = f"{m}:{s_str}"
|
||||
|
||||
if t >= self._max_time > 0:
|
||||
# display red when time == maxTime (using simple HTML)
|
||||
super().setText(f"<font color='red'>{time_string}</font>")
|
||||
self.setText(f"<font color='red'>{time_string}</font>")
|
||||
self.stop()
|
||||
else:
|
||||
super().setText(time_string)
|
||||
self.setText(time_string)
|
||||
|
|
|
|||
|
|
@ -1285,8 +1285,6 @@ class FlexibleReviewer(Reviewer):
|
|||
FlexiblePushButton(text=tr.studying_more()),
|
||||
on_clicked=partial(self.showContextMenu),
|
||||
)
|
||||
# Right side: add timer
|
||||
self.timer = self.mw.bottomWidget.right_bucket.add_widget(FlexibleTimerLabel())
|
||||
|
||||
def browse_queue(self, queue_type: Union[str, Any]) -> None:
|
||||
if queue_type == QueuedCards.LEARNING:
|
||||
|
|
@ -1376,8 +1374,12 @@ class FlexibleReviewer(Reviewer):
|
|||
self._create_middle_buttons_for_question_side()
|
||||
self._clear_bottom_web()
|
||||
|
||||
assert self.timer, "timer should exist."
|
||||
self.timer.start(max_time=self._max_time())
|
||||
# Right side: add timer
|
||||
if (max_time := self._max_time()) > 0:
|
||||
self.timer = self.mw.bottomWidget.right_bucket.add_widget(
|
||||
widget=FlexibleTimerLabel()
|
||||
)
|
||||
self.timer.start(max_time=max_time)
|
||||
|
||||
def _should_stop_timer_on_answer(self) -> bool:
|
||||
conf = self.mw.col.decks.config_dict_for_deck_id(self.card.current_deck_id())
|
||||
|
|
@ -1393,8 +1395,7 @@ class FlexibleReviewer(Reviewer):
|
|||
self._create_middle_buttons_for_answer_side()
|
||||
self._clear_bottom_web()
|
||||
|
||||
assert self.timer, "timer should exist."
|
||||
if self._should_stop_timer_on_answer():
|
||||
if self.timer and self._should_stop_timer_on_answer():
|
||||
self.timer.stop()
|
||||
|
||||
def onEnterKey(self) -> None:
|
||||
|
|
|
|||
Loading…
Reference in a new issue