SILICON
Loading...
Searching...
No Matches
enums.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#include <QGraphicsItem>
20#include <QVariant>
21#include <utility>
22
23namespace SILICON::ui {
24
25// 1. Define safe roles and categories
26enum class ItemDataRole { Category = Qt::UserRole + 1 };
27
28enum class ItemCategory : uint32_t {
29 None = 0,
30 GraphicalItem = 1 << 0,
31 WireSegment = 1 << 1,
32 Component = 1 << 2,
34 IO = 1 << 4,
35 Input = 1 << 5,
36 Output = 1 << 6
37};
38
40{
41 return static_cast<ItemCategory>(std::to_underlying(a) | std::to_underlying(b));
42}
43
45{
46 return static_cast<ItemCategory>(std::to_underlying(a) & std::to_underlying(b));
47}
48
49inline bool hasCategory(const QGraphicsItem* item, ItemCategory cat)
50{
51 if (!item)
52 return false;
53 QVariant val = item->data(std::to_underlying(ItemDataRole::Category));
54 if (!val.isValid())
55 return false;
56 return (val.toUInt() & std::to_underlying(cat)) == std::to_underlying(cat);
57}
58
59template <typename TargetType>
60TargetType* category_cast(QGraphicsItem* item, ItemCategory expectedCategory)
61{
62 return hasCategory(item, expectedCategory) ? static_cast<TargetType*>(item) : nullptr;
63}
64
65template <typename TargetType>
66const TargetType* category_cast(const QGraphicsItem* item, ItemCategory expectedCategory)
67{
68 return hasCategory(item, expectedCategory) ? static_cast<const TargetType*>(item)
69 : nullptr;
70}
71
119
120} // namespace SILICON::ui
Base class for all digital circuit components.
Base class for all graphical items in the diagram.
bool hasCategory(const QGraphicsItem *item, ItemCategory cat)
Definition enums.hpp:49
@ SINGLE_INPUT
Definition enums.hpp:80
@ WIRE_SEGMENT
Definition enums.hpp:75
@ SINGLE_OUTPUT
Definition enums.hpp:81
@ E_FLIP_FLOP
Definition enums.hpp:97
@ JK_FLIP_FLOP
Definition enums.hpp:98
@ WIRE_MERGER
Definition enums.hpp:88
@ D_FLIP_FLOP
Definition enums.hpp:96
@ ROM_COMPONENT
Definition enums.hpp:109
@ DEMULTIPLEXER
Definition enums.hpp:106
@ WIRE_SPLITTER
Definition enums.hpp:87
constexpr ItemCategory operator&(ItemCategory a, ItemCategory b)
Definition enums.hpp:44
constexpr ItemCategory operator|(ItemCategory a, ItemCategory b)
Definition enums.hpp:39
TargetType * category_cast(QGraphicsItem *item, ItemCategory expectedCategory)
Definition enums.hpp:60