-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_calculator_widget.py
229 lines (203 loc) · 9.52 KB
/
basic_calculator_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
# ================================================
# UI for a Basic Calculator
# ================================================
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QGridLayout, QPushButton, QLabel, QWidget, QStyle
from PyQt6.QtGui import QFont, QIcon
from PyQt6.QtCore import Qt, QSize
from calculator_services import CalculatorServices
from calculator_implementation import create_calculate
import sys
import time
class BasicCalculator(QWidget):
def __init__(self):
super().__init__()
# 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.do_math_operation = services["do_math_operation"]
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"]
# Initial state and counters
self.delete_press_count = 0
self.last_delete_press_time = 0
self.state = CalculatorServices.initial_state
QIcon.setThemeName("Papirus")
print("==============")
print("Initial state:", self.state)
print("get_display_from_state:", self.get_display_from_state)
print("get_pending_op_from_state:", self.get_pending_op_from_state)
print("get_memo_from_state:", self.get_memo_from_state)
print("==============")
# ------Create Constants and Widgets---------
# Constants for Styles
grid_color = "#F5F5DC"
button_color = "#ebebeb"
button_hover_color = "#d2d2d2"
background_color = "#F6F7F9"
operation_button_color = "#b0b0b0"
operation_button_border_color = "#656565"
operation_hover_background_color = "#c8c8c8"
operation_button_font = QFont('Arial', 16, QFont.Weight.Bold)
BUTTON_PADDING = "5px"
BUTTON_FONT_SIZE = "14pt"
BORDER_RADIUS = "5px"
BORDER_COLOR = "#656565"
HOVER_COLOR = "#c8c8c8"
# 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: {button_hover_color};
border: 1px solid {button_hover_color};
}}
"""
# Style for Operation Buttons
operation_button_style = f"""
QPushButton {{
{button_style_base}
background-color: {operation_button_color};
font-family: '{operation_button_font.family()}';
font-size: {operation_button_font.pointSize()}pt;
font-weight: {operation_button_font.weight()};
}}
QPushButton:hover {{
background-color: {HOVER_COLOR};
border: 1px solid {operation_button_border_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, 5)
self.memo = QLabel("")
self.memo.setAlignment(Qt.AlignmentFlag.AlignRight)
self.memo.setStyleSheet(f"background-color: {background_color}; font-size: 17px;")
self.grid.addWidget(self.memo, 0, 5, 1, 1)
self.helper = QLabel("")
self.helper.setAlignment(Qt.AlignmentFlag.AlignRight)
self.helper.setStyleSheet(f"background-color: {background_color};")
self.grid.addWidget(self.helper, 1, 0, 1, 6)
# Call to setup buttons
self.setup_buttons(button_style, operation_button_style)
# ------Create Functions---------
def setup_buttons(self, button_style, operation_button_style):
# Buttons
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), ('.', 5, 2),
('+', 4, 3), ('-', 3, 3), ('*', 2, 3), ('/', 1, 3), ('=', 5, 3),
('√', 1, 4), ('±', 2, 4), ('1/x', 3, 4), ('%', 4, 4),
('C', 1, 1), ('CE', 1, 0), ('MC', 5, 0), ('MR', 5, 5),
('MS', 4, 5), ('M+', 3, 5), ('M-', 2, 5)
]
# Identify operation buttons for style application.
operation_buttons = ['+', '-', '*', '/']
# 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, 1, 2)
back_button.clicked.connect(self.create_handler('←'))
# Adding 'accessories-calculator' icon to empty grid space
python_icon = QIcon.fromTheme("accessories-calculator")
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, 5, 4)
for (text, row, col) in buttons:
button = QPushButton(text)
if text in operation_buttons:
button.setStyleSheet(operation_button_style)
else:
button.setStyleSheet(button_style)
button.setFont(QFont('Arial', 14))
self.grid.addWidget(button, row, col)
button.clicked.connect(self.create_handler(text))
# Function to bind handler to an action
def create_handler(self, text):
def handler():
self.handle_input(text)
return handler
# Function to route action to a handler
def handle_input(self, input_text):
input_mapping = CalculatorServices.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)
print("Input action:", input_action)
print("Current state:", self.state)
if input_action is not None:
self.state = self.calculate(input_action, self.state)
self.update_display()
# Function to display current state
def update_display(self):
print("==============")
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))
self.helper.setText(self.get_pending_op_from_state(self.state))
self.memo.setText(self.get_memo_from_state(self.state))
def keyPressEvent(self, event):
key = event.key()
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_Plus: '+', Qt.Key.Key_Minus: '-', Qt.Key.Key_Asterisk: '*', Qt.Key.Key_Slash: '/',
Qt.Key.Key_Enter: '=', Qt.Key.Key_Return: '='
}
if key in key_mapping:
self.handle_input(key_mapping[key])
elif key == Qt.Key.Key_Delete:
current_time = time.time()
if current_time - self.last_delete_press_time < 1:
self.delete_press_count += 1
else:
self.delete_press_count = 1 # Reset counter if more than 1 second has passed
self.last_delete_press_time = current_time
if self.delete_press_count == 1:
self.handle_input('CE')
elif self.delete_press_count == 2:
self.handle_input('C')
class BasicCalculatorWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Calculator")
self.setGeometry(100, 100, 400, 200)
self.calculator = BasicCalculator()
self.setCentralWidget(self.calculator)
# Standalone example entry point
if __name__ == "__main__":
app = QApplication([])
calculator_window = BasicCalculatorWindow()
calculator_window.show()
app.exec()