diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2020-03-09 18:43:52 +0100 |
---|---|---|
committer | Tor Arne Vestbø <torarnv@gmail.com> | 2020-03-12 11:49:08 +0100 |
commit | bee3d0fc1310da6685dce9cf9b1334fb8295aa9a (patch) | |
tree | ec5965fda8d2285d42649a39162ca0b3704f1858 /src/gui/painting/qcoregraphics.mm | |
parent | 52e9c8b4be6718bda1a46859d6b1c13664c57c2a (diff) |
macOS: Streamline QIcon to NSImage conversion
The conversion uses NSBitmapImageRep and correctly sets the display
pixel ratio and size of the resulting image, reducing the need for
clients to deal with this.
It also propagates the isMask property of the icon to the NSImage.
The function returns an auto-released object, as is customary for
class-functions like these.
Change-Id: If97f3d383959cd0f58a0d1249f5c26e52c1da9cd
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/gui/painting/qcoregraphics.mm')
-rw-r--r-- | src/gui/painting/qcoregraphics.mm | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/src/gui/painting/qcoregraphics.mm b/src/gui/painting/qcoregraphics.mm index b5620535fb..94ba004c93 100644 --- a/src/gui/painting/qcoregraphics.mm +++ b/src/gui/painting/qcoregraphics.mm @@ -151,32 +151,47 @@ QT_END_NAMESPACE return [nsImage autorelease]; } -@end -QT_BEGIN_NAMESPACE ++ (instancetype)imageFromQIcon:(const QIcon &)icon +{ + return [NSImage imageFromQIcon:icon withSize:0]; +} -NSImage *qt_mac_create_nsimage(const QIcon &icon, int defaultSize) ++ (instancetype)imageFromQIcon:(const QIcon &)icon withSize:(int)size { if (icon.isNull()) return nil; - NSImage *nsImage = [[NSImage alloc] init]; - QList<QSize> availableSizes = icon.availableSizes(); - if (availableSizes.isEmpty() && defaultSize > 0) - availableSizes << QSize(defaultSize, defaultSize); + auto nsImage = [[NSImage alloc] initWithSize:NSZeroSize]; + + auto availableSizes = icon.availableSizes(); + if (availableSizes.isEmpty() && size > 0) + availableSizes << QSize(size, size); + for (QSize size : qAsConst(availableSizes)) { - QPixmap pm = icon.pixmap(size); - if (pm.isNull()) + QImage image = icon.pixmap(size).toImage(); + if (image.isNull()) + continue; + + QCFType<CGImageRef> cgImage = image.toCGImage(); + if (!cgImage) continue; - QImage image = pm.toImage(); - CGImageRef cgImage = qt_mac_toCGImage(image); - NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; - [nsImage addRepresentation:imageRep]; - [imageRep release]; - CGImageRelease(cgImage); + + auto *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; + imageRep.size = (image.size() / image.devicePixelRatioF()).toCGSize(); + [nsImage addRepresentation:[imageRep autorelease]]; } - return nsImage; + + [nsImage setTemplate:icon.isMask()]; + + if (size) + nsImage.size = CGSizeMake(size, size); + + return [nsImage autorelease]; } +@end + +QT_BEGIN_NAMESPACE QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size) { |