def __computeTextPosition(self): if not self.__text is None: qf = QFontMetrics(self.font) self.__textx = self.x + self.margin self.__texty = self.y + self.margin + qf.ascent() + ( self.height - 2 * self.margin - len(self.__text) * qf.height()) / 2 self.__texth = qf.height()
def do_size_hint(self, option, index): text = index.data(Qt.DisplayRole).toString() font = QFont(option.font) font.setPointSize(QFontInfo(font).pointSize() * 1.5) m = QFontMetrics(font) return QSize(m.width(text), m.height())
def renderTextNearCursor(self, textString, offset = 10, textColor = black, fontSize = 11): """ Renders text near the cursor position, on the top right side of the cursor (slightly above it). See example in DNA Line mode. @param textString: string @param offset: The offset that will be added to x and y values of the cursor position to get the base position of the text to be rendered. @see: DnaLineMode.Draw @see: self._getFontForTextNearCursor() @see: self.renderTextAtPosition() """ if not textString: return #Extra precaution if the caller passes a junk value such as None #for the color if not isinstance(textColor, tuple) and isinstance(textColor, list): textColor = black pos = self.cursor().pos() # x, y coordinates need to be in window coordinate system. # See QGLWidget.mapToGlobal for more details. pos = self.mapFromGlobal(pos) # Important to turn off the lighting. Otherwise the text color would # be dull and may also become even more light if some other object # is rendered as a transparent object. Example in DNA Line mode, when the # second axis end sphere is rendered as a transparent sphere, it affects # the text rendering as well (if GL_LIGHTING is not disabled) # [-- Ninad 2007-12-03] glDisable(GL_LIGHTING) #Add 'stoppers' for the cursor text. Example: If the cursor is near the #extreme right hand corner of the 3D workspace, the following code #ensures that all the text string is visible. It does this check for #right(x) and top(for y) borders of the glpane. xOffset = offset yOffset = offset #signForDX and signForDY are used by the code that draws the same #text in the background (offset by 1 pixel in 4 directions) signForDX = 1 signForDY = 1 xLimit = self.width - pos.x() #Note that at the top edge, y coord is 0 yLimit = pos.y() textString = QString(textString) font = self._getFontForTextNearCursor(fontSize = fontSize, isBold = True) #Now determine the total x and y pixels used to render the text #(add some tolerance to that number) fm = QFontMetrics(font) xPixels = fm.width(textString) + 10 yPixels = fm.height() + 10 if xLimit < xPixels: xOffset = - (xPixels - xLimit) signForDX = -1 if yLimit < yPixels: yOffset = - (yPixels - pos.y()) signForDY = -1 x = pos.x() + xOffset y = pos.y() - yOffset offset_val = 1 deltas_for_halo_color = (( offset_val, offset_val), (-offset_val, -offset_val), (-offset_val, offset_val), ( offset_val, -offset_val)) # halo color halo_color = getTextHaloColor(textColor) for dx, dy in deltas_for_halo_color: self.qglColor(RGBf_to_QColor(halo_color)) # Note: self.renderText is QGLWidget.renderText method. self.renderText(x + dx*signForDX , y + dy*signForDY, textString, font) ## self.qglClearColor(RGBf_to_QColor(halo_color)) ## # REVIEW: why is qglClearColor needed here? Why is it done *after* renderText? ## # [bruce 081204 questions; same Qs for the other uses of qglClearColor in this file] # Note: It is necessary to set the font color, otherwise it may change! self.qglColor(RGBf_to_QColor(textColor)) x = pos.x() + xOffset y = pos.y() - yOffset self.renderText(x , y , textString, font) ## self.qglClearColor(RGBf_to_QColor(textColor)) ## # is qglClearColor related to glClearColor? [bruce 071214 question] glEnable(GL_LIGHTING)
def do_size_hint(self, option, index): text = index.data(Qt.DisplayRole).toString() font = QFont(option.font) font.setPointSize(QFontInfo(font).pointSize() * 1.5) m = QFontMetrics(font) return QSize(m.width(text), m.height())
def __computeTextPosition(self): if not self.__text is None: qf = QFontMetrics(self.parent.font()) self.__textx = self.x + (self.width - qf.width(self.__text)) / 2 self.__texty = self.y + qf.ascent() + (self.height - qf.height()) / 2