-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
373 lines (275 loc) · 12.4 KB
/
main.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import sys
import os
from pathlib import Path
from PyQt5.QtWidgets import (
QApplication,
QGroupBox,
QLabel,
QLineEdit,
QMainWindow,
QMessageBox,
QPushButton,
QVBoxLayout,
QHBoxLayout,
QWidget,
QGridLayout,
)
from PyQt5.QtGui import (
QIntValidator,
QIcon,
QPixmap,
)
from PyQt5 import QtCore
import matplotlib
matplotlib.use('Qt5Agg')
import numpy as np
import quadratic_equation
import canvas
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.sc = canvas.MplCanvas(self, width=6, height=6, dpi=100)
# relative path
self.dirname = os.path.dirname(__file__)
self.graph_ico = os.path.join(self.dirname, 'icons/graph_ico.png')
self.stop = os.path.join(self.dirname, 'icons/stop_writing.png')
# window name and icon
self.setWindowTitle('Quadratic Equation')
self.setWindowIcon(QIcon(self.graph_ico))
# minimum size of main window
self.setMinimumWidth(1200)
self.setMinimumHeight(600)
# maximum size of main window
self.setMaximumWidth(1200)
self.setMaximumHeight(600)
# button for solving quation
buttonSolve = QPushButton('Solve')
buttonSolve.clicked.connect(lambda: self.solve())
# button for closing window
buttonClose = QPushButton('Close')
buttonClose.clicked.connect(self.close)
# horizontal layout for buttons - stretch method added
horizontalLayoutButtons = QHBoxLayout()
horizontalLayoutButtons.addStretch(1)
horizontalLayoutButtons.addWidget(buttonSolve)
horizontalLayoutButtons.addWidget(buttonClose)
# vertical layout for numerical solution - input and results
verticalLayoutNumerical = QVBoxLayout()
# calling method for creating layout for input coefficients
self.layoutCoef()
# calling method for creating layout for assembled equation
self.layoutEquation()
# calling methon for creating layout for solution - discriminant and roots
self.layoutSolution()
# group box for coefficients
groupBoxCoef = QGroupBox("Coefficients")
groupBoxCoef.setLayout(self.layout_coef)
# group box for equation
groupBoxEquation = QGroupBox("Quadratic equation")
groupBoxEquation.setLayout(self.layout_equation)
# group box for solution
groupBoxSolution = QGroupBox("Solution")
groupBoxSolution.setLayout(self.layout_solution)
# vertical layout for numerical solution - input and results
verticalLayoutNumerical.addWidget(groupBoxCoef)
verticalLayoutNumerical.addWidget(groupBoxEquation)
verticalLayoutNumerical.addWidget(groupBoxSolution, 1)
# horizontal layout for numetical values on the left side and graph on the right side
mainHorizontalLayout = QHBoxLayout()
mainHorizontalLayout.addLayout(verticalLayoutNumerical)
mainHorizontalLayout.addWidget(self.sc)
# vertical layout for final app solution - buttons on the bottom part of app
outerVerticalLayout = QVBoxLayout()
outerVerticalLayout.addLayout(mainHorizontalLayout)
outerVerticalLayout.addStretch(1)
outerVerticalLayout.addLayout(horizontalLayoutButtons)
w = QWidget()
w.setLayout(outerVerticalLayout)
self.setCentralWidget(w)
def layoutCoef(self):
validator = QIntValidator(-10000, 1000, self)
self.layout_coef = QGridLayout()
self.label_description = QLabel("Quadratic equation: ax<sup>2</sup>+bx+c=0")
self.label_description.setAlignment(QtCore.Qt.AlignLeft)
self.layout_coef.addWidget(self.label_description,0,0,1,6)
self.label_a = QLabel("a:")
self.label_a.setAlignment(QtCore.Qt.AlignLeft)
self.layout_coef.addWidget(self.label_a,1,0)
self.edit_a = QLineEdit(self)
self.edit_a.setAlignment(QtCore.Qt.AlignRight)
self.edit_a.setToolTip("Enter coefficent a ≠ 0")
self.edit_a.setValidator(validator)
self.layout_coef.addWidget(self.edit_a,1,1)
self.label_b = QLabel("b:")
self.label_b.setAlignment(QtCore.Qt.AlignLeft)
self.layout_coef.addWidget(self.label_b,1,2)
self.edit_b = QLineEdit(self)
self.edit_b.setAlignment(QtCore.Qt.AlignRight)
self.edit_b.setToolTip("Enter coefficent b")
self.edit_b.setValidator(validator)
self.layout_coef.addWidget(self.edit_b,1,3)
self.label_c = QLabel("c:")
self.label_c.setAlignment(QtCore.Qt.AlignLeft)
self.layout_coef.addWidget(self.label_c,1,4)
self.edit_c = QLineEdit(self)
self.edit_c.setAlignment(QtCore.Qt.AlignRight)
self.edit_c.setToolTip("Enter coefficent c")
self.edit_c.setValidator(validator)
self.layout_coef.addWidget(self.edit_c,1,5)
def layoutEquation(self):
self.layout_equation = QGridLayout()
self.label_equation = QLabel("")
# self.label_equation.setFont(QFont('Sans Serif', 10))
self.label_equation.setAlignment(QtCore.Qt.AlignCenter)
self.layout_equation.addWidget(self.label_equation,0,0,1,6)
def layoutSolution(self):
self.layout_solution = QGridLayout()
self.label_three_type_solution = QLabel('Type of solution')
self.label_three_type_solution.setAlignment(QtCore.Qt.AlignCenter)
self.layout_solution.addWidget(self.label_three_type_solution,0,0,1,6)
self.label_discriminant = QLabel('Discriminant D:')
self.layout_solution.addWidget(self.label_discriminant,1,0)
self.label_discriminant_number = QLabel('')
self.label_discriminant_number.setAlignment(QtCore.Qt.AlignCenter)
self.layout_solution.addWidget(self.label_discriminant_number,1,1,1,5)
self.label_x1_root = QLabel('Root x1:')
self.layout_solution.addWidget(self.label_x1_root,2,0)
self.label_x1_root_number = QLabel('')
self.label_x1_root_number.setAlignment(QtCore.Qt.AlignCenter)
# self.label_x1_root_number.setFont(QFont('Sans Serif', 10))
self.layout_solution.addWidget(self.label_x1_root_number,2,1,1,5)
self.label_x2_root = QLabel('Root x2:')
self.layout_solution.addWidget(self.label_x2_root,3,0)
self.label_x2_root_number = QLabel('')
self.label_x2_root_number.setAlignment(QtCore.Qt.AlignCenter)
# self.label_x2_root_number.setFont(QFont('Sans Serif', 10))
self.layout_solution.addWidget(self.label_x2_root_number,3,1,1,5)
self.label_vertex = QLabel('Vertex:')
self.layout_solution.addWidget(self.label_vertex,4,0)
self.label_vertex_number = QLabel('')
self.label_vertex_number.setAlignment(QtCore.Qt.AlignCenter)
#self.label_vertex_number.setFont(QFont('Sans Serif', 10))
self.layout_solution.addWidget(self.label_vertex_number,4,1,1,5)
def solve(self):
self.display()
def display(self):
coeff_a = self.edit_a.text()
coeff_b = self.edit_b.text()
coeff_c = self.edit_c.text()
try:
a = int(coeff_a)
b = int(coeff_b)
c = int(coeff_c)
self.edit_a.setStyleSheet("background-color : #E4F7FF; color : black")
self.edit_b.setStyleSheet("background-color : #E4F7FF; color : black")
self.edit_c.setStyleSheet("background-color : #E4F7FF; color : black")
try:
res = 1 / a
self.edit_a.setStyleSheet("background-color : #E4F7FF; color : black")
self.edit_b.setStyleSheet("background-color : #E4F7FF; color : black")
self.edit_c.setStyleSheet("background-color : #E4F7FF; color : black")
except Exception:
self.edit_a.setStyleSheet("background-color : #FFA762; color : black")
messagebox = QMessageBox(QMessageBox.Information, "Error", "Coefficient 'a' cannot be zero!", buttons=QMessageBox.Ok, parent=self)
messagebox.setIconPixmap(QPixmap(self.stop))
messagebox.exec_()
else:
self.label_equation.setText(self.display_equation_format(a, b, c))
qe = quadratic_equation.QuadraticEquation(a, b, c)
self.discriminant = qe.discriminant()
self.label_three_type_solution.setText(qe.equation_type(self.discriminant))
self.label_discriminant_number.setText(str(self.discriminant))
qe.solve()
self.x1_root = qe.x1
self.x2_root = qe.x2
if (qe.x1 == qe.x2):
self.label_x1_root.setText("Root x1 = Root x2:")
self.label_x2_root.setVisible(False)
self.label_x2_root_number.setVisible(False)
else:
self.label_x1_root.setText("Root x1:")
self.label_x2_root.setVisible(True)
self.label_x2_root_number.setVisible(True)
self.label_x1_root_number.setText(str(self.x1_root))
self.label_x2_root_number.setText(str(self.x2_root))
self.vertex_x = round(qe.vertex_x,3)
self.vertex_y = round(qe.vertex_y,3)
self.label_vertex_number.setText('('+str(self.vertex_x)+', '+str(self.vertex_y)+')')
self.sc.axes.cla()
x = np.linspace(self.vertex_x-10, self.vertex_x+10, 1000)
y = a * x ** 2 + b * x + c
self.sc.axes.plot(x, y, linewidth=3, color='#002535')
self.sc.axes.set_facecolor('#E4F7FF')
self.sc.axes.plot(self.vertex_x, self.vertex_y, marker="o", markersize=8, markeredgecolor="#002535", markerfacecolor="#EE6B05")
self.sc.axes.set_xlabel('x-axis', fontsize=12)
self.sc.axes.set_ylabel('y-axis', fontsize=12)
self.description = 'V = [' + str(self.vertex_x) + ', ' + str(self.vertex_y) + ']'
self.sc.axes.annotate(self.description, xy =(self.vertex_x, self.vertex_y),
xytext =(self.vertex_x - 9, self.vertex_y + 1), arrowprops = dict(facecolor ='#EE6B05',
shrink = 0.05))
self.sc.draw()
except Exception:
self.edit_a.setStyleSheet("background-color : #FFA762; color : black")
self.edit_b.setStyleSheet("background-color : #FFA762; color : black")
self.edit_c.setStyleSheet("background-color : #FFA762; color : black")
messagebox = QMessageBox(QMessageBox.Information, "Error", "Input can only be an integer!", buttons=QMessageBox.Ok, parent=self)
messagebox.setIconPixmap(QPixmap(self.stop))
messagebox.exec_()
def display_equation_format(self, a, b, c):
"""
Assembly quadratic equation
a - coefficient a - int
b - coefficient b - int
c - coefficient c - int
return string (example 5x<sup>2</sup>+5x+5)
"""
equation = self.first_term(a) + self.second_term(b) + self.third_term(c) + "=0"
return equation
def first_term(self, a):
"""
First quadratic term in quadratic equation
a - coefficient a - int
return string (example 5x<sup>2</sup> )
"""
if a == 1:
a = "x<sup>2</sup>"
elif a == -1:
a = "-x<sup>2</sup>"
else:
a = str(a) + "x<sup>2</sup>"
return a
def second_term(self, b):
"""
Second linear term in quadratic equation
b - coefficient b - int
return string (example 5x )
"""
if b == 1:
b = "+x"
elif b == -1:
b = "-x"
elif b > 0:
b = "+" + str(b) + "x"
elif b == 0:
b = ""
else:
b = str(b) + "x"
return b
def third_term(self, c):
"""
Third constant term in quadratic equation
c - coefficient c - int
return string (example 5)
"""
if c > 0:
c = "+" + str(c)
elif c == 0:
c = ""
else:
c = str(c)
return c
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.setStyleSheet(Path('style.qss').read_text())
app.exec()