Enter numbers with commas

This commit is contained in:
Paul S 2021-07-06 16:49:43 +02:00
parent f1123a6fd5
commit 6183acf307
4 changed files with 260 additions and 182 deletions

View file

@ -1,7 +1,13 @@
# Changelog GarageCalc1 # 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 ## [0.5.2] - 2021-07-02
## Changed ## Fixed
- Verbesserungen in Sprachdatei "hu_HU.ts" - Verbesserungen in Sprachdatei "hu_HU.ts"
## [0.5.1] - 2021-07-02 ## [0.5.1] - 2021-07-02

View file

@ -47,7 +47,6 @@ class TableWidget(QTableWidget):
super().__init__() super().__init__()
self.parent = parent self.parent = parent
self.setParent(parent)
# self.setSelectionMode(QAbstractItemView.ContiguousSelection) # self.setSelectionMode(QAbstractItemView.ContiguousSelection)
self.setSelectionMode(QAbstractItemView.SingleSelection) self.setSelectionMode(QAbstractItemView.SingleSelection)
@ -245,8 +244,7 @@ class TableWidget(QTableWidget):
for idx in sel_idx: for idx in sel_idx:
self.clipboard_data.append(idx.data()) self.clipboard_data.append(idx.data())
def item_del(self): def item_del(self, ask_cofirmation=True):
ask_cofirmation = True
sel_idx = self.selectionModel().selectedIndexes() sel_idx = self.selectionModel().selectedIndexes()
for idx in sel_idx: for idx in sel_idx:
row = idx.row() row = idx.row()
@ -262,3 +260,9 @@ class TableWidget(QTableWidget):
ask_cofirmation = False ask_cofirmation = False
item.setData(Qt.DisplayRole, None) 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

View file

@ -22,13 +22,13 @@ from PySide2.QtUiTools import QUiLoader
import xlsxwriter import xlsxwriter
# Local imports # 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 # my own classes imports
from clsTableWidget import TableWidget from clsTableWidget import TableWidget
# Local globals # Local globals
APP_VERSION = "v0.5.2" APP_VERSION = "v0.6"
DIR_APPDATA = os.getenv('LOCALAPPDATA') DIR_APPDATA = os.getenv('LOCALAPPDATA')
@ -51,16 +51,21 @@ ICON_QUIT = "./img/system-shutdown.png"
UI_MAIN = "./ui/main.ui" 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_LENGTH = "6"
DEFAULT_GARAGE_WIDTH = "2.5" DEFAULT_GARAGE_WIDTH = "2,5"
DEFAULT_GARAGE_HEIGHT = "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_COL_COUNT = 6
TBL_STUFF_ROW_COUNT = 50 TBL_STUFF_ROW_COUNT = 50
@ -129,21 +134,21 @@ def main():
#################################################################### ####################################################################
def create_examples(table): def create_examples(table):
from random import randint, choice from random import randint, choice, uniform
from string import ascii_letters, punctuation, digits from string import ascii_letters, punctuation, digits
min = 12 min = 12
max = 15 max = 15
string_format = ascii_letters 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_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))) generated_string2 = "".join(choice(string_format) for x in range(randint(min, max)))
table.setItem(row, 0, QTableWidgetItem(generated_string1)) table.setItem(row, 0, QTableWidgetItem(generated_string1))
table.setItem(row, 1, QTableWidgetItem(str(randint(1, 10)))) table.setItem(row, 1, QTableWidgetItem(str(round(uniform(1.1, 10.2), 2)).replace('.', ',')))
table.setItem(row, 2, QTableWidgetItem(str(randint(1, 10)))) table.setItem(row, 2, QTableWidgetItem(str(round(uniform(1.5, 10.2), 2)).replace('.', ',')))
table.setItem(row, 3, QTableWidgetItem(str(randint(1, 10)))) table.setItem(row, 3, QTableWidgetItem(str(round(uniform(1.125, 8.75), 2)).replace('.', ',')))
table.setItem(row, 4, QTableWidgetItem(str(randint(20, 100)))) table.setItem(row, 4, QTableWidgetItem(str(round(uniform(20, 100), 2)).replace('.', ',')))
table.setItem(row, 5, QTableWidgetItem(generated_string2)) table.setItem(row, 5, QTableWidgetItem(generated_string2))
#################################################################### ####################################################################
@ -169,11 +174,15 @@ class MainWindow(QMainWindow):
self.calc_voluminae() self.calc_voluminae()
self.ui.efWeight.setText(str("0")) self.ui.efWeight.setText(str("0"))
self.retranslateUi() self.retranslateUi()
self.ui.tableStuff.setFocus()
# TODO: disable for PROD! # TODO: disable for PROD!
create_examples(self.ui.tableStuff) # create_examples(self.ui.tableStuff)
self.ui.tableGarage.setItem(0, 3, QTableWidgetItem("Garázs az udvaron")) # 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): def create_menu(self, language=None):
menuMain = self.menuBar() menuMain = self.menuBar()
@ -217,7 +226,7 @@ class MainWindow(QMainWindow):
QCoreApplication.translate("main","Length") + " [m]", QCoreApplication.translate("main","Length") + " [m]",
QCoreApplication.translate("main","Width") + " [m]", QCoreApplication.translate("main","Width") + " [m]",
QCoreApplication.translate("main","Height") + " [m]", QCoreApplication.translate("main","Height") + " [m]",
QCoreApplication.translate("main","Weight") + " [m]", QCoreApplication.translate("main","Weight") + " [kg]",
QCoreApplication.translate("main","Comment") QCoreApplication.translate("main","Comment")
]) ])
@ -249,7 +258,7 @@ class MainWindow(QMainWindow):
# implement custom class 'TableWidget' # implement custom class 'TableWidget'
layoutGb = self.ui.gbStuff.layout() 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.setColumnCount(TBL_STUFF_COL_COUNT)
self.ui.tableStuff.setRowCount(TBL_STUFF_ROW_COUNT) self.ui.tableStuff.setRowCount(TBL_STUFF_ROW_COUNT)
self.ui.tableStuff.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.ui.tableStuff.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
@ -332,9 +341,10 @@ class MainWindow(QMainWindow):
tblGarage.setRowCount(1) tblGarage.setRowCount(1)
# clear stuff # clear stuff
# tblStuff.clear() ## tblStuff.clear()
tblStuff.setRowCount(0) # tblStuff.setRowCount(0)
tblStuff.setRowCount(TBL_STUFF_ROW_COUNT) # tblStuff.setRowCount(TBL_STUFF_ROW_COUNT)
tblStuff.clearContents()
# clear results # clear results
self.ui.efVol_Garage.clear() self.ui.efVol_Garage.clear()
@ -368,7 +378,13 @@ class MainWindow(QMainWindow):
if reply == QMessageBox.No: if reply == QMessageBox.No:
return False return False
self.init_ui() 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): def file_save(self):
tblGarage = self.ui.tableGarage tblGarage = self.ui.tableGarage
@ -407,26 +423,14 @@ class MainWindow(QMainWindow):
# get garage length # get garage length
if item_length: if item_length:
garage_length = item_length.text() garage_length = item_length.text()
try:
garage_length = float(garage_length)
except ValueError:
garage_length = 0.0
# get garage width # get garage width
if item_width: if item_width:
garage_width = item_width.text() garage_width = item_width.text()
try:
garage_width = float(garage_width)
except ValueError:
garage_width = 0.0
# get garage height # get garage height
if item_height: if item_height:
garage_height = item_height.text() garage_height = item_height.text()
try:
garage_height = float(garage_height)
except ValueError:
garage_height = 0.0
# get garage comment # get garage comment
if item_comment: if item_comment:
@ -444,44 +448,32 @@ class MainWindow(QMainWindow):
weight = None weight = None
comment = None comment = None
item_stuff = tblStuff.item(row, COL_STUFF) item_stuff = tblStuff.item(row, COL_STUFF_NAME)
item_length = tblStuff.item(row, COL_LENGTH) item_length = tblStuff.item(row, COL_STUFF_LENGTH)
item_width = tblStuff.item(row, COL_WIDTH) item_width = tblStuff.item(row, COL_STUFF_WIDTH)
item_height = tblStuff.item(row, COL_HEIGHT) item_height = tblStuff.item(row, COL_STUFF_HEIGHT)
item_weight = tblStuff.item(row, COL_WEIGHT) item_weight = tblStuff.item(row, COL_STUFF_WEIGHT)
item_comment = tblStuff.item(row, COL_COMMENT) item_comment = tblStuff.item(row, COL_STUFF_COMMENT)
if item_stuff: if item_stuff:
stuff_text = item_stuff.text() stuff_text = item_stuff.text()
if item_length: if item_length:
try: length = item_length.text()
length = float(item_length.text())
except ValueError:
length = None
if item_width: if item_width:
try: width = item_width.text()
width = float(item_width.text())
except ValueError:
width = None
if item_height: if item_height:
try: height = item_height.text()
height = float(item_height.text())
except ValueError:
height = None
if item_weight: if item_weight:
try: weight = item_weight.text()
weight = float(item_weight.text())
except ValueError:
weight = None
if item_comment: if item_comment:
comment = item_comment.text() 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]) writer.writerow([stuff_text, length, width, height, weight, comment])
is_file_saved = True is_file_saved = True
@ -515,7 +507,7 @@ class MainWindow(QMainWindow):
options=options) options=options)
if fileName: if fileName:
self.init_ui() self.init_ui()
self.ui.tableStuff.sortByColumn(-1) # reset sorting self.ui.tableStuff.sortByColumn(-1, Qt.AscendingOrder) # reset sorting
file = open(fileName, "r", newline='') file = open(fileName, "r", newline='')
reader = csv.reader(file, delimiter=';') reader = csv.reader(file, delimiter=';')
@ -524,50 +516,50 @@ class MainWindow(QMainWindow):
for row in reader: for row in reader:
if row_idx == 0: # first row (index=0) ist always garage dimension if row_idx == 0: # first row (index=0) ist always garage dimension
try: try:
garage_length = row[1] garage_length = str(row[COL_GARAGE_LENGTH].replace(".", ","))
garage_width = row[2] garage_width = str(row[COL_GARAGE_WIDTH].replace(".", ","))
garage_height = row[3] garage_height = str(row[COL_GARAGE_HEIGHT].replace(".", ","))
garage_comment = row[4] garage_comment = row[COL_GARAGE_COMMENT]
tblGarage.setItem(0, COL_LENGTH-1, QTableWidgetItem(garage_length)) tblGarage.setItem(0, 0, QTableWidgetItem(garage_length))
tblGarage.setItem(0, COL_WIDTH-1, QTableWidgetItem(garage_width)) tblGarage.setItem(0, 1, QTableWidgetItem(garage_width))
tblGarage.setItem(0, COL_HEIGHT-1, QTableWidgetItem(garage_height)) tblGarage.setItem(0, 2, QTableWidgetItem(garage_height))
tblGarage.setItem(0, COL_COMMENT-1, QTableWidgetItem(garage_comment)) tblGarage.setItem(0, 3, QTableWidgetItem(garage_comment))
except IndexError as ex: except IndexError as ex:
pass pass
if row_idx > 0: if row_idx > 0:
try: try:
stuff = row[0] stuff = row[COL_STUFF_NAME]
stuff_length = row[1] stuff_length = str(row[COL_STUFF_LENGTH].replace(".", ","))
stuff_width = row[2] stuff_width = str(row[COL_STUFF_WIDTH].replace(".", ","))
stuff_height = row[3] stuff_height = str(row[COL_STUFF_HEIGHT].replace(".", ","))
stuff_weight = row[4] stuff_weight = str(row[COL_STUFF_WEIGHT].replace(".", ","))
stuff_comment = row[5] 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 = QTableWidgetItem()
item.setData(Qt.DisplayRole, float(stuff_length)) item.setData(Qt.DisplayRole, stuff_length)
tblStuff.setItem(row_idx - 1, COL_LENGTH, item) 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 = QTableWidgetItem()
item.setData(Qt.DisplayRole, float(stuff_width)) item.setData(Qt.DisplayRole, stuff_width)
tblStuff.setItem(row_idx - 1, COL_WIDTH, item) 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 = QTableWidgetItem(stuff_height)
item.setData(Qt.DisplayRole, float(stuff_height)) item.setData(Qt.DisplayRole, stuff_height)
tblStuff.setItem(row_idx - 1, COL_HEIGHT, item) 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 = QTableWidgetItem(stuff_weight)
item.setData(Qt.DisplayRole, float(stuff_weight)) item.setData(Qt.DisplayRole, stuff_weight)
tblStuff.setItem(row_idx - 1, COL_WEIGHT, item) 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: except IndexError as ex:
pass pass
@ -575,8 +567,11 @@ class MainWindow(QMainWindow):
row_idx += 1 row_idx += 1
tblStuff.setRowCount(TBL_STUFF_ROW_COUNT) tblStuff.setRowCount(TBL_STUFF_ROW_COUNT)
# optimize row height
tblStuff.resizeRowsToContents() 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()
self.is_modified = False self.is_modified = False
self.opened_file = os.path.basename(fileName) self.opened_file = os.path.basename(fileName)
@ -604,14 +599,14 @@ class MainWindow(QMainWindow):
# write col header # write col header
start_row = 0 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 start_row = 1
worksheet.write(start_row, COL_LENGTH, QCoreApplication.translate("main","Length") + " [m]") worksheet.write_string(start_row, COL_STUFF_LENGTH, QCoreApplication.translate("main","Length") + " [m]")
worksheet.write(start_row, COL_WIDTH, QCoreApplication.translate("main","Width") + " [m]") worksheet.write_string(start_row, COL_STUFF_WIDTH, QCoreApplication.translate("main","Width") + " [m]")
worksheet.write(start_row, COL_HEIGHT, QCoreApplication.translate("main","Height") + " [m]") worksheet.write_string(start_row, COL_STUFF_HEIGHT, QCoreApplication.translate("main","Height") + " [m]")
worksheet.write(start_row, COL_COMMENT, QCoreApplication.translate("main","Comment")) worksheet.write_string(start_row, COL_STUFF_COMMENT, QCoreApplication.translate("main","Comment"))
worksheet.set_column(0, 0, 25) worksheet.set_column(0, 0, 25)
worksheet.set_column(1, 3, 10) worksheet.set_column(1, 3, 10)
worksheet.set_column(4, 5, 20) worksheet.set_column(4, 5, 20)
@ -619,120 +614,110 @@ class MainWindow(QMainWindow):
start_row = 2 start_row = 2
# loop over table Garage # loop over table Garage
for row in range(tblGarage.rowCount()): for row in range(tblGarage.rowCount()):
# get garage length
garage_length = tblGarage.item(0, 0).text() 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() 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() 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() garage_comment = tblGarage.item(0, 3).text()
worksheet.write(start_row + row, COL_LENGTH, garage_length) worksheet.write_number(start_row + row, COL_STUFF_LENGTH, float(garage_length.replace(',', '.')))
worksheet.write(start_row + row, COL_WIDTH, garage_width) worksheet.write_number(start_row + row, COL_STUFF_WIDTH, float(garage_width.replace(',', '.')))
worksheet.write(start_row + row, COL_HEIGHT, garage_height) worksheet.write_number(start_row + row, COL_STUFF_HEIGHT, float(garage_height.replace(',', '.')))
worksheet.write(start_row + row, COL_COMMENT, garage_comment) worksheet.write_string(start_row + row, COL_STUFF_COMMENT, garage_comment)
start_row = 4 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 start_row = 5
worksheet.write(start_row, COL_LENGTH, QCoreApplication.translate("main","Length") + " [m]") worksheet.write_string(start_row, COL_STUFF_LENGTH, QCoreApplication.translate("main","Length") + " [m]")
worksheet.write(start_row, COL_WIDTH, QCoreApplication.translate("main","Width") + " [m]") worksheet.write_string(start_row, COL_STUFF_WIDTH, QCoreApplication.translate("main","Width") + " [m]")
worksheet.write(start_row, COL_HEIGHT, QCoreApplication.translate("main","Height") + " [m]") worksheet.write_string(start_row, COL_STUFF_HEIGHT, QCoreApplication.translate("main","Height") + " [m]")
worksheet.write(start_row, COL_WEIGHT, QCoreApplication.translate("main","Weight") + " [kg]") worksheet.write_string(start_row, COL_STUFF_WEIGHT, QCoreApplication.translate("main","Weight") + " [kg]")
worksheet.write(start_row, COL_COMMENT, QCoreApplication.translate("main","Comment") ) worksheet.write_string(start_row, COL_STUFF_COMMENT, QCoreApplication.translate("main","Comment") )
start_row = 6 start_row = 6
# loop over table Stuff # loop over table Stuff
formula = ""
row_idx = start_row row_idx = start_row
for row in range(tblStuff.rowCount()): for row in range(tblStuff.rowCount()):
item_stuff = tblStuff.item(row, COL_STUFF) item_stuff = tblStuff.item(row, COL_STUFF_NAME)
item_length = tblStuff.item(row, COL_LENGTH) item_length = tblStuff.item(row, COL_STUFF_LENGTH)
item_width = tblStuff.item(row, COL_WIDTH) item_width = tblStuff.item(row, COL_STUFF_WIDTH)
item_height = tblStuff.item(row, COL_HEIGHT) item_height = tblStuff.item(row, COL_STUFF_HEIGHT)
item_weight = tblStuff.item(row, COL_WEIGHT) item_weight = tblStuff.item(row, COL_STUFF_WEIGHT)
item_comment = tblStuff.item(row, COL_COMMENT) item_comment = tblStuff.item(row, COL_STUFF_COMMENT)
if item_stuff: if item_stuff:
stuff_text = item_stuff.text() stuff_text = item_stuff.text()
if len(stuff_text)>0: 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: if item_length:
try: try:
length = float(item_length.text()) length = item_length.text()
if length: if length:
worksheet.write(start_row + row, COL_LENGTH, length) worksheet.write_number(start_row + row, COL_STUFF_LENGTH, float(length.replace(',', '.')))
except ValueError: except ValueError:
pass pass
if item_width: if item_width:
try: try:
width = float(item_width.text()) width = item_width.text()
if width: if width:
worksheet.write(start_row + row, COL_WIDTH, width) worksheet.write_number(start_row + row, COL_STUFF_WIDTH, float(width.replace(',', '.')))
except ValueError: except ValueError:
pass pass
if item_height: if item_height:
try: try:
height = float(item_height.text()) height = item_height.text()
if height: if height:
worksheet.write(start_row + row, COL_HEIGHT, height) worksheet.write_number(start_row + row, COL_STUFF_HEIGHT, float(height.replace(',', '.')))
except ValueError: except ValueError:
pass pass
if item_weight: if item_weight:
try: try:
weight = float(item_weight.text()) weight = item_weight.text()
if weight: if weight:
worksheet.write(start_row + row, COL_WEIGHT, weight) worksheet.write_number(start_row + row, COL_STUFF_WEIGHT, float(weight.replace(',', '.')))
except ValueError: except ValueError:
pass pass
if item_comment: if item_comment:
comment = item_comment.text() comment = item_comment.text()
if len(comment)>0: 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: if item_stuff or item_length or item_width or item_height or item_weight:
row_idx += 1 row_idx += 1
formula = formula + str_iff(formula, "", "=ROUND(", " + ") + f"(B{row_idx} * C{row_idx} * D{row_idx})"
formula += ", 2)"
row_idx += 1 row_idx += 1
# loop over Results # 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 row_idx += 1
worksheet.write(row_idx, 0, QCoreApplication.translate("main","Volume of the garage") + ":") worksheet.write_string(row_idx, 0, QCoreApplication.translate("main","Volume of the garage") + ":")
worksheet.write(row_idx, 1, float(self.ui.efVol_Garage.text())) # 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 row_idx += 1
worksheet.write(row_idx, 0, QCoreApplication.translate("main","Volume of the items") + ":") worksheet.write_string(row_idx, 0, QCoreApplication.translate("main","Free space in the garage") + ":")
worksheet.write(row_idx, 1, float(self.ui.efVol_Stuff.text())) # 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 row_idx += 1
worksheet.write(row_idx, 0, QCoreApplication.translate("main","Free space in the garage") + ":") worksheet.write_string(row_idx, 0, QCoreApplication.translate("main", "Total weight") + ":")
worksheet.write(row_idx, 1, float(self.ui.efVol_Free.text())) # 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 fit_col_widths(tblStuff, worksheet)
worksheet.write(row_idx, 0, QCoreApplication.translate("main", "Total weight") + ":")
worksheet.write(row_idx, 1, float(self.ui.efWeight.text()))
workbook.close() workbook.close()
msg = QCoreApplication.translate("main", "Successfully exported to EXCEL") + " :-)" msg = QCoreApplication.translate("main", "Successfully exported to EXCEL") + " :-)"
@ -760,7 +745,7 @@ class MainWindow(QMainWindow):
garage_length = tblGarage.item(0, 0).text() garage_length = tblGarage.item(0, 0).text()
if garage_length: if garage_length:
try: try:
garage_length = float(garage_length) garage_length = float(garage_length.replace(',', '.'))
except ValueError: except ValueError:
garage_length = 0.0 garage_length = 0.0
is_error = True is_error = True
@ -770,7 +755,7 @@ class MainWindow(QMainWindow):
garage_width = tblGarage.item(0, 1).text() garage_width = tblGarage.item(0, 1).text()
if garage_width: if garage_width:
try: try:
garage_width = float(garage_width) garage_width = float(garage_width.replace(',', '.'))
except ValueError: except ValueError:
garage_width = 0.0 garage_width = 0.0
is_error = True is_error = True
@ -780,13 +765,13 @@ class MainWindow(QMainWindow):
garage_height = tblGarage.item(0, 2).text() garage_height = tblGarage.item(0, 2).text()
if garage_height: if garage_height:
try: try:
garage_height = float(garage_height) garage_height = float(garage_height.replace(',', '.'))
except ValueError: except ValueError:
garage_height = 0.0 garage_height = 0.0
is_error = True is_error = True
if not is_error: 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: else:
msg = QCoreApplication.translate("main", "Error in the garage dimension") + " :-(" msg = QCoreApplication.translate("main", "Error in the garage dimension") + " :-("
self.statusBar.showMessage(msg, 5000) self.statusBar.showMessage(msg, 5000)
@ -810,34 +795,34 @@ class MainWindow(QMainWindow):
length = 0 length = 0
width = 0 width = 0
height = 0 height = 0
item_length = tblStuff.item(row, COL_LENGTH) item_length = tblStuff.item(row, COL_STUFF_LENGTH)
item_width = tblStuff.item(row, COL_WIDTH) item_width = tblStuff.item(row, COL_STUFF_WIDTH)
item_height = tblStuff.item(row, COL_HEIGHT) item_height = tblStuff.item(row, COL_STUFF_HEIGHT)
if item_length: if item_length:
try: try:
length = float(item_length.text()) length = float(item_length.text().replace(',', '.'))
except ValueError: except ValueError:
length = 0 length = 0
is_error = True is_error = True
if item_width: if item_width:
try: try:
width = float(item_width.text()) width = float(item_width.text().replace(',', '.'))
except ValueError: except ValueError:
width = 0 width = 0
is_error = True is_error = True
if item_height: if item_height:
try: try:
height = float(item_height.text()) height = float(item_height.text().replace(',', '.'))
except ValueError: except ValueError:
height = 0 height = 0
is_error = True 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: if is_error:
# stuff_vol = 0.0 # stuff_vol = 0.0
@ -851,16 +836,22 @@ class MainWindow(QMainWindow):
# get garage vol # get garage vol
garage_vol = self.get_garage_vol() garage_vol = self.get_garage_vol()
# print(f"{garage_vol=}")
# get stuff vol # get stuff vol
stuff_vol = self.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 # display results
self.ui.efVol_Garage.setText(f"{garage_vol:2.2f}") self.ui.efVol_Garage.setText(f"{str(garage_vol).replace('.', ',')}")
self.ui.efVol_Stuff.setText(f"{stuff_vol:2.2f}") self.ui.efVol_Stuff.setText(f"{str(stuff_vol).replace('.', ',')}")
self.ui.efVol_Free.setText(f"{garage_vol - stuff_vol:2.2f}") 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;") self.ui.efVol_Free.setStyleSheet("border: 1px solid red;")
else: else:
self.ui.efVol_Free.setStyleSheet("") self.ui.efVol_Free.setStyleSheet("")
@ -874,16 +865,16 @@ class MainWindow(QMainWindow):
for row in range(row_count): for row in range(row_count):
weight = 0.0 weight = 0.0
item = tblStuff.item(row, COL_WEIGHT) item = tblStuff.item(row, COL_STUFF_WEIGHT)
if item: if item:
try: try:
weight = float(item.text()) weight = float(str(item.text()).replace(',','.'))
except ValueError: except ValueError:
weight = 0.0 weight = 0.0
weight_sum = round(weight_sum + weight, 2) 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): def store_selected_language(self, menu):
""" Stores selected menu labels to ini-file. """ """ Stores selected menu labels to ini-file. """

View file

@ -13,12 +13,14 @@ import os
from PySide2.QtWidgets import QMessageBox, QApplication, QMainWindow, QTableWidgetItem, QStatusBar, QAction from PySide2.QtWidgets import QMessageBox, QApplication, QMainWindow, QTableWidgetItem, QStatusBar, QAction
from PySide2 import QtCore from PySide2 import QtCore
from PySide2.QtGui import QIcon, QPixmap from PySide2.QtGui import QIcon, QPixmap
from PySide2.QtCore import QFile, QSize from PySide2.QtCore import QFile, QSize, Qt
from PySide2.QtUiTools import QUiLoader from PySide2.QtUiTools import QUiLoader
# local globals # local globals
APP_ICON = "./img/icons8-garage-32.ico" APP_ICON = "./img/icons8-garage-32.ico"
STD_COL_WIDTH: int = 7
def resource_path(relative_path): def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """ """ Get absolute path to resource, works for dev and for PyInstaller """
try: try:
@ -52,3 +54,78 @@ def show_about():
msg.setStandardButtons(QMessageBox.Ok) msg.setStandardButtons(QMessageBox.Ok)
msg.exec_() 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