Skip to content

Instantly share code, notes, and snippets.

@benlau
Created March 11, 2015 04:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benlau/a0d57b865fe9eaf03c10 to your computer and use it in GitHub Desktop.
Save benlau/a0d57b865fe9eaf03c10 to your computer and use it in GitHub Desktop.
// Experimental InverseMouseArea
import QtQuick 2.0
Item {
id : component
property Item sensingArea : null
signal pressed
property var _mouseArea : null
function _setupMouseArea() {
if (!_mouseArea) {
_mouseArea = mouseAreaBuilder.createObject(component);
}
var p = sensingArea;
if (!p)
p = _topMostItem();
_mouseArea.parent = p;
}
function _destroyMouseArea() {
if (_mouseArea) {
_mouseArea.destroy();
_mouseArea = null;
}
}
function _inBound(pt) {
var ret = false;
if (pt.x >= component.x &&
pt.y >= component.y &&
pt.x <= component.x + component.width &&
pt.y <= component.y + component.height) {
ret = true;
}
return ret;
}
function _topMostItem() {
var p = component;
while (p.parent) {
p = p.parent;
}
return p;
}
Component {
id : mouseAreaBuilder
MouseArea {
propagateComposedEvents : true
anchors.fill: parent
z: 1000000000
onPressed: {
mouse.accepted = false;
var pt = mapToItem(component.parent,mouse.x,mouse.y)
if (!_inBound(pt))
component.pressed();
}
}
}
onEnabledChanged: {
_destroyMouseArea();
if (enabled) {
_setupMouseArea();
}
}
Component.onCompleted: {
if (enabled)
_setupMouseArea();
}
Component.onDestruction: {
_destroyMouseArea();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment