Vai al contenuto

log_view

log_view

This module contains the log window of the application.

LogViewer

Bases: CTkToplevel

Window application to display the content of the log.

Source code in log_view.py
class LogViewer(ctk.CTkToplevel):
    """
    Window application to display the content of the log.
    """

    def __init__(self, parent: MainWindow, logs: Tuple = None):
        """
        Initialize the log window.
        :param parent: The parent window.
        :param logs: The log messages to display.
        """
        super().__init__(parent)
        self.geometry('640x480')
        self.update()
        geom = self.wm_geometry().split('+')[0]
        geom = geom.split('x')
        self.wm_minsize(floor(int(geom[0]) * 0.7), floor(int(geom[1]) * 0.7))
        self.title("Riepilogo log")
        self.iconbitmap(MainWindow.ICON)
        self.attributes('-topmost', 1)
        self.after(200, lambda: self.iconbitmap(MainWindow.ICON))
        self.after(250, lambda: self.attributes('-topmost', 0))
        self.after(250, self.focus_force)
        self.__gui = self.__make_layout()
        if logs is not None:
            self.update_logs(logs)

    def __make_layout(self) -> dict[str, Any]:
        """
        Create the layout of the main window.

        :return: The layout of the main window.
        """
        font = ('Arial', 15)
        text_frame = ctk.CTkFrame(self, fg_color="white", border_width=1)
        text_frame.pack(pady=10, padx=10, fill=ctk.BOTH, expand=True)

        log_text = ctk.CTkTextbox(text_frame, wrap=ctk.WORD,
                                  font=font, fg_color="white")
        log_text.pack(pady=4, padx=4, fill=ctk.BOTH, expand=True)
        log_text.configure(state=ctk.DISABLED)
        for level, color in MainWindow.COLORS.items():
            log_text.tag_config(level, foreground=color)
        gui = {'log_text': log_text}

        button_frame = ctk.CTkFrame(self, fg_color='transparent')
        button_frame.pack(pady=(10, 20))
        ok_button = ctk.CTkButton(button_frame, text="OK", font=font)
        ok_button.grid(row=0, column=0, padx=10)
        gui['ok_button'] = ok_button
        reload_button = ctk.CTkButton(button_frame, text="Reload", font=font)
        reload_button.grid(row=0, column=1, padx=10)
        gui['reload_button'] = reload_button
        return gui

    def set_ok_button_callback(self, callback: Callable) -> None:
        """
        Set the callback of the ok button.
        :param callback: The callback of the ok button.
        """
        self.__gui['ok_button'].configure(command=callback)

    def set_reload_button_callback(self, callback: Callable) -> None:
        """
        Set the callback of the reload button.
        :param callback: The callback of the reload button.
        """
        self.__gui['reload_button'].configure(command=callback)

    def set_logs_updated(self, updated: bool) -> None:
        """
        Set or reset the reload indicator in the window.
        :param updated: True if the logs are updated, False otherwise.
        """
        theme = ThemeManager.theme["CTkButton"]
        color = 'red' if updated else theme['fg_color']
        hover = '#C30300' if updated else theme['hover_color']
        self.__gui['reload_button'].configure(fg_color=color, hover_color=hover)

    def update_logs(self, logs: Tuple) -> None:
        """
        Update the log messages on the GUI.
        :param logs: The log messages to display.
        """
        log_text = self.__gui['log_text']
        log_text.configure(state=ctk.NORMAL)
        log_text.delete(1.0, ctk.END)
        for msg in reversed(logs):
            log_text.insert('end', f'{msg}\n', msg.split(' ', 1)[0])
        log_text.configure(state=ctk.DISABLED)

__init__(parent, logs=None)

Initialize the log window. :param parent: The parent window. :param logs: The log messages to display.

Source code in log_view.py
def __init__(self, parent: MainWindow, logs: Tuple = None):
    """
    Initialize the log window.
    :param parent: The parent window.
    :param logs: The log messages to display.
    """
    super().__init__(parent)
    self.geometry('640x480')
    self.update()
    geom = self.wm_geometry().split('+')[0]
    geom = geom.split('x')
    self.wm_minsize(floor(int(geom[0]) * 0.7), floor(int(geom[1]) * 0.7))
    self.title("Riepilogo log")
    self.iconbitmap(MainWindow.ICON)
    self.attributes('-topmost', 1)
    self.after(200, lambda: self.iconbitmap(MainWindow.ICON))
    self.after(250, lambda: self.attributes('-topmost', 0))
    self.after(250, self.focus_force)
    self.__gui = self.__make_layout()
    if logs is not None:
        self.update_logs(logs)

__make_layout()

Create the layout of the main window.

:return: The layout of the main window.

Source code in log_view.py
def __make_layout(self) -> dict[str, Any]:
    """
    Create the layout of the main window.

    :return: The layout of the main window.
    """
    font = ('Arial', 15)
    text_frame = ctk.CTkFrame(self, fg_color="white", border_width=1)
    text_frame.pack(pady=10, padx=10, fill=ctk.BOTH, expand=True)

    log_text = ctk.CTkTextbox(text_frame, wrap=ctk.WORD,
                              font=font, fg_color="white")
    log_text.pack(pady=4, padx=4, fill=ctk.BOTH, expand=True)
    log_text.configure(state=ctk.DISABLED)
    for level, color in MainWindow.COLORS.items():
        log_text.tag_config(level, foreground=color)
    gui = {'log_text': log_text}

    button_frame = ctk.CTkFrame(self, fg_color='transparent')
    button_frame.pack(pady=(10, 20))
    ok_button = ctk.CTkButton(button_frame, text="OK", font=font)
    ok_button.grid(row=0, column=0, padx=10)
    gui['ok_button'] = ok_button
    reload_button = ctk.CTkButton(button_frame, text="Reload", font=font)
    reload_button.grid(row=0, column=1, padx=10)
    gui['reload_button'] = reload_button
    return gui

set_logs_updated(updated)

Set or reset the reload indicator in the window. :param updated: True if the logs are updated, False otherwise.

Source code in log_view.py
def set_logs_updated(self, updated: bool) -> None:
    """
    Set or reset the reload indicator in the window.
    :param updated: True if the logs are updated, False otherwise.
    """
    theme = ThemeManager.theme["CTkButton"]
    color = 'red' if updated else theme['fg_color']
    hover = '#C30300' if updated else theme['hover_color']
    self.__gui['reload_button'].configure(fg_color=color, hover_color=hover)

set_ok_button_callback(callback)

Set the callback of the ok button. :param callback: The callback of the ok button.

Source code in log_view.py
def set_ok_button_callback(self, callback: Callable) -> None:
    """
    Set the callback of the ok button.
    :param callback: The callback of the ok button.
    """
    self.__gui['ok_button'].configure(command=callback)

set_reload_button_callback(callback)

Set the callback of the reload button. :param callback: The callback of the reload button.

Source code in log_view.py
def set_reload_button_callback(self, callback: Callable) -> None:
    """
    Set the callback of the reload button.
    :param callback: The callback of the reload button.
    """
    self.__gui['reload_button'].configure(command=callback)

update_logs(logs)

Update the log messages on the GUI. :param logs: The log messages to display.

Source code in log_view.py
def update_logs(self, logs: Tuple) -> None:
    """
    Update the log messages on the GUI.
    :param logs: The log messages to display.
    """
    log_text = self.__gui['log_text']
    log_text.configure(state=ctk.NORMAL)
    log_text.delete(1.0, ctk.END)
    for msg in reversed(logs):
        log_text.insert('end', f'{msg}\n', msg.split(' ', 1)[0])
    log_text.configure(state=ctk.DISABLED)