QmlNodeEditor
SnapGrid.qml
Go to the documentation of this file.
1 import QtQuick 2.15
2 import QtQuick.Controls 2.15
3 import QtQuick.Shapes 1.15
4 
5 Item {
6  id: snapGrid
7  anchors.fill: parent
8  property int cellSize: 20
9  property int snapDistance: 5
10 
11  property int rows: height / cellSize
12  property int columns: width / cellSize
13 
14  function snapPosition(point) {
15  var snappedX = Math.round(point.x / snapGrid.cellSize) * snapGrid.cellSize;
16  var snappedY = Math.round(point.y / snapGrid.cellSize) * snapGrid.cellSize;
17 
18  return Qt.point(
19  Math.abs(point.x - snappedX) > snapGrid.snapDistance ? point.x : snappedX,
20  Math.abs(point.y - snappedY) > snapGrid.snapDistance ? point.y : snappedY);
21  }
22 
23  function snapX(x) {
24  var snapped = Math.round(x / snapGrid.cellSize) * snapGrid.cellSize;
25  return Math.abs(x - snapped) > snapGrid.snapDistance ? x : snapped;
26  }
27 
28  function snapY(y) {
29  var snapped = Math.round(y / snapGrid.cellSize) * snapGrid.cellSize;
30  return Math.abs(y - snapped) > snapGrid.snapDistance ? y : snapped;
31  }
32 
33  Repeater {
34  model: snapGrid.rows
35  Shape {
36  ShapePath {
37  startX: 0
38  startY: snapGrid.cellSize * index
39  strokeColor: "#eeeeee"
40  PathLine {
41  x: snapGrid.width
42  y: snapGrid.cellSize * index
43  }
44  }
45  }
46  }
47 
48  Repeater {
49  model: snapGrid.columns
50  Shape {
51  ShapePath {
52  startX: snapGrid.cellSize * index
53  startY: 0
54  strokeColor: "#eeeeee"
55  PathLine {
56  x: snapGrid.cellSize * index
57  y: snapGrid.width
58  }
59  }
60  }
61  }
62 }