diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2017-11-07 19:25:13 +0100 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2017-11-08 14:39:52 +0000 |
commit | b0c895fa7aa7f4d47dc9fc9ab3a65fbfb69f89ed (patch) | |
tree | 99bf53d6660d37d4aed2542128a421ed7d24cd58 /src/plugins/platforms/cocoa/qcocoahelpers.mm | |
parent | f8807b82207d7f4f41536f777473c8870673c186 (diff) |
macOS: Simplify helpers for flipping between quadrant I and IV
The different flip-functions have been replaced by a single function,
qt_mac_flip, with overloads for points and rects. This function is
primarily used to implement QCocoaScreen::map(To|From)Native, which
most clients of qt_flip* have been moved to. This makes it clearer
what kind of reference geometry we're flipping in relation to, and
simplifies call-sites.
Change-Id: I8a0862f94bb2c64a83a1c3168f984a195c0af6db
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'src/plugins/platforms/cocoa/qcocoahelpers.mm')
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoahelpers.mm | 53 |
1 files changed, 20 insertions, 33 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoahelpers.mm b/src/plugins/platforms/cocoa/qcocoahelpers.mm index 67d42a2544..85d12040de 100644 --- a/src/plugins/platforms/cocoa/qcocoahelpers.mm +++ b/src/plugins/platforms/cocoa/qcocoahelpers.mm @@ -232,50 +232,37 @@ QString qt_mac_applicationName() return appName; } -int qt_mac_primaryScreenHeight() -{ - QMacAutoReleasePool pool; - NSArray *screens = [NSScreen screens]; - if ([screens count] > 0) { - // The first screen in the screens array is documented to - // have the (0,0) origin and is designated the primary screen. - NSRect screenFrame = [[screens objectAtIndex: 0] frame]; - return screenFrame.size.height; - } - return 0; -} +// ------------------------------------------------------------------------- -int qt_mac_flipYCoordinate(int y) -{ - return qt_mac_primaryScreenHeight() - y; -} +/*! + \fn QPointF qt_mac_flip(const QPointF &pos, const QRectF &reference) + \fn QRectF qt_mac_flip(const QRectF &rect, const QRectF &reference) -qreal qt_mac_flipYCoordinate(qreal y) -{ - return qt_mac_primaryScreenHeight() - y; -} + Flips the Y coordinate of the point/rect between quadrant I and IV. -QPointF qt_mac_flipPoint(const NSPoint &p) -{ - return QPointF(p.x, qt_mac_flipYCoordinate(p.y)); -} + The native coordinate system on macOS uses quadrant I, with origin + in bottom left, and Qt uses quadrant IV, with origin in top left. -NSPoint qt_mac_flipPoint(const QPoint &p) -{ - return NSMakePoint(p.x(), qt_mac_flipYCoordinate(p.y())); -} + By flipping the Y coordinate, we can map the point/rect between + the two coordinate systems. -NSPoint qt_mac_flipPoint(const QPointF &p) + The flip is always in relation to a reference rectangle, e.g. + the frame of the parent view, or the screen geometry. In the + latter case the specialized QCocoaScreen::mapFrom/To functions + should be used instead. +*/ +QPointF qt_mac_flip(const QPointF &pos, const QRectF &reference) { - return NSMakePoint(p.x(), qt_mac_flipYCoordinate(p.y())); + return QPointF(pos.x(), reference.height() - pos.y()); } -NSRect qt_mac_flipRect(const QRect &rect) +QRectF qt_mac_flip(const QRectF &rect, const QRectF &reference) { - int flippedY = qt_mac_flipYCoordinate(rect.y() + rect.height()); - return NSMakeRect(rect.x(), flippedY, rect.width(), rect.height()); + return QRectF(qt_mac_flip(rect.bottomLeft(), reference), rect.size()); } +// ------------------------------------------------------------------------- + Qt::MouseButton cocoaButton2QtButton(NSInteger buttonNum) { if (buttonNum >= 0 && buttonNum <= 31) |