def __reset(self): """Resets all the values to the default""" # Note: python 3.5 and 3.6 deepcopy() behaves different. The 3.6 # fails to copy QFont at all while 3.5 copies it improperly. # So to be 100% sure it works, here is a manual copying... self.__dirName = None self.__overrides = {} self.__userDirName = USER_SKIN_DIR + \ _DEFAULT_SKIN_SETTINGS['name'] + os.path.sep if not os.path.exists(self.__userDirName): self.__userDirName = None elif not os.path.isdir(self.__userDirName): self.__userDirName = None self.__values = {} for key, value in _DEFAULT_SKIN_SETTINGS.items(): if isinstance(value, QFont): self.__values[key] = QFont(_DEFAULT_SKIN_SETTINGS[key]) else: self.__values[key] = value for key, value in _DEFAULT_CFLOW_SETTINGS.items(): if isinstance(value, QFont): self.__values[key] = QFont(_DEFAULT_CFLOW_SETTINGS[key]) else: self.__values[key] = value self.__appCSS = deepcopy(_DEFAULT_APP_CSS) self.__applyOverrides() self.minTextZoom = self.__calculateMinTextZoom() self.minCFlowZoom = self.__calculateMinCFlowZoom()
def paint(self, painter, option, widget): """Draws the independent comment""" del option # unused argument del widget # unused argument settings = self.canvas.settings painter.setPen(self.getPainterPen(self.isSelected(), self.borderColor)) painter.setBrush(QBrush(self.bgColor)) rectWidth = self.minWidth - 2 * settings.hCellPadding rectHeight = self.minHeight - 2 * settings.vCellPadding painter.drawRoundedRect(self.baseX + settings.hCellPadding, self.baseY + settings.vCellPadding, rectWidth, rectHeight, 0, 0) if self.text: # Draw the text in the rectangle font = QFont(settings.monoFont) font.setItalic(True) painter.setFont(font) pen = QPen(self.fgColor) painter.setPen(pen) painter.drawText( self._leftEdge + settings.hCellPadding + settings.hDocLinkPadding + self.iconItem.iconWidth() + settings.hDocLinkPadding, self.baseY + settings.vCellPadding + settings.vDocLinkPadding, self.textRect.width(), self.textRect.height(), Qt.AlignLeft, self.text)
def detectRevisionMarginWidth(self): """Caculates the margin width depending on the margin font and the current zoom""" skin = GlobalData().skin font = QFont(skin.lineNumFont) font.setPointSize(font.pointSize() + self.getZoom()) fontMetrics = QFontMetrics(font, self) return fontMetrics.width('W' * self.__maxLength) + 3
def populateSampleSkin(): """Populates the sample skin in the user directory""" dName = USER_SKIN_DIR + SAMPLE_SKIN + os.path.sep if os.path.exists(dName): if not os.path.isdir(dName): logging.error( 'Error creating a sample skin at %s. ' 'The file system entry is already occupied ' 'by something else', dName) return # Try to create the dir try: os.makedirs(dName, exist_ok=True) except Exception as exc: logging.error('Error creating a sample skin at %s: %s', dName, str(exc)) fName = dName + 'app.css' try: saveToFile(fName, _DEFAULT_APP_CSS) except Exception as exc: logging.error( 'Error creating a sample skin at %s. ' 'Cannot write app.css: %s', dName, str(exc)) fName = dName + 'skin.json' try: values = {} for key, value in _DEFAULT_SKIN_SETTINGS.items(): if isinstance(value, QFont): values[key] = QFont(_DEFAULT_SKIN_SETTINGS[key]) else: values[key] = value values['name'] = SAMPLE_SKIN with open(fName, 'w', encoding=DEFAULT_ENCODING) as diskfile: json.dump(values, diskfile, indent=4, default=colorFontToJSON) except Exception as exc: logging.error( 'Error creating a sample skin at %s. ' 'Cannot write skin.json: %s', dName, str(exc)) fName = dName + 'cflow.json' try: values = {} for key, value in _DEFAULT_CFLOW_SETTINGS.items(): if isinstance(value, QFont): values[key] = QFont(_DEFAULT_CFLOW_SETTINGS[key]) else: values[key] = value with open(fName, 'w', encoding=DEFAULT_ENCODING) as diskfile: json.dump(values, diskfile, indent=4, default=colorFontToJSON) except Exception as exc: logging.error( 'Error creating a sample skin at %s. ' 'Cannot write cflow.json: %s', dName, str(exc))
def paint(self, painter, option, widget): """Draws a filled rectangle and then adds a title""" # Draw the rectangle QGraphicsRectItem.paint(self, painter, option, widget) # Draw text over the rectangle font = QFont("Arial", 10) font.setBold(True) painter.setFont(font) painter.setPen(QPen(QColor(90, 90, 88))) painter.drawText(self.__node.posX - self.__node.width / 2.0, self.__node.posY - self.__node.height / 2.0, self.__node.width, self.__node.height, Qt.AlignCenter, self.__node.label)
def __setRevisionText(self): """Sets the revision margin text""" for revNumber in self.__revisionInfo: author = self.__revisionInfo[revNumber]['author'] if '@' in author: # Most probably this is an e-mail address. Leave just name. self.__revisionInfo[revNumber]['shortAuthor'] = author.split( '@')[0] else: self.__revisionInfo[revNumber]['shortAuthor'] = author skin = GlobalData().skin revisionMarginFont = QFont(skin.lineNumFont) revisionMarginFont.setItalic(True) style = QsciStyle(-1, "Revision margin style", skin.revisionMarginColor, skin.revisionMarginPaper, revisionMarginFont) lineNumber = 0 self.__maxLength = -1 # Altering line background support currentRevision = -1 needMarker = True for lineRevision in self.__lineRevisions: if lineRevision in self.__revisionInfo: marginText = " " + ":".join([ str(lineRevision), self.__revisionInfo[lineRevision]['shortAuthor'] ]) else: marginText = " " + str(lineRevision) textLength = len(marginText) if textLength > self.__maxLength: self.__maxLength = textLength self.setMarginText(lineNumber, marginText, style) # Set the line background if needed if lineRevision != currentRevision: currentRevision = lineRevision needMarker = not needMarker if needMarker: self.markerAdd(lineNumber, self.__alterMarker) lineNumber += 1 self.setRevisionMarginWidth()
def paint(self, painter, option, widget): """Draws a filled rectangle and then adds a title""" # Hide the dotted outline for clickable system modules itemOption = QStyleOptionGraphicsItem(option) if itemOption.state & QStyle.State_Selected != 0: itemOption.state = itemOption.state & ~QStyle.State_Selected # Draw the rectangle QGraphicsRectItem.paint(self, painter, itemOption, widget) # Draw text over the rectangle font = QFont("Arial", 10) font.setBold(True) painter.setFont(font) painter.drawText(self.__node.posX - self.__node.width / 2.0, self.__node.posY - self.__node.height / 2.0, self.__node.width, self.__node.height, Qt.AlignCenter, self.__node.label)
def __getDotFont(parts): """Provides a QFont object if a font spec is found""" for part in parts: if 'fontname=' in part: fontName = part.replace('fontname=', '') fontName = fontName.replace('[', '') fontName = fontName.replace(']', '') fontName = fontName.replace(',', '') return QFont(fontName) return None
def paint(self, painter, option, widget): """Draws a rectangle, adds title, classes/funcs/globs sections""" # Hide the dotted outline itemOption = QStyleOptionGraphicsItem(option) if itemOption.state & QStyle.State_Selected != 0: itemOption.state = itemOption.state & ~QStyle.State_Selected # Draw the rectangle QGraphicsRectItem.paint(self, painter, itemOption, widget) # Start from the bottom to automatically take potential spare # pixels for the title font = QFont("Arial", 10) painter.setFont(font) posX = self.__node.posX - self.__node.width / 2.0 posY = self.__node.posY + self.__node.height / 2.0 - 1.5 * self.__pixelsPerLine occupiedPixels = 0 for index in range(len(self.__lines) - 1, 0, -1): if self.__lines[index] is None: # Draw a separation line painter.drawLine(posX + 1, posY + self.__pixelsPerLine / 2.0, posX + self.__node.width, posY + self.__pixelsPerLine / 2.0) elif self.__lines[index] != "": # Draw a text line # Sometimes the bottom part of 'g' is not drawn so I add 2 # spare pixels. painter.drawText(int(posX), int(posY), int(self.__node.width), self.__pixelsPerLine + 2, Qt.AlignCenter, self.__lines[index]) occupiedPixels += self.__pixelsPerLine posY -= self.__pixelsPerLine # Draw the title in bold font.setBold(True) painter.setFont(font) available = self.__node.height - occupiedPixels posY = self.__node.posY - self.__node.height / 2.0 painter.drawText(int(posX), int(posY), int(self.__node.width), int(available), Qt.AlignCenter, self.__lines[0])
def paint(self, painter, option, widget): """Draws the independent comment""" settings = self.canvas.settings rectWidth = self.minWidth - 2 * settings.hCellPadding rectHeight = self.minHeight - 2 * settings.vCellPadding if self.isSelected(): selectPen = QPen(settings.selectColor) selectPen.setWidth(settings.selectPenWidth) selectPen.setJoinStyle(Qt.RoundJoin) painter.setPen(selectPen) else: pen = QPen(self.__borderColor) pen.setWidth(settings.docLinkLineWidth) pen.setJoinStyle(Qt.RoundJoin) painter.setPen(pen) brush = QBrush(self.__bgColor) painter.setBrush(brush) painter.drawRoundedRect(self.baseX + settings.hCellPadding, self.baseY + settings.vCellPadding, rectWidth, rectHeight, 0, 0) if self._text: # Draw the text in the rectangle font = QFont(settings.monoFont) font.setItalic(True) painter.setFont(font) pen = QPen(self.__fgColor) painter.setPen(pen) painter.drawText( self._leftEdge + settings.hCellPadding + settings.hDocLinkPadding + self.iconItem.width() + settings.hDocLinkPadding, self.baseY + settings.vCellPadding + settings.vDocLinkPadding, self._textRect.width(), self._textRect.height(), Qt.AlignLeft, self._text)
def __reset(self): """Resets all the values to the default""" # Note: python 3.5 and 3.6 deepcopy() behaves different. The 3.6 # fails to copy QFont at all while 3.5 copies it improperly. # So to be 100% sure it works, here is a manual copying... self.__values = {} for key, value in _DEFAULT_SKIN_SETTINGS.items(): if isinstance(value, QFont): self.__values[key] = QFont(_DEFAULT_SKIN_SETTINGS[key]) else: self.__values[key] = value self.__cfValues = {} for key, value in _DEFAULT_CFLOW_SETTINGS.items(): if isinstance(value, QFont): self.__cfValues[key] = QFont(_DEFAULT_CFLOW_SETTINGS[key]) else: self.__cfValues[key] = value self.__appCSS = deepcopy(_DEFAULT_APP_CSS) self.minTextZoom = self.__calculateMinTextZoom() self.minCFlowZoom = self.__calculateMinCFlowZoom()
def __init__(self, edge, modObj): text = edge.label.replace('\\n', '\n') QGraphicsTextItem.__init__(self, text) self.__modObj = modObj font = QFont("Arial", 10) self.setFont(font) metric = QFontMetrics(font) rec = metric.boundingRect(0, 0, 10000, 10000, Qt.AlignLeft, text) self.setPos(edge.labelX - rec.width() / 2, edge.labelY - rec.height() / 2) # To make double click delivered self.setFlag(QGraphicsItem.ItemIsSelectable, True)
def paint(self, painter, option, widget): """Draws a filled rectangle and then adds a title""" # Hide the dotted outline itemOption = QStyleOptionGraphicsItem(option) if itemOption.state & QStyle.State_Selected != 0: itemOption.state = itemOption.state & ~QStyle.State_Selected # Draw the rectangle QGraphicsRectItem.paint(self, painter, itemOption, widget) # Draw text over the rectangle font = QFont("Arial", 10) painter.setFont(font) hSpacer = 10 ySpacer = 5 painter.drawText(self.__node.posX - self.__node.width / 2.0 + hSpacer, self.__node.posY - self.__node.height / 2.0 + ySpacer, self.__node.width - hSpacer, self.__node.height - ySpacer, Qt.AlignLeft, self.__srcobj.text)
def getZoomedMarginFont(): """Provides the current margin font respecting zoom""" font = QFont(GlobalData().skin['lineNumFont']) font.setPointSize(font.pointSize() + Settings()['zoom']) return font
def getZoomedCFBadgeFont(): """Provides the current control flow badge font respecting zoom""" font = QFont(GlobalData().skin['badgeFont']) font.setPointSize(font.pointSize() + Settings()['flowZoom']) return font
def getZoomedCFMonoFont(): """Provides the current mono font respecting zoom""" font = QFont(GlobalData().skin['cfMonoFont']) font.setPointSize(font.pointSize() + Settings()['flowZoom']) return font
def buildFont(fontAsStr): """Converts saved font into QFont object""" fontAsStr = fontAsStr.strip() font = QFont() font.fromString(fontAsStr) return font
import io import gprof2dot from ui.qt import (Qt, QPointF, pyqtSignal, QFont, QPen, QColor, QFontMetrics, QPainterPath, QPainter, QWidget, QLabel, QVBoxLayout, QGraphicsScene, QGraphicsPathItem, QStyle, QGraphicsTextItem, QStyleOptionGraphicsItem, QGraphicsItem, QGraphicsRectItem, QSizePolicy) from ui.labels import HeaderFitLabel from utils.settings import Settings from utils.globals import GlobalData from utils.pixmapcache import getPixmap from diagram.plaindotparser import getGraphFromPlainDotData from diagram.importsdgmgraphics import DiagramWidget from .proftable import FLOAT_FORMAT DEFAULT_FONT = QFont("Arial", 10) class FuncConnectionLabel(QGraphicsTextItem): """Connector label""" def __init__(self, edge, edgeFont): text = edge.label.replace('\\n', '\n') QGraphicsTextItem.__init__(self, text) if edgeFont: self.setFont(edgeFont) else: self.setFont(DEFAULT_FONT) metric = QFontMetrics(self.font()) rec = metric.boundingRect(0, 0, 10000, 10000, Qt.AlignCenter, text)