E5Gui/E5LineEdit.py

changeset 2990
583beaf0b4b8
parent 2953
703452a2876f
child 3057
10516539f238
child 3160
209a07d7e401
equal deleted inserted replaced
2989:7efa8b8b6903 2990:583beaf0b4b8
6 """ 6 """
7 Module implementing specialized line edits. 7 Module implementing specialized line edits.
8 """ 8 """
9 9
10 from PyQt4.QtCore import pyqtSignal, Qt, QEvent, qVersion 10 from PyQt4.QtCore import pyqtSignal, Qt, QEvent, qVersion
11 from PyQt4.QtGui import QLineEdit, QStyle, QPainter, QPalette, QStyleOptionFrameV2, \ 11 from PyQt4.QtGui import QLineEdit, QStyle, QPainter, QPalette, \
12 QWidget, QHBoxLayout, QBoxLayout, QLayout, QApplication, QSpacerItem, QSizePolicy 12 QStyleOptionFrameV2, QWidget, QHBoxLayout, QBoxLayout, QLayout, \
13 QApplication, QSpacerItem, QSizePolicy
13 14
14 import UI.PixmapCache 15 import UI.PixmapCache
15 16
16 17
17 class E5LineEditSideWidget(QWidget): 18 class E5LineEditSideWidget(QWidget):
85 if self.isRightToLeft(): 86 if self.isRightToLeft():
86 self.__rightLayout.setDirection(QBoxLayout.RightToLeft) 87 self.__rightLayout.setDirection(QBoxLayout.RightToLeft)
87 else: 88 else:
88 self.__rightLayout.setDirection(QBoxLayout.LeftToRight) 89 self.__rightLayout.setDirection(QBoxLayout.LeftToRight)
89 90
90 horizontalSpacer = QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum) 91 horizontalSpacer = QSpacerItem(
92 0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum)
91 self.__mainLayout.addWidget( 93 self.__mainLayout.addWidget(
92 self.__leftWidget, 0, Qt.AlignVCenter | Qt.AlignLeft) 94 self.__leftWidget, 0, Qt.AlignVCenter | Qt.AlignLeft)
93 self.__mainLayout.addItem(horizontalSpacer) 95 self.__mainLayout.addItem(horizontalSpacer)
94 self.__mainLayout.addWidget( 96 self.__mainLayout.addWidget(
95 self.__rightWidget, 0, Qt.AlignVCenter | Qt.AlignRight) 97 self.__rightWidget, 0, Qt.AlignVCenter | Qt.AlignRight)
148 if not self.text() and \ 150 if not self.text() and \
149 self.__inactiveText and \ 151 self.__inactiveText and \
150 not self.hasFocus(): 152 not self.hasFocus():
151 panel = QStyleOptionFrameV2() 153 panel = QStyleOptionFrameV2()
152 self.initStyleOption(panel) 154 self.initStyleOption(panel)
153 textRect = \ 155 textRect = self.style().subElementRect(
154 self.style().subElementRect(QStyle.SE_LineEditContents, panel, self) 156 QStyle.SE_LineEditContents, panel, self)
155 textRect.adjust(2, 0, 0, 0) 157 textRect.adjust(2, 0, 0, 0)
156 left = self.textMargin(self.LeftSide) 158 left = self.textMargin(self.LeftSide)
157 right = self.textMargin(self.RightSide) 159 right = self.textMargin(self.RightSide)
158 textRect.adjust(left, 0, -right, 0) 160 textRect.adjust(left, 0, -right, 0)
159 painter = QPainter(self) 161 painter = QPainter(self)
160 painter.setPen(self.palette().brush( 162 painter.setPen(self.palette().brush(
161 QPalette.Disabled, QPalette.Text).color()) 163 QPalette.Disabled, QPalette.Text).color())
162 painter.drawText( 164 painter.drawText(
163 textRect, Qt.AlignLeft | Qt.AlignVCenter, self.__inactiveText) 165 textRect, Qt.AlignLeft | Qt.AlignVCenter,
166 self.__inactiveText)
164 167
165 def _updateTextMargins(self): 168 def _updateTextMargins(self):
166 """ 169 """
167 Protected slot to update the text margins. 170 Protected slot to update the text margins.
168 """ 171 """
178 def addWidget(self, widget, position): 181 def addWidget(self, widget, position):
179 """ 182 """
180 Public method to add a widget to a side. 183 Public method to add a widget to a side.
181 184
182 @param widget reference to the widget to add (QWidget) 185 @param widget reference to the widget to add (QWidget)
183 @param position position to add to (E5LineEdit.LeftSide, E5LineEdit.RightSide) 186 @param position position to add to (E5LineEdit.LeftSide,
187 E5LineEdit.RightSide)
184 """ 188 """
185 if widget is None: 189 if widget is None:
186 return 190 return
187 191
188 if self.isRightToLeft(): 192 if self.isRightToLeft():
228 232
229 def textMargin(self, position): 233 def textMargin(self, position):
230 """ 234 """
231 Public method to get the text margin for a side. 235 Public method to get the text margin for a side.
232 236
233 @param position side to get margin for (E5LineEdit.LeftSide, E5LineEdit.RightSide) 237 @param position side to get margin for (E5LineEdit.LeftSide,
238 E5LineEdit.RightSide)
234 @return text margin (integer) 239 @return text margin (integer)
235 """ 240 """
236 spacing = self.__rightLayout.spacing() 241 spacing = self.__rightLayout.spacing()
237 w = 0 242 w = 0
238 if position == self.LeftSide: 243 if position == self.LeftSide:
267 self.setPlaceholderText(inactiveText) 272 self.setPlaceholderText(inactiveText)
268 273
269 274
270 class E5ClearableLineEdit(E5LineEdit): 275 class E5ClearableLineEdit(E5LineEdit):
271 """ 276 """
272 Class implementing a line edit widget showing some inactive text and a clear button, 277 Class implementing a line edit widget showing some inactive text and a
273 if it has some contents. 278 clear button, if it has some contents.
274 """ 279 """
275 def __init__(self, parent=None, inactiveText="", side=E5LineEdit.RightSide): 280 def __init__(self, parent=None, inactiveText="",
281 side=E5LineEdit.RightSide):
276 """ 282 """
277 Constructor 283 Constructor
278 284
279 @param parent reference to the parent widget (QWidget) 285 @param parent reference to the parent widget (QWidget)
280 @keyparam inactiveText text to be shown on inactivity (string) 286 @keyparam inactiveText text to be shown on inactivity (string)
281 @keyparam side side the clear button should be shown at (E5LineEdit.RightSide, 287 @keyparam side side the clear button should be shown at
282 E5LineEdit.LeftSide) 288 (E5LineEdit.RightSide, E5LineEdit.LeftSide)
283 """ 289 """
284 assert side in [E5LineEdit.RightSide, E5LineEdit.LeftSide] 290 assert side in [E5LineEdit.RightSide, E5LineEdit.LeftSide]
285 291
286 super().__init__(parent, inactiveText) 292 super().__init__(parent, inactiveText)
287 293

eric ide

mercurial