SILICON
Loading...
Searching...
No Matches
portGeometry.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <algorithm>
21#include <utility>
22
23#include <QPoint>
24#include <QRect>
25
26
27namespace SILICON::ui {
28enum class PortSide {
33};
34
36
37struct RectEdges {
38 int left;
39 int top;
40 int right;
41 int bottom;
42};
43
44[[nodiscard]] inline RectEdges rectEdges(const QRect& rect)
45{
46 const QRect normalized = rect.normalized();
47
48 // QRect::right()/bottom() are inclusive pixel coordinates, so an 80-pixel
49 // rectangle reports 79. Port geometry uses the QGraphicsRectItem boundary
50 // instead: x + width and y + height.
51 return {.left = normalized.x(),
52 .top = normalized.y(),
53 .right = normalized.x() + normalized.width(),
54 .bottom = normalized.y() + normalized.height()};
55}
56
57} // namespace portGeometryDetail
58
62[[nodiscard]] inline bool isPortPositionOutside(const QPoint& pt, const QRect& rect)
63{
64 const auto edges = SILICON::ui::portGeometryDetail::rectEdges(rect);
65 return pt.x() < edges.left || pt.x() > edges.right || pt.y() < edges.top
66 || pt.y() > edges.bottom;
67}
68
73[[nodiscard]] inline std::pair<PortSide, QPoint> nearestPortSide(const QPoint& pt,
74 const QRect& rect)
75{
76 const auto edges = SILICON::ui::portGeometryDetail::rectEdges(rect);
77
78 const int leftOverflow = std::max(0, edges.left - pt.x());
79 const int rightOverflow = std::max(0, pt.x() - edges.right);
80 const int topOverflow = std::max(0, edges.top - pt.y());
81 const int bottomOverflow = std::max(0, pt.y() - edges.bottom);
82
83 const int horizontalOverflow = std::max(leftOverflow, rightOverflow);
84 const int verticalOverflow = std::max(topOverflow, bottomOverflow);
85 if (horizontalOverflow > 0 || verticalOverflow > 0) {
86 if (horizontalOverflow >= verticalOverflow) {
87 const auto side = leftOverflow >= rightOverflow ? PortSide::LEFT : PortSide::RIGHT;
88 // At a corner, clamp the other coordinate to keep the projection on the
89 // rectangle. Otherwise the resulting port line would remain outside it.
90 return {side, QPoint(side == PortSide::LEFT ? edges.left : edges.right,
91 std::clamp(pt.y(), edges.top, edges.bottom))};
92 }
93
94 const auto side = topOverflow >= bottomOverflow ? PortSide::UP : PortSide::DOWN;
95 return {side, QPoint(std::clamp(pt.x(), edges.left, edges.right),
96 side == PortSide::UP ? edges.top : edges.bottom)};
97 }
98
99 const int leftDistance = pt.x() - edges.left;
100 const int rightDistance = edges.right - pt.x();
101 const int topDistance = pt.y() - edges.top;
102 const int bottomDistance = edges.bottom - pt.y();
103 const int nearest =
104 std::min({leftDistance, rightDistance, topDistance, bottomDistance});
105
106 if (nearest == leftDistance)
107 return {PortSide::LEFT, QPoint(edges.left, pt.y())};
108 if (nearest == rightDistance)
109 return {PortSide::RIGHT, QPoint(edges.right, pt.y())};
110 if (nearest == topDistance)
111 return {PortSide::UP, QPoint(pt.x(), edges.top)};
112 return {PortSide::DOWN, QPoint(pt.x(), edges.bottom)};
113}
114
115} // namespace SILICON::ui
RectEdges rectEdges(const QRect &rect)
std::pair< PortSide, QPoint > nearestPortSide(const QPoint &pt, const QRect &rect)
Finds the nearest rectangle side for an interior or exterior point.
bool isPortPositionOutside(const QPoint &pt, const QRect &rect)
Returns whether a position lies strictly beyond a rectangle boundary.