def __init__(self, interface): self.interface = interface try: font = _FONT_CACHE[self.interface] except KeyError: # Default system font size on Cocoa is 12pt if self.interface.size == SYSTEM_DEFAULT_FONT_SIZE: font_size = NSFont.systemFontSize else: font_size = self.interface.size if self.interface.family == SYSTEM: if self.interface.weight == BOLD: font = NSFont.boldSystemFontOfSize(font_size) else: font = NSFont.systemFontOfSize(font_size) elif self.interface.family == MESSAGE: font = NSFont.messageFontOfSize(font_size) else: if self.interface.family is SERIF: family = 'Times-Roman' elif self.interface.family is SANS_SERIF: family = 'Helvetica' elif self.interface.family is CURSIVE: family = 'Apple Chancery' elif self.interface.family is FANTASY: family = 'Papyrus' elif self.interface.family is MONOSPACE: family = 'Courier New' else: family = self.interface.family full_name = '{family}{weight}{style}'.format( family=family, weight=(' ' + self.interface.weight.title()) if self.interface.weight is not NORMAL else '', style=(' ' + self.interface.style.title()) if self.interface.style is not NORMAL else '', ) font = NSFont.fontWithName(full_name, size=self.interface.size) if font is None: print("Unable to load font: {}pt {}".format( self.interface.size, full_name)) else: _FONT_CACHE[self.interface] = font self.native = font
def drawWithFrame_inView_(self, cellFrame: NSRect, view) -> None: # The data to display. try: label = self.objectValue.attrs['label'] icon = self.objectValue.attrs['icon'] except AttributeError: # Value is a simple string. label = self.objectValue icon = None if icon and icon.native: offset = 28.5 NSGraphicsContext.currentContext.saveGraphicsState() yOffset = cellFrame.origin.y if view.isFlipped: xform = NSAffineTransform.transform() xform.translateXBy(8, yBy=cellFrame.size.height) xform.scaleXBy(1.0, yBy=-1.0) xform.concat() yOffset = 0.5 - cellFrame.origin.y interpolation = NSGraphicsContext.currentContext.imageInterpolation NSGraphicsContext.currentContext.imageInterpolation = NSImageInterpolationHigh icon.native.drawInRect(NSRect(NSPoint(cellFrame.origin.x, yOffset), NSSize(16.0, 16.0)), fromRect=NSRect( NSPoint(0, 0), NSSize(icon.native.size.width, icon.native.size.height)), operation=NSCompositingOperationSourceOver, fraction=1.0) NSGraphicsContext.currentContext.imageInterpolation = interpolation NSGraphicsContext.currentContext.restoreGraphicsState() else: # No icon; just the text label offset = 5 if label: # Find the right color for the text if self.isHighlighted(): primaryColor = NSColor.alternateSelectedControlTextColor else: if False: primaryColor = NSColor.disabledControlTextColor else: primaryColor = NSColor.textColor textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSForegroundColorAttributeName] = primaryColor textAttributes[NSFontAttributeName] = NSFont.systemFontOfSize(13) at(label).drawAtPoint(NSPoint(cellFrame.origin.x + offset, cellFrame.origin.y), withAttributes=textAttributes)
def __init__(self, interface): self.interface = interface try: font = _FONT_CACHE[self.interface] except KeyError: if self.interface.family == SYSTEM: font = NSFont.systemFontOfSize(self.interface.size) elif self.interface.family == MESSAGE: font = NSFont.messageFontOfSize(self.interface.size) else: if self.interface.family is SERIF: family = 'Times-Roman' elif self.interface.family is SANS_SERIF: family = 'Helvetica' elif self.interface.family is CURSIVE: family = 'Apple Chancery' elif self.interface.family is FANTASY: family = 'Papyrus' elif self.interface.family is MONOSPACE: family = 'Courier New' else: family = self.interface.family full_name = '{family}{weight}{style}'.format( family=family, weight=(' ' + self.interface.weight.title()) if self.interface.weight is not NORMAL else '', style=(' ' + self.interface.style.title()) if self.interface.style is not NORMAL else '', ) font = NSFont.fontWithName(full_name, size=self.interface.size) if font is None: print( "Unable to load font: {}pt {}".format( self.interface.size, full_name ) ) else: _FONT_CACHE[self.interface] = font self.native = font
def drawInteriorWithFrame_inView_(self, cellFrame: NSRect, view) -> None: # The data to display. icon = self.objectValue.attrs['icon'] title = self.objectValue.attrs['title'] subtitle = self.objectValue.attrs['subtitle'] if icon and icon.native: NSGraphicsContext.currentContext.saveGraphicsState() yOffset = cellFrame.origin.y if view.isFlipped: xform = NSAffineTransform.transform() xform.translateXBy(4, yBy=cellFrame.size.height) xform.scaleXBy(1.0, yBy=-1.0) xform.concat() yOffset = 0.5 - cellFrame.origin.y interpolation = NSGraphicsContext.currentContext.imageInterpolation NSGraphicsContext.currentContext.imageInterpolation = NSImageInterpolationHigh icon.native.drawInRect(NSRect( NSPoint(cellFrame.origin.x, yOffset + 4), NSSize(40.0, 40.0)), fromRect=NSRect( NSPoint(0, 0), NSSize(icon.native.size.width, icon.native.size.height)), operation=NSCompositingOperationSourceOver, fraction=1.0) NSGraphicsContext.currentContext.imageInterpolation = interpolation NSGraphicsContext.currentContext.restoreGraphicsState() else: path = NSBezierPath.bezierPathWithRect( NSRect(NSPoint(cellFrame.origin.x, cellFrame.origin.y + 4), NSSize(40.0, 40.0))) NSColor.grayColor.set() path.fill() if title: # Find the right color for the text if self.isHighlighted(): primaryColor = NSColor.alternateSelectedControlTextColor else: if False: primaryColor = NSColor.disabledControlTextColor else: primaryColor = NSColor.textColor textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSForegroundColorAttributeName] = primaryColor textAttributes[NSFontAttributeName] = NSFont.systemFontOfSize(15) at(title).drawAtPoint(NSPoint(cellFrame.origin.x + 48, cellFrame.origin.y + 4), withAttributes=textAttributes) if subtitle: # Find the right color for the text if self.isHighlighted(): primaryColor = NSColor.alternateSelectedControlTextColor else: if False: primaryColor = NSColor.disabledControlTextColor else: primaryColor = NSColor.textColor textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSForegroundColorAttributeName] = primaryColor textAttributes[NSFontAttributeName] = NSFont.systemFontOfSize(13) at(subtitle).drawAtPoint(NSPoint(cellFrame.origin.x + 48, cellFrame.origin.y + 24), withAttributes=textAttributes)
def __init__(self, interface): self.interface = interface try: font = _FONT_CACHE[self.interface] except KeyError: # Default system font size on Cocoa is 12pt if self.interface.size == SYSTEM_DEFAULT_FONT_SIZE: font_size = NSFont.systemFontSize else: font_size = self.interface.size if self.interface.family == SYSTEM: if self.interface.weight == BOLD: font = NSFont.boldSystemFontOfSize(font_size) else: font = NSFont.systemFontOfSize(font_size) elif self.interface.family == MESSAGE: font = NSFont.messageFontOfSize(font_size) else: if self.interface.family is SERIF: family = 'Times-Roman' elif self.interface.family is SANS_SERIF: family = 'Helvetica' elif self.interface.family is CURSIVE: family = 'Apple Chancery' elif self.interface.family is FANTASY: family = 'Papyrus' elif self.interface.family is MONOSPACE: family = 'Courier New' else: family = self.interface.family font = NSFont.fontWithName(family, size=self.interface.size) if font is None: print( "Unable to load font: {}pt {}".format( self.interface.size, family ) ) # Convert the base font definition into a font with all the desired traits. attributes_mask = 0 if self.interface.weight == BOLD: attributes_mask |= NSFontMask.Bold.value if self.interface.style == ITALIC: attributes_mask |= NSFontMask.Italic.value elif self.interface.style == SMALL_CAPS: attributes_mask |= NSFontMask.SmallCaps.value if attributes_mask: font = NSFontManager.sharedFontManager.convertFont(font, toHaveTrait=attributes_mask) full_name = '{family}{weight}{style}'.format( family=family, weight=(' ' + self.interface.weight.title()) if self.interface.weight is not NORMAL else '', style=(' ' + self.interface.style.title()) if self.interface.style is not NORMAL else '', ) if font is None: print( "Unable to load font: {}pt {}".format( self.interface.size, full_name ) ) else: _FONT_CACHE[self.interface] = font self.native = font