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) |
163 |
170 |
164 def addWidget(self, widget, position): |
171 def addWidget(self, widget, position): |
165 """ |
172 """ |
166 Public method to add a widget to a side. |
173 Public method to add a widget to a side. |
167 |
174 |
168 @param widget reference to the widget to add (QWidget) |
175 @param widget reference to the widget to add |
169 @param position position to add to (E5LineEdit.LeftSide, |
176 @type QWidget |
170 E5LineEdit.RightSide) |
177 @param position position to add to |
|
178 @type E5LineEditSide |
171 """ |
179 """ |
172 if widget is None: |
180 if widget is None: |
173 return |
181 return |
174 |
182 |
175 if self.isRightToLeft(): |
183 if self.isRightToLeft(): |
176 if position == self.LeftSide: |
184 if position == E5LineEditSide.LEFT: |
177 position = self.RightSide |
185 position = E5LineEditSide.RIGHT |
178 else: |
186 else: |
179 position = self.LeftSide |
187 position = E5LineEditSide.LEFT |
180 if position == self.LeftSide: |
188 if position == E5LineEditSide.LEFT: |
181 self.__leftLayout.addWidget(widget) |
189 self.__leftLayout.addWidget(widget) |
182 else: |
190 else: |
183 self.__rightLayout.insertWidget(1, widget) |
191 self.__rightLayout.insertWidget(1, widget) |
184 |
192 |
185 def removeWidget(self, widget): |
193 def removeWidget(self, widget): |
186 """ |
194 """ |
187 Public method to remove a widget from a side. |
195 Public method to remove a widget from a side. |
188 |
196 |
189 @param widget reference to the widget to remove (QWidget) |
197 @param widget reference to the widget to remove |
|
198 @type QWidget |
190 """ |
199 """ |
191 if widget is None: |
200 if widget is None: |
192 return |
201 return |
193 |
202 |
194 self.__leftLayout.removeWidget(widget) |
203 self.__leftLayout.removeWidget(widget) |
215 |
224 |
216 def textMargin(self, position): |
225 def textMargin(self, position): |
217 """ |
226 """ |
218 Public method to get the text margin for a side. |
227 Public method to get the text margin for a side. |
219 |
228 |
220 @param position side to get margin for (E5LineEdit.LeftSide, |
229 @param position side to get margin for |
221 E5LineEdit.RightSide) |
230 @type E5LineEditSide |
222 @return text margin (integer) |
231 @return text margin |
|
232 @rtype int |
223 """ |
233 """ |
224 spacing = self.__rightLayout.spacing() |
234 spacing = self.__rightLayout.spacing() |
225 w = 0 |
235 w = 0 |
226 w = ( |
236 w = ( |
227 self.__leftWidget.sizeHint().width() |
237 self.__leftWidget.sizeHint().width() |
228 if position == self.LeftSide else |
238 if position == E5LineEditSide.LEFT else |
229 self.__rightWidget.sizeHint().width() |
239 self.__rightWidget.sizeHint().width() |
230 ) |
240 ) |
231 if w == 0: |
241 if w == 0: |
232 return 0 |
242 return 0 |
233 return w + spacing * 2 |
243 return w + spacing * 2 |
253 """ |
263 """ |
254 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 |
255 clear button, if it has some contents. |
265 clear button, if it has some contents. |
256 """ |
266 """ |
257 def __init__(self, parent=None, inactiveText="", |
267 def __init__(self, parent=None, inactiveText="", |
258 side=E5LineEdit.RightSide): |
268 side=E5LineEditSide.RIGHT): |
259 """ |
269 """ |
260 Constructor |
270 Constructor |
261 |
271 |
262 @param parent reference to the parent widget (QWidget) |
272 @param parent reference to the parent widget |
263 @param inactiveText text to be shown on inactivity (string) |
273 @type QWidget |
|
274 @param inactiveText text to be shown on inactivity |
|
275 @type str |
264 @param side side the clear button should be shown at |
276 @param side side the clear button should be shown at |
265 (E5LineEdit.RightSide, E5LineEdit.LeftSide) |
277 @type E5LineEditSide |
266 @exception ValueError raised to indicate a bad parameter value |
278 """ |
267 """ |
|
268 if side not in [E5LineEdit.RightSide, E5LineEdit.LeftSide]: |
|
269 raise ValueError("Bad value for 'side' parameter.") |
|
270 |
|
271 super().__init__(parent, inactiveText) |
279 super().__init__(parent, inactiveText) |
272 |
280 |
273 from E5Gui.E5LineEditButton import E5LineEditButton |
281 from E5Gui.E5LineEditButton import E5LineEditButton |
274 self.__clearButton = E5LineEditButton(self) |
282 self.__clearButton = E5LineEditButton(self) |
275 self.__clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft")) |
283 self.__clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft")) |