Пример #1
0
 def load_image(self, path):
     if path.startswith(('http://', 'https://')):
         self.native = NSImage.alloc().initByReferencingURL(
             NSURL.URLWithString_(path))
     else:
         self.native = NSImage.alloc().initWithContentsOfFile(
             self._get_full_path(path))
Пример #2
0
    def __init__(self, interface, path=None, url=None):
        self.interface = interface
        self.path = path
        self.url = url

        if path:
            self.native = NSImage.alloc().initWithContentsOfFile(str(path))
        elif url:
            self.native = NSImage.alloc().initByReferencingURL(
                NSURL.URLWithString_(url))
Пример #3
0
def apply_round_clipping(image_view_impl: ImageView) -> None:
    """Clips an image in a given toga_cocoa.ImageView to a circular mask."""

    image = image_view_impl.native.image  # get native NSImage

    composed_image = NSImage.alloc().initWithSize(image.size)
    composed_image.lockFocus()

    ctx = NSGraphicsContext.currentContext
    ctx.saveGraphicsState()
    ctx.imageInterpolation = NSImageInterpolationHigh

    image_frame = NSRect(NSPoint(0, 0), image.size)
    clip_path = NSBezierPath.bezierPathWithRoundedRect(
        image_frame,
        xRadius=image.size.width / 2,
        yRadius=image.size.height / 2)
    clip_path.addClip()

    zero_rect = NSRect(NSPoint(0, 0), NSMakeSize(0, 0))
    image.drawInRect(image_frame,
                     fromRect=zero_rect,
                     operation=NSCompositeSourceOver,
                     fraction=1)
    composed_image.unlockFocus()
    ctx.restoreGraphicsState()

    image_view_impl.native.image = composed_image
Пример #4
0
    def set_image(self, image):
        if image:
            self.native.image = self.interface._image._impl.native
        else:
            width = 0
            height = 0
            if self.interface.style.width:
                width = self.interface.style.width
            if self.interface.style.height:
                height = self.interface.style.height

            self.native.image = NSImage.alloc().initWithSize(
                NSSize(width, height))
Пример #5
0
def resize_image_to(image: NSImage, height: int) -> NSImage:

    new_size = NSMakeSize(height, height)
    new_image = NSImage.alloc().initWithSize(new_size)
    new_image.lockFocus()
    image.size = new_size

    ctx = NSGraphicsContext.currentContext
    ctx.saveGraphicsState()
    ctx.imageInterpolation = NSImageInterpolationHigh

    image.drawAtPoint(
        NSZeroPoint,
        fromRect=CGRectMake(0, 0, new_size.width, new_size.height),
        operation=NSCompositingOperationCopy,
        fraction=1.0,
    )

    new_image.unlockFocus()
    ctx.restoreGraphicsState()

    return new_image
Пример #6
0
    def native(self):

        if self._native:
            return self._native

        if self.path:
            self._native = NSImage.alloc().initWithContentsOfFile(self.path)
            return self._native

        elif self.for_path:
            # always return a new pointer since an old one may be invalidated
            # icons are cached by AppKit anyways
            path = str(self.for_path)
            if osp.exists(path):
                return NSWorkspace.sharedWorkspace.iconForFile(path)
            else:
                _, extension = osp.splitext(path)
                return NSWorkspace.sharedWorkspace.iconForFileType(extension)

        elif self.template:
            cocoa_template = Icon._to_cocoa_template[self.template]
            self._native = NSImage.imageNamed(cocoa_template)
            return self._native
Пример #7
0
    def __init__(self, interface):
        self.interface = interface
        self.interface._impl = self
        file_path, file_extension = os.path.splitext(self.interface.filename)
        valid_icon_extensions = ('.png', '.bmp', '.ico')

        if file_extension == '.icns':
            self.native = NSImage.alloc().initWithContentsOfFile(self.interface.filename)
        elif os.path.isfile(file_path + '.icns'):
            self.native = NSImage.alloc().initWithContentsOfFile(file_path + '.icns')
        elif file_extension in valid_icon_extensions:
            self.native = NSImage.alloc().initWithContentsOfFile(self.interface.filename)
        elif os.path.isfile(file_path + '.png'):
            self.native = NSImage.alloc().initWithContentsOfFile(file_path + '.png')
        elif os.path.isfile(file_path + '.bmp'):
            self.native = NSImage.alloc().initWithContentsOfFile(file_path + '.bmp')
        else:
            print("[Cocoa] No valid icon format available for {}; "
                  "fall back on Tiberius instead".format(
                self.interface.filename))
            tiberius_file = toga_Icon.TIBERIUS_ICON.filename + '.icns'
            self.interface.icon = toga_Icon.TIBERIUS_ICON
            self.native = NSImage.alloc().initWithContentsOfFile(tiberius_file)
Пример #8
0
 def load_image(self, path):
     if path.startswith(('http://', 'https://')):
         self.native = NSImage.alloc().initByReferencingURL(NSURL.URLWithString_(path))
     else:
         self.native = NSImage.alloc().initWithContentsOfFile(self._get_full_path(path))
Пример #9
0
    def __init__(self, interface, path):
        self.interface = interface
        self.interface._impl = self
        self.path = path

        self.native = NSImage.alloc().initWithContentsOfFile(str(path))
Пример #10
0
 def __init__(self, interface):
     self.interface = interface
     interface.__impl = self
     self.native = NSImage.alloc().initWithContentsOfFile(
         interface.filename)