-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.py
377 lines (311 loc) · 14.3 KB
/
Shape.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
374
375
376
377
import os
from PyQt5.QtWidgets import (
QFileDialog,
QMessageBox, # Import for creating message boxes
QComboBox, # Import for creating combo boxes
)
from PyQt5.QtGui import (
QValidator, # Import for input validation
)
from PyQt5.QtGui import (
QPixmap, # Import for loading images
)
import pandas as pd
class ShapeFunctionality:
def custom_messagebox(self, text="Error!"):
"""
Displays a custom message box with an error icon.
Args:
text (str, optional): The message to display. Defaults to "Error!".
"""
messagebox = QMessageBox(QMessageBox.Warning, "Error", text, buttons = QMessageBox.Ok, parent=self)
messagebox.setIconPixmap(QPixmap('stop_writing.png'))
messagebox.exec_()
def custom_combo(self):
"""
Creates a custom QComboBox widget with a predefined list of colors.
Args:
self: The parent widget.
Returns:
QComboBox: A QComboBox widget populated with color options.
"""
# Create a new QComboBox instance and set its parent widget
custom_combo = QComboBox(self)
# Define a list of color names to populate the combo box
colors = ["black", "blue", "gray", "green", "magenta",
"orange", "pink", "red", "violet", "yellow"]
# Add all color names to the combo box's item list
custom_combo.addItems(colors)
# Set a fixed width and height for the combo box to ensure consistent appearance
custom_combo.setFixedWidth(150)
custom_combo.setFixedHeight(28)
# Return the created QComboBox instance
return custom_combo
def check_state_and_set_color(self, sender):
"""
This function checks the validation state of a QLineEdit sender and sets its background color accordingly.
Args:
sender (QtWidgets.QLineEdit): The QLineEdit widget whose state and color need to be updated.
"""
sender = self.sender()
validator = sender.validator()
state = validator.validate(sender.text(), 0)[0]
color = '#f6989d' # Default color (red)
if sender.text() == "":
color = '#f6989d' # Empty field remains red
elif state == QValidator.Acceptable or sender.text() == "0":
color = '#c4df9b' # Valid input turns green
elif state == QValidator.Intermediate:
color = '#fff79a' # Intermediate state turns yellow
sender.setStyleSheet('QLineEdit { background-color: %s }' % color)
def check_state_rad_and_set_color(self, sender):
"""
This function checks the validation state of a QLineEdit sender and sets its background color accordingly.
Args:
sender (QtWidgets.QLineEdit): The QLineEdit widget whose state and color need to be updated.
"""
sender = self.sender()
validator = sender.validator()
state = validator.validate(sender.text(), 0)[0]
if sender.text() == "0" or sender.text() == "":
color = '#f6989d' # Empty or "0" field remains red
elif state == QValidator.Acceptable:
color = '#c4df9b' # Valid input turns green
elif state == QValidator.Intermediate:
color = '#fff79a' # Intermediate state turns yellow
else:
color = '#f6989d' # red
sender.setStyleSheet('QLineEdit { background-color: %s }' % color)
def export_excel(self, shape):
"""
Exports shape calculation data and plot to an Excel file.
This function retrieves data from the user interface, creates Pandas DataFrames,
and writes them to a new Excel file with formatting. It also saves the shape plot
(assuming it's a Matplotlib figure) as an image and inserts it into the Excel sheet.
Raises:
Exception: If an error occurs during the export process.
"""
file_name, _ = QFileDialog.getSaveFileName(
self, 'Export to Excel', os.path.join(os.getcwd(), 'Results', f"{shape}.xlsx"), 'Excel (*.xlsx)')
if not file_name:
return # User canceled the file selection dialog
# Prepare data for the Excel sheet
try:
if shape == 'Circle':
# Create dictionaries containing circle property data and calculation results
data = {
'Property': [self.label_radius.text(),
self.label_centerX.text(),
self.label_centerY.text()],
'Value': [float(self.edit_radius.text()),
float(self.edit_centerX.text()),
float(self.edit_centerY.text())],
'Unit': ['cm',
'cm',
'cm']
}
results = {
'Result': [self.label_perimeter.text(),
self.label_area.text()],
'Value': [float(self.label_res_perimeter.text()),
float(self.label_res_area.text())],
'Unit': ['cm',
'cm²']
}
elif shape == 'Ellipse':
# Create dictionaries containing ellipse property data and calculation results
data = {
'Property': [self.label_axis_a.text(),
self.label_axis_b.text(),
self.label_centerX.text(),
self.label_centerY.text()],
'Value': [float(self.edit_axis_a.text()),
float(self.edit_axis_b.text()),
float(self.edit_centerX.text()),
float(self.edit_centerY.text())],
'Unit': ['cm',
'cm',
'cm',
'cm']
}
results = {
'Result': [self.label_perimeter.text(),
self.label_area.text()],
'Value': [float(self.label_res_perimeter.text()),
float(self.label_res_area.text())],
'Unit': ['cm',
'cm²']
}
elif shape == 'Square':
# Create dictionaries containing square property data and calculation results
data = {
'Property': [self.label_side.text(),
self.label_centerX.text(),
self.label_centerY.text()],
'Value': [float(self.edit_side.text()),
float(self.edit_centerX.text()),
float(self.edit_centerY.text())],
'Unit': ['cm',
'cm',
'cm']
}
results = {
'Result': [self.label_perimeter.text(),
self.label_area.text()],
'Value': [float(self.label_res_perimeter.text()),
float(self.label_res_area.text())],
'Unit': ['cm',
'cm²']
}
elif shape == 'Sphere':
# Create dictionaries containing square property data and calculation results
data = {
'Property': [self.label_radius.text(),
self.label_centerX.text(),
self.label_centerY.text(),
self.label_centerZ.text()],
'Value': [float(self.edit_radius.text()),
float(self.edit_centerX.text()),
float(self.edit_centerY.text()),
float(self.edit_centerZ.text())],
'Unit': ['cm',
'cm',
'cm',
'cm']
}
results = {
'Result': [self.label_surface.text(),
self.label_volume.text()],
'Value': [float(self.label_res_surface.text()),
float(self.label_res_volume.text())],
'Unit': ['cm²',
'cm³']
}
elif shape == 'Ellipsoid':
# Create dictionaries containing square property data and calculation results
data = {
'Property': [self.label_axis_a.text(),
self.label_axis_b.text(),
self.label_axis_c.text(),
self.label_centerX.text(),
self.label_centerY.text(),
self.label_centerZ.text()],
'Value': [float(self.edit_axis_a.text()),
float(self.edit_axis_b.text()),
float(self.edit_axis_c.text()),
float(self.edit_centerX.text()),
float(self.edit_centerY.text()),
float(self.edit_centerZ.text())],
'Unit': ['cm',
'cm',
'cm',
'cm',
'cm',
'cm']
}
results = {
'Result': [self.label_surface.text(),
self.label_volume.text()],
'Value': [float(self.label_res_surface.text()),
float(self.label_res_volume.text())],
'Unit': ['cm²',
'cm³']
}
elif shape == 'Cube':
# Create dictionaries containing square property data and calculation results
data = {
'Property': [self.label_edge.text(),
self.label_centerX.text(),
self.label_centerY.text(),
self.label_centerZ.text()],
'Value': [float(self.edit_edge.text()),
float(self.edit_centerX.text()),
float(self.edit_centerY.text()),
float(self.edit_centerZ.text())],
'Unit': ['cm',
'cm',
'cm',
'cm']
}
results = {
'Result': [self.label_surface.text(),
self.label_volume.text()],
'Value': [float(self.label_res_surface.text()),
float(self.label_res_volume.text())],
'Unit': ['cm²',
'cm³']
}
# Create Pandas DataFrames from the dictionaries
df = pd.DataFrame(data)
df_res = pd.DataFrame(results)
# Create an Excel writer object with the specified filename
writer = pd.ExcelWriter(file_name, engine='xlsxwriter')
# Create a workbook and worksheet within the Excel writer
workbook = writer.book
worksheet = workbook.add_worksheet(f"{shape} Calculation")
# Define a header format with background color and styling
header_format = workbook.add_format({
'bg_color': '#EAF1FF',
'bold': True,
'align': 'center',
'valign': 'vcenter',
'border': 1
})
# Determine starting rows for property and result data
property_start_row = 0
result_start_row = 8
# Write the circle property data to the Excel sheet
df.to_excel(writer, sheet_name=f"{shape} Calculation", startrow=property_start_row, startcol=0, index=False)
# Write the calculation results data to the Excel sheet with a starting row offset
df_res.to_excel(writer, sheet_name=f"{shape} Calculation", startrow=result_start_row, startcol=0, index=False)
# Write the column headers for both dataframes using the defined format
for col_idx, col in enumerate(df.columns):
worksheet.write(0, col_idx, col, header_format)
for col_idx, col in enumerate(df_res.columns):
worksheet.write(8, col_idx, col, header_format)
image_file = f".\\Results\\{shape}_plot.png"
image_cell = 'E2' # Adjust default image cell if needed
# Save the Matplotlib figure (assuming self.fig is a valid figure) as an image
self.fig.savefig(image_file)
# Insert the saved image into the worksheet at the specified cell
worksheet.insert_image(image_cell, image_file)
writer.close()
QMessageBox.information(self, 'Success', 'Data exported to Excel successfully.')
except Exception as e:
QMessageBox.warning(self, 'Error', f'An error occurred while exporting the data: {e}')
def clear_results_2D(self, sc):
"""Clears results and the plot.
This method clears the text in the output fields for area and perimeter,
and clears the plot. It also disables buttons related to results and exporting.
Args:
sc: The Matplotlib canvas object used for plotting.
"""
sc.axes.cla()
sc.draw()
self.label_res_area.setText("0.0")
self.label_res_perimeter.setText("0.0")
# Disable buttons
self.exportPictAction.setEnabled(False)
self.exportXlsxAction.setEnabled(False)
self.clearAction.setEnabled(False)
self.buttonPicture.setEnabled(False)
self.buttonExport.setEnabled(False)
self.buttonClear.setEnabled(False)
def clear_results_3D(self, sc):
"""Clears results and the plot.
This method clears the text in the output fields for surface and volume,
and clears the plot. It also disables buttons related to results and exporting.
Args:
sc: The Matplotlib canvas object used for plotting.
"""
sc.axes.cla()
sc.draw()
self.label_res_surface.setText("0.0")
self.label_res_volume.setText("0.0")
# Disable buttons
self.exportPictAction.setEnabled(False)
self.exportXlsxAction.setEnabled(False)
self.clearAction.setEnabled(False)
self.buttonPicture.setEnabled(False)
self.buttonExport.setEnabled(False)
self.buttonClear.setEnabled(False)