Tabelle "Stuff": Zeilen köpieren, löschen und einfügen

This commit is contained in:
Paul S 2021-06-27 13:31:08 +02:00
parent 6380729e08
commit 83057ae3be
4 changed files with 106 additions and 10 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
env2

View file

@ -1,8 +1,13 @@
# Changelog GarageCalc1 # Changelog GarageCalc1
## [0.2] - 2021-06-27
### Added
- Möglichkeit Zeilen zu kopieren und einzufügen (sowohl Kontextmenü auf Zeilenüberschrift als auch Tastenkombination Strg+C/Strg+V)
- Möglichkeiot Zeilen zu entfernen (sowohl Kontextmenü auf Zeilenüberschrift als auch Entfernen-Taste)
## [0.1] - 2021-06-27 ## [0.1] - 2021-06-27
### Added ### Added
- first version - Erste Version
# Changelog GarageCalc1 # Changelog GarageCalc1

View file

@ -14,7 +14,7 @@ import csv
# Third party imports # Third party imports
from PySide2.QtWidgets import QApplication, QMainWindow, QTableWidgetItem, QStatusBar, QAction, QFileDialog, \ from PySide2.QtWidgets import QApplication, QMainWindow, QTableWidgetItem, QStatusBar, QAction, QFileDialog, \
QAbstractItemView, QMenu, QMessageBox QAbstractItemView, QMenu, QMessageBox, QInputDialog
from PySide2.QtGui import QIcon from PySide2.QtGui import QIcon
from PySide2.QtCore import QFile, QSize, Qt from PySide2.QtCore import QFile, QSize, Qt
from PySide2.QtUiTools import QUiLoader from PySide2.QtUiTools import QUiLoader
@ -26,7 +26,7 @@ from utils import show_about, resource_path
UI_FILE = "main.ui" UI_FILE = "main.ui"
APP_NAME = "Garagenraum-Rechner" APP_NAME = "Garagenraum-Rechner"
APP_DISPNAME = "GarageCalc" APP_DISPNAME = "GarageCalc"
APP_VERSION = "v0.1" APP_VERSION = "v0.2"
APP_AUTHOR = "Paul Salajean" APP_AUTHOR = "Paul Salajean"
APP_DESCR = "Berechnet zur Verfügung stehenden Garagenraum" APP_DESCR = "Berechnet zur Verfügung stehenden Garagenraum"
APP_COPYRIGHT = "(c) Paul Salajean 2021" APP_COPYRIGHT = "(c) Paul Salajean 2021"
@ -68,6 +68,7 @@ class MyMainWindow(QMainWindow):
self.ui.efWeight.setText(str("0")) self.ui.efWeight.setText(str("0"))
self.is_modified = False self.is_modified = False
self.opened_file = None self.opened_file = None
self.remembered_row = None
def load_ui(self): def load_ui(self):
loader = QUiLoader() loader = QUiLoader()
@ -155,19 +156,105 @@ class MyMainWindow(QMainWindow):
tblGarage.itemChanged.connect(self.on_garage_changed) tblGarage.itemChanged.connect(self.on_garage_changed)
tblStuff.itemChanged.connect(self.on_stuff_changed) tblStuff.itemChanged.connect(self.on_stuff_changed)
def keyPressEvent(self, event):
tblStuff = self.ui.tableStuff
modifiers = QApplication.keyboardModifiers()
key = event.key()
if modifiers == Qt.ControlModifier:
# get selected row
sel_rows_idx = tblStuff.selectionModel().selectedRows()
row = sel_rows_idx[0].row() # there is onle on row because of singleton selection mode
if key == Qt.Key_C:
self.remembered_row = row
elif key == Qt.Key_V:
self.row_insert(tblStuff, self.remembered_row, row)
if key == Qt.Key_Delete:
self.row_remove(tblStuff)
def row_remove(self, table):
# get selected row
sel_rows_idx = table.selectionModel().selectedRows()
row = sel_rows_idx[0].row() # there is onle on row because of singleton selection mode
stuff = None
item_stuff = table.item(row, COL_STUFF)
if item_stuff:
stuff = item_stuff.text()
if stuff:
msg = f"Zeile Nr. {row+1} '{stuff}' entfernen?"
else:
msg = f"Zeile Nr. {row+1} entfernen?"
reply = QMessageBox.question(self, "Zeile entfernen", msg, \
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if reply == QMessageBox.Yes:
table.removeRow(row)
self.calc_voluminae()
def row_insert(self, table, source_row, target_row):
# check if empty
item_stuff = table.item(target_row, COL_STUFF)
item_length = table.item(target_row, COL_LENGTH)
item_width = table.item(target_row, COL_WIDTH)
item_height = table.item(target_row, COL_HEIGHT)
item_weight = table.item(target_row, COL_WEIGHT)
if item_stuff or item_length or item_width or item_height or item_weight:
msg = "Es sind bereits Werte in dieser Zeile vorhanden. Trotzdem fortfahren?"
reply = QMessageBox.question(self, "Zeile einfügen", msg, \
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if reply == QMessageBox.No:
return False
item_stuff = table.item(source_row, COL_STUFF)
item_length = table.item(source_row, COL_LENGTH)
item_width = table.item(source_row, COL_WIDTH)
item_height = table.item(source_row, COL_HEIGHT)
item_weight = table.item(source_row, COL_WEIGHT)
if item_stuff:
table.setItem(target_row, COL_STUFF, QTableWidgetItem(item_stuff.text()))
if item_length:
table.setItem(target_row, COL_LENGTH, QTableWidgetItem(item_length.text()))
if item_width:
table.setItem(target_row, COL_WIDTH, QTableWidgetItem(item_width.text()))
if item_height:
table.setItem(target_row, COL_HEIGHT, QTableWidgetItem(item_height.text()))
if item_weight:
table.setItem(target_row, COL_WEIGHT, QTableWidgetItem(item_weight.text()))
def show_rowheader_context_menu(self, position): def show_rowheader_context_menu(self, position):
tblStuff = self.ui.tableStuff tblStuff = self.ui.tableStuff
row = self.headers.logicalIndexAt(position) row = self.headers.logicalIndexAt(position)
menu = QMenu() menu = QMenu()
remove_row = menu.addAction("Zeile entfernen") row_add = menu.addAction("Zeile hinzufügen")
insert_row = menu.addAction("Zeile einfügen") row_remove = menu.addAction("Zeile entfernen (Entf.-Taste)")
menu.addSeparator()
row_copy = menu.addAction("Zeile kopieren (Strg+C)")
row_insert = menu.addAction("Zeile einfügen (Strg+V)")
ac = menu.exec_(tblStuff.mapToGlobal(position)) ac = menu.exec_(tblStuff.mapToGlobal(position))
if ac == remove_row: if ac == row_remove:
tblStuff.removeRow(row) # tblStuff.removeRow(row)
self.calc_voluminae() # self.calc_voluminae()
elif ac == insert_row: self.row_remove(tblStuff)
elif ac == row_add:
tblStuff.insertRow(row) tblStuff.insertRow(row)
elif ac == row_copy:
self.remembered_row = row
elif ac == row_insert:
self.row_insert(tblStuff, self.remembered_row, row)
def init_ui(self): def init_ui(self):
tblGarage = self.ui.tableGarage tblGarage = self.ui.tableGarage

View file

@ -225,7 +225,10 @@
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="selectionMode"> <property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum> <enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property> </property>
<column> <column>
<property name="text"> <property name="text">