From 6183acf3070042a76e33407966c6adc6d3a03c22 Mon Sep 17 00:00:00 2001 From: Paul S Date: Tue, 6 Jul 2021 16:49:43 +0200 Subject: [PATCH] Enter numbers with commas --- changelog.md | 8 +- src/clsTableWidget.py | 10 +- src/main.py | 345 ++++++++++++++++++++---------------------- src/utils.py | 79 +++++++++- 4 files changed, 260 insertions(+), 182 deletions(-) diff --git a/changelog.md b/changelog.md index 580e931..3ae34b3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,13 @@ # Changelog GarageCalc1 +## [0.6] - 2021-07-06 +## Added +- Dezimalzahlen werden nun mit Dezimaltrennzeichen Komma (",") eingegeben statt Punkt (".") +- Export nach EXCEL exportiert nun Formeln für die Berechnung des Ergebnis, keine zuvor berechneten Ergebniswerte +- Export nach EXCEL: automatische Spaltenbreite + ## [0.5.2] - 2021-07-02 -## Changed +## Fixed - Verbesserungen in Sprachdatei "hu_HU.ts" ## [0.5.1] - 2021-07-02 diff --git a/src/clsTableWidget.py b/src/clsTableWidget.py index 15f362f..8af1e30 100644 --- a/src/clsTableWidget.py +++ b/src/clsTableWidget.py @@ -47,7 +47,6 @@ class TableWidget(QTableWidget): super().__init__() self.parent = parent - self.setParent(parent) # self.setSelectionMode(QAbstractItemView.ContiguousSelection) self.setSelectionMode(QAbstractItemView.SingleSelection) @@ -245,8 +244,7 @@ class TableWidget(QTableWidget): for idx in sel_idx: self.clipboard_data.append(idx.data()) - def item_del(self): - ask_cofirmation = True + def item_del(self, ask_cofirmation=True): sel_idx = self.selectionModel().selectedIndexes() for idx in sel_idx: row = idx.row() @@ -262,3 +260,9 @@ class TableWidget(QTableWidget): ask_cofirmation = False item.setData(Qt.DisplayRole, None) + + if len(sel_idx) == self.columnCount() * self.rowCount(): + try: + self.parent.is_modified = False # set parents modification flag (if present) + except AttributeError: + pass diff --git a/src/main.py b/src/main.py index 9d196d6..4b5d027 100644 --- a/src/main.py +++ b/src/main.py @@ -22,13 +22,13 @@ from PySide2.QtUiTools import QUiLoader import xlsxwriter # Local imports -from utils import show_about, resource_path +from utils import show_about, resource_path, str_iff, fit_col_widths # my own classes imports from clsTableWidget import TableWidget # Local globals -APP_VERSION = "v0.5.2" +APP_VERSION = "v0.6" DIR_APPDATA = os.getenv('LOCALAPPDATA') @@ -51,16 +51,21 @@ ICON_QUIT = "./img/system-shutdown.png" UI_MAIN = "./ui/main.ui" -COL_STUFF = 0 -COL_LENGTH = 1 -COL_WIDTH = 2 -COL_HEIGHT = 3 -COL_WEIGHT = 4 -COL_COMMENT = 5 - DEFAULT_GARAGE_LENGTH = "6" -DEFAULT_GARAGE_WIDTH = "2.5" -DEFAULT_GARAGE_HEIGHT = "2.5" +DEFAULT_GARAGE_WIDTH = "2,5" +DEFAULT_GARAGE_HEIGHT = "2,5" + +COL_GARAGE_LENGTH = 1 +COL_GARAGE_WIDTH = 2 +COL_GARAGE_HEIGHT = 3 +COL_GARAGE_COMMENT = 4 + +COL_STUFF_NAME = 0 +COL_STUFF_LENGTH = 1 +COL_STUFF_WIDTH = 2 +COL_STUFF_HEIGHT = 3 +COL_STUFF_WEIGHT = 4 +COL_STUFF_COMMENT = 5 TBL_STUFF_COL_COUNT = 6 TBL_STUFF_ROW_COUNT = 50 @@ -129,21 +134,21 @@ def main(): #################################################################### def create_examples(table): - from random import randint, choice + from random import randint, choice, uniform from string import ascii_letters, punctuation, digits min = 12 max = 15 string_format = ascii_letters - for row in range(table.rowCount()): + for row in range(10): generated_string1 = "".join(choice(string_format) for x in range(randint(min, max))) generated_string2 = "".join(choice(string_format) for x in range(randint(min, max))) table.setItem(row, 0, QTableWidgetItem(generated_string1)) - table.setItem(row, 1, QTableWidgetItem(str(randint(1, 10)))) - table.setItem(row, 2, QTableWidgetItem(str(randint(1, 10)))) - table.setItem(row, 3, QTableWidgetItem(str(randint(1, 10)))) - table.setItem(row, 4, QTableWidgetItem(str(randint(20, 100)))) + table.setItem(row, 1, QTableWidgetItem(str(round(uniform(1.1, 10.2), 2)).replace('.', ','))) + table.setItem(row, 2, QTableWidgetItem(str(round(uniform(1.5, 10.2), 2)).replace('.', ','))) + table.setItem(row, 3, QTableWidgetItem(str(round(uniform(1.125, 8.75), 2)).replace('.', ','))) + table.setItem(row, 4, QTableWidgetItem(str(round(uniform(20, 100), 2)).replace('.', ','))) table.setItem(row, 5, QTableWidgetItem(generated_string2)) #################################################################### @@ -169,11 +174,15 @@ class MainWindow(QMainWindow): self.calc_voluminae() self.ui.efWeight.setText(str("0")) self.retranslateUi() - self.ui.tableStuff.setFocus() # TODO: disable for PROD! - create_examples(self.ui.tableStuff) - self.ui.tableGarage.setItem(0, 3, QTableWidgetItem("Garázs az udvaron")) + # create_examples(self.ui.tableStuff) + # self.ui.tableGarage.setItem(0, 3, QTableWidgetItem("Garázs az udvaron")) + + for col in range(self.ui.tableStuff.columnCount()): # optimize column width + self.ui.tableStuff.resizeColumnToContents(col) + self.ui.tableStuff.resizeRowsToContents() # optimize row height + self.ui.tableStuff.setFocus() def create_menu(self, language=None): menuMain = self.menuBar() @@ -217,7 +226,7 @@ class MainWindow(QMainWindow): QCoreApplication.translate("main","Length") + " [m]", QCoreApplication.translate("main","Width") + " [m]", QCoreApplication.translate("main","Height") + " [m]", - QCoreApplication.translate("main","Weight") + " [m]", + QCoreApplication.translate("main","Weight") + " [kg]", QCoreApplication.translate("main","Comment") ]) @@ -249,7 +258,7 @@ class MainWindow(QMainWindow): # implement custom class 'TableWidget' layoutGb = self.ui.gbStuff.layout() - self.ui.tableStuff = TableWidget() + self.ui.tableStuff = TableWidget(self) self.ui.tableStuff.setColumnCount(TBL_STUFF_COL_COUNT) self.ui.tableStuff.setRowCount(TBL_STUFF_ROW_COUNT) self.ui.tableStuff.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) @@ -332,9 +341,10 @@ class MainWindow(QMainWindow): tblGarage.setRowCount(1) # clear stuff - # tblStuff.clear() - tblStuff.setRowCount(0) - tblStuff.setRowCount(TBL_STUFF_ROW_COUNT) + ## tblStuff.clear() + # tblStuff.setRowCount(0) + # tblStuff.setRowCount(TBL_STUFF_ROW_COUNT) + tblStuff.clearContents() # clear results self.ui.efVol_Garage.clear() @@ -368,7 +378,13 @@ class MainWindow(QMainWindow): if reply == QMessageBox.No: return False self.init_ui() - self.ui.tableStuff.sortByColumn(-1) # reset sorting + + self.ui.tableStuff.sortByColumn(-1, Qt.AscendingOrder) # reset sorting + + for col in range(self.ui.tableStuff.columnCount()): # optimize column width + self.ui.tableStuff.resizeColumnToContents(col) + self.ui.tableStuff.resizeRowsToContents() # optimize row height + self.ui.tableStuff.setFocus() def file_save(self): tblGarage = self.ui.tableGarage @@ -407,26 +423,14 @@ class MainWindow(QMainWindow): # get garage length if item_length: garage_length = item_length.text() - try: - garage_length = float(garage_length) - except ValueError: - garage_length = 0.0 # get garage width if item_width: garage_width = item_width.text() - try: - garage_width = float(garage_width) - except ValueError: - garage_width = 0.0 # get garage height if item_height: garage_height = item_height.text() - try: - garage_height = float(garage_height) - except ValueError: - garage_height = 0.0 # get garage comment if item_comment: @@ -444,44 +448,32 @@ class MainWindow(QMainWindow): weight = None comment = None - item_stuff = tblStuff.item(row, COL_STUFF) - item_length = tblStuff.item(row, COL_LENGTH) - item_width = tblStuff.item(row, COL_WIDTH) - item_height = tblStuff.item(row, COL_HEIGHT) - item_weight = tblStuff.item(row, COL_WEIGHT) - item_comment = tblStuff.item(row, COL_COMMENT) + item_stuff = tblStuff.item(row, COL_STUFF_NAME) + item_length = tblStuff.item(row, COL_STUFF_LENGTH) + item_width = tblStuff.item(row, COL_STUFF_WIDTH) + item_height = tblStuff.item(row, COL_STUFF_HEIGHT) + item_weight = tblStuff.item(row, COL_STUFF_WEIGHT) + item_comment = tblStuff.item(row, COL_STUFF_COMMENT) if item_stuff: stuff_text = item_stuff.text() if item_length: - try: - length = float(item_length.text()) - except ValueError: - length = None + length = item_length.text() if item_width: - try: - width = float(item_width.text()) - except ValueError: - width = None + width = item_width.text() if item_height: - try: - height = float(item_height.text()) - except ValueError: - height = None + height = item_height.text() if item_weight: - try: - weight = float(item_weight.text()) - except ValueError: - weight = None + weight = item_weight.text() if item_comment: comment = item_comment.text() - if item_stuff or item_length or item_width or item_height or item_weight: + if stuff_text or length or width or height or weight: writer.writerow([stuff_text, length, width, height, weight, comment]) is_file_saved = True @@ -515,7 +507,7 @@ class MainWindow(QMainWindow): options=options) if fileName: self.init_ui() - self.ui.tableStuff.sortByColumn(-1) # reset sorting + self.ui.tableStuff.sortByColumn(-1, Qt.AscendingOrder) # reset sorting file = open(fileName, "r", newline='') reader = csv.reader(file, delimiter=';') @@ -524,50 +516,50 @@ class MainWindow(QMainWindow): for row in reader: if row_idx == 0: # first row (index=0) ist always garage dimension try: - garage_length = row[1] - garage_width = row[2] - garage_height = row[3] - garage_comment = row[4] + garage_length = str(row[COL_GARAGE_LENGTH].replace(".", ",")) + garage_width = str(row[COL_GARAGE_WIDTH].replace(".", ",")) + garage_height = str(row[COL_GARAGE_HEIGHT].replace(".", ",")) + garage_comment = row[COL_GARAGE_COMMENT] - tblGarage.setItem(0, COL_LENGTH-1, QTableWidgetItem(garage_length)) - tblGarage.setItem(0, COL_WIDTH-1, QTableWidgetItem(garage_width)) - tblGarage.setItem(0, COL_HEIGHT-1, QTableWidgetItem(garage_height)) - tblGarage.setItem(0, COL_COMMENT-1, QTableWidgetItem(garage_comment)) + tblGarage.setItem(0, 0, QTableWidgetItem(garage_length)) + tblGarage.setItem(0, 1, QTableWidgetItem(garage_width)) + tblGarage.setItem(0, 2, QTableWidgetItem(garage_height)) + tblGarage.setItem(0, 3, QTableWidgetItem(garage_comment)) except IndexError as ex: pass if row_idx > 0: try: - stuff = row[0] - stuff_length = row[1] - stuff_width = row[2] - stuff_height = row[3] - stuff_weight = row[4] - stuff_comment = row[5] + stuff = row[COL_STUFF_NAME] + stuff_length = str(row[COL_STUFF_LENGTH].replace(".", ",")) + stuff_width = str(row[COL_STUFF_WIDTH].replace(".", ",")) + stuff_height = str(row[COL_STUFF_HEIGHT].replace(".", ",")) + stuff_weight = str(row[COL_STUFF_WEIGHT].replace(".", ",")) + stuff_comment = row[COL_STUFF_COMMENT] - tblStuff.setItem(row_idx - 1, COL_STUFF, QTableWidgetItem(stuff)) + tblStuff.setItem(row_idx - 1, COL_STUFF_NAME, QTableWidgetItem(stuff)) item = QTableWidgetItem() - item.setData(Qt.DisplayRole, float(stuff_length)) - tblStuff.setItem(row_idx - 1, COL_LENGTH, item) + item.setData(Qt.DisplayRole, stuff_length) + tblStuff.setItem(row_idx - 1, COL_STUFF_LENGTH, item) - # tblStuff.setItem(row_idx - 1, COL_WIDTH, QTableWidgetItem(float(stuff_width))) + # tblStuff.setItem(row_idx - 1, COL_STUFF_WIDTH, QTableWidgetItem(float(stuff_width))) item = QTableWidgetItem() - item.setData(Qt.DisplayRole, float(stuff_width)) - tblStuff.setItem(row_idx - 1, COL_WIDTH, item) + item.setData(Qt.DisplayRole, stuff_width) + tblStuff.setItem(row_idx - 1, COL_STUFF_WIDTH, item) - #tblStuff.setItem(row_idx - 1, COL_HEIGHT, QTableWidgetItem(float(stuff_height))) + #tblStuff.setItem(row_idx - 1, COL_STUFF_HEIGHT, QTableWidgetItem(float(stuff_height))) item = QTableWidgetItem(stuff_height) - item.setData(Qt.DisplayRole, float(stuff_height)) - tblStuff.setItem(row_idx - 1, COL_HEIGHT, item) + item.setData(Qt.DisplayRole, stuff_height) + tblStuff.setItem(row_idx - 1, COL_STUFF_HEIGHT, item) - # tblStuff.setItem(row_idx - 1, COL_WEIGHT, QTableWidgetItem(float(stuff_weight))) + # tblStuff.setItem(row_idx - 1, COL_STUFF_WEIGHT, QTableWidgetItem(float(stuff_weight))) item = QTableWidgetItem(stuff_weight) - item.setData(Qt.DisplayRole, float(stuff_weight)) - tblStuff.setItem(row_idx - 1, COL_WEIGHT, item) + item.setData(Qt.DisplayRole, stuff_weight) + tblStuff.setItem(row_idx - 1, COL_STUFF_WEIGHT, item) - tblStuff.setItem(row_idx - 1, COL_COMMENT, QTableWidgetItem(stuff_comment)) + tblStuff.setItem(row_idx - 1, COL_STUFF_COMMENT, QTableWidgetItem(stuff_comment)) except IndexError as ex: pass @@ -575,16 +567,19 @@ class MainWindow(QMainWindow): row_idx += 1 tblStuff.setRowCount(TBL_STUFF_ROW_COUNT) - # optimize row height - tblStuff.resizeRowsToContents() - self.is_modified = False - self.opened_file = os.path.basename(fileName) + for col in range(self.ui.tableStuff.columnCount()): # optimize column width + self.ui.tableStuff.resizeColumnToContents(col) + self.ui.tableStuff.resizeRowsToContents() # optimize row height + self.ui.tableStuff.setFocus() - if fileName: - self.setWindowTitle(self.opened_file) - else: - self.setWindowTitle(f"{qApp.applicationName()} {APP_VERSION} - {APP_AUTHOR}") + self.is_modified = False + self.opened_file = os.path.basename(fileName) + + if fileName: + self.setWindowTitle(self.opened_file) + else: + self.setWindowTitle(f"{qApp.applicationName()} {APP_VERSION} - {APP_AUTHOR}") def file_export(self): tblGarage = self.ui.tableGarage @@ -604,14 +599,14 @@ class MainWindow(QMainWindow): # write col header start_row = 0 - worksheet.write(start_row, 0, QCoreApplication.translate("main", "Dimension of the garage")) + worksheet.write_string(start_row, 0, QCoreApplication.translate("main", "Dimension of the garage")) start_row = 1 - worksheet.write(start_row, COL_LENGTH, QCoreApplication.translate("main","Length") + " [m]") - worksheet.write(start_row, COL_WIDTH, QCoreApplication.translate("main","Width") + " [m]") - worksheet.write(start_row, COL_HEIGHT, QCoreApplication.translate("main","Height") + " [m]") - worksheet.write(start_row, COL_COMMENT, QCoreApplication.translate("main","Comment")) + worksheet.write_string(start_row, COL_STUFF_LENGTH, QCoreApplication.translate("main","Length") + " [m]") + worksheet.write_string(start_row, COL_STUFF_WIDTH, QCoreApplication.translate("main","Width") + " [m]") + worksheet.write_string(start_row, COL_STUFF_HEIGHT, QCoreApplication.translate("main","Height") + " [m]") + worksheet.write_string(start_row, COL_STUFF_COMMENT, QCoreApplication.translate("main","Comment")) worksheet.set_column(0, 0, 25) worksheet.set_column(1, 3, 10) worksheet.set_column(4, 5, 20) @@ -619,120 +614,110 @@ class MainWindow(QMainWindow): start_row = 2 # loop over table Garage for row in range(tblGarage.rowCount()): - # get garage length garage_length = tblGarage.item(0, 0).text() - try: - garage_length = float(garage_length) - except ValueError: - garage_length = 0.0 - - # get garage width garage_width = tblGarage.item(0, 1).text() - try: - garage_width = float(garage_width) - except ValueError: - garage_width = 0.0 - - # get garage height garage_height = tblGarage.item(0, 2).text() - try: - garage_height = float(garage_height) - except ValueError: - garage_height = 0.0 - - # get garage comment garage_comment = tblGarage.item(0, 3).text() - worksheet.write(start_row + row, COL_LENGTH, garage_length) - worksheet.write(start_row + row, COL_WIDTH, garage_width) - worksheet.write(start_row + row, COL_HEIGHT, garage_height) - worksheet.write(start_row + row, COL_COMMENT, garage_comment) + worksheet.write_number(start_row + row, COL_STUFF_LENGTH, float(garage_length.replace(',', '.'))) + worksheet.write_number(start_row + row, COL_STUFF_WIDTH, float(garage_width.replace(',', '.'))) + worksheet.write_number(start_row + row, COL_STUFF_HEIGHT, float(garage_height.replace(',', '.'))) + worksheet.write_string(start_row + row, COL_STUFF_COMMENT, garage_comment) start_row = 4 - worksheet.write(start_row, 0, QCoreApplication.translate("main", "Dimensions of the objects to be stored")) + worksheet.write_string(start_row, 0, QCoreApplication.translate("main", "Dimensions of the objects to be stored")) start_row = 5 - worksheet.write(start_row, COL_LENGTH, QCoreApplication.translate("main","Length") + " [m]") - worksheet.write(start_row, COL_WIDTH, QCoreApplication.translate("main","Width") + " [m]") - worksheet.write(start_row, COL_HEIGHT, QCoreApplication.translate("main","Height") + " [m]") - worksheet.write(start_row, COL_WEIGHT, QCoreApplication.translate("main","Weight") + " [kg]") - worksheet.write(start_row, COL_COMMENT, QCoreApplication.translate("main","Comment") ) + worksheet.write_string(start_row, COL_STUFF_LENGTH, QCoreApplication.translate("main","Length") + " [m]") + worksheet.write_string(start_row, COL_STUFF_WIDTH, QCoreApplication.translate("main","Width") + " [m]") + worksheet.write_string(start_row, COL_STUFF_HEIGHT, QCoreApplication.translate("main","Height") + " [m]") + worksheet.write_string(start_row, COL_STUFF_WEIGHT, QCoreApplication.translate("main","Weight") + " [kg]") + worksheet.write_string(start_row, COL_STUFF_COMMENT, QCoreApplication.translate("main","Comment") ) start_row = 6 # loop over table Stuff + formula = "" row_idx = start_row for row in range(tblStuff.rowCount()): - item_stuff = tblStuff.item(row, COL_STUFF) - item_length = tblStuff.item(row, COL_LENGTH) - item_width = tblStuff.item(row, COL_WIDTH) - item_height = tblStuff.item(row, COL_HEIGHT) - item_weight = tblStuff.item(row, COL_WEIGHT) - item_comment = tblStuff.item(row, COL_COMMENT) + item_stuff = tblStuff.item(row, COL_STUFF_NAME) + item_length = tblStuff.item(row, COL_STUFF_LENGTH) + item_width = tblStuff.item(row, COL_STUFF_WIDTH) + item_height = tblStuff.item(row, COL_STUFF_HEIGHT) + item_weight = tblStuff.item(row, COL_STUFF_WEIGHT) + item_comment = tblStuff.item(row, COL_STUFF_COMMENT) if item_stuff: stuff_text = item_stuff.text() if len(stuff_text)>0: - worksheet.write(start_row + row, COL_STUFF, stuff_text) + worksheet.write_string(start_row + row, COL_STUFF_NAME, stuff_text) if item_length: try: - length = float(item_length.text()) + length = item_length.text() if length: - worksheet.write(start_row + row, COL_LENGTH, length) + worksheet.write_number(start_row + row, COL_STUFF_LENGTH, float(length.replace(',', '.'))) except ValueError: pass if item_width: try: - width = float(item_width.text()) + width = item_width.text() if width: - worksheet.write(start_row + row, COL_WIDTH, width) + worksheet.write_number(start_row + row, COL_STUFF_WIDTH, float(width.replace(',', '.'))) except ValueError: pass if item_height: try: - height = float(item_height.text()) + height = item_height.text() if height: - worksheet.write(start_row + row, COL_HEIGHT, height) + worksheet.write_number(start_row + row, COL_STUFF_HEIGHT, float(height.replace(',', '.'))) except ValueError: pass if item_weight: try: - weight = float(item_weight.text()) + weight = item_weight.text() if weight: - worksheet.write(start_row + row, COL_WEIGHT, weight) + worksheet.write_number(start_row + row, COL_STUFF_WEIGHT, float(weight.replace(',', '.'))) except ValueError: pass if item_comment: comment = item_comment.text() if len(comment)>0: - worksheet.write(start_row + row, COL_COMMENT, comment) + worksheet.write_string(start_row + row, COL_STUFF_COMMENT, comment) if item_stuff or item_length or item_width or item_height or item_weight: row_idx += 1 + formula = formula + str_iff(formula, "", "=ROUND(", " + ") + f"(B{row_idx} * C{row_idx} * D{row_idx})" + + formula += ", 2)" row_idx += 1 # loop over Results - worksheet.write(row_idx, 0, QCoreApplication.translate("main","Result")) + worksheet.write_string(row_idx, 0, QCoreApplication.translate("main","Result")) row_idx += 1 - worksheet.write(row_idx, 0, QCoreApplication.translate("main","Volume of the garage") + ":") - worksheet.write(row_idx, 1, float(self.ui.efVol_Garage.text())) + worksheet.write_string(row_idx, 0, QCoreApplication.translate("main","Volume of the garage") + ":") + # worksheet.write(row_idx, 1, self.ui.efVol_Garage.text()) + worksheet.write_formula(row_idx, 1, '=B3 * C3 * D3') + row_idx += 1 + worksheet.write_string(row_idx, 0, QCoreApplication.translate("main","Volume of the items") + ":") + #worksheet.write_number(row_idx, 1, float(self.ui.efVol_Stuff.text().replace(',', '.'))) + worksheet.write_formula(row_idx, 1, formula) row_idx += 1 - worksheet.write(row_idx, 0, QCoreApplication.translate("main","Volume of the items") + ":") - worksheet.write(row_idx, 1, float(self.ui.efVol_Stuff.text())) + worksheet.write_string(row_idx, 0, QCoreApplication.translate("main","Free space in the garage") + ":") + # worksheet.write(row_idx, 1, self.ui.efVol_Free.text()) + worksheet.write_formula(row_idx, 1, f"=B{row_idx-1}-B{row_idx}") row_idx += 1 - worksheet.write(row_idx, 0, QCoreApplication.translate("main","Free space in the garage") + ":") - worksheet.write(row_idx, 1, float(self.ui.efVol_Free.text())) + worksheet.write_string(row_idx, 0, QCoreApplication.translate("main", "Total weight") + ":") + # worksheet.write(row_idx, 1, self.ui.efWeight.text()) + worksheet.write_formula(row_idx, 1, f"=SUM(E7:E{row_idx-5}") - row_idx += 1 - worksheet.write(row_idx, 0, QCoreApplication.translate("main", "Total weight") + ":") - worksheet.write(row_idx, 1, float(self.ui.efWeight.text())) + fit_col_widths(tblStuff, worksheet) workbook.close() msg = QCoreApplication.translate("main", "Successfully exported to EXCEL") + " :-)" @@ -760,7 +745,7 @@ class MainWindow(QMainWindow): garage_length = tblGarage.item(0, 0).text() if garage_length: try: - garage_length = float(garage_length) + garage_length = float(garage_length.replace(',', '.')) except ValueError: garage_length = 0.0 is_error = True @@ -770,7 +755,7 @@ class MainWindow(QMainWindow): garage_width = tblGarage.item(0, 1).text() if garage_width: try: - garage_width = float(garage_width) + garage_width = float(garage_width.replace(',', '.')) except ValueError: garage_width = 0.0 is_error = True @@ -780,13 +765,13 @@ class MainWindow(QMainWindow): garage_height = tblGarage.item(0, 2).text() if garage_height: try: - garage_height = float(garage_height) + garage_height = float(garage_height.replace(',', '.')) except ValueError: garage_height = 0.0 is_error = True if not is_error: - garage_vol = round(float(garage_length) * float(garage_width) * float(garage_height), 2) + garage_vol = round(garage_length * garage_width * garage_height, 2) else: msg = QCoreApplication.translate("main", "Error in the garage dimension") + " :-(" self.statusBar.showMessage(msg, 5000) @@ -810,34 +795,34 @@ class MainWindow(QMainWindow): length = 0 width = 0 height = 0 - item_length = tblStuff.item(row, COL_LENGTH) - item_width = tblStuff.item(row, COL_WIDTH) - item_height = tblStuff.item(row, COL_HEIGHT) + item_length = tblStuff.item(row, COL_STUFF_LENGTH) + item_width = tblStuff.item(row, COL_STUFF_WIDTH) + item_height = tblStuff.item(row, COL_STUFF_HEIGHT) if item_length: try: - length = float(item_length.text()) + length = float(item_length.text().replace(',', '.')) except ValueError: length = 0 is_error = True if item_width: try: - width = float(item_width.text()) + width = float(item_width.text().replace(',', '.')) except ValueError: width = 0 is_error = True if item_height: try: - height = float(item_height.text()) + height = float(item_height.text().replace(',', '.')) except ValueError: height = 0 is_error = True - vol = round(length * width * height, 2) + vol = length * width * height - stuff_vol = stuff_vol + vol + stuff_vol = round(stuff_vol + vol, 2) if is_error: # stuff_vol = 0.0 @@ -851,16 +836,22 @@ class MainWindow(QMainWindow): # get garage vol garage_vol = self.get_garage_vol() - + # print(f"{garage_vol=}") + # get stuff vol stuff_vol = self.get_stuff_vol() + # print(f"{stuff_vol=}") + + # get free space + free_vol = garage_vol - stuff_vol + # print(f"{free_vol=}") # display results - self.ui.efVol_Garage.setText(f"{garage_vol:2.2f}") - self.ui.efVol_Stuff.setText(f"{stuff_vol:2.2f}") - self.ui.efVol_Free.setText(f"{garage_vol - stuff_vol:2.2f}") + self.ui.efVol_Garage.setText(f"{str(garage_vol).replace('.', ',')}") + self.ui.efVol_Stuff.setText(f"{str(stuff_vol).replace('.', ',')}") + self.ui.efVol_Free.setText(f"{str(free_vol).replace('.', ',')}") - if ( garage_vol - stuff_vol ) < 0: + if free_vol < 0: self.ui.efVol_Free.setStyleSheet("border: 1px solid red;") else: self.ui.efVol_Free.setStyleSheet("") @@ -874,16 +865,16 @@ class MainWindow(QMainWindow): for row in range(row_count): weight = 0.0 - item = tblStuff.item(row, COL_WEIGHT) + item = tblStuff.item(row, COL_STUFF_WEIGHT) if item: try: - weight = float(item.text()) + weight = float(str(item.text()).replace(',','.')) except ValueError: weight = 0.0 weight_sum = round(weight_sum + weight, 2) - self.ui.efWeight.setText(f"{weight_sum:2.2f}") + self.ui.efWeight.setText(f"{str(weight_sum).replace('.', ',')}") def store_selected_language(self, menu): """ Stores selected menu labels to ini-file. """ diff --git a/src/utils.py b/src/utils.py index a6d03e5..c4f9ae8 100644 --- a/src/utils.py +++ b/src/utils.py @@ -13,12 +13,14 @@ import os from PySide2.QtWidgets import QMessageBox, QApplication, QMainWindow, QTableWidgetItem, QStatusBar, QAction from PySide2 import QtCore from PySide2.QtGui import QIcon, QPixmap -from PySide2.QtCore import QFile, QSize +from PySide2.QtCore import QFile, QSize, Qt from PySide2.QtUiTools import QUiLoader # local globals APP_ICON = "./img/icons8-garage-32.ico" +STD_COL_WIDTH: int = 7 + def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: @@ -52,3 +54,78 @@ def show_about(): msg.setStandardButtons(QMessageBox.Ok) msg.exec_() + +def str_iff(input: str, output1: str, output2: str, output3: str) -> str: + """ Return outputs based on length of given input """ + str_ret = "" + str_length = len(input) + + if str_length < 0: + str_ret = output1 + + if str_length == 0: + str_ret = output2 + + if str_length > 0: + str_ret = output3 + + return str_ret + +def fit_col_widths(table, worksheet): + try: + model = table.model().sourceModel() + except AttributeError: + model = table.model() + + max_col_lengths = [] + dict_max_col_length = {} + sel_indexes = table.selectedIndexes() + sel_rows_idx = table.selectionModel().selectedRows() + sel_rows = [] + for sel_index in sel_rows_idx: + sel_rows.append(sel_index.row()) + + for col_ind in range(model.columnCount()): + cur_item_length = 0 + lengths = [] + + # get column header + row = 0 + col_header_text = str(model.headerData(col_ind, Qt.Horizontal)) + if not table.isColumnHidden(col_ind): # export only visible column headers + cur_item_length = len(col_header_text) + lengths.append(cur_item_length) + + # get columns data lengths + row = 1 + # iterate over all rows + for row_ind in range(model.rowCount()): + if row_ind in sel_rows: + item = model.item(row_ind, col_ind) + if item: + cur_item_text = item.text() + cur_item_length = len(cur_item_text) + else: + cur_item_length = 0 + + if not table.isColumnHidden(col_ind): # export only visible column headers + lengths.append(cur_item_length) + + if not table.isColumnHidden(col_ind): # export only visible column headers + dict_max_col_length[col_ind] = lengths + + col_ind = 0 + for col in dict_max_col_length: + lengths = dict_max_col_length[col] + col_width = max(lengths) + + ## special: enlage first col + if col == 0: + col_width = 20 + # debug info + # print(f"worksheet.set_column({col_ind} -> {col_width})") + # worksheet.write(29, col_ind, col_width) + ## debug info + if col_width > STD_COL_WIDTH: # anticipated default size + worksheet.set_column(col_ind, col_ind, col_width*1.25) + col_ind += 1