def fontDatabase_has_font(font): """ This is helper function checks if a certain font family exists on the current system. """ fontDatabase = QFontDatabase() families = fontDatabase.families() return (font.family() in families)
def update_label_font_display(self, newLabelFont): """ Update the displayed font properties for a newly selected label font. Updated properties are font style, size, example text. """ # block signals from to avoid that update_current_label_font # is called multiple times. We'll call it once at the end self.labelStyleComboBox.blockSignals(True) self.labelSizeComboBox.blockSignals(True) fontFamily = newLabelFont.family() fontDatabase = QFontDatabase() availableStyles = fontDatabase.styles(fontFamily) targetStyle = fontDatabase.styleString(newLabelFont) targetStyleIndex = 0 # FIXME: There seems to be a bug in Qt 4.8 # "Normal" styleStrings are "Regular" in styles leading to # a mismatch below if QT_VERSION >= 0x040800: if targetStyle == "Normal": targetStyle = "Regular" self.labelStyleComboBox.clear() for (index, style) in enumerate(availableStyles): self.labelStyleComboBox.addItem(style) if targetStyle == style: targetStyleIndex = index self.labelStyleComboBox.setCurrentIndex(targetStyleIndex) availableSizes = fontDatabase.pointSizes(fontFamily) targetSize = newLabelFont.pointSize() targetSizeIndex = 0 self.labelSizeComboBox.clear() for (index, size) in enumerate(availableSizes): self.labelSizeComboBox.addItem(str(size)) if targetSize == size: targetSizeIndex = index self.labelSizeComboBox.setCurrentIndex(targetSizeIndex) self.update_current_label_font() # turn signals back on self.labelStyleComboBox.blockSignals(False) self.labelSizeComboBox.blockSignals(False)
def importTheme(filename, widget, schemeWidget): """Loads the colors theme from a file""" try: d = ET.parse(filename) root = d.getroot() if root.tag != 'frescobaldi-theme': raise ValueError(_("No theme found.")) except Exception as e: QMessageBox.critical( widget, app.caption(_("Error")), _("Can't read from source:\n\n{url}\n\n{error}").format( url=filename, error=e)) return schemeWidget.scheme.blockSignals(True) key = schemeWidget.addScheme(root.get('name')) schemeWidget.scheme.blockSignals(False) tfd = textformats.TextFormatData(key) fontElt = root.find('font') defaultfont = "Lucida Console" if os.name == "nt" else "monospace" if fontElt.get('fontFamily') in QFontDatabase().families(): fontFamily = fontElt.get('fontFamily') else: fontFamily = defaultfont font = QFont(fontFamily) font.setPointSizeF(float(fontElt.get('fontSize'))) tfd.font = font for elt in root.find('baseColors'): tfd.baseColors[elt.tag] = QColor(elt.get('color')) for elt in root.find('defaultStyles'): tfd.defaultStyles[elt.tag] = eltToStyle(elt) for style in root.find('allStyles'): if not style in tfd.allStyles: tfd.allStyles[style] = {} for elt in style: tfd.allStyles[style.tag][elt.tag] = eltToStyle(elt) widget.addSchemeData(key, tfd) schemeWidget.disableDefault(False) schemeWidget.currentChanged.emit() schemeWidget.changed.emit()
def __init__(self, parent=None): super(XFontPickerWidget, self).__init__(parent) # load the user interface projexui.loadUi(__file__, self) # define custom properties database = QFontDatabase() for family in sorted(database.families()): item = QTreeWidgetItem(self.uiFontTREE, [family]) item.setFont(0, QFont(family)) # set default properties # create connections self.uiSizeSPN.valueChanged.connect(self.setPointSize) self.uiFontTREE.itemDoubleClicked.connect(self.accepted)
def _get_label_font_from_widget(self): """ Helper function extracting the currently selected labels font info from the widget. """ fontFamily = self.labelFontComboBox.currentFont().family() fontStyle = self.labelStyleComboBox.currentText() if self.labelSizeComboBox.currentText(): fontSize = int(self.labelSizeComboBox.currentText()) else: fontSize = 20 fontDataBase = QFontDatabase() newLabelFont = fontDataBase.font(fontFamily, fontStyle, fontSize) return newLabelFont
def _setPlatformDefaults(self): """Set default values, which depend on platform """ """Monaco - old Mac font, Menlo - modern Mac font, Monospace - default for other platforms """ fontFamilies = ("Menlo", "Monaco", "Monospace") availableFontFamilies = QFontDatabase().families() for fontFamily in fontFamilies: if fontFamily in availableFontFamilies: self._data['Qutepart']['Font']['Family'] = fontFamily break else: self._data['Qutepart']['Font']['Family'] = 'Monospace' self._data['PlatformDefaultsHaveBeenSet'] = True
def _setPlatformDefaults(self): """Set default values, which depend on platform """ """Monaco - old Mac font, Menlo - modern Mac font, Consolas - default font for Windows Vista+ Lucida Console - on Windows XP Monospace - default for other platforms """ fontFamilies = ("Menlo", "Monaco", "Monospace", "Consolas", "Lucida Console") availableFontFamilies = QFontDatabase().families() for fontFamily in fontFamilies: if fontFamily in availableFontFamilies: self._data['Qutepart']['Font']['Family'] = fontFamily break else: self._data['Qutepart']['Font']['Family'] = 'Monospace' self._data['Qutepart']['Font']['Size'] = max( QApplication.instance().font().pointSize(), 12) self._data['PlatformDefaultsHaveBeenSet'] = True
def _width_with_metrics(self, text): # Return the width of this piece of text when rendered using this # font. fontName = self.font["BaseFont"].name at = fontName.find("+") if at != -1: fontName = fontName[at + 1:] if "-" in fontName: family, style = fontName.split("-")[:2] elif " " in fontName: family, style = fontName.split(" ")[:2] elif "," in fontName: family, style = fontName.split(",")[:2] else: family = fontName style = "" font = QFontDatabase().font(family, style, self.size) font.setPointSizeF(self.size) fm = QFontMetricsF(font) return fm.width(text)
def font_is_installed(font): """Check if font is installed""" return [ fam for fam in QFontDatabase().families() if to_text_string(fam) == font ]
def font_names(): model = listmodel.ListModel(sorted(QFontDatabase().families())) model.setRoleFunction(Qt.FontRole, QFont) return model
def font_is_installed(font): """Check if font is installed""" return [fam for fam in QFontDatabase().families() if unicode(fam)==font]
IMC.dictPath = os.path.join(base, u"dict") # Initialize the font system. We need a monospaced font with clear visual # separation between 0/O, 1/l, etc, and a good complement of Unicode, with # at a minimum full support of Greek, Cyrillic and Hebrew. These features are # found in Liberation Mono, which is free, and by Courier New, which is less # readable but is bundled in most OSs by default, and by Everson Mono. # We prefer Liberation Mono, but set that or Courier New as the default font family # and as our App's default font. pqMain uses it as a default when # reading the font family name from the saved settings. pqMsgs.noteEvent("Setting up default font name") lm_name = QString(u'Liberation Mono') qfdb = QFontDatabase() qf = qfdb.font(lm_name, QString(u'Regular'), 12) if qf.family() == lm_name: # Lib. Mono is installed in this system, good to go IMC.defaultFontFamily = lm_name else: # Let's try to install Lib. Mono. Was it bundled with us? fpath = os.path.join(base, u"fonts") if os.path.exists(fpath): # It was; add each .ttf in it to the Qt font database # We are *assuming* that what's in fonts is Liberation Mono Regular # for sure, and optionally other variants like bold. for fname in os.listdir(fpath): if fname.endswith(u'.ttf'): qfdb.addApplicationFont(QString(os.path.join(fpath, fname))) IMC.defaultFontFamily = lm_name