BonPrinter v1.2.0
Thermal Printer tool
Loading...
Searching...
No Matches
model.py
Go to the documentation of this file.
1"""!
2********************************************************************************
3@file model.py
4@brief Application data storage model
5********************************************************************************
6"""
7
8import os
9import logging
10from typing import Optional, TYPE_CHECKING
11
12from Source.version import __title__
13from Source.Util.app_log import LogConfig
14from Source.Util.app_data import S_DEFAULT_OUTPUT_PATH, write_output_path_settings, read_last_dir, write_last_dir, \
15 read_show_price_settings, write_show_price_settings, read_print_report_settings, write_print_report_settings, \
16 read_output_path_settings
17from Source.Util.gui_toolkit import connect_signal
18
19from Source.Model import config
20from Source.Model import report
21from Source.Model.authentication import Authentication
22from Source.Model import sound
23from Source.Model.calculator import Calculator
24from Source.Model import monitor
25from Source.Model import language
26from Source.Worker.open_notepad import OpenNotepadWorker # pylint: disable=wrong-import-position
27from Source.Worker import printer # pylint: disable=wrong-import-position
28if TYPE_CHECKING:
29 from Source.Controller.main_window import MainWindow
30
31log = logging.getLogger(__title__)
32
33S_WRITE_TEST_FILE_NAME = "_write_test.txt"
34
35
36class Model:
37 """!
38 @brief Holds the data of the application
39 @param ui : main window object
40 @param log_config : log configuration of the application
41 """
42
43 def __init__(self, ui: "MainWindow", log_config: LogConfig) -> None:
44 self.ui = ui
45 self.ui.model = self # set model before function return to get language for status messages before
46 self.log_config = log_config
47 self.s_output_path: str = read_output_path_settings()
48 self.b_enable_report = read_print_report_settings()
49 self.b_show_price: bool = read_show_price_settings()
50 self.s_last_path: str | None = read_last_dir()
51 self.c_auth: Authentication = Authentication(ui)
52 self.c_calc: Calculator = Calculator()
53 self.c_config: config.Config = config.Config(ui) # init before printer and report
58 self.update_output_path(self.s_output_path) # update after set language. call to create output if not exist
60
61 # printer
63 self.c_printer.set_header(self.c_config.get_header1(),
64 self.c_config.get_header2())
65 self.c_printer.start() # start printer thread
66
67 def update_enable_report_status(self, b_enable_report: Optional[bool] = None) -> None:
68 """!
69 @brief Update report enable status.
70 @param b_enable_report : [True] enable print report; [False] disable print report; [None] toggle print report
71 """
72 if b_enable_report is None:
73 b_enable_report = not self.b_show_price
74 self.b_enable_report = b_enable_report
75 write_print_report_settings(self.b_show_price)
76 if self.b_enable_report:
77 s_statusbar_text = ["Enable Print Report", "Druckbericht aktiviert"]
78 else:
79 s_statusbar_text = ["Disable Print Report", "Druckbericht deaktiviert"]
80 self.ui.set_status(s_statusbar_text)
81 self.ui.update_screen()
82
83 def update_show_price_status(self, b_show_price: Optional[bool] = None) -> None:
84 """!
85 @brief Update Show price status.
86 @param b_show_price : [True] show price below article name; [False] show not; [None] toggle actual setting
87 """
88 if b_show_price is None:
89 b_show_price = not self.b_show_price
90 self.b_show_price = b_show_price
91 write_show_price_settings(self.b_show_price)
92 if self.b_show_price:
93 s_statusbar_text = ["Show Price", "Preis anzeigen"]
94 else:
95 s_statusbar_text = ["Hide Price", "Preis verbergen"]
96 self.ui.set_status(s_statusbar_text)
97 self.ui.update_screen()
98
99 def update_output_path(self, s_output_path: str) -> None:
100 """!
101 @brief Update output path
102 @param s_output_path : new output path to set
103 """
104 if not os.path.exists(s_output_path):
105 self.ui.set_status([f"Path not exist: {s_output_path}",
106 f"Pfad existiert nicht: {s_output_path}"], True)
107 if not os.path.exists(S_DEFAULT_OUTPUT_PATH):
108 log.debug("Create default output folder")
109 os.mkdir(S_DEFAULT_OUTPUT_PATH)
110 s_output_path = S_DEFAULT_OUTPUT_PATH
111
112 try: # test write access
113 s_test_file = s_output_path + "/" + S_WRITE_TEST_FILE_NAME
114 with open(s_test_file, mode="w", encoding="utf-8") as file:
115 file.write("Write Access Test")
116 os.remove(s_test_file)
117 except BaseException:
118 b_access = False
119 else:
120 b_access = True
121
122 if b_access:
123 self.s_output_path = s_output_path
124 write_output_path_settings(self.s_output_path)
125 self.ui.set_status([f"Output path changed to: {s_output_path}",
126 f"Ausgangsverzeichnis geƤndert zu: {s_output_path}"])
127 else:
128 self.ui.set_status([f"No Write access to path: {s_output_path}",
129 f"Kein Schreibzugriff auf das Verzeichnis: {s_output_path}"], True)
130
131 def get_last_path(self) -> str | None:
132 """!
133 @brief Get last path to open dialog
134 @return last path
135 """
136 path = self.s_output_path
137 if self.s_last_path:
138 if os.path.exists(self.s_last_path):
139 path = self.s_last_path
140 else:
141 self.s_last_path = None
142 return path
143
144 def set_last_path(self, path: str) -> None:
145 """!
146 @brief Set last path and save in persistent storage
147 @param path : path to set as last path
148 """
149 self.s_last_path = path
150 write_last_dir(path)
Class Authentication: Part of the module to hold the data of the current authentication session.
Class Calculator: Hold actual data to calculate.
Definition calculator.py:15
Class Configuration: Read/Write configuration data (user or articles)
Definition config.py:111
Class to set language.
Definition language.py:87
Holds the data of the application.
Definition model.py:36
None update_enable_report_status(self, Optional[bool] b_enable_report=None)
Update report enable status.
Definition model.py:67
Authentication c_auth
Definition model.py:51
None update_output_path(self, str s_output_path)
Update output path.
Definition model.py:99
report.Report c_report
Definition model.py:54
None update_show_price_status(self, Optional[bool] b_show_price=None)
Update Show price status.
Definition model.py:83
None __init__(self, "MainWindow" ui, LogConfig log_config)
Definition model.py:43
monitor.MonitorScale c_monitor
Definition model.py:56
None set_last_path(self, str path)
Set last path and save in persistent storage.
Definition model.py:144
Calculator c_calc
Definition model.py:52
sound.Sound c_sound
Definition model.py:55
config.Config c_config
Definition model.py:53
str|None get_last_path(self)
Get last path to open dialog.
Definition model.py:131
printer.Printer c_printer
Definition model.py:62
language.Language c_language
Definition model.py:57
Class to scale text and item positions.
Definition monitor.py:55
Report class to log and create sales articles.
Definition report.py:96
Sounds for actions.
Definition sound.py:22
Class create bons and printout.
Definition printer.py:70