-
Notifications
You must be signed in to change notification settings - Fork 1
/
description_element.py
120 lines (95 loc) · 4.73 KB
/
description_element.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from PyQt5 import QtCore, QtGui, QtWidgets
import itertools, glob, os, uuid, imagesize
# Local Import
import image_element, prompt_element, QPromptLine
class DescriptionElement(QtWidgets.QWidget):
selectedDescriptions = []
def __init__(self, promptParent, mainFrame):
self.promptParent = promptParent
self.mainFrame = mainFrame
self.parentLayout = self.promptParent.parentFrameLayout
self.parentFrame = self.promptParent.parentFrame
self.isEditing = False
super(DescriptionElement, self).__init__()
self.layout = QtWidgets.QHBoxLayout(self)
self.layout.setContentsMargins(0, 5, 0, 0)
self.horizontalSpacer = QtWidgets.QSpacerItem(40, 20)
self.layout.addItem(self.horizontalSpacer)
self.entry = QPromptLine.QPromptLine(self)
self.entry.setMinimumSize(QtCore.QSize(200, 35))
self.deleteButton = QtWidgets.QPushButton(self)
self.deleteButton.setEnabled(True)
self.deleteButton.setAutoFillBackground(False)
self.deleteButton.setStyleSheet("")
self.deleteButton.setText("")
deleteIcon = QtGui.QIcon()
deleteIcon.addPixmap(QtGui.QPixmap("assets/trash.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.deleteButton.setIcon(deleteIcon)
self.layout.addWidget(self.entry)
self.layout.addWidget(self.deleteButton)
self.parentLayout.addWidget(self)
self.promptParent.parentFrameLayout.addWidget(self)
self.promptParent.addDescriptionElement(self)
self.deleteButton.clicked.connect(self.delete_element)
self.entry.returnPressed.connect(self.duplicate_element)
self.entry.selectionChanged.connect(lambda: self.entry.setSelection(0, 0)) # disable selection
self.entry.clicked.connect(self.selectEntry)
@staticmethod
def getDescriptionWithText(txt, promptParent):
for i in promptParent.descriptions:
if i.entry.text() == txt:
return i
@staticmethod
def createDescriptionFromImport(parentPrompt, mainFrame, txt):
desc = DescriptionElement(parentPrompt, mainFrame=mainFrame)
desc.entry.setText(txt)
desc.entry.setReadOnly(True)
return desc
def isUniqueElement(self):
if [desc.entry.text() for desc in self.promptParent.descriptions].count(self.entry.text()) > 1:
return False
return True
def updateAllChildImages(self): # TODO: Use a class whose description element and prompt element go to avoid these repetitions
for imageWidget in image_element.ImageElement.allImages:
if self.promptParent in imageWidget.usedDict.keys(): # Not mandatory but allows to go a little bit faster
if self in imageWidget.usedDict[self.promptParent]:
imageWidget.updateCaption()
def duplicate_element(self):
if self.entry.text() and self.isUniqueElement():
self.entry.setReadOnly(True)
self.updateAllChildImages()
if not self.isEditing:
new = DescriptionElement(self.promptParent, self.mainFrame)
new.entry.setFocus()
self.isEditing = False
def delete_element(self):
self.close()
self.promptParent.removeDescriptionElement(self)
for img in image_element.ImageElement.allImages:
if img.usedDict.get(self.promptParent) and self in img.usedDict[self.promptParent]:
img.usedDict[self.promptParent].remove(self)
img.deselect()
img.updateCaption()
self.deselect()
def select(self):
self.entry.setStyleSheet("QWidget { background-color: rgb(68, 140, 203) }")
DescriptionElement.selectedDescriptions.append(self)
def deselect(self):
self.entry.setStyleSheet("")
if self in DescriptionElement.selectedDescriptions:
DescriptionElement.selectedDescriptions.remove(self)
def selectEntry(self):
if self.entry.isReadOnly(): # only after user created the prombt
if self.promptParent == prompt_element.PromptElement.currentSelected:
if self in DescriptionElement.selectedDescriptions:
self.deselect()
else:
self.select()
else:
if prompt_element.PromptElement.currentSelected: prompt_element.PromptElement.currentSelected.deselectAllElement()
prompt_element.PromptElement.currentSelected = self.promptParent
prompt_element.PromptElement.currentSelected.selectAllChildImages()
self.promptParent.select()
self.select()
for img in image_element.ImageElement.getSelectedImages():
img.updateBackground()