#!/usr/bin/env python3 """ project: GarageCalc1 file: utils.py summary: helper functions """ # Standard library imports import sys import os # Third party imports from PySide2.QtWidgets import QMessageBox, QApplication, QMainWindow, QTableWidgetItem, QStatusBar from PySide2 import QtCore from PySide2.QtGui import QIcon, QPixmap from PySide2.QtCore import QFile, QSize, Qt from PySide2.QtUiTools import QUiLoader # local globals STD_COL_WIDTH: int = 7 # local imports import icons_rc def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) def show_about(ini_file="", opened_file=""): if not opened_file: opened_file = "" qApp = QApplication.instance() msg = QMessageBox() msg.setIconPixmap(QPixmap(u":ICONS/ICON_APP")) text = "
" + qApp.translate("utils", "Used icons") + ": 'Cute Color' " + qApp.translate("utils", "from") + " Icons8
" text = text + "" + qApp.translate("utils", "Currently opened file") + ":
" + opened_file + "
" + qApp.translate("utils", "Location of the Ini-file") + ":
" + ini_file + "
Python " + qApp.translate("utils", "Version") + ": " + f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} {sys.version_info.releaselevel}"
text = text + "
" + f"{sys.executable}" + "
"
text = text + "
Qt " + qApp.translate("utils", "Version") + ": " + f"{QtCore.__version__}"
msg.setText(text)
msg.setWindowTitle("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
def convert_uom_to_length(val, unit_in, unit_out):
SI = {'mm':0.001, 'cm':0.01, 'm':1.0}
return val*SI[unit_in]/SI[unit_out]
def convert_uom_to_mass(val, unit_in, unit_out):
SI = {'g':0.001, 'kg':1.0}
return val*SI[unit_in]/SI[unit_out]
def optimizeTableLayout(table):
for col in range(table.columnCount()): # optimize column width
table.resizeColumnToContents(col)
table.resizeRowsToContents() # optimize row height
def table_has_items(tablewidget):
for row in range(tablewidget.rowCount()):
for col in range(tablewidget.columnCount()):
item = tablewidget.item(row, col)
if item:
return True
return False