def paintEvent(self, event): super(PromptLineEdit, self).paintEvent(event) qt_api = os.environ['QT_API'].lower() if self._prompt_text and not self.text() and self.isEnabled(): if qt_api in PYQT4_API: from PyQt4.QtGui import QStyleOptionFrameV3 option = QStyleOptionFrameV3() elif qt_api in PYSIDE_API: from PySide.QtGui import QStyleOptionFrameV3 option = QStyleOptionFrameV3() elif qt_api in PYQT5_API: from PyQt5.QtWidgets import QStyleOptionFrame option = QStyleOptionFrame() else: msg = 'Qt bindings "%s" is not supported' % qt_api raise PythonQtError(msg) self.initStyleOption(option) left, top, right, bottom = self.getTextMargins() va = self.style().visualAlignment(self.layoutDirection(), self.alignment()) rect = self.style().subElementRect( QtWidgets.QStyle.SE_LineEditContents, option, self).adjusted(2, 0, 0, 0).adjusted(left, top, -right, -bottom) fm = QtGui.QFontMetrics(self.font()) text = fm.elidedText(self._prompt_text, QtCore.Qt.ElideRight, rect.width()) painter = QtGui.QPainter(self) painter.setPen(self.palette().color(QtGui.QPalette.Disabled, QtGui.QPalette.Text)) painter.drawText(rect, va, text)
def sizeHint(self): # Most of this is taken from QLineEdit's sources, the difference # is only in the use of minimumContentsLength instead of a # hardcoded constant. self.ensurePolished() fm = self.fontMetrics() textmargins = self.textMargins() contentsmargins = self.contentsMargins() h = (max(fm.height(), 14) + 2 * self._verticalMargin + textmargins.top() + textmargins.bottom() + contentsmargins.top() + contentsmargins.bottom()) nchar = self.__minimumContentsLength if not nchar > 0: nchar = 17 w = (fm.width("X") * nchar + 2 * self._horizontalMargin + textmargins.left() + textmargins.right() + contentsmargins.left() + contentsmargins.right()) opt = QStyleOptionFrameV3() self.initStyleOption(opt) size = self.style().sizeFromContents( QStyle.CT_LineEdit, opt, QSize(w, h).expandedTo(QApplication.globalStrut()), self) return size
def drawBorder(self, painter): """ Draw the border of the plot canvas :param QPainter painter: Painter .. seealso:: :py:meth:`setBorderRadius()` """ if self.__data.borderRadius > 0: if self.frameWidth() > 0: QwtPainter.drawRoundedFrame( painter, QRectF(self.frameRect()), self.__data.borderRadius, self.__data.borderRadius, self.palette(), self.frameWidth(), self.frameStyle(), ) else: opt = QStyleOptionFrame() opt.initFrom(self) try: shape_mask = QFrame.Shape_Mask.value shadow_mask = QFrame.Shadow_Mask.value except AttributeError: shape_mask = QFrame.Shape_Mask shadow_mask = QFrame.Shadow_Mask frameShape = self.frameStyle() & shape_mask frameShadow = self.frameStyle() & shadow_mask opt.frameShape = QFrame.Shape(int(opt.frameShape) | frameShape) if frameShape in ( QFrame.Box, QFrame.HLine, QFrame.VLine, QFrame.StyledPanel, QFrame.Panel, ): opt.lineWidth = self.lineWidth() opt.midLineWidth = self.midLineWidth() else: opt.lineWidth = self.frameWidth() if frameShadow == self.Sunken: opt.state |= QStyle.State_Sunken elif frameShadow == self.Raised: opt.state |= QStyle.State_Raised self.style().drawControl(QStyle.CE_ShapedFrame, opt, painter, self)
def paintEvent(self, event ): """QLineEdit.paintEvent implementation. Draws prompt """ QLineEdit.paintEvent( self, event ) if self._promptText and not self.text() and self.isEnabled() : option = QStyleOptionFrameV3() self.initStyleOption( option ) left, top, right, bottom = self.getTextMargins() va = self.style().visualAlignment( self.layoutDirection(), self.alignment() ) rect = self.style().subElementRect( QStyle.SE_LineEditContents, option, self ).adjusted( 2, 0, 0, 0 ).adjusted( left, top, -right, -bottom ) fm = QFontMetrics ( self.font() ) text = fm.elidedText( self._promptText, Qt.ElideRight, rect.width() ) painter = QPainter ( self ) painter.setPen( self.palette().color( QPalette.Disabled, QPalette.Text ) ) painter.drawText( rect, va, text )
def drawCanvas(self, painter, withBackground): hackStyledBackground = False if (withBackground and self.testAttribute(Qt.WA_StyledBackground) and self.testPaintAttribute(self.HackStyledBackground)): # Antialiasing rounded borders is done by # inserting pixels with colors between the # border color and the color on the canvas, # When the border is painted before the plot items # these colors are interpolated for the canvas # and the plot items need to be clipped excluding # the anialiased pixels. In situations, where # the plot items fill the area at the rounded # borders this is noticeable. # The only way to avoid these annoying "artefacts" # is to paint the border on top of the plot items. if (self.__data.styleSheet.hasBorder and not self.__data.styleSheet.borderPath.isEmpty()): # We have a border with at least one rounded corner hackStyledBackground = True if withBackground: painter.save() if self.testAttribute(Qt.WA_StyledBackground): if hackStyledBackground: # paint background without border painter.setPen(Qt.NoPen) painter.setBrush(self.__data.styleSheet.background.brush) painter.setBrushOrigin( self.__data.styleSheet.background.origin) painter.setClipPath(self.__data.styleSheet.borderPath) painter.drawRect(self.contentsRect()) else: qwtDrawStyledBackground(self, painter) elif self.autoFillBackground(): painter.setPen(Qt.NoPen) painter.setBrush(self.palette().brush(self.backgroundRole())) if self.__data.borderRadius > 0.0 and self.rect( ) == self.frameRect(): if self.frameWidth() > 0: painter.setClipPath(self.borderPath(self.rect())) painter.drawRect(self.rect()) else: painter.setRenderHint(QPainter.Antialiasing, True) painter.drawPath(self.borderPath(self.rect())) else: painter.drawRect(self.rect()) painter.restore() painter.save() if not self.__data.styleSheet.borderPath.isEmpty(): painter.setClipPath(self.__data.styleSheet.borderPath, Qt.IntersectClip) else: if self.__data.borderRadius > 0.0: painter.setClipPath(self.borderPath(self.frameRect()), Qt.IntersectClip) else: # print('**DEBUG: QwtPlotCanvas.drawCanvas') painter.setClipRect(self.contentsRect(), Qt.IntersectClip) self.plot().drawCanvas(painter) painter.restore() if withBackground and hackStyledBackground: # Now paint the border on top opt = QStyleOptionFrame() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Frame, opt, painter, self)