4 # |
4 # |
5 |
5 |
6 """ |
6 """ |
7 Module implementing specialized line edits. |
7 Module implementing specialized line edits. |
8 """ |
8 """ |
|
9 |
|
10 import enum |
9 |
11 |
10 from PyQt5.QtCore import pyqtSignal, Qt, QEvent |
12 from PyQt5.QtCore import pyqtSignal, Qt, QEvent |
11 from PyQt5.QtWidgets import ( |
13 from PyQt5.QtWidgets import ( |
12 QLineEdit, QWidget, QHBoxLayout, QBoxLayout, QLayout, QApplication, |
14 QLineEdit, QWidget, QHBoxLayout, QBoxLayout, QLayout, QApplication, |
13 QSpacerItem, QSizePolicy |
15 QSpacerItem, QSizePolicy |
42 if evt.type() == QEvent.Type.LayoutRequest: |
44 if evt.type() == QEvent.Type.LayoutRequest: |
43 self.sizeHintChanged.emit() |
45 self.sizeHintChanged.emit() |
44 return QWidget.event(self, evt) |
46 return QWidget.event(self, evt) |
45 |
47 |
46 |
48 |
|
49 class E5LineEditSide(enum.Enum): |
|
50 """ |
|
51 Class defining the line edit sides. |
|
52 """ |
|
53 LEFT = 0 |
|
54 RIGHT = 1 |
|
55 |
|
56 |
47 class E5LineEdit(QLineEdit): |
57 class E5LineEdit(QLineEdit): |
48 """ |
58 """ |
49 Class implementing a line edit widget showing some inactive text. |
59 Class implementing a line edit widget showing some inactive text. |
50 """ |
60 """ |
51 LeftSide = 0 |
|
52 RightSide = 1 |
|
53 |
|
54 def __init__(self, parent=None, inactiveText=""): |
61 def __init__(self, parent=None, inactiveText=""): |
55 """ |
62 """ |
56 Constructor |
63 Constructor |
57 |
64 |
58 @param parent reference to the parent widget (QWidget) |
65 @param parent reference to the parent widget (QWidget) |
59 @param inactiveText text to be shown on inactivity (string) |
66 @param inactiveText text to be shown on inactivity (string) |
60 """ |
67 """ |
61 super(E5LineEdit, self).__init__(parent) |
68 super().__init__(parent) |
62 |
69 |
63 self.setMinimumHeight(22) |
70 self.setMinimumHeight(22) |
64 |
71 |
65 self.setPlaceholderText(inactiveText) |
72 self.setPlaceholderText(inactiveText) |
66 |
73 |
149 |
156 |
150 def _updateTextMargins(self): |
157 def _updateTextMargins(self): |
151 """ |
158 """ |
152 Protected slot to update the text margins. |
159 Protected slot to update the text margins. |
153 """ |
160 """ |
154 if self.__leftMargin == 0: |
161 left = ( |
155 left = self.__leftWidget.sizeHint().width() |
162 self.__leftWidget.sizeHint().width() |
156 else: |
163 if self.__leftMargin == 0 else |
157 left = self.__leftMargin |
164 self.__leftMargin |
|
165 ) |
158 right = self.__rightWidget.sizeHint().width() |
166 right = self.__rightWidget.sizeHint().width() |
159 top = 0 |
167 top = 0 |
160 bottom = 0 |
168 bottom = 0 |
161 self.setTextMargins(left, top, right, bottom) |
169 self.setTextMargins(left, top, right, bottom) |
162 |
170 |
163 def addWidget(self, widget, position): |
171 def addWidget(self, widget, position): |
164 """ |
172 """ |
165 Public method to add a widget to a side. |
173 Public method to add a widget to a side. |
166 |
174 |
167 @param widget reference to the widget to add (QWidget) |
175 @param widget reference to the widget to add |
168 @param position position to add to (E5LineEdit.LeftSide, |
176 @type QWidget |
169 E5LineEdit.RightSide) |
177 @param position position to add to |
|
178 @type E5LineEditSide |
170 """ |
179 """ |
171 if widget is None: |
180 if widget is None: |
172 return |
181 return |
173 |
182 |
174 if self.isRightToLeft(): |
183 if self.isRightToLeft(): |
175 if position == self.LeftSide: |
184 if position == E5LineEditSide.LEFT: |
176 position = self.RightSide |
185 position = E5LineEditSide.RIGHT |
177 else: |
186 else: |
178 position = self.LeftSide |
187 position = E5LineEditSide.LEFT |
179 if position == self.LeftSide: |
188 if position == E5LineEditSide.LEFT: |
180 self.__leftLayout.addWidget(widget) |
189 self.__leftLayout.addWidget(widget) |
181 else: |
190 else: |
182 self.__rightLayout.insertWidget(1, widget) |
191 self.__rightLayout.insertWidget(1, widget) |
183 |
192 |
184 def removeWidget(self, widget): |
193 def removeWidget(self, widget): |
185 """ |
194 """ |
186 Public method to remove a widget from a side. |
195 Public method to remove a widget from a side. |
187 |
196 |
188 @param widget reference to the widget to remove (QWidget) |
197 @param widget reference to the widget to remove |
|
198 @type QWidget |
189 """ |
199 """ |
190 if widget is None: |
200 if widget is None: |
191 return |
201 return |
192 |
202 |
193 self.__leftLayout.removeWidget(widget) |
203 self.__leftLayout.removeWidget(widget) |
214 |
224 |
215 def textMargin(self, position): |
225 def textMargin(self, position): |
216 """ |
226 """ |
217 Public method to get the text margin for a side. |
227 Public method to get the text margin for a side. |
218 |
228 |
219 @param position side to get margin for (E5LineEdit.LeftSide, |
229 @param position side to get margin for |
220 E5LineEdit.RightSide) |
230 @type E5LineEditSide |
221 @return text margin (integer) |
231 @return text margin |
|
232 @rtype int |
222 """ |
233 """ |
223 spacing = self.__rightLayout.spacing() |
234 spacing = self.__rightLayout.spacing() |
224 w = 0 |
235 w = 0 |
225 if position == self.LeftSide: |
236 w = ( |
226 w = self.__leftWidget.sizeHint().width() |
237 self.__leftWidget.sizeHint().width() |
227 else: |
238 if position == E5LineEditSide.LEFT else |
228 w = self.__rightWidget.sizeHint().width() |
239 self.__rightWidget.sizeHint().width() |
|
240 ) |
229 if w == 0: |
241 if w == 0: |
230 return 0 |
242 return 0 |
231 return w + spacing * 2 |
243 return w + spacing * 2 |
232 |
244 |
233 def inactiveText(self): |
245 def inactiveText(self): |
251 """ |
263 """ |
252 Class implementing a line edit widget showing some inactive text and a |
264 Class implementing a line edit widget showing some inactive text and a |
253 clear button, if it has some contents. |
265 clear button, if it has some contents. |
254 """ |
266 """ |
255 def __init__(self, parent=None, inactiveText="", |
267 def __init__(self, parent=None, inactiveText="", |
256 side=E5LineEdit.RightSide): |
268 side=E5LineEditSide.RIGHT): |
257 """ |
269 """ |
258 Constructor |
270 Constructor |
259 |
271 |
260 @param parent reference to the parent widget (QWidget) |
272 @param parent reference to the parent widget |
261 @param inactiveText text to be shown on inactivity (string) |
273 @type QWidget |
|
274 @param inactiveText text to be shown on inactivity |
|
275 @type str |
262 @param side side the clear button should be shown at |
276 @param side side the clear button should be shown at |
263 (E5LineEdit.RightSide, E5LineEdit.LeftSide) |
277 @type E5LineEditSide |
264 @exception ValueError raised to indicate a bad parameter value |
278 """ |
265 """ |
279 super().__init__(parent, inactiveText) |
266 if side not in [E5LineEdit.RightSide, E5LineEdit.LeftSide]: |
|
267 raise ValueError("Bad value for 'side' parameter.") |
|
268 |
|
269 super(E5ClearableLineEdit, self).__init__(parent, inactiveText) |
|
270 |
280 |
271 from E5Gui.E5LineEditButton import E5LineEditButton |
281 from E5Gui.E5LineEditButton import E5LineEditButton |
272 self.__clearButton = E5LineEditButton(self) |
282 self.__clearButton = E5LineEditButton(self) |
273 self.__clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft")) |
283 self.__clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft")) |
274 self.addWidget(self.__clearButton, side) |
284 self.addWidget(self.__clearButton, side) |