def __init__(self, width, title, min_value=None, max_value=None, unit_of_measure=None, is_vertical=False, parent=None): LogItem.__init__(self, parent) self.__width = width self.__title = title self.__min_value = min_value self.__max_value = max_value self.__uom = unit_of_measure self.__is_vertical = is_vertical self.__selected = False # title font self.__font1 = QFont() self.__font1.setBold(True) self.__font1.setPointSize(10) # value font self.__font2 = QFont() self.__font2.setPointSize(9) fm1 = QFontMetrics(self.__font1) fm2 = QFontMetrics(self.__font2) self.__height = self.LEGEND_LINE_MARGIN * 3 + fm1.height( ) + fm2.height() + 10 + self.LEGEND_ITEM_MARGIN
def displayPlot(self): QGraphicsScene.clear(self) self.sceneRect() fm = QFontMetrics(QFont()) # display lines fitting in sceneRect last = None for z1, z2, text in self.data: brush = QBrush( QColor.fromHsl(z2 / (self.zMax - self.zMin) * 360.0, 128, 128) ) self.addRect( self.xOffset + self.legendWidth, z1 * self.zScale + self.yOffset, self.barWidth, (z2 - z1) * self.zScale, QPen(), brush, ) if last is None: legend_item = self.addSimpleText("{}".format(z1)) legend_item.setPos(self.xOffset, z1 * self.zScale + self.yOffset) legend_item = self.addSimpleText("{}".format(z2)) legend_item.setPos(self.xOffset, z2 * self.zScale + self.yOffset) last = z2 text_item = self.addSimpleText(text) text_item.setPos( self.xOffset * 2 + self.legendWidth + self.barWidth, (z1 + z2) / 2.0 * self.zScale - fm.height() / 2.0 + self.yOffset, )
def scale_icon_size(standard_size: int) -> int: """ Scales an icon size accounting for device DPI """ fm = QFontMetrics((QFont())) scale = 1.1 * standard_size / 24.0 return int(math.floor(max(Qgis.UI_SCALE_FACTOR * fm.height() * scale, float(standard_size))))
def setText(self, text): if not self.text: font = QFont() font.setPointSize(10) self.rect = self.scene.addRect(QRectF(0, 0, 0, 0), QPen(), QBrush(QColor("white"))) self.text = self.scene.addText("", font) self.text.setPlainText(text) self.textWidth = 0.0 self.textHeight = 0.0 for line in text.split("\n"): fm = QFontMetrics(self.text.font()) fw = fm.width(line) self.textHeight += fm.height() if fw > self.textWidth: self.textWidth = fw
def __init__(self, yTitle, parent): QGraphicsScene.__init__(self, parent) # width of the scale bar fm = QFontMetrics(QFont()) # width = size of "100.0" with the default font + 10% self.yTitle = yTitle self.barWidth = max(fm.width(yTitle), fm.width("000.00")) self.xOffset = self.barWidth self.yOffset = fm.height() * 2 # define the transformation between distance, altitude and scene coordinates self.xRatio = 1.0 self.yRatio = 1.0 self.marker = PointMarker(self) self.clear()
def _calc_size(self): realSize = self.realsize canvaswidth = self.canvas.width() mapunitsperpixel = abs(self.canvas.mapUnitsPerPixel()) mapunits = self.canvas.mapUnits() prefered_units = roam.config.settings.get("prefer_units", "meters") newunits = Qgis.fromLiteral(prefered_units, Qgis.Meters) mapunitsperpixel *= Qgis.fromUnitToUnitFactor(mapunits, newunits) mapunits = newunits # Convert the real distance into pixels barwidth = realSize / mapunitsperpixel if barwidth < 30: barwidth = canvaswidth / 4 while barwidth > canvaswidth / 3: barwidth /= 3 realSize = barwidth * mapunitsperpixel # Round powerof10 = math.floor(math.log10(realSize)) scaler = math.pow(10.0, powerof10) realSize = round(realSize / scaler) * scaler barwidth = realSize / mapunitsperpixel label, realSize = self._label_size(mapunits, realSize) metrics = QFontMetrics(self.font) fontwidth = metrics.width(label) fontheight = metrics.height() sizelabel = QLocale.system().toString(realSize) sizelabel = "{} {}".format(sizelabel, label) barwidth = self._adjust_bar_size(barwidth, mapunits) barwidth = barwidth + fontwidth return barwidth, realSize, sizelabel, (fontwidth, fontheight)
def _setMinimumHeight(self): font = self.lexer.defaultFont(0) fm = QFontMetrics(font) self.setMinimumHeight(fm.height() + 10)
def _setMinimumHeight(self): fnt = self.settings.value("pythonConsole/fontfamilytext", "Monospace") fntSize = self.settings.value("pythonConsole/fontsize", 10, type=int) fm = QFontMetrics(QFont(fnt, fntSize)) self.setMinimumHeight(fm.height() + 10)
class ScaleBarItem(QGraphicsItem): def __init__(self, canvas, parent=None): super(ScaleBarItem, self).__init__(parent) self.canvas = canvas self.realsize = 100 black = QColor(Qt.black) black.setAlpha(150) white = QColor(Qt.white) white.setAlpha(150) blackpen = QPen(black, 4) whitepen = QPen(white, 8) self.pens = [whitepen, blackpen] self.whitepen = QPen(white, 1) self.blackbrush = QBrush(black) self.ticksize = 10 self.fontsize = 15 self.font = QFont() self.font.setPointSize(self.fontsize) self.font.setStyleHint(QFont.Times, QFont.PreferAntialias) self.font.setBold(True) self.metrics = QFontMetrics(self.font) def boundingRect(self): try: width, realsize, label, fontsize = self._calc_size() halfheight = (self.ticksize + fontsize[1]) / 2 halfwidth = (width + fontsize[0]) / 2 return QRectF(-halfwidth, -halfheight, halfwidth, halfheight) except ZeroDivisionError: return QRectF() def paint(self, painter, styleoptions, widget=None): try: width, realsize, label, fontsize = self._calc_size() except ZeroDivisionError: return mapunits = self.canvas.mapUnits() # painter.drawRect(self.boundingRect()) array = QPolygon() canvasheight = self.canvas.height() canvaswidth = self.canvas.width() margin = 20 originy = 0 originx = 0 self.setPos(margin, canvasheight - margin) x1, y1 = originx, originy x2, y2 = originx, originy + self.ticksize x3, y3 = originx + width, originy + self.ticksize midx, midy = originx + width / 2, originy + self.ticksize / 2 x4, y4 = originx + width, originy for pen in self.pens: painter.setPen(pen) # Drwa the scale bar painter.drawLine(x1, y1, x2, y2) painter.drawLine(x2, y2, x3, y3) painter.drawLine(x3, y3, x4, y4) painter.drawLine(midx, midy, midx, y1) # Draw the text fontwidth = self.metrics.width("0") fontheight = self.metrics.height() fontheight /= 2 fontwidth /= 2 path = QPainterPath() point = QPointF(x1 - fontwidth, y1 - fontheight) path.addText(point, self.font, "0") painter.setPen(self.whitepen) painter.setBrush(self.blackbrush) painter.setRenderHints(QPainter.Antialiasing) painter.setFont(self.font) painter.drawPath(path) fontwidth = self.metrics.width(label) fontheight = self.metrics.height() fontheight /= 2 fontwidth /= 2 point = QPointF(x4 - fontwidth, y4 - fontheight) path.addText(point, self.font, label) painter.drawPath(path) def _calc_size(self): realSize = self.realsize canvaswidth = self.canvas.width() mapunitsperpixel = abs(self.canvas.mapUnitsPerPixel()) mapunits = self.canvas.mapUnits() prefered_units = roam.config.settings.get("prefer_units", "meters") newunits = Qgis.fromLiteral(prefered_units, Qgis.Meters) mapunitsperpixel *= Qgis.fromUnitToUnitFactor(mapunits, newunits) mapunits = newunits # Convert the real distance into pixels barwidth = realSize / mapunitsperpixel if barwidth < 30: barwidth = canvaswidth / 4 while barwidth > canvaswidth / 3: barwidth /= 3 realSize = barwidth * mapunitsperpixel # Round powerof10 = math.floor(math.log10(realSize)) scaler = math.pow(10.0, powerof10) realSize = round(realSize / scaler) * scaler barwidth = realSize / mapunitsperpixel label, realSize = self._label_size(mapunits, realSize) metrics = QFontMetrics(self.font) fontwidth = metrics.width(label) fontheight = metrics.height() sizelabel = QLocale.system().toString(realSize) sizelabel = "{} {}".format(sizelabel, label) barwidth = self._adjust_bar_size(barwidth, mapunits) barwidth = barwidth + fontwidth return barwidth, realSize, sizelabel, (fontwidth, fontheight) def _label_size(self, unit, currentsize): if unit == Qgis.Meters: if currentsize > 1000: return "km", currentsize / 1000 elif currentsize < 0.01: return "mm", currentsize * 1000 elif currentsize < 0.1: return "cm", currentsize * 100 else: return "m", currentsize elif unit == Qgis.Feet: print(currentsize) if currentsize > 5280.0: return "miles", currentsize / 5000 elif currentsize == 5280.0: return "mile", currentsize / 5000 elif currentsize < 1: return "inches", currentsize * 10 elif currentsize == 1.0: return "foot", currentsize else: return "feet", currentsize elif unit == Qgis.Degrees: if currentsize == 1.0: return "degree", currentsize else: return "degrees", currentsize else: return str(unit), currentsize def _adjust_bar_size(self, barsize, unit): if unit == Qgis.Feet: if barsize > 5280.0 or barsize == 5280.0: return (barsize * 5290) / 5000 elif barsize < 1: return (barsize * 10) / 12 return barsize