QPlatformScreen::grabWindow() support on Mac

Added QCocoaScreen::grabWindow() and copied Qt 4 implimentation.
examples/desktop/screenshot works but WId window is not handled yet.

Change-Id: If228bb59d98a5166788ab38293d7e6a6300d1f85
Reviewed-by: Morten Johan Sørvig <morten.sorvig@nokia.com>
This commit is contained in:
Tasuku Suzuki 2012-07-01 17:27:30 +09:00 committed by Qt by Nokia
parent 2b1a501ac5
commit 89d7a55db4
3 changed files with 64 additions and 1 deletions

View File

@ -61,6 +61,8 @@ public:
QCocoaScreen(int screenIndex);
~QCocoaScreen();
QPixmap grabWindow(WId window, int x, int y, int width, int height) const;
QRect geometry() const { return m_geometry; }
QRect availableGeometry() const { return m_availableGeometry; }
int depth() const { return m_depth; }

View File

@ -88,6 +88,67 @@ QCocoaScreen::~QCocoaScreen()
delete m_cursor;
}
extern CGContextRef qt_mac_cg_context(const QPaintDevice *pdev);
QPixmap QCocoaScreen::grabWindow(WId window, int x, int y, int width, int height) const
{
// TODO window should be handled
Q_UNUSED(window)
const int maxDisplays = 128; // 128 displays should be enough for everyone.
CGDirectDisplayID displays[maxDisplays];
CGDisplayCount displayCount;
CGRect cgRect;
if (width < 0 || height < 0) {
// get all displays
cgRect = CGRectInfinite;
} else {
cgRect = CGRectMake(x, y, width, height);
}
const CGDisplayErr err = CGGetDisplaysWithRect(cgRect, maxDisplays, displays, &displayCount);
if (err && displayCount == 0)
return QPixmap();
// calculate pixmap size
QSize windowSize(width, height);
if (width < 0 || height < 0) {
QRect windowRect;
for (uint i = 0; i < displayCount; ++i) {
const CGRect cgRect = CGDisplayBounds(displays[i]);
QRect qRect(cgRect.origin.x, cgRect.origin.y, cgRect.size.width, cgRect.size.height);
windowRect = windowRect.united(qRect);
}
if (width < 0)
windowSize.setWidth(windowRect.width());
if (height < 0)
windowSize.setHeight(windowRect.height());
}
QPixmap windowPixmap(windowSize);
windowPixmap.fill(Qt::transparent);
for (uint i = 0; i < displayCount; ++i) {
const CGRect bounds = CGDisplayBounds(displays[i]);
int w = (width < 0 ? bounds.size.width : width);
int h = (height < 0 ? bounds.size.height : height);
QRect displayRect = QRect(x, y, w, h);
QCFType<CGImageRef> image = CGDisplayCreateImageForRect(displays[i],
CGRectMake(displayRect.x(), displayRect.y(), displayRect.width(), displayRect.height()));
QPixmap pix(w, h);
pix.fill(Qt::transparent);
CGRect rect = CGRectMake(0, 0, w, h);
CGContextRef ctx = qt_mac_cg_context(&pix);
qt_mac_drawCGImage(ctx, &rect, image);
CGContextRelease(ctx);
QPainter painter(&windowPixmap);
painter.drawPixmap(bounds.origin.x, bounds.origin.y, pix);
}
return windowPixmap;
}
QCocoaIntegration::QCocoaIntegration()
: mFontDb(new QCoreTextFontDatabase())
, mEventDispatcher(new QCocoaEventDispatcher())

View File

@ -226,7 +226,7 @@ CGContextRef qt_mac_cg_context(const QPaintDevice *pdev)
CGContextRef ret = 0;
QPlatformPixmap *data = const_cast<QPixmap *>(pm)->data_ptr().data();
if (data->classId() == QPlatformPixmap::RasterClass) {
if (data && data->classId() == QPlatformPixmap::RasterClass) {
QImage *image = data->buffer();
ret = CGBitmapContextCreate(image->bits(), image->width(), image->height(),
8, image->bytesPerLine(), colorspace, flags);