SILICON
Loading...
Searching...
No Matches
geometry.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 <cstddef>
21#include <utility>
22
25
26namespace SILICON::ui {
27inline constexpr int EXTERNAL_PORT_MARGIN_GRID = 2;
28inline constexpr int EXTERNAL_PORT_SPACING_GRID = 2;
29
31[[nodiscard]] inline QPoint automaticPortPosition(const QSize& size, bool input,
32 std::size_t index, std::size_t count)
33{
34 const int margin = gridToPixels(EXTERNAL_PORT_MARGIN_GRID);
35 const int spacing = gridToPixels(EXTERNAL_PORT_SPACING_GRID);
36 const int columnHeight = (static_cast<int>(count) - 1) * spacing;
37 const int firstPortY = size.height() / 2 - columnHeight / 2;
38 return {input ? -margin : size.width() + margin,
39 firstPortY + static_cast<int>(index) * spacing};
40}
41
42[[nodiscard]] inline QRect shapeRect(const QSize& size)
43{
44 return {QPoint(0, 0), size};
45}
46
48[[nodiscard]] inline QPoint sanitizePortPosition(const QPointF& position,
49 const QRect& rect)
50{
51 const auto [side, projection] =
53 const int extension = gridToPixels(EXTERNAL_PORT_MARGIN_GRID);
54 switch (side) {
55 case PortSide::LEFT: return projection + QPoint(-extension, 0);
56 case PortSide::RIGHT: return projection + QPoint(extension, 0);
57 case PortSide::UP: return projection + QPoint(0, -extension);
58 case PortSide::DOWN: return projection + QPoint(0, extension);
59 }
60 std::unreachable();
61}
62} // namespace SILICON::ui
std::pair< PortSide, QPoint > nearestPortSide(const QPoint &pt, const QRect &rect)
Finds the nearest rectangle side for an interior or exterior point.
constexpr int EXTERNAL_PORT_SPACING_GRID
Definition geometry.hpp:28
int pixelsToNearestGrid(qreal value)
Definition grid.hpp:49
int gridToPixels(int value)
Definition grid.hpp:29
constexpr int EXTERNAL_PORT_MARGIN_GRID
Definition geometry.hpp:27
QRect shapeRect(const QSize &size)
Definition geometry.hpp:42
QPoint sanitizePortPosition(const QPointF &position, const QRect &rect)
Definition geometry.hpp:48
QPoint automaticPortPosition(const QSize &size, bool input, std::size_t index, std::size_t count)
Definition geometry.hpp:31