-
Notifications
You must be signed in to change notification settings - Fork 0
/
ten_key_widget.py
296 lines (261 loc) · 12.3 KB
/
ten_key_widget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# ================================================
# UI for Ten Key Input and Display
# ================================================
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QGridLayout, QPushButton, QLabel, QWidget, QStyle, QSizePolicy
from PyQt6.QtGui import QFont, QIcon, QMouseEvent
from PyQt6.QtCore import Qt, QSize, pyqtSignal, pyqtSlot
from calculator_services import CalculatorServices
from compute_services import ComputeServices
from calculator_implementation import create_calculate
from compute_implementation import create_compute
from enum import Enum
class TenKeyConfig(Enum):
DEFAULT = 'default' # 10 digits, back, clear entry, decimal
DIGITS_ONLY = 'digits_only' # 10 digits
DIGITS_CE_BACK = 'digits_ce_back' # 10 digits, back, clear entry
DIGITS_CE_DECIMAL = 'digits_ce_decimal' # 10 digits, clear entry, decimal
DIGITS_MR_DECIMAL = 'digits_mr_decimal' # 10 digits, memory recall, decimal
class TenKey(QWidget):
# Define custom signals
buttonClicked = pyqtSignal(str)
inputClicked = pyqtSignal(object)
def set_button_color(self, color):
if color is None:
return '#e0f2e0'
else:
return color
def __init__(self, config='default',alt_button_color=None):
super().__init__()
# Set the Ten Key configuration
self.config = TenKeyConfig(config)
# Set up services
services = CalculatorServices.create_services()
self.calculate = create_calculate(services)
self.accumulate_non_zero_digit = services["accumulate_non_zero_digit"]
self.accumulate_zero = services["accumulate_zero"]
self.accumulate_separator = services["accumulate_separator"]
self.get_number_from_accumulator = services["get_number_from_accumulator"]
self.get_display_from_state = services["get_display_from_state"]
self.get_pending_op_from_state = services["get_pending_op_from_state"]
self.get_memo_from_state = services["get_memo_from_state"]
self.button_color = self.set_button_color(alt_button_color)
# Initial state
self.state = CalculatorServices.initial_state
QIcon.setThemeName("Papirus")
print("=======Ten Key Input and Display=======")
print("Initial state:", self.state)
print("=======================================")
# ------Create Constants and Widgets---------
# Constants for Styles
grid_color = "#F5F5DC"
background_color = "#F6F7F9"
memory_recall = "#8FBC8F"
BUTTON_PADDING = "5px"
BUTTON_FONT_SIZE = "14pt"
BORDER_RADIUS = "5px"
BORDER_COLOR = "#656565"
HOVER_COLOR = "#c8c8c8"
BUTTON_COLOR = self.button_color
# Style Sheets
# Common Button Style
button_style_base = f"""
background-color: {BUTTON_COLOR};
border: 1px solid {BORDER_COLOR};
border-radius: {BORDER_RADIUS};
padding: {BUTTON_PADDING};
"""
# Style for Normal Buttons
button_style = f"""
QPushButton {{
{button_style_base}
}}
QPushButton:hover {{
background-color: {HOVER_COLOR};
border: 1px solid {HOVER_COLOR};
}}
"""
# Style for Memory Recall Button
button_style_recall = f"""
QPushButton {{
{button_style_base}
background-color: {memory_recall};
}}
QPushButton:hover {{
background-color: {memory_recall};
border: 1px solid {HOVER_COLOR};
}}
"""
# Main Layout
main_layout = QVBoxLayout()
self.grid = QGridLayout()
self.grid.setContentsMargins(10, 10, 10, 10)
main_layout.addLayout(self.grid)
# Set the main layout to the widget
self.setLayout(main_layout)
# Text Blocks
self.result = QLabel("0")
self.result.setAlignment(Qt.AlignmentFlag.AlignRight)
self.result.setStyleSheet(f"background-color: {background_color}; font-size: 27px;")
self.grid.addWidget(self.result, 0, 0, 1, 3)
# Call to setup buttons
self.setup_buttons(button_style, button_style_recall)
# ------Create Functions---------
def setup_buttons(self, button_style, button_style_recall):
# Buttons
def back_button(r,c):
# Adding backspace button separately to set icon
back_button_icon = QIcon.fromTheme("back")
back_button = QPushButton()
back_button.setIcon(back_button_icon)
back_button.setIconSize(QSize(23, 23))
back_button.setStyleSheet(button_style)
self.grid.addWidget(back_button, r, c)
back_button.clicked.connect(self.create_handler('←'))
back_button.clicked.connect(self.handle_button_clicked)
def python_icon():
# Adding python icon to empty grid space
python_icon = QIcon.fromTheme("python")
python_button = QPushButton() # Placing the icon in a QPushButton since it has .setIcon()
python_button.setIcon(python_icon)
python_button.setIconSize(QSize(23, 23))
python_button.setStyleSheet(f"background-color: transparent; border: none; padding: 0;") # Hide the button
self.grid.addWidget(python_button, 1, 2)
if self.config == TenKeyConfig.DEFAULT:
buttons = [
('1', 4, 0), ('2', 4, 1), ('3', 4, 2),
('4', 3, 0), ('5', 3, 1), ('6', 3, 2),
('7', 2, 0), ('8', 2, 1), ('9', 2, 2),
('CE', 5, 0),('0', 5, 1), ('.', 5, 2)
]
back_button(1,0)
python_icon()
elif self.config == TenKeyConfig.DIGITS_ONLY:
buttons = [
('1', 4, 0), ('2', 4, 1), ('3', 4, 2),
('4', 3, 0), ('5', 3, 1), ('6', 3, 2),
('7', 2, 0), ('8', 2, 1), ('9', 2, 2),
('0', 5, 1)
]
elif self.config == TenKeyConfig.DIGITS_CE_DECIMAL:
buttons = [
('1', 4, 0), ('2', 4, 1), ('3', 4, 2),
('4', 3, 0), ('5', 3, 1), ('6', 3, 2),
('7', 2, 0), ('8', 2, 1), ('9', 2, 2),
('0', 5, 1), ('CE', 5, 0), ('.', 5, 2)
]
elif self.config == TenKeyConfig.DIGITS_CE_BACK:
buttons = [
('1', 4, 0), ('2', 4, 1), ('3', 4, 2),
('4', 3, 0), ('5', 3, 1), ('6', 3, 2),
('7', 2, 0), ('8', 2, 1), ('9', 2, 2),
('0', 5, 1), ('CE', 5, 0)
]
back_button(5,2)
elif self.config == TenKeyConfig.DIGITS_MR_DECIMAL:
buttons = [
('1', 4, 0), ('2', 4, 1), ('3', 4, 2),
('4', 3, 0), ('5', 3, 1), ('6', 3, 2),
('7', 2, 0), ('8', 2, 1), ('9', 2, 2),
('0', 5, 0), ('.', 5, 1), ('MR', 5, 2)
]
for (text, row, col) in buttons:
button = QPushButton(text)
if text == 'MR':
button.setStyleSheet(button_style_recall)
else:
button.setStyleSheet(button_style)
button.setFont(QFont('Arial', 14))
self.grid.addWidget(button, row, col)
button.clicked.connect(self.create_handler(text))
button.clicked.connect(self.handle_button_clicked)
# Function to bind handler to an action
def create_handler(self, text):
def handler():
self.handle_input(text)
return handler
def handle_button_clicked(self):
display = self.result.text()
self.buttonClicked.emit(display)
# Function to route action to a handler
def handle_input(self, input_text):
input_mapping = CalculatorServices.ten_key_input_mapping
input_action, param = input_mapping.get(input_text, (None, None))
if callable(input_action) and param is not None:
input_action = input_action(param)
if input_action is not None:
self.state = self.calculate(input_action, self.state)
self.inputClicked.emit(input_action)
self.update_display()
# Function to reset ten-key widget.
def reset_input(self):
input_mapping = CalculatorServices.ten_key_input_mapping
input_action, param = input_mapping.get('CE', (None, None))
self.state = self.calculate(input_action, self.state)
self.update_display()
# Function to set accumulator back.
def back_input(self):
input_mapping = CalculatorServices.ten_key_input_mapping
input_action, param = input_mapping.get('←', (None, None))
if input_action is not None:
self.state = self.calculate(input_action, self.state)
self.inputClicked.emit(input_action)
self.update_display()
self.handle_button_clicked()
# Function to display current state
def update_display(self):
#print("=======Ten Key Input and Display=======")
#print("Output State:", self.state)
#print("Display Text:", self.get_display_from_state(self.state))
#print("Pending Op Text:", self.get_pending_op_from_state(self.state))
#print("Memo Text:", self.get_memo_from_state(self.state))
#print("=======================================")
self.result.setText(self.get_display_from_state(self.state))
def keyPressEvent(self, event):
key = event.key()
if self.config == TenKeyConfig.DEFAULT:
key_mapping = {
Qt.Key.Key_0: '0', Qt.Key.Key_1: '1', Qt.Key.Key_2: '2', Qt.Key.Key_3: '3',
Qt.Key.Key_4: '4', Qt.Key.Key_5: '5', Qt.Key.Key_6: '6', Qt.Key.Key_7: '7',
Qt.Key.Key_8: '8', Qt.Key.Key_9: '9', Qt.Key.Key_Period: '.', Qt.Key.Key_Backspace: '←',
Qt.Key.Key_Delete: 'CE'
}
elif self.config == TenKeyConfig.DIGITS_ONLY:
key_mapping = {
Qt.Key.Key_0: '0', Qt.Key.Key_1: '1', Qt.Key.Key_2: '2', Qt.Key.Key_3: '3',
Qt.Key.Key_4: '4', Qt.Key.Key_5: '5', Qt.Key.Key_6: '6', Qt.Key.Key_7: '7',
Qt.Key.Key_8: '8', Qt.Key.Key_9: '9'
}
elif self.config == TenKeyConfig.DIGITS_CE_DECIMAL:
key_mapping = {
Qt.Key.Key_0: '0', Qt.Key.Key_1: '1', Qt.Key.Key_2: '2', Qt.Key.Key_3: '3',
Qt.Key.Key_4: '4', Qt.Key.Key_5: '5', Qt.Key.Key_6: '6', Qt.Key.Key_7: '7',
Qt.Key.Key_8: '8', Qt.Key.Key_9: '9', Qt.Key.Key_Delete: 'CE', Qt.Key.Key_Period: '.'
}
elif self.config == TenKeyConfig.DIGITS_CE_BACK:
key_mapping = {
Qt.Key.Key_0: '0', Qt.Key.Key_1: '1', Qt.Key.Key_2: '2', Qt.Key.Key_3: '3',
Qt.Key.Key_4: '4', Qt.Key.Key_5: '5', Qt.Key.Key_6: '6', Qt.Key.Key_7: '7',
Qt.Key.Key_8: '8', Qt.Key.Key_9: '9', Qt.Key.Key_Delete: 'CE', Qt.Key.Key_Backspace: '←'
}
elif self.config == TenKeyConfig.DIGITS_MR_DECIMAL:
key_mapping = {
Qt.Key.Key_0: '0', Qt.Key.Key_1: '1', Qt.Key.Key_2: '2', Qt.Key.Key_3: '3',
Qt.Key.Key_4: '4', Qt.Key.Key_5: '5', Qt.Key.Key_6: '6', Qt.Key.Key_7: '7',
Qt.Key.Key_8: '8', Qt.Key.Key_9: '9', Qt.Key.Key_Period: '.'
}
if key in key_mapping:
self.handle_input(key_mapping[key])
self.handle_button_clicked()
class TenKeyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Ten Key Input and Display")
self.setGeometry(100, 100, 400, 200)
self.ten_key = TenKey('default')
self.setCentralWidget(self.ten_key)
# Standalone example entry point
if __name__ == "__main__":
app = QApplication([])
calculator_window = TenKeyWindow()
calculator_window.show()
app.exec()