def setText(self, text): if len(text) == 0: return lines = text.split('\n') qf = QFontMetrics(self.font) fmw = max(qf.maxWidth(), 10) nlines = [] w = self.width - 2 * self.margin for line in lines: if qf.width(line) > w: while qf.width(line) > w: for i in xrange(w / fmw, len(line)): if qf.width(line, i) > w: if line[i].isalnum() and line[i - 1].isalnum(): nlines.append(line[0:i - 1] + ( '-' if line[i - 2].isalnum() else '')) line = line[i - 1:] else: nlines.append(line[0:i]) line = line[i:] break nlines.append(QString(line)) else: nlines.append(QString(line)) self.__text = nlines self.__computeTextPosition()
def drawInitialization(self): progress = self.doInitialisation() msg = "Initialisation ... " + progress fm = QFontMetrics(self.font()) tw = fm.width(msg) self.drawText((self.width() - tw) / 2, self.height() / 2, msg) QTimer.singleShot(1, self.updateGL)
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 mouseMoveEvent(self, event): if self.drag is not None: QTreeView.mouseMoveEvent(self, event) return if ((event.globalPos() - self.mouse_press_qpoint).manhattanLength() < QApplication.startDragDistance()): return # # starting a drag # [logic bug, after bruce change 070507: should not do this # if we already started dragging out a selection. How can we tell? # Only by whether the initial press had eventInRect, I think # (not yet recorded), or at least, the initial move (#e could record here).] # index = self.indexAt(event.pos()) sellst = self.selectedList() # bruce 070507 move earlier DEBUG2 = True if index.isValid(): thisnode = index.internalPointer().node #bruce 070507 bring in some code from modelTreeGui.py alreadySelected = (thisnode in sellst) item = index.internalPointer() rect = self.visualRect(index) if DEBUG2: print "visualRect coords", rect.left(), rect.right(), rect.top( ), rect.bottom() qfm = QFontMetrics(QLineEdit(self).font()) rect.setWidth(qfm.width(item.node.name) + _ICONSIZE[0] + 4) if DEBUG2: print "visualRect coords, modified:", rect.left(), rect.right( ), rect.top(), rect.bottom() # looks like icon and text, a bit taller than text (guesses) eventInRect = rect.contains(event.pos()) if DEBUG2: print "valid index: eventInRect = %r, item = %r, index = %r, alreadySelected = %r" % \ (eventInRect, item, index, alreadySelected)####### else: thisnode = item = None alreadySelected = eventInRect = False if not eventInRect: # nothing to drag, but [bruce 070507] let super handle it (for dragging over nodes to select) self.drag_is_not_DND = True ### not yet used QTreeView.mouseMoveEvent(self, event) return if thisnode in sellst: # if dragging something selected, drag along all other selected ones dragged_nodes = sellst else: # if dragging something unselected, ignore any selected ones dragged_nodes = [thisnode] qdrag = QDrag(self) drag_type = 'move' # how do I decide between 'move' and 'copy'? self.drag = (dragged_nodes, drag_type, qdrag) mimedata = QMimeData() mimedata.setText("need a string here for a valid mimetype") qdrag.setMimeData(mimedata) display_prefs = {} pixmap = dragged_nodes[0].node_icon(display_prefs) qdrag.setPixmap(pixmap) qdrag.setHotSpot(QPoint(-8, 8)) qdrag.start()
def mouseMoveEvent(self, event): if self.drag is not None: QTreeView.mouseMoveEvent(self, event) return if ((event.globalPos() - self.mouse_press_qpoint).manhattanLength() < QApplication.startDragDistance()): return # # starting a drag # [logic bug, after bruce change 070507: should not do this # if we already started dragging out a selection. How can we tell? # Only by whether the initial press had eventInRect, I think # (not yet recorded), or at least, the initial move (#e could record here).] # index = self.indexAt(event.pos()) sellst = self.selectedList() # bruce 070507 move earlier DEBUG2 = True if index.isValid(): thisnode = index.internalPointer().node #bruce 070507 bring in some code from modelTreeGui.py alreadySelected = (thisnode in sellst) item = index.internalPointer() rect = self.visualRect(index) if DEBUG2: print "visualRect coords",rect.left(), rect.right(), rect.top(), rect.bottom() qfm = QFontMetrics(QLineEdit(self).font()) rect.setWidth(qfm.width(item.node.name) + _ICONSIZE[0] + 4) if DEBUG2: print "visualRect coords, modified:",rect.left(), rect.right(), rect.top(), rect.bottom() # looks like icon and text, a bit taller than text (guesses) eventInRect = rect.contains(event.pos()) if DEBUG2: print "valid index: eventInRect = %r, item = %r, index = %r, alreadySelected = %r" % \ (eventInRect, item, index, alreadySelected)####### else: thisnode = item = None alreadySelected = eventInRect = False if not eventInRect: # nothing to drag, but [bruce 070507] let super handle it (for dragging over nodes to select) self.drag_is_not_DND = True ### not yet used QTreeView.mouseMoveEvent(self, event) return if thisnode in sellst: # if dragging something selected, drag along all other selected ones dragged_nodes = sellst else: # if dragging something unselected, ignore any selected ones dragged_nodes = [ thisnode ] qdrag = QDrag(self) drag_type = 'move' # how do I decide between 'move' and 'copy'? self.drag = (dragged_nodes, drag_type, qdrag) mimedata = QMimeData() mimedata.setText("need a string here for a valid mimetype") qdrag.setMimeData(mimedata) display_prefs = { } pixmap = dragged_nodes[0].node_icon(display_prefs) qdrag.setPixmap(pixmap) qdrag.setHotSpot(QPoint(-8, 8)) qdrag.start()
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