diff options
author | Morten Johan Sørvig <morten.sorvig@theqtcompany.com> | 2015-11-10 12:43:59 +0100 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@theqtcompany.com> | 2016-03-21 16:26:18 +0000 |
commit | 820e69d6c214f95e6db101c6e7caf28b0a248c69 (patch) | |
tree | e48510c2eb3070f41cf4873764bc7210cbdaddef | |
parent | 3ee01f74031d9e1d0aad0bdf699e0a4ff5e288ba (diff) |
Cocoa: Unify mouse handling logic
Reduce the code duplication for the various button
type handlers. Fan in: (example)
rightMouseDown
handleMouseDownEvent
handleMouseEvent
The primary mouseDown function is still separate
with some duplicated logic.
Remove the "invalid button tracking" warning. qWarnings
are for application developers in case of improper
use of API etc, not internal Qt errors.
Change-Id: Idb1a311e37446399668c2a207831fccc84716ca1
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@theqtcompany.com>
-rw-r--r-- | src/plugins/platforms/cocoa/qnsview.h | 3 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qnsview.mm | 169 |
2 files changed, 106 insertions, 66 deletions
diff --git a/src/plugins/platforms/cocoa/qnsview.h b/src/plugins/platforms/cocoa/qnsview.h index cbd92df9cf..59df75b980 100644 --- a/src/plugins/platforms/cocoa/qnsview.h +++ b/src/plugins/platforms/cocoa/qnsview.h @@ -118,6 +118,9 @@ Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(QNSViewMouseMoveHelper)); - (void)resetMouseButtons; - (void)handleMouseEvent:(NSEvent *)theEvent; +- (bool)handleMouseDownEvent:(NSEvent *)theEvent; +- (bool)handleMouseDraggedEvent:(NSEvent *)theEvent; +- (bool)handleMouseUpEvent:(NSEvent *)theEvent; - (void)mouseDown:(NSEvent *)theEvent; - (void)mouseDragged:(NSEvent *)theEvent; - (void)mouseUp:(NSEvent *)theEvent; diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm index 0733ac3092..ecc9f2db11 100644 --- a/src/plugins/platforms/cocoa/qnsview.mm +++ b/src/plugins/platforms/cocoa/qnsview.mm @@ -818,6 +818,61 @@ QT_WARNING_POP QWindowSystemInterface::handleFrameStrutMouseEvent(m_window, timestamp, qtWindowPoint, qtScreenPoint, m_frameStrutButtons); } +- (bool)handleMouseDownEvent:(NSEvent *)theEvent +{ + if (m_window && (m_window->flags() & Qt::WindowTransparentForInput)) + return false; + + Qt::MouseButton button = cocoaButton2QtButton([theEvent buttonNumber]); + + if (button == Qt::RightButton) + m_sendUpAsRightButton = true; + + m_buttons |= button; + + [self handleMouseEvent:theEvent]; + return true; +} + +- (bool)handleMouseDraggedEvent:(NSEvent *)theEvent +{ + if (m_window && (m_window->flags() & Qt::WindowTransparentForInput)) + return false; + + Qt::MouseButton button = cocoaButton2QtButton([theEvent buttonNumber]); + + // Forward the event to the next responder if Qt did not accept the + // corresponding mouse down for this button + if (!m_acceptedMouseDowns.contains(button)) + return false; + + if (!(m_buttons & (m_sendUpAsRightButton ? Qt::RightButton : Qt::LeftButton))) { + qCWarning(lcQpaCocoaWindow) << "QNSView mouseDragged: Internal mouse button tracking" + << "invalid (missing Qt::LeftButton)"; + } + + [self handleMouseEvent:theEvent]; + return true; +} + +- (bool)handleMouseUpEvent:(NSEvent *)theEvent +{ + if (m_window && (m_window->flags() & Qt::WindowTransparentForInput)) + return false; + + Qt::MouseButton button = cocoaButton2QtButton([theEvent buttonNumber]); + + if (m_sendUpAsRightButton && button == Qt::LeftButton) + button = Qt::RightButton; + if (button == Qt::RightButton) + m_sendUpAsRightButton = false; + + m_buttons &= ~button; + + [self handleMouseEvent:theEvent]; + return true; +} + - (void)mouseDown:(NSEvent *)theEvent { if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) @@ -871,24 +926,58 @@ QT_WARNING_POP - (void)mouseDragged:(NSEvent *)theEvent { - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super mouseDragged:theEvent]; - if (!(m_buttons & (m_sendUpAsRightButton ? Qt::RightButton : Qt::LeftButton))) - qWarning("QNSView mouseDragged: Internal mouse button tracking invalid (missing Qt::LeftButton)"); - [self handleMouseEvent:theEvent]; + const bool accepted = [self handleMouseDraggedEvent:theEvent]; + if (!accepted) + [super mouseDragged:theEvent]; } - (void)mouseUp:(NSEvent *)theEvent { - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super mouseUp:theEvent]; - if (m_sendUpAsRightButton) { - m_buttons &= ~Qt::RightButton; - m_sendUpAsRightButton = false; - } else { - m_buttons &= ~Qt::LeftButton; - } - [self handleMouseEvent:theEvent]; + const bool accepted = [self handleMouseUpEvent:theEvent]; + if (!accepted) + [super mouseUp:theEvent]; +} + +- (void)rightMouseDown:(NSEvent *)theEvent +{ + const bool accepted = [self handleMouseDownEvent:theEvent]; + if (!accepted) + [super rightMouseDown:theEvent]; +} + +- (void)rightMouseDragged:(NSEvent *)theEvent +{ + const bool accepted = [self handleMouseDraggedEvent:theEvent]; + if (!accepted) + [super rightMouseDragged:theEvent]; +} + +- (void)rightMouseUp:(NSEvent *)theEvent +{ + const bool accepted = [self handleMouseUpEvent:theEvent]; + if (!accepted) + [super rightMouseUp:theEvent]; +} + +- (void)otherMouseDown:(NSEvent *)theEvent +{ + const bool accepted = [self handleMouseDownEvent:theEvent]; + if (!accepted) + [super otherMouseDown:theEvent]; +} + +- (void)otherMouseDragged:(NSEvent *)theEvent +{ + const bool accepted = [self handleMouseDraggedEvent:theEvent]; + if (!accepted) + [super otherMouseDragged:theEvent]; +} + +- (void)otherMouseUp:(NSEvent *)theEvent +{ + const bool accepted = [self handleMouseUpEvent:theEvent]; + if (!accepted) + [super otherMouseUp:theEvent]; } - (void)updateTrackingAreas @@ -999,58 +1088,6 @@ QT_WARNING_POP m_platformWindow->m_enterLeaveTargetWindow = 0; } -- (void)rightMouseDown:(NSEvent *)theEvent -{ - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super rightMouseDown:theEvent]; - m_buttons |= Qt::RightButton; - m_sendUpAsRightButton = true; - [self handleMouseEvent:theEvent]; -} - -- (void)rightMouseDragged:(NSEvent *)theEvent -{ - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super rightMouseDragged:theEvent]; - if (!(m_buttons & Qt::RightButton)) - qWarning("QNSView rightMouseDragged: Internal mouse button tracking invalid (missing Qt::RightButton)"); - [self handleMouseEvent:theEvent]; -} - -- (void)rightMouseUp:(NSEvent *)theEvent -{ - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super rightMouseUp:theEvent]; - m_buttons &= ~Qt::RightButton; - m_sendUpAsRightButton = false; - [self handleMouseEvent:theEvent]; -} - -- (void)otherMouseDown:(NSEvent *)theEvent -{ - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super otherMouseDown:theEvent]; - m_buttons |= cocoaButton2QtButton([theEvent buttonNumber]); - [self handleMouseEvent:theEvent]; -} - -- (void)otherMouseDragged:(NSEvent *)theEvent -{ - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super otherMouseDragged:theEvent]; - if (!(m_buttons & ~(Qt::LeftButton | Qt::RightButton))) - qWarning("QNSView otherMouseDragged: Internal mouse button tracking invalid (missing Qt::MiddleButton or Qt::ExtraButton*)"); - [self handleMouseEvent:theEvent]; -} - -- (void)otherMouseUp:(NSEvent *)theEvent -{ - if (m_window && (m_window->flags() & Qt::WindowTransparentForInput) ) - return [super otherMouseUp:theEvent]; - m_buttons &= ~cocoaButton2QtButton([theEvent buttonNumber]); - [self handleMouseEvent:theEvent]; -} - struct QCocoaTabletDeviceData { QTabletEvent::TabletDevice device; |