-
Notifications
You must be signed in to change notification settings - Fork 0
/
QKVLineEdit.py
70 lines (53 loc) · 1.81 KB
/
QKVLineEdit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from PyQt5.QtWidgets import QWidget, QLineEdit, QHBoxLayout, QScrollArea, QVBoxLayout
class KVLineEdit(QWidget):
def __init__(self):
super().__init__()
print("KVLineEdit created")
hbox = QHBoxLayout()
self.key = QLineEdit()
self.value = QLineEdit()
hbox.addWidget(self.key)
hbox.addWidget(self.value)
hbox.setContentsMargins(0,0,0,0)
hbox.setSpacing(0)
self.setLayout(hbox)
def getKey(self) -> str:
return self.key.text()
def getValue(self) -> str:
return self.value.text()
class KVList(QWidget):
def __init__(self):
super().__init__()
self.pairs = []
self.vbox = QVBoxLayout()
self.vbox.setSpacing(0)
self.vbox.setContentsMargins(0,0,0,0)
self.setLayout(self.vbox)
self.addKVPair()
def addKVPair(self):
newPair = KVLineEdit()
self.pairs.append(newPair)
self.vbox.addWidget(newPair)
def removeKVPair(self):
if self.pairs:
toRemove = self.pairs.pop()
self.vbox.removeWidget(toRemove)
def getPairs(self):
retPairs = {}
for pair in self.pairs:
retPairs[pair.getKey()] = pair.getValue()
return retPairs
def setPairs(self, pairsToSet):
if not pairsToSet:
for i in range(len(self.pairs)):
self.removeKVPair()
diff = len(pairsToSet) - len(self.pairs)
if diff < 0:
for _ in range(diff):
self.addKVPair()
elif diff > 0:
for _ in range(0, diff, -1):
self.removeKVPair()
for i, key in enumerate(pairsToSet):
self.pairs[i].key.setText(key)
self.pairs[i].value.setText(pairsToSet[key])