DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
CircuitDiagram.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
9#pragma once
10
11#include <QWidget>
12#include "DynExpDefinitions.h"
13#include "Object.h"
14
15namespace Ui
16{
17 class CircuitDiagram;
18}
19
20namespace DynExp
21{
22 class DynExpCore;
23}
24
25class CircuitDiagram : public QDialog
26{
27 Q_OBJECT
28
30
31 using NodeMapType = std::unordered_map<DynExp::ItemIDType, CircuitDiagramItem>;
32 using NodeListType = std::vector<typename NodeMapType::mapped_type*>;
33
34 static constexpr double ZoomFactor = 1.6;
35
36 struct GraphType
37 {
41
42 double EvaluateEnergy() const;
43 double DistanceTo(const GraphType& Other) const;
44
45 bool operator==(const GraphType& Other) const;
46 bool operator!=(const GraphType& Other) const { return !(*this == Other); }
47 };
48
50 {
51 public:
54
55 void InsertLinks(const DynExp::ParamsBase::ObjectLinkParamsType& ObjectLinkParams, const DynExp::DynExpCore& DynExpCore,
57
58 size_t GetNumLinkedItems();
59
67 QTreeWidgetItem* TreeWidgetItem;
68
73 QString NetworkAddress = nullptr;
74
78 QPointF TopLeftPos;
79
81 qreal ItemHeight{};
82
84 {
93
94 std::string_view LinkTitle;
95 std::vector<LinkedItemType> LinkedItems; // May contain LinkedItemType with Item = nullptr.
97 };
98
102 std::vector<LinkedParamType> LinkedParams;
103
107 const bool Empty;
108
109 // Item's displayed parts
111 {
112 QGraphicsPathItem* Frame = nullptr;
113 QGraphicsSimpleTextItem* Label = nullptr;
114 std::vector<QGraphicsEllipseItem*> Sockets;
115 std::vector<QGraphicsPathItem*> Links;
116 };
117
118 QGraphicsPathItem* StateFrame = nullptr;
120 QGraphicsPixmapItem* StatePixmap = nullptr;
121 QGraphicsSimpleTextItem* StateLabel = nullptr;
122 QGraphicsPathItem* ItemFrame = nullptr;
123 QGraphicsSimpleTextItem* ItemLabel = nullptr;
124 QIcon ItemIcon;
125 QGraphicsPixmapItem* ItemPixmap = nullptr;
126 QGraphicsEllipseItem* OutputSocket = nullptr;
127 std::vector<ParamGraphicsItemsType> ParamGraphicsItems;
129 QGraphicsPixmapItem* NetworkPixmap = nullptr;
130 QGraphicsPathItem* NetworkLink = nullptr;
131 };
132
133public:
134 CircuitDiagram(QWidget *parent);
136
137 QTreeWidgetItem* GetSelectedEntry();
138
144 bool Redraw(const DynExp::DynExpCore& DynExpCore);
145
151 bool UpdateStates(const DynExp::DynExpCore& DynExpCore);
152
153protected:
154 virtual void mouseDoubleClickEvent(QMouseEvent* Event) override;
155 virtual void wheelEvent(QWheelEvent* Event) override;
156
157private:
158 // Constants for rendering the circuit diagram
159 static constexpr int NodeVSep = 40;
160 static constexpr int NodeHSep = 220;
161 static constexpr int TreeWidgetParentTypeColumn = 0;
162 static constexpr int TreeWidgetItemNameColumn = 0;
163 static constexpr int TreeWidgetItemTypeColumn = 1;
164 static constexpr int TreeWidgetItemStateColumn = 2;
165 static constexpr int OuterPenLineWidth = 4;
166 static constexpr int InnerPenLineWidth = 1;
167 static constexpr int CornerRoundingRadius = 8;
168 static constexpr int InnerWidth = 140;
169 static constexpr int InnerStartHeight = 50;
170 static constexpr int InnerMargin = 6;
171 static constexpr int InnerMarginTop = 10;
172 static constexpr int InnerMarginBottom = 20;
173 static constexpr int StateIconSize = 16;
174 static constexpr int TypeIconSize = 24;
175 static constexpr int IconMargin = 4;
176 static constexpr int AdditionalHeightPerParam = 12;
177 static constexpr int ParamSep = 6;
178 static constexpr int SocketDiameter = 6;
179 static constexpr int SocketPenLineWidth = 2;
180 static constexpr int NetworkIconDistance = 30;
181 static const QColor SocketOuterColor;
182 static const QColor SocketInnerColor;
183
184 static QLinearGradient GetGrayLinearGradient();
185
186 // Helper functions
187 void Clear();
188 void BuildTree(const DynExp::DynExpCore& DynExpCore);
189 void ArrangeTree();
191 void Render();
192 void RenderItem(CircuitDiagramItem& Item, bool DrawOutputSocket);
194 void UpdateItem(CircuitDiagramItem& Item);
195 int TransformIconSize(int Size) const;
196 void RescaleIcons();
197
198 // Zoom functions
199 void ZoomIn();
200 void ZoomOut();
201 void ZoomReset();
202
203 // Displayed items
207
208 // UI
209 std::unique_ptr<Ui::CircuitDiagram> ui;
210 std::unique_ptr<QGraphicsScene> Scene;
212
213 // Item currently selected by a double-click
215 QTreeWidgetItem* SelectedTreeWidgetItem;
216
217private slots:
218 void OnContextMenuRequested(QPoint Position);
219 void OnZoomIn();
220 void OnZoomOut();
221 void OnZoomReset();
222 void OnSaveDiagram();
223};
Implementation of DynExp objects as the base for derived resources and implementation of the object p...
QGraphicsEllipseItem * OutputSocket
QString NetworkAddress
String of a network address and a network port indicating whether this item connects to a network....
QTreeWidgetItem * TreeWidgetItem
Pointer to QTreeWidgetItem listed in main window's tree view. Pointer may be dereferenced after this ...
CircuitDiagramItem(QTreeWidgetItem *TreeWidgetItem)
QGraphicsSimpleTextItem * StateLabel
void InsertLinks(const DynExp::ParamsBase::ObjectLinkParamsType &ObjectLinkParams, const DynExp::DynExpCore &DynExpCore, NodeMapType *HardwareAdapterNodes, NodeMapType *InstrumentNodes)
QPointF TopLeftPos
Node's top left position in the diagram in painting area's coordinates.
const bool Empty
Indicates whether this item points to an existing object.
std::vector< ParamGraphicsItemsType > ParamGraphicsItems
QGraphicsPixmapItem * NetworkPixmap
std::vector< LinkedParamType > LinkedParams
List of linked params making use of this item.
QGraphicsSimpleTextItem * ItemLabel
static constexpr int AdditionalHeightPerParam
static QLinearGradient GetGrayLinearGradient()
bool Redraw(const DynExp::DynExpCore &DynExpCore)
Rebuilds the complete circuit diagram.
NodeMapType HardwareAdapterNodes
static constexpr int InnerWidth
QTreeWidgetItem * SelectedTreeWidgetItem
static constexpr int StateIconSize
void UpdateItem(CircuitDiagramItem &Item)
QTreeWidgetItem * GetSelectedEntry()
static constexpr int ParamSep
void OnContextMenuRequested(QPoint Position)
static constexpr int InnerMarginTop
static constexpr double ZoomFactor
std::unordered_map< DynExp::ItemIDType, CircuitDiagramItem > NodeMapType
static constexpr int CornerRoundingRadius
static constexpr int TreeWidgetParentTypeColumn
std::unique_ptr< Ui::CircuitDiagram > ui
std::vector< typename NodeMapType::mapped_type * > NodeListType
static constexpr int NetworkIconDistance
void RenderLinks(CircuitDiagramItem &Item)
bool UpdateStates(const DynExp::DynExpCore &DynExpCore)
Updates the items' states shown in the circuit diagram displayed currently.
void RenderItem(CircuitDiagramItem &Item, bool DrawOutputSocket)
static constexpr int TreeWidgetItemTypeColumn
static constexpr int TreeWidgetItemStateColumn
NodeMapType InstrumentNodes
static const QColor SocketInnerColor
virtual void mouseDoubleClickEvent(QMouseEvent *Event) override
std::unique_ptr< QGraphicsScene > Scene
static constexpr int TypeIconSize
static constexpr int InnerStartHeight
static constexpr int IconMargin
static constexpr int SocketDiameter
static constexpr int NodeHSep
static constexpr int InnerMargin
NodeMapType ModuleNodes
static constexpr int SocketPenLineWidth
static constexpr int NodeVSep
static constexpr int TreeWidgetItemNameColumn
static const QColor SocketOuterColor
virtual void wheelEvent(QWheelEvent *Event) override
static constexpr int InnerPenLineWidth
void RefineBySimulatedAnnealing(GraphType &Graph)
void BuildTree(const DynExp::DynExpCore &DynExpCore)
static constexpr int OuterPenLineWidth
int TransformIconSize(int Size) const
static constexpr int InnerMarginBottom
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition DynExpCore.h:127
std::vector< std::reference_wrapper< LinkBase > > ObjectLinkParamsType
Type of a list of all owned object link parameters.
Definition Object.h:352
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
bool operator==(const GraphType &Other) const
bool operator!=(const GraphType &Other) const
double DistanceTo(const GraphType &Other) const