def eventFilter(self, obj, event): if (event.type() == QEvent.ToolTip): view = obj.parent() if not view: return False pos = event.pos() index = view.indexAt(pos) if not index.isValid(): return False itemText = view.model().data(index) itemTooltip = view.model().data(index, Qt.ToolTipRole) fm = QFontMetrics(view.font()) itemTextWidth = fm.width(itemText) rect = view.visualRect(index) rectWidth = rect.width() if (itemTextWidth > rectWidth) and itemTooltip: QtWidgets.QToolTip.showText(event.globalPos(), itemTooltip, view, rect) else: QtWidgets.QToolTip.hideText() return True return False
def __init__(self, lexer=None): super(EditorWidget, self).__init__() font = QFont() font.setFamily('Courier') font.setFixedPitch(True) font.setPointSize(10) self.setFont(font) self.setMarginsFont(font) fontmetrics = QFontMetrics(font) self.setMarginsFont(font) self.setMarginWidth(0, fontmetrics.width("00000") + 6) self.setMarginLineNumbers(0, True) self.setMarginsBackgroundColor(QColor("#cccccc")) self.setBraceMatching(QsciScintilla.SloppyBraceMatch) self.setCaretLineVisible(True) self.setCaretLineBackgroundColor(QColor("#ffe4e4")) if lexer is not None: lexer.setDefaultFont(font) self.setLexer(lexer) self.setReadOnly(True) self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, 'Courier'.encode())
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 paint_preview(self): rect = self.rect() painter = QPainter(self) painter.fillRect(rect, self.plugIn.canvas.canvasColor()) painter.setRenderHint(QPainter.Antialiasing) foregroundColor = QColor( QadVariables.get( QadMsg.translate("Environment variables", "DYNEDITFORECOLOR"))) backGroundColor = QColor( QadVariables.get( QadMsg.translate("Environment variables", "DYNEDITBACKCOLOR"))) borderColor = QColor( QadVariables.get( QadMsg.translate("Environment variables", "DYNEDITBORDERCOLOR"))) opacity = 100 - self.transparency font_size = 8 + self.size height = font_size + 15 selectionColor = QColor(Qt.white) selectionBackGroundColor = QColor(51, 153, 255) # azzurro (R=51 G=153 B=255) self.setEdit(self.edit1, foregroundColor, backGroundColor, borderColor, selectionColor, selectionBackGroundColor, opacity) fm = QFontMetrics(self.edit1.currentFont()) width1 = fm.width(self.edit1.toPlainText() + "__") + 2 self.edit1.resize(width1, height) self.edit1.selectAll() # seleziono tutto il testo self.setEdit(self.edit2, foregroundColor, backGroundColor, borderColor, backGroundColor, foregroundColor, opacity) fm = QFontMetrics(self.edit2.currentFont()) width2 = fm.width(self.edit2.toPlainText() + "__") + 2 self.edit2.resize(width2, height) offset = height / 3 x = (rect.width() - (width1 + offset + width2)) / 2 y = (rect.height() - height) / 2 self.edit1.move(x, y) self.edit2.move(x + width1 + offset, y)
def setData(self, data): fm = QFontMetrics(QFont()) self.data = data self.zMin = None self.zMax = None self.width = None for z1, z2, text in self.data: if self.zMin is None or z1 < self.zMin: self.zMin = z1 if self.zMax is None or z2 > self.zMax: self.zMax = z2 tw = fm.width(text) if self.width is None or tw > self.width: self.width = tw tw1 = fm.width("{}".format(z1)) tw2 = fm.width("{}".format(z2)) tw = max(tw1, tw2) if tw > self.legendWidth: self.legendWidth = tw # update ratio and offset self.setSceneRect(self.sceneRect())
def _adjust_layer_drop_down_width(self): """ Adjusts the layers combobox drop down to expand based on the layer name. """ if len(self._profile_spatial_layers) > 0: longest_item = max(self._profile_spatial_layers, key=len) font_meter = QFontMetrics(self.fontMetrics()) item_width = font_meter.width(longest_item) + 80 self.stdm_layers_combo.setStyleSheet('''* QComboBox QAbstractItemView{ min-width: 60px; width: %s px; } ''' % item_width)
def init_gui(self): """ Initializes form widgets """ source_names = self.prefix_source_names() self.populate_source_cbo(source_names) self.populate_leading_zero() self.populate_separator(self.separator_cbo) self.populate_columns_list() column_header = QApplication.translate('CodeProperty', 'Columns') separator_header = QApplication.translate('CodeProperty', 'Separator') self.column_code_view.model().setHorizontalHeaderLabels( [column_header, separator_header]) header = self.column_code_view.horizontalHeader() header.setSectionResizeMode(0, QHeaderView.Stretch) header.setSectionResizeMode(1, QHeaderView.ResizeToContents) if self._source: self.prefix_source_cbo.setCurrentIndex( self.prefix_source_cbo.findText(self._source)) if self._columns: self.set_columns() self.set_disable_auto_increment() self.set_enable_editing() self.set_hide_prefix() if self._leading_zero: self.leading_zero_cbo.setCurrentIndex( self.leading_zero_cbo.findData(self._leading_zero)) if self._separator: self.separator_cbo.setCurrentIndex( self.separator_cbo.findData(self._separator)) longest_item = max(source_names, key=len) font_meter = QFontMetrics(self.fontMetrics()) item_width = font_meter.width(longest_item) + 18 self.prefix_source_cbo.setStyleSheet('''* QComboBox QAbstractItemView{ min-width: 150px; width: %spx; } ''' % item_width) # Link checkbox and label self.disable_auto_increment_lbl.setBuddy( self.disable_auto_increment_chk) self.enable_editing_lbl.setBuddy(self.enable_editing_chk) self.hide_prefix_lbl.setBuddy(self.hide_prefix_chk)
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 _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 setCommonOptions(self): # Enable non-ASCII characters self.setUtf8(True) settings = QgsSettings() # Default font font = QFontDatabase.systemFont(QFontDatabase.FixedFont) self.setFont(font) self.setMarginsFont(font) self.setBraceMatching(QsciScintilla.SloppyBraceMatch) self.setMatchedBraceBackgroundColor( QColor( settings.value( "pythonConsole/matchedBraceBackgroundColorEditor", QColor(self.MATCHED_BRACE_BACKGROUND_COLOR)))) self.setMatchedBraceForegroundColor( QColor( settings.value( "pythonConsole/matchedBraceForegroundColorEditor", QColor(self.MATCHED_BRACE_FOREGROUND_COLOR)))) #self.setWrapMode(QsciScintilla.WrapWord) #self.setWrapVisualFlags(QsciScintilla.WrapFlagByText, # QsciScintilla.WrapFlagNone, 4) self.setSelectionForegroundColor( QColor( settings.value("pythonConsole/selectionForegroundColorEditor", QColor(self.SELECTION_FOREGROUND_COLOR)))) self.setSelectionBackgroundColor( QColor( settings.value("pythonConsole/selectionBackgroundColorEditor", QColor(self.SELECTION_BACKGROUND_COLOR)))) # Show line numbers fontmetrics = QFontMetrics(font) self.setMarginWidth(1, fontmetrics.width("0000") + 5) self.setMarginLineNumbers(1, True) self.setMarginsForegroundColor( QColor( settings.value("pythonConsole/marginForegroundColorEditor", QColor(self.MARGIN_FOREGROUND_COLOR)))) self.setMarginsBackgroundColor( QColor( settings.value("pythonConsole/marginBackgroundColorEditor", QColor(self.MARGIN_BACKGROUND_COLOR)))) self.setIndentationGuidesForegroundColor( QColor( settings.value("pythonConsole/marginForegroundColorEditor", QColor(self.MARGIN_FOREGROUND_COLOR)))) self.setIndentationGuidesBackgroundColor( QColor( settings.value("pythonConsole/marginBackgroundColorEditor", QColor(self.MARGIN_BACKGROUND_COLOR)))) # Highlight current line caretLineColorEditor = settings.value( "pythonConsole/caretLineColorEditor", QColor(self.CARET_LINE_COLOR)) cursorColorEditor = settings.value("pythonConsole/cursorColorEditor", QColor(self.CURSOR_COLOR)) self.setCaretLineVisible(True) self.setCaretWidth(2) self.setCaretLineBackgroundColor(caretLineColorEditor) self.setCaretForegroundColor(cursorColorEditor) # Folding self.setFolding(QsciScintilla.PlainFoldStyle) foldColor = QColor( settings.value("pythonConsole/foldColorEditor", QColor(self.FOLD_COLOR))) self.setFoldMarginColors(foldColor, foldColor) # Mark column 80 with vertical line self.setEdgeMode(QsciScintilla.EdgeLine) self.setEdgeColumn(80) self.setEdgeColor( QColor( settings.value("pythonConsole/edgeColorEditor", QColor(self.EDGE_COLOR)))) # Indentation self.setAutoIndent(True) self.setIndentationsUseTabs(False) self.setIndentationWidth(4) self.setTabIndents(True) self.setBackspaceUnindents(True) self.setTabWidth(4) self.setIndentationGuides(True) # Autocomletion self.setAutoCompletionThreshold(2) self.setAutoCompletionSource(QsciScintilla.AcsAPIs) self.setFonts(10) self.initLexer()
def setCommonOptions(self): # Enable non-ASCII characters self.setUtf8(True) settings = QgsSettings() # Default font font = QFontDatabase.systemFont(QFontDatabase.FixedFont) self.setFont(font) self.setMarginsFont(font) self.setBraceMatching(QsciScintilla.SloppyBraceMatch) self.setMatchedBraceBackgroundColor(QColor(settings.value("pythonConsole/matchedBraceBackgroundColorEditor", QColor(self.MATCHED_BRACE_BACKGROUND_COLOR)))) self.setMatchedBraceForegroundColor(QColor(settings.value("pythonConsole/matchedBraceForegroundColorEditor", QColor(self.MATCHED_BRACE_FOREGROUND_COLOR)))) #self.setWrapMode(QsciScintilla.WrapWord) #self.setWrapVisualFlags(QsciScintilla.WrapFlagByText, # QsciScintilla.WrapFlagNone, 4) self.setSelectionForegroundColor(QColor(settings.value("pythonConsole/selectionForegroundColorEditor", QColor(self.SELECTION_FOREGROUND_COLOR)))) self.setSelectionBackgroundColor(QColor(settings.value("pythonConsole/selectionBackgroundColorEditor", QColor(self.SELECTION_BACKGROUND_COLOR)))) # Show line numbers fontmetrics = QFontMetrics(font) self.setMarginWidth(1, fontmetrics.width("0000") + 5) self.setMarginLineNumbers(1, True) self.setMarginsForegroundColor(QColor(settings.value("pythonConsole/marginForegroundColorEditor", QColor(self.MARGIN_FOREGROUND_COLOR)))) self.setMarginsBackgroundColor(QColor(settings.value("pythonConsole/marginBackgroundColorEditor", QColor(self.MARGIN_BACKGROUND_COLOR)))) self.setIndentationGuidesForegroundColor(QColor(settings.value("pythonConsole/marginForegroundColorEditor", QColor(self.MARGIN_FOREGROUND_COLOR)))) self.setIndentationGuidesBackgroundColor(QColor(settings.value("pythonConsole/marginBackgroundColorEditor", QColor(self.MARGIN_BACKGROUND_COLOR)))) # Highlight current line caretLineColorEditor = settings.value("pythonConsole/caretLineColorEditor", QColor(self.CARET_LINE_COLOR)) cursorColorEditor = settings.value("pythonConsole/cursorColorEditor", QColor(self.CURSOR_COLOR)) self.setCaretLineVisible(True) self.setCaretWidth(2) self.setCaretLineBackgroundColor(caretLineColorEditor) self.setCaretForegroundColor(cursorColorEditor) # Folding self.setFolding(QsciScintilla.PlainFoldStyle) foldColor = QColor(settings.value("pythonConsole/foldColorEditor", QColor(self.FOLD_COLOR))) self.setFoldMarginColors(foldColor, foldColor) # Mark column 80 with vertical line self.setEdgeMode(QsciScintilla.EdgeLine) self.setEdgeColumn(80) self.setEdgeColor(QColor(settings.value("pythonConsole/edgeColorEditor", QColor(self.EDGE_COLOR)))) # Indentation self.setAutoIndent(True) self.setIndentationsUseTabs(False) self.setIndentationWidth(4) self.setTabIndents(True) self.setBackspaceUnindents(True) self.setTabWidth(4) self.setIndentationGuides(True) # Autocompletion self.setAutoCompletionThreshold(2) self.setAutoCompletionSource(QsciScintilla.AcsAPIs) self.setFonts(10) self.initLexer()
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