Move a reusable image conversion function into QCocoaHelpers.

Change-Id: I5fc8f84bd504e405968e212b3125179a2273ae79
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@theqtcompany.com>
bb10
Jake Petroules 2015-12-06 22:26:01 -08:00 committed by Timur Pocheptsov
parent 1f2734b82b
commit f157babbed
4 changed files with 43 additions and 14 deletions

View File

@ -66,6 +66,7 @@ NSImage *qt_mac_create_nsimage(const QIcon &icon);
CGImageRef qt_mac_toCGImage(const QImage &qImage);
CGImageRef qt_mac_toCGImageMask(const QImage &qImage);
QImage qt_mac_toQImage(CGImageRef image);
QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size);
NSSize qt_mac_toNSSize(const QSize &qtSize);
NSRect qt_mac_toNSRect(const QRect &rect);

View File

@ -50,6 +50,25 @@
#include <Carbon/Carbon.h>
@interface NSGraphicsContext (QtAdditions)
+ (NSGraphicsContext *)qt_graphicsContextWithCGContext:(CGContextRef)graphicsPort flipped:(BOOL)initialFlippedState;
@end
@implementation NSGraphicsContext (QtAdditions)
+ (NSGraphicsContext *)qt_graphicsContextWithCGContext:(CGContextRef)graphicsPort flipped:(BOOL)initialFlippedState
{
#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_10, __IPHONE_NA)
if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_10)
return [self graphicsContextWithCGContext:graphicsPort flipped:initialFlippedState];
#endif
return [self graphicsContextWithGraphicsPort:graphicsPort flipped:initialFlippedState];
}
@end
QT_BEGIN_NAMESPACE
//
@ -785,6 +804,26 @@ CGContextRef qt_mac_cg_context(QPaintDevice *pdev)
return ret;
}
QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size)
{
const NSSize pixmapSize = NSMakeSize(size.width(), size.height());
QPixmap pixmap(pixmapSize.width, pixmapSize.height);
pixmap.fill(Qt::transparent);
[image setSize:pixmapSize];
const NSRect iconRect = NSMakeRect(0, 0, pixmapSize.width, pixmapSize.height);
QMacCGContext ctx(&pixmap);
if (!ctx)
return QPixmap();
NSGraphicsContext *gc = [NSGraphicsContext qt_graphicsContextWithCGContext:ctx flipped:YES];
if (!gc)
return QPixmap();
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:gc];
[image drawInRect:iconRect fromRect:iconRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
[NSGraphicsContext restoreGraphicsState];
return pixmap;
}
QImage qt_mac_toQImage(CGImageRef image)
{
const size_t w = CGImageGetWidth(image),

View File

@ -259,18 +259,7 @@ QPixmap QCocoaTheme::fileIconPixmap(const QFileInfo &fileInfo, const QSizeF &siz
NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:QCFString::toNSString(fileInfo.canonicalFilePath())];
if (!iconImage)
return QPixmap();
NSSize pixmapSize = NSMakeSize(size.width(), size.height());
QPixmap pixmap(pixmapSize.width, pixmapSize.height);
pixmap.fill(Qt::transparent);
[iconImage setSize:pixmapSize];
NSRect iconRect = NSMakeRect(0, 0, pixmapSize.width, pixmapSize.height);
CGContextRef ctx = qt_mac_cg_context(&pixmap);
NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:YES];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:gc];
[iconImage drawInRect:iconRect fromRect:iconRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
[NSGraphicsContext restoreGraphicsState];
return pixmap;
return qt_mac_toQPixmap(iconImage, size);
}
QVariant QCocoaTheme::themeHint(ThemeHint hint) const

View File

@ -105,8 +105,8 @@ class QMacCGContext
public:
QMacCGContext(QPainter *p); //qpaintengine_mac.mm
inline QMacCGContext() { context = 0; }
inline QMacCGContext(const QPaintDevice *pdev) {
extern CGContextRef qt_mac_cg_context(const QPaintDevice *);
inline QMacCGContext(QPaintDevice *pdev) {
extern CGContextRef qt_mac_cg_context(QPaintDevice *);
context = qt_mac_cg_context(pdev);
}
inline QMacCGContext(CGContextRef cg, bool takeOwnership=false) {