46 |
53 |
47 class EricLineEditSide(enum.Enum): |
54 class EricLineEditSide(enum.Enum): |
48 """ |
55 """ |
49 Class defining the line edit sides. |
56 Class defining the line edit sides. |
50 """ |
57 """ |
|
58 |
51 LEFT = 0 |
59 LEFT = 0 |
52 RIGHT = 1 |
60 RIGHT = 1 |
53 |
61 |
54 |
62 |
55 class EricLineEdit(QLineEdit): |
63 class EricLineEdit(QLineEdit): |
56 """ |
64 """ |
57 Class implementing a line edit widget showing some inactive text. |
65 Class implementing a line edit widget showing some inactive text. |
58 """ |
66 """ |
|
67 |
59 def __init__(self, parent=None, placeholderText=""): |
68 def __init__(self, parent=None, placeholderText=""): |
60 """ |
69 """ |
61 Constructor |
70 Constructor |
62 |
71 |
63 @param parent reference to the parent widget |
72 @param parent reference to the parent widget |
64 @type QWidget |
73 @type QWidget |
65 @param placeholderText text to be shown on inactivity |
74 @param placeholderText text to be shown on inactivity |
66 @type str |
75 @type str |
67 """ |
76 """ |
68 super().__init__(parent) |
77 super().__init__(parent) |
69 |
78 |
70 self.setMinimumHeight(22) |
79 self.setMinimumHeight(22) |
71 |
80 |
72 self.setPlaceholderText(placeholderText) |
81 self.setPlaceholderText(placeholderText) |
73 |
82 |
74 self.__mainLayout = QHBoxLayout(self) |
83 self.__mainLayout = QHBoxLayout(self) |
75 self.__mainLayout.setContentsMargins(0, 0, 0, 0) |
84 self.__mainLayout.setContentsMargins(0, 0, 0, 0) |
76 self.__mainLayout.setSpacing(0) |
85 self.__mainLayout.setSpacing(0) |
77 |
86 |
78 self.__leftMargin = 0 |
87 self.__leftMargin = 0 |
79 self.__leftWidget = EricLineEditSideWidget(self) |
88 self.__leftWidget = EricLineEditSideWidget(self) |
80 self.__leftWidget.resize(0, 0) |
89 self.__leftWidget.resize(0, 0) |
81 self.__leftLayout = QHBoxLayout(self.__leftWidget) |
90 self.__leftLayout = QHBoxLayout(self.__leftWidget) |
82 self.__leftLayout.setContentsMargins(0, 0, 2, 0) |
91 self.__leftLayout.setContentsMargins(0, 0, 2, 0) |
83 if QApplication.isRightToLeft(): |
92 if QApplication.isRightToLeft(): |
84 self.__leftLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
93 self.__leftLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
85 else: |
94 else: |
86 self.__leftLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
95 self.__leftLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
87 self.__leftLayout.setSizeConstraint( |
96 self.__leftLayout.setSizeConstraint(QLayout.SizeConstraint.SetFixedSize) |
88 QLayout.SizeConstraint.SetFixedSize) |
97 |
89 |
|
90 self.__rightWidget = EricLineEditSideWidget(self) |
98 self.__rightWidget = EricLineEditSideWidget(self) |
91 self.__rightWidget.resize(0, 0) |
99 self.__rightWidget.resize(0, 0) |
92 self.__rightLayout = QHBoxLayout(self.__rightWidget) |
100 self.__rightLayout = QHBoxLayout(self.__rightWidget) |
93 self.__rightLayout.setContentsMargins(0, 0, 2, 0) |
101 self.__rightLayout.setContentsMargins(0, 0, 2, 0) |
94 if self.isRightToLeft(): |
102 if self.isRightToLeft(): |
95 self.__rightLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
103 self.__rightLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
96 else: |
104 else: |
97 self.__rightLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
105 self.__rightLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
98 |
106 |
99 horizontalSpacer = QSpacerItem( |
107 horizontalSpacer = QSpacerItem( |
100 0, 0, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) |
108 0, 0, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum |
|
109 ) |
101 self.__mainLayout.addWidget( |
110 self.__mainLayout.addWidget( |
102 self.__leftWidget, 0, |
111 self.__leftWidget, |
103 Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft) |
112 0, |
|
113 Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft, |
|
114 ) |
104 self.__mainLayout.addItem(horizontalSpacer) |
115 self.__mainLayout.addItem(horizontalSpacer) |
105 self.__mainLayout.addWidget( |
116 self.__mainLayout.addWidget( |
106 self.__rightWidget, 0, |
117 self.__rightWidget, |
107 Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight) |
118 0, |
|
119 Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight, |
|
120 ) |
108 if self.isRightToLeft(): |
121 if self.isRightToLeft(): |
109 self.__mainLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
122 self.__mainLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
110 else: |
123 else: |
111 self.__mainLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
124 self.__mainLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
112 |
125 |
113 self.setWidgetSpacing(3) |
126 self.setWidgetSpacing(3) |
114 self.__leftWidget.sizeHintChanged.connect(self._updateTextMargins) |
127 self.__leftWidget.sizeHintChanged.connect(self._updateTextMargins) |
115 self.__rightWidget.sizeHintChanged.connect(self._updateTextMargins) |
128 self.__rightWidget.sizeHintChanged.connect(self._updateTextMargins) |
116 |
129 |
117 def setLeftMargin(self, margin): |
130 def setLeftMargin(self, margin): |
118 """ |
131 """ |
119 Public method to set the left margin. |
132 Public method to set the left margin. |
120 |
133 |
121 @param margin left margin in pixel (integer) |
134 @param margin left margin in pixel (integer) |
122 """ |
135 """ |
123 self.__leftMargin = margin |
136 self.__leftMargin = margin |
124 |
137 |
125 def leftMargin(self): |
138 def leftMargin(self): |
126 """ |
139 """ |
127 Public method to get the size of the left margin. |
140 Public method to get the size of the left margin. |
128 |
141 |
129 @return left margin in pixel (integer) |
142 @return left margin in pixel (integer) |
130 """ |
143 """ |
131 return self.__leftMargin |
144 return self.__leftMargin |
132 |
145 |
133 def event(self, evt): |
146 def event(self, evt): |
134 """ |
147 """ |
135 Public method to handle events. |
148 Public method to handle events. |
136 |
149 |
137 @param evt reference to the event (QEvent) |
150 @param evt reference to the event (QEvent) |
138 @return flag indicating, whether the event was recognized (boolean) |
151 @return flag indicating, whether the event was recognized (boolean) |
139 """ |
152 """ |
140 if evt.type() == QEvent.Type.LayoutDirectionChange: |
153 if evt.type() == QEvent.Type.LayoutDirectionChange: |
141 if self.isRightToLeft(): |
154 if self.isRightToLeft(): |
142 self.__mainLayout.setDirection( |
155 self.__mainLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
143 QBoxLayout.Direction.RightToLeft) |
156 self.__leftLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
144 self.__leftLayout.setDirection( |
157 self.__rightLayout.setDirection(QBoxLayout.Direction.RightToLeft) |
145 QBoxLayout.Direction.RightToLeft) |
|
146 self.__rightLayout.setDirection( |
|
147 QBoxLayout.Direction.RightToLeft) |
|
148 else: |
158 else: |
149 self.__mainLayout.setDirection( |
159 self.__mainLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
150 QBoxLayout.Direction.LeftToRight) |
160 self.__leftLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
151 self.__leftLayout.setDirection( |
161 self.__rightLayout.setDirection(QBoxLayout.Direction.LeftToRight) |
152 QBoxLayout.Direction.LeftToRight) |
|
153 self.__rightLayout.setDirection( |
|
154 QBoxLayout.Direction.LeftToRight) |
|
155 return QLineEdit.event(self, evt) |
162 return QLineEdit.event(self, evt) |
156 |
163 |
157 def _updateTextMargins(self): |
164 def _updateTextMargins(self): |
158 """ |
165 """ |
159 Protected slot to update the text margins. |
166 Protected slot to update the text margins. |
160 """ |
167 """ |
161 left = ( |
168 left = ( |
162 self.__leftWidget.sizeHint().width() |
169 self.__leftWidget.sizeHint().width() |
163 if self.__leftMargin == 0 else |
170 if self.__leftMargin == 0 |
164 self.__leftMargin |
171 else self.__leftMargin |
165 ) |
172 ) |
166 right = self.__rightWidget.sizeHint().width() |
173 right = self.__rightWidget.sizeHint().width() |
167 top = 0 |
174 top = 0 |
168 bottom = 0 |
175 bottom = 0 |
169 self.setTextMargins(left, top, right, bottom) |
176 self.setTextMargins(left, top, right, bottom) |
170 |
177 |
171 def addWidget(self, widget, position): |
178 def addWidget(self, widget, position): |
172 """ |
179 """ |
173 Public method to add a widget to a side. |
180 Public method to add a widget to a side. |
174 |
181 |
175 @param widget reference to the widget to add |
182 @param widget reference to the widget to add |
176 @type QWidget |
183 @type QWidget |
177 @param position position to add to |
184 @param position position to add to |
178 @type EricLineEditSide |
185 @type EricLineEditSide |
179 """ |
186 """ |
180 if widget is None: |
187 if widget is None: |
181 return |
188 return |
182 |
189 |
183 if self.isRightToLeft(): |
190 if self.isRightToLeft(): |
184 if position == EricLineEditSide.LEFT: |
191 if position == EricLineEditSide.LEFT: |
185 position = EricLineEditSide.RIGHT |
192 position = EricLineEditSide.RIGHT |
186 else: |
193 else: |
187 position = EricLineEditSide.LEFT |
194 position = EricLineEditSide.LEFT |
188 if position == EricLineEditSide.LEFT: |
195 if position == EricLineEditSide.LEFT: |
189 self.__leftLayout.addWidget(widget) |
196 self.__leftLayout.addWidget(widget) |
190 else: |
197 else: |
191 self.__rightLayout.insertWidget(1, widget) |
198 self.__rightLayout.insertWidget(1, widget) |
192 |
199 |
193 def removeWidget(self, widget): |
200 def removeWidget(self, widget): |
194 """ |
201 """ |
195 Public method to remove a widget from a side. |
202 Public method to remove a widget from a side. |
196 |
203 |
197 @param widget reference to the widget to remove |
204 @param widget reference to the widget to remove |
198 @type QWidget |
205 @type QWidget |
199 """ |
206 """ |
200 if widget is None: |
207 if widget is None: |
201 return |
208 return |
202 |
209 |
203 self.__leftLayout.removeWidget(widget) |
210 self.__leftLayout.removeWidget(widget) |
204 self.__rightLayout.removeWidget(widget) |
211 self.__rightLayout.removeWidget(widget) |
205 widget.hide() |
212 widget.hide() |
206 |
213 |
207 def widgetSpacing(self): |
214 def widgetSpacing(self): |
208 """ |
215 """ |
209 Public method to get the side widget spacing. |
216 Public method to get the side widget spacing. |
210 |
217 |
211 @return side widget spacing (integer) |
218 @return side widget spacing (integer) |
212 """ |
219 """ |
213 return self.__leftLayout.spacing() |
220 return self.__leftLayout.spacing() |
214 |
221 |
215 def setWidgetSpacing(self, spacing): |
222 def setWidgetSpacing(self, spacing): |
216 """ |
223 """ |
217 Public method to set the side widget spacing. |
224 Public method to set the side widget spacing. |
218 |
225 |
219 @param spacing side widget spacing (integer) |
226 @param spacing side widget spacing (integer) |
220 """ |
227 """ |
221 self.__leftLayout.setSpacing(spacing) |
228 self.__leftLayout.setSpacing(spacing) |
222 self.__rightLayout.setSpacing(spacing) |
229 self.__rightLayout.setSpacing(spacing) |
223 self._updateTextMargins() |
230 self._updateTextMargins() |
224 |
231 |
225 def textMargin(self, position): |
232 def textMargin(self, position): |
226 """ |
233 """ |
227 Public method to get the text margin for a side. |
234 Public method to get the text margin for a side. |
228 |
235 |
229 @param position side to get margin for |
236 @param position side to get margin for |
230 @type EricLineEditSide |
237 @type EricLineEditSide |
231 @return text margin |
238 @return text margin |
232 @rtype int |
239 @rtype int |
233 """ |
240 """ |
234 spacing = self.__rightLayout.spacing() |
241 spacing = self.__rightLayout.spacing() |
235 w = 0 |
242 w = 0 |
236 w = ( |
243 w = ( |
237 self.__leftWidget.sizeHint().width() |
244 self.__leftWidget.sizeHint().width() |
238 if position == EricLineEditSide.LEFT else |
245 if position == EricLineEditSide.LEFT |
239 self.__rightWidget.sizeHint().width() |
246 else self.__rightWidget.sizeHint().width() |
240 ) |
247 ) |
241 if w == 0: |
248 if w == 0: |
242 return 0 |
249 return 0 |
243 return w + spacing * 2 |
250 return w + spacing * 2 |
244 |
251 |