2022-02-25 20:03:36 +00:00
|
|
|
"""Module that implements a button cycling functionality"""
|
2024-03-22 07:19:13 +00:00
|
|
|
|
2022-02-25 20:03:36 +00:00
|
|
|
from abc import abstractmethod
|
|
|
|
from menu.timer import Timer
|
2022-12-05 17:58:23 +00:00
|
|
|
from piscsi.file_cmds import FileCmds
|
2022-02-25 20:03:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Cycler:
|
|
|
|
"""Class implementing button cycling functionality. Message is shown at the center of
|
|
|
|
the screen where repeated button presses cycle through the available selection
|
|
|
|
possibilities. Inactivity (cycle_timeout) actives cycle entry last shown on the screen."""
|
2022-11-30 05:19:17 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
menu_controller,
|
|
|
|
sock_cmd,
|
2022-12-05 17:58:23 +00:00
|
|
|
piscsi_cmd,
|
2022-11-30 05:19:17 +00:00
|
|
|
cycle_timeout=3,
|
|
|
|
return_string="Return ->",
|
|
|
|
return_entry=True,
|
|
|
|
empty_messages=True,
|
|
|
|
):
|
2022-02-25 20:03:36 +00:00
|
|
|
self._cycle_profile_timer_flag = Timer(activation_delay=cycle_timeout)
|
|
|
|
self._menu_controller = menu_controller
|
|
|
|
self.sock_cmd = sock_cmd
|
2022-12-05 17:58:23 +00:00
|
|
|
self.piscsi_cmd = piscsi_cmd
|
2023-12-11 06:56:35 +00:00
|
|
|
self.file_cmd = FileCmds(piscsi=self.piscsi_cmd)
|
2022-02-25 20:03:36 +00:00
|
|
|
self.cycle_entries = self.populate_cycle_entries()
|
|
|
|
self.return_string = return_string
|
|
|
|
self.return_entry = return_entry
|
|
|
|
self.empty_messages = empty_messages
|
|
|
|
if self.return_entry is True:
|
|
|
|
self.cycle_entries.insert(0, self.return_string)
|
|
|
|
self.selected_config_file_index = 0
|
|
|
|
self.message = str(self.cycle_entries[self.selected_config_file_index])
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def populate_cycle_entries(self):
|
|
|
|
"""Returns a list of entries to cycle"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def perform_selected_entry_action(self, selected_entry):
|
|
|
|
"""Performs an action on the selected cycle menu entry"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def perform_return_action(self):
|
|
|
|
"""Perform the return action, i.e., when no selection is chosen"""
|
|
|
|
|
|
|
|
def update(self):
|
2022-11-30 05:19:17 +00:00
|
|
|
"""Returns True if object has completed its task and can be deleted"""
|
2022-02-25 20:03:36 +00:00
|
|
|
|
|
|
|
if self._cycle_profile_timer_flag is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
self._cycle_profile_timer_flag.check_timer()
|
|
|
|
if self.message is not None:
|
|
|
|
self._menu_controller.show_timed_mini_message(self.message)
|
|
|
|
self._menu_controller.get_menu_renderer().render()
|
|
|
|
|
|
|
|
if self._cycle_profile_timer_flag.enabled is False: # timer is running
|
|
|
|
return None
|
|
|
|
|
|
|
|
selected_cycle_entry = str(self.cycle_entries[self.selected_config_file_index])
|
|
|
|
|
|
|
|
if self.return_entry is True:
|
|
|
|
if selected_cycle_entry != self.return_string:
|
|
|
|
if self.empty_messages is True:
|
|
|
|
self._menu_controller.show_timed_mini_message("")
|
|
|
|
self._menu_controller.show_timed_message("")
|
|
|
|
return self.perform_selected_entry_action(selected_cycle_entry)
|
|
|
|
|
|
|
|
self._menu_controller.show_timed_mini_message("")
|
|
|
|
self._menu_controller.show_timed_message("")
|
|
|
|
return self.perform_return_action()
|
|
|
|
|
|
|
|
return self.perform_selected_entry_action(selected_cycle_entry)
|
|
|
|
|
|
|
|
def cycle(self):
|
|
|
|
"""Cycles between entries in the cycle menu"""
|
|
|
|
if self._cycle_profile_timer_flag is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.selected_config_file_index += 1
|
|
|
|
|
|
|
|
if self.selected_config_file_index > len(self.cycle_entries) - 1:
|
|
|
|
self.selected_config_file_index = 0
|
|
|
|
|
|
|
|
self.message = str(self.cycle_entries[self.selected_config_file_index])
|
|
|
|
|
|
|
|
self._cycle_profile_timer_flag.reset_timer()
|