splitter-control + optimize row height

This commit is contained in:
Paul S 2021-07-02 15:45:59 +02:00
parent 7e2272017f
commit fb88dac713
10 changed files with 341 additions and 326 deletions

View file

@ -65,12 +65,14 @@ class TableWidget(QTableWidget):
# self.setEditTriggers(QAbstractItemView.DoubleClicked | QAbstractItemView.EditKeyPressed | QAbstractItemView.AnyKeyPressed)
self.headers = self.verticalHeader()
self.headers.setContextMenuPolicy(Qt.CustomContextMenu)
self.headers.customContextMenuRequested.connect(self.on_rowheadercontext_menu)
self.headers.setSelectionMode(QAbstractItemView.SingleSelection)
self.headers.sectionClicked.connect(self.select_row)
self.headers.setSectionsMovable(True)
self.vertHeader = self.verticalHeader()
self.vertHeader.setContextMenuPolicy(Qt.CustomContextMenu)
self.vertHeader.customContextMenuRequested.connect(self.on_rowheadercontext_menu)
self.vertHeader.setSelectionMode(QAbstractItemView.SingleSelection)
self.vertHeader.sectionClicked.connect(self.select_row)
self.vertHeader.setSectionsMovable(True)
# optimize row height
self.resizeRowsToContents()
self.row_selected = False
@ -114,7 +116,7 @@ class TableWidget(QTableWidget):
row_delete_items = menu.addAction(QIcon(ICON_ERASER), QCoreApplication.translate("TableWidget", "Delete items"))
ac = menu.exec_(self.mapToGlobal(position))
row = self.headers.logicalIndexAt(position)
row = self.vertHeader.logicalIndexAt(position)
if ac == row_cut:
self.item_cut()

View file

@ -15,7 +15,7 @@ import configparser
# Third party imports
from PySide2.QtWidgets import QApplication, QMainWindow, QTableWidgetItem, QStatusBar, QFileDialog, \
QAbstractItemView, QMenu, QMessageBox, QHBoxLayout, QVBoxLayout, QAction, QActionGroup
QAbstractItemView, QMenu, QMessageBox, QHBoxLayout, QVBoxLayout, QAction, QActionGroup, QSizePolicy
from PySide2.QtGui import QIcon
from PySide2.QtCore import QFile, QSize, Qt, QCoreApplication, QTranslator
from PySide2.QtUiTools import QUiLoader
@ -28,7 +28,7 @@ from utils import show_about, resource_path
from clsTableWidget import TableWidget
# Local globals
APP_VERSION = "v0.4.2"
APP_VERSION = "v0.5"
DIR_APPDATA = os.getenv('LOCALAPPDATA')
@ -56,20 +56,25 @@ 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"
TBL_STUFF_COL_COUNT = 5
TBL_STUFF_COL_COUNT = 6
TBL_STUFF_ROW_COUNT = 50
WIN_WIDTH = 700
WIN_HEIGHT = 640
TXT_UNSAVED_CHANGES = QCoreApplication.translate("main", "There are unsaved entries. Without saving, all changes are lost. Continue anyway?")
####################################################################
def main():
qApp = QApplication(sys.argv)
global APP_NAME
qApp.setApplicationName(APP_NAME)
qApp.setApplicationDisplayName(APP_DISPNAME)
qApp.setApplicationVersion(APP_VERSION)
@ -108,19 +113,39 @@ def main():
qApp.installTranslator(translator)
app_name = qApp.translate("main", APP_NAME)
qApp.setApplicationName(app_name)
winMain = MainWindow(language)
if qApp.primaryScreen().size().width() <= 700:
if qApp.primaryScreen().size().width() <= 800:
winMain.showMaximized()
else:
winMain.resize(610, 640)
winMain.show()
winMain.resize(WIN_WIDTH, WIN_HEIGHT)
winMain.show()
sys.exit(qApp.exec_())
####################################################################
def create_examples(table):
from random import randint, choice
from string import ascii_letters, punctuation, digits
min = 12
max = 15
string_format = ascii_letters
for row in range(table.rowCount()):
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, 5, QTableWidgetItem(generated_string2))
####################################################################
class MainWindow(QMainWindow):
def __init__(self, language):
@ -140,15 +165,16 @@ class MainWindow(QMainWindow):
self.create_toolbar()
self.create_statusbar()
global APP_NAME
APP_NAME = qApp.setApplicationName(qApp.translate("main", "Garage Space Calculator"))
self.statusBar.showMessage(f"{APP_NAME} {APP_VERSION} - {APP_AUTHOR}", 5000)
self.statusBar.showMessage(f"{APP_DISPNAME} {APP_VERSION} - {APP_AUTHOR}", 5000)
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"))
def create_menu(self, language=None):
menuMain = self.menuBar()
self.menuSettings = menuMain.addMenu(QCoreApplication.translate("main", "&Settings"))
@ -176,18 +202,24 @@ class MainWindow(QMainWindow):
self.menuLanguage.setTitle(QCoreApplication.translate("main", "Language"))
# tables
self.ui.tableGarage.setVerticalHeaderLabels([QCoreApplication.translate("main","Garage")])
self.ui.tableGarage.setVerticalHeaderLabels([
QCoreApplication.translate("main","Garage")])
self.ui.tableGarage.setHorizontalHeaderLabels([
QCoreApplication.translate("main","Length") + " [m]",
QCoreApplication.translate("main","Width") + " [m]",
QCoreApplication.translate("main","Height") + " [m]"
QCoreApplication.translate("main","Height") + " [m]",
QCoreApplication.translate("main","Comment")
])
self.ui.tableStuff.setHorizontalHeaderLabels([QCoreApplication.translate("main","Stuff"),
self.ui.tableStuff.setHorizontalHeaderLabels([
QCoreApplication.translate("main","Stuff"),
QCoreApplication.translate("main","Length") + " [m]",
QCoreApplication.translate("main","Width") + " [m]",
QCoreApplication.translate("main","Height") + " [m]",
QCoreApplication.translate("main","Weight") + " [m]"])
QCoreApplication.translate("main","Weight") + " [m]",
QCoreApplication.translate("main","Comment")
])
# labels
self.ui.gbGarage.setTitle(QCoreApplication.translate("main","Dimension of the garage"))
@ -220,8 +252,13 @@ class MainWindow(QMainWindow):
self.ui.tableStuff = TableWidget()
self.ui.tableStuff.setColumnCount(TBL_STUFF_COL_COUNT)
self.ui.tableStuff.setRowCount(TBL_STUFF_ROW_COUNT)
self.ui.tableStuff.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.ui.splitter.setStretchFactor(1, 10)
layoutGb.addWidget(self.ui.tableStuff)
def create_actions(self):
self.actionNew = QAction()
self.actionNew.setIcon(QIcon(resource_path(ICON_NEW)))
@ -310,7 +347,7 @@ class MainWindow(QMainWindow):
if self.opened_file:
self.setWindowTitle(self.opened_file)
else:
self.setWindowTitle(f"{APP_NAME} {APP_VERSION} by {APP_AUTHOR}")
self.setWindowTitle(f"{qApp.applicationName()} {APP_VERSION} - {APP_AUTHOR}")
self.ui.efVol_Free.setStyleSheet("")
self.is_modified = False
@ -358,10 +395,12 @@ class MainWindow(QMainWindow):
garage_length = 0
garage_width = 0
garage_height = 0
garage_comment = ""
item_length = tblGarage.item(0, 0)
item_width = tblGarage.item(0, 1)
item_height = tblGarage.item(0, 2)
item_comment = tblGarage.item(0, 3)
# loop over table Garage
for row in range(tblGarage.rowCount()):
@ -389,8 +428,12 @@ class MainWindow(QMainWindow):
except ValueError:
garage_height = 0.0
# get garage comment
if item_comment:
garage_comment = item_comment.text()
if garage_length or garage_width or garage_height:
writer.writerow([QCoreApplication.translate("main","Garage"), garage_length, garage_width, garage_height])
writer.writerow([QCoreApplication.translate("main","Garage"), garage_length, garage_width, garage_height, garage_comment])
# loop over table Stuff
for row in range(tblStuff.rowCount()):
@ -399,12 +442,14 @@ class MainWindow(QMainWindow):
width = None
height = None
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)
if item_stuff:
stuff_text = item_stuff.text()
@ -433,8 +478,11 @@ class MainWindow(QMainWindow):
except ValueError:
weight = None
if item_comment:
comment = item_comment.text()
if item_stuff or item_length or item_width or item_height or item_weight:
writer.writerow([stuff_text, length, width, height, weight])
writer.writerow([stuff_text, length, width, height, weight, comment])
is_file_saved = True
self.opened_file = os.path.basename(fileName)
@ -478,10 +526,12 @@ class MainWindow(QMainWindow):
garage_length = row[1]
garage_width = row[2]
garage_height = row[3]
garage_comment = row[4]
tblGarage.setItem(0, COL_LENGTH-1, QTableWidgetItem(str(garage_length)))
tblGarage.setItem(0, COL_WIDTH-1, QTableWidgetItem(str(garage_width)))
tblGarage.setItem(0, COL_HEIGHT-1, QTableWidgetItem(str(garage_height)))
tblGarage.setItem(0, COL_COMMENT-1, QTableWidgetItem(garage_comment))
except IndexError as ex:
pass
@ -493,12 +543,14 @@ class MainWindow(QMainWindow):
stuff_width = row[2]
stuff_height = row[3]
stuff_weight = row[4]
stuff_comment = row[5]
tblStuff.setItem(row_idx - 1, COL_STUFF, QTableWidgetItem(stuff))
tblStuff.setItem(row_idx - 1, COL_LENGTH, QTableWidgetItem(str(stuff_length)))
tblStuff.setItem(row_idx - 1, COL_WIDTH, QTableWidgetItem(str(stuff_width)))
tblStuff.setItem(row_idx - 1, COL_HEIGHT, QTableWidgetItem(str(stuff_height)))
tblStuff.setItem(row_idx - 1, COL_WEIGHT, QTableWidgetItem(str(stuff_weight)))
tblStuff.setItem(row_idx - 1, COL_COMMENT, QTableWidgetItem(stuff_comment))
except IndexError as ex:
pass
@ -506,6 +558,8 @@ 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)
@ -513,7 +567,7 @@ class MainWindow(QMainWindow):
if fileName:
self.setWindowTitle(self.opened_file)
else:
self.setWindowTitle(f"{APP_NAME} {APP_VERSION} by {APP_AUTHOR}")
self.setWindowTitle(f"{qApp.applicationName()} {APP_VERSION} - {APP_AUTHOR}")
def file_export(self):
tblGarage = self.ui.tableGarage
@ -540,8 +594,10 @@ class MainWindow(QMainWindow):
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.set_column(0, 0, 25)
worksheet.set_column(1, 3, 10)
worksheet.set_column(4, 5, 20)
start_row = 2
# loop over table Garage
@ -567,9 +623,13 @@ class MainWindow(QMainWindow):
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)
start_row = 4
worksheet.write(start_row, 0, QCoreApplication.translate("main", "Dimensions of the objects to be stored"))
@ -579,6 +639,7 @@ class MainWindow(QMainWindow):
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") )
start_row = 6
# loop over table Stuff
@ -589,6 +650,7 @@ class MainWindow(QMainWindow):
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)
if item_stuff:
stuff_text = item_stuff.text()
@ -627,6 +689,11 @@ class MainWindow(QMainWindow):
except ValueError:
pass
if item_comment:
comment = item_comment.text()
if len(comment)>0:
worksheet.write(start_row + row, COL_COMMENT, comment)
if item_stuff or item_length or item_width or item_height or item_weight:
row_idx += 1