Improve Metadatainput
This commit is contained in:
parent
96c1231ed0
commit
76f04c1ae3
@ -1,4 +1,5 @@
|
|||||||
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QSpacerItem
|
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QSpacerItem, QSpinBox, QDoubleSpinBox
|
||||||
|
from PyQt6.QtWidgets import QGridLayout, QMenu, QListWidget, QPushButton
|
||||||
|
|
||||||
class MetadataInput(QWidget):
|
class MetadataInput(QWidget):
|
||||||
def __init__(self, elements: list[tuple[str, str]]=None):
|
def __init__(self, elements: list[tuple[str, str]]=None):
|
||||||
@ -16,3 +17,69 @@ class MetadataInput(QWidget):
|
|||||||
self.layout.addWidget(QLabel(name))
|
self.layout.addWidget(QLabel(name))
|
||||||
self.layout.addWidget(QLineEdit(init_val))
|
self.layout.addWidget(QLineEdit(init_val))
|
||||||
self.layout.addItem(QSpacerItem(0, 1))
|
self.layout.addItem(QSpacerItem(0, 1))
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
QWidget class that contains a variable amount of key - input pairs.
|
||||||
|
One pair per line in a dense layout
|
||||||
|
pairs. The value may be text - line edit, float - qdoublespinbox and int - qspinbox.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class MetadataInput2(QWidget):
|
||||||
|
def __init__(self, elements: list[tuple[str, str]]=None):
|
||||||
|
super().__init__()
|
||||||
|
self.layout = QGridLayout()
|
||||||
|
self.layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
self.layout.setSpacing(0)
|
||||||
|
for (n, v) in elements:
|
||||||
|
self.add_element(n, v)
|
||||||
|
self.setLayout(self.layout)
|
||||||
|
|
||||||
|
def add_element(self, name, init_val=""):
|
||||||
|
row = self.layout.rowCount()
|
||||||
|
label_widget = QLabel(name)
|
||||||
|
self.layout.addWidget(label_widget, row, 0)
|
||||||
|
|
||||||
|
if isinstance(init_val, str):
|
||||||
|
input_widget = QLineEdit(init_val)
|
||||||
|
elif isinstance(init_val, int):
|
||||||
|
input_widget = QSpinBox()
|
||||||
|
input_widget.setValue(init_val)
|
||||||
|
elif isinstance(init_val, float):
|
||||||
|
input_widget = QDoubleSpinBox()
|
||||||
|
input_widget.setValue(init_val)
|
||||||
|
else:
|
||||||
|
input_widget = QLineEdit(str(init_val))
|
||||||
|
|
||||||
|
self.layout.addWidget(input_widget, row, 1)
|
||||||
|
|
||||||
|
# Add a small button with an X from the theme
|
||||||
|
remove_button = QPushButton("X")
|
||||||
|
remove_button.setFixedSize(20, 20)
|
||||||
|
remove_button.clicked.connect(lambda: self.remove_element(label_widget))
|
||||||
|
self.layout.addWidget(remove_button, row, 2)
|
||||||
|
|
||||||
|
def remove_element(self, label_widget):
|
||||||
|
# get position of label widget
|
||||||
|
# TODO Fix
|
||||||
|
row = self.layout.indexOf(label_widget)
|
||||||
|
# remove all widgets in row
|
||||||
|
for i in range(self.layout.columnCount()):
|
||||||
|
widget = self.layout.itemAtPosition(row, i).widget()
|
||||||
|
print(widget)
|
||||||
|
self.layout.removeWidget(widget)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_dict(self):
|
||||||
|
"""
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Dictionary of all key-values pairs
|
||||||
|
"""
|
||||||
|
d = {}
|
||||||
|
for row in range(self.layout.rowCount()):
|
||||||
|
key = self.layout.itemAtPosition(row, 0).widget().text()
|
||||||
|
value = self.layout.itemAtPosition(row, 1).widget().text()
|
||||||
|
d[key] = value
|
||||||
|
return d
|
Loading…
x
Reference in New Issue
Block a user