DynExp
Highly flexible laboratory automation for dynamically changing experiments.
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 "ui_CircuitDiagram.h"
13 #include "DynExpDefinitions.h"
14 #include "Object.h"
15 
16 namespace DynExp
17 {
18  class DynExpCore;
19 }
20 
21 class CircuitDiagram : public QDialog
22 {
23  Q_OBJECT
24 
25  class CircuitDiagramItem;
26 
27  using NodeMapType = std::unordered_map<DynExp::ItemIDType, CircuitDiagramItem>;
28  using NodeListType = std::vector<typename NodeMapType::mapped_type*>;
29 
30  static constexpr double ZoomFactor = 1.6;
31 
32  struct GraphType
33  {
37 
38  double EvaluateEnergy() const;
39  double DistanceTo(const GraphType& Other) const;
40 
41  bool operator==(const GraphType& Other) const;
42  bool operator!=(const GraphType& Other) const { return !(*this == Other); }
43  };
44 
46  {
47  public:
48  CircuitDiagramItem() : TreeWidgetItem(nullptr), Empty(true) {}
50 
51  void InsertLinks(const DynExp::ParamsBase::ObjectLinkParamsType& ObjectLinkParams, const DynExp::DynExpCore& DynExpCore,
53 
54  size_t GetNumLinkedItems();
55 
63  QTreeWidgetItem* TreeWidgetItem;
64 
69  QString NetworkAddress = nullptr;
70 
74  QPointF TopLeftPos;
75 
77  qreal ItemHeight{};
78 
80  {
82  {
83  LinkedItemType() noexcept : Item(nullptr), SocketIndex(0) {}
85 
87  size_t SocketIndex;
88  };
89 
90  std::string_view LinkTitle;
91  std::vector<LinkedItemType> LinkedItems; // May contain LinkedItemType with Item = nullptr.
93  };
94 
98  std::vector<LinkedParamType> LinkedParams;
99 
103  const bool Empty;
104 
105  // Item's displayed parts
107  {
108  QGraphicsPathItem* Frame = nullptr;
109  QGraphicsSimpleTextItem* Label = nullptr;
110  std::vector<QGraphicsEllipseItem*> Sockets;
111  std::vector<QGraphicsPathItem*> Links;
112  };
113 
114  QGraphicsPathItem* StateFrame = nullptr;
115  QIcon StateIcon;
116  QGraphicsPixmapItem* StatePixmap = nullptr;
117  QGraphicsSimpleTextItem* StateLabel = nullptr;
118  QGraphicsPathItem* ItemFrame = nullptr;
119  QGraphicsSimpleTextItem* ItemLabel = nullptr;
120  QIcon ItemIcon;
121  QGraphicsPixmapItem* ItemPixmap = nullptr;
122  QGraphicsEllipseItem* OutputSocket = nullptr;
123  std::vector<ParamGraphicsItemsType> ParamGraphicsItems;
124  QIcon NetworkIcon;
125  QGraphicsPixmapItem* NetworkPixmap = nullptr;
126  QGraphicsPathItem* NetworkLink = nullptr;
127  };
128 
129 public:
130  CircuitDiagram(QWidget *parent);
131  ~CircuitDiagram();
132 
133  QTreeWidgetItem* GetSelectedEntry();
134 
140  bool Redraw(const DynExp::DynExpCore& DynExpCore);
141 
147  bool UpdateStates(const DynExp::DynExpCore& DynExpCore);
148 
149 protected:
150  virtual void mouseDoubleClickEvent(QMouseEvent* Event) override;
151  virtual void wheelEvent(QWheelEvent* Event) override;
152 
153 private:
154  // Constants for rendering the circuit diagram
155  static constexpr int NodeVSep = 40;
156  static constexpr int NodeHSep = 220;
157  static constexpr int TreeWidgetParentTypeColumn = 0;
158  static constexpr int TreeWidgetItemNameColumn = 0;
159  static constexpr int TreeWidgetItemTypeColumn = 1;
160  static constexpr int TreeWidgetItemStateColumn = 2;
161  static constexpr int OuterPenLineWidth = 4;
162  static constexpr int InnerPenLineWidth = 1;
163  static constexpr int CornerRoundingRadius = 8;
164  static constexpr int InnerWidth = 140;
165  static constexpr int InnerStartHeight = 50;
166  static constexpr int InnerMargin = 6;
167  static constexpr int InnerMarginTop = 10;
168  static constexpr int InnerMarginBottom = 20;
169  static constexpr int StateIconSize = 16;
170  static constexpr int TypeIconSize = 24;
171  static constexpr int IconMargin = 4;
172  static constexpr int AdditionalHeightPerParam = 12;
173  static constexpr int ParamSep = 6;
174  static constexpr int SocketDiameter = 6;
175  static constexpr int SocketPenLineWidth = 2;
176  static constexpr int NetworkIconDistance = 30;
177  static const QColor SocketOuterColor;
178  static const QColor SocketInnerColor;
179 
180  static QLinearGradient GetGrayLinearGradient();
181 
182  // Helper functions
183  void Clear();
184  void BuildTree(const DynExp::DynExpCore& DynExpCore);
185  void ArrangeTree();
187  void Render();
188  void RenderItem(CircuitDiagramItem& Item, bool DrawOutputSocket);
189  void RenderLinks(CircuitDiagramItem& Item);
190  void UpdateItem(CircuitDiagramItem& Item);
191  int TransformIconSize(int Size) const;
192  void RescaleIcons();
193 
194  // Zoom functions
195  void ZoomIn();
196  void ZoomOut();
197  void ZoomReset();
198 
199  // Displayed items
203 
204  // Item currently selected by a double-click
206  QTreeWidgetItem* SelectedTreeWidgetItem;
207 
208  // UI
209  Ui::CircuitDiagram ui;
210  std::unique_ptr<QGraphicsScene> Scene;
211  QMenu* ContextMenu;
212 
213 private slots:
214  void OnContextMenuRequested(QPoint Position);
215  void OnZoomIn();
216  void OnZoomOut();
217  void OnZoomReset();
218  void OnSaveDiagram();
219 };
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 * ItemPixmap
QGraphicsPixmapItem * NetworkPixmap
QGraphicsPixmapItem * StatePixmap
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::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.
CircuitDiagram(QWidget *parent)
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
Ui::CircuitDiagram ui
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
double EvaluateEnergy() const
bool operator!=(const GraphType &Other) const
double DistanceTo(const GraphType &Other) const