Meshroom/meshroom/ui/qml/Controls/DelegateSelectionBox.qml
Yann Lanthony 6d2e9a2ba9 [ui] Utils: add SelectionBox and DelegateSelectionBox
- SelectionBox: generic Selection box component.
- DelegateSelectionBox: specialized SelectionBox to select model delegates
  from an instantiator (Repeater, ListView).

Also Introduce a Geom2D helper class to provide missing features for
intersection testing in QML.
2024-12-06 10:12:11 +01:00

32 lines
1.3 KiB
QML

import QtQuick
import Meshroom.Helpers
/*
A SelectionBox that can be used to select delegates in a model instantiator (Repeater, ListView...).
Interesection test is done in the coordinate system of the container Item, using delegate's bounding boxes.
The list of selected indices is emitted when the selection ends.
*/
SelectionBox {
id: root
// The Item instantiating the delegates.
property Item modelInstantiator
// The Item containing the delegates (used for coordinate mapping).
property Item container
// Emitted when the selection has ended, with the list of selected indices and modifiers.
signal delegateSelectionEnded(list<int> indices, int modifiers)
onSelectionEnded: function(selectionRect, modifiers) {
let selectedIndices = [];
const mappedSelectionRect = mapToItem(container, selectionRect);
for (var i = 0; i < modelInstantiator.count; ++i) {
const delegate = modelInstantiator.itemAt(i);
const delegateRect = Qt.rect(delegate.x, delegate.y, delegate.width, delegate.height);
if (Geom2D.rectRectIntersect(mappedSelectionRect, delegateRect)) {
selectedIndices.push(i);
}
}
delegateSelectionEnded(selectedIndices, modifiers);
}
}