ngscopeclient v0.1.1
FilterGraphEditor.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* ngscopeclient *
4* *
5* Copyright (c) 2012-2025 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
35#ifndef FilterGraphEditor_h
36#define FilterGraphEditor_h
37
38#include "Dialog.h"
39#include "Session.h"
40#include "Bijection.h"
42
43#include <imgui_node_editor.h>
44#include <imgui_node_editor_internal.h>
45
46#include <typeinfo>
47#include <typeindex>
48
49template<class T>
50class lessID
51{
52public:
53 bool operator()(const T& a, const T& b) const
54 { return a.AsPointer() < b.AsPointer(); }
55};
56
58{
59public:
60 bool operator()(
61 const std::pair<ax::NodeEditor::PinId, ax::NodeEditor::PinId>& a,
62 const std::pair<ax::NodeEditor::PinId, ax::NodeEditor::PinId>& b) const
63 {
64 auto fpa = a.first.AsPointer();
65 auto fpb = b.first.AsPointer();
66 if(fpa < fpb)
67 return true;
68 else if(fpa > fpb)
69 return false;
70 else
71 return a.second.AsPointer() < b.second.AsPointer();
72 }
73};
74
76
78{
79public:
80
82
84 std::string m_name;
85
87 ax::NodeEditor::NodeId m_id;
88
90 ax::NodeEditor::NodeId m_outputId;
91
93 ax::NodeEditor::NodeId m_inputId;
94
96 std::set<ax::NodeEditor::NodeId, lessID<ax::NodeEditor::NodeId> > m_children;
97
99 std::set<ax::NodeEditor::PinId, lessID<ax::NodeEditor::PinId> > m_childSourcePins;
100
102 std::set<ax::NodeEditor::PinId, lessID<ax::NodeEditor::PinId> > m_childSinkPins;
103
105 Bijection<
107 ax::NodeEditor::PinId,
108 std::less<StreamDescriptor>,
110
112 Bijection<
114 ax::NodeEditor::PinId,
115 std::less<StreamDescriptor>,
117
119 Bijection<
121 ax::NodeEditor::LinkId,
122 std::less<StreamDescriptor>,
124
126 Bijection<
127 std::pair<FlowGraphNode*, int>,
128 ax::NodeEditor::PinId,
129 std::less< std::pair<FlowGraphNode*, int> >,
131
133 Bijection<
134 std::pair<FlowGraphNode*, int>,
135 ax::NodeEditor::PinId,
136 std::less< std::pair<FlowGraphNode*, int> >,
138
140 Bijection<
141 std::pair<FlowGraphNode*, int>,
142 ax::NodeEditor::LinkId,
143 std::less< std::pair<FlowGraphNode*, int> >,
145
146 void RefreshChildren();
147 void RefreshLinks();
148 void MoveBy(ImVec2 displacement);
149
150protected:
151 FilterGraphEditor& m_parent;
152};
153
155{
156public:
157 FilterGraphEditor(Session& session, MainWindow* parent);
158 virtual ~FilterGraphEditor();
159
160 virtual bool Render();
161 virtual bool DoRender();
162
163 std::map<uintptr_t, std::string> GetGroupIDs();
164
165 static bool IsBackEdge(FlowGraphNode* src, FlowGraphNode* dst);
166
167protected:
168 friend class FilterGraphGroup;
169
170 std::map<std::shared_ptr<Instrument>, std::vector<InstrumentChannel*> > GetAllVisibleChannels();
171 std::vector<FlowGraphNode*> GetAllNodes();
172
173 void RefreshGroupPorts();
174
175 ax::NodeEditor::PinId CanonicalizePin(ax::NodeEditor::PinId port);
176
178 void DoNodeForGroup(std::shared_ptr<FilterGraphGroup> group);
179 void DoInternalLinksForGroup(std::shared_ptr<FilterGraphGroup> group);
180 void DoNodeForGroupOutputs(std::shared_ptr<FilterGraphGroup> group);
181 void DoNodeForGroupInputs(std::shared_ptr<FilterGraphGroup> group);
182 void DoNodeForChannel(
183 InstrumentChannel* channel,
184 std::shared_ptr<Instrument> inst,
185 bool multiInst,
186 int64_t runtime);
187 void DoNodeForTrigger(Trigger* trig);
189 void HandleDoubleClicks();
190 void HandleLinkCreationRequests(Filter*& fReconfigure);
191 void HandleLinkDeletionRequests(Filter*& fReconfigure);
193 void DoAddMenu();
194
195 void HandleOverlaps();
197 const std::vector<ax::NodeEditor::NodeId>& nodes,
198 const std::vector<bool>& isgroup,
199 const std::vector<bool>& dragging,
200 const std::vector<bool>& nocollide,
201 const std::vector<ImVec2>& positions,
202 const std::vector<ImVec2>& sizes,
203 std::vector<ImVec2>& forces);
204 void ApplyNodeForces(
205 const std::vector<ax::NodeEditor::NodeId>& nodes,
206 const std::vector<bool>& isgroup,
207 const std::vector<bool>& dragging,
208 const std::vector<ImVec2>& positions,
209 std::vector<ImVec2>& forces);
210
211 void SetCanvasManagedDPI();
212 void SetImGuiManagedDPI();
213
215
216 void NodeIcon(InstrumentChannel* chan, ImVec2 iconpos, ImVec2 iconsize, ImDrawList* list);
217
219 void FilterSubmenu(StreamDescriptor src, const std::string& name, Filter::Category cat);
220 void CreateChannelMenu();
221
224
227
229 ax::NodeEditor::Config m_config;
230
232 ax::NodeEditor::EditorContext* m_context;
233
235 Bijection<
237 ax::NodeEditor::PinId,
238 std::less<StreamDescriptor>,
240
242 Bijection<
243 std::pair<FlowGraphNode*, int>,
244 ax::NodeEditor::PinId,
245 std::less< std::pair<FlowGraphNode*, int> >,
247
249 Bijection<
250 std::pair<ax::NodeEditor::PinId, ax::NodeEditor::PinId>,
251 ax::NodeEditor::LinkId,
254
257
259 uintptr_t m_nextID;
260
261 ax::NodeEditor::NodeId GetID(FlowGraphNode* node);
262
263 ax::NodeEditor::NodeId GetID(InstrumentChannel* chan)
264 { return m_session.m_idtable.emplace(chan); }
265
266 ax::NodeEditor::NodeId GetID(Trigger* trig)
267 { return m_session.m_idtable.emplace(trig); }
268
269 ax::NodeEditor::NodeId GetID(std::shared_ptr<FilterGraphGroup> group)
270 { return m_session.m_idtable.emplace(group.get()); }
271
272 uintptr_t AllocateID();
273 ax::NodeEditor::PinId GetID(StreamDescriptor stream);
274 ax::NodeEditor::PinId GetID(std::pair<FlowGraphNode*, size_t> input);
275 ax::NodeEditor::LinkId GetID(std::pair<ax::NodeEditor::PinId, ax::NodeEditor::PinId> link);
276
277 ax::NodeEditor::PinId GetSourcePinForLink(StreamDescriptor source, FlowGraphNode* sink);
278 ax::NodeEditor::PinId GetSinkPinForLink(StreamDescriptor source, std::pair<FlowGraphNode*, int> sink);
279
282
284 std::map<
285 ax::NodeEditor::NodeId,
286 std::shared_ptr<EmbeddableDialog>,
288
290 ax::NodeEditor::NodeId m_selectedProperties;
291
292 ImVec2 m_createMousePos;
293
295 std::pair<FlowGraphNode*, int> m_createInput;
296
298 Bijection<
299 std::shared_ptr<FilterGraphGroup>,
300 ax::NodeEditor::NodeId,
301 std::less< std::shared_ptr<FilterGraphGroup> >,
304
305 //DEBUG: forces for display
306 std::map<
307 ax::NodeEditor::NodeId,
308 ImVec2,
310 > m_nodeForces;
311
312 //DEBUG: render vector for force
313 void RenderForceVector(ImDrawList* list, ImVec2 pos, ImVec2 size, ImVec2 vec);
314
316 // Serialization
317
318 static bool SaveSettingsCallback(
319 const char* data,
320 size_t size,
321 ax::NodeEditor::SaveReasonFlags flags,
322 void* pThis);
323
324 static size_t LoadSettingsCallback(char* data, void* pThis);
325};
326
327#endif
Declaration of Bijection.
Declaration of Dialog.
Declaration of Session.
A strict one-to-one mapping from objects of type T1 to type T2 (which must be different types).
Definition: Bijection.h:48
Generic dialog box or other popup window.
Definition: Dialog.h:44
Definition: EmbeddableDialog.h:41
Definition: FilterGraphEditor.h:155
void DoNodeForChannel(InstrumentChannel *channel, std::shared_ptr< Instrument > inst, bool multiInst, int64_t runtime)
Make a node for a single channel, of any type.
Definition: FilterGraphEditor.cpp:1997
ax::NodeEditor::Config m_config
Graph editor setup.
Definition: FilterGraphEditor.h:229
ax::NodeEditor::EditorContext * m_context
Context containing current state of the graph editor.
Definition: FilterGraphEditor.h:232
Bijection< std::shared_ptr< FilterGraphGroup >, ax::NodeEditor::NodeId, std::less< std::shared_ptr< FilterGraphGroup > >, lessID< ax::NodeEditor::NodeId > > m_groups
Groups.
Definition: FilterGraphEditor.h:303
void CreateChannelMenu()
Runs the "add input" menu.
Definition: FilterGraphEditor.cpp:1624
std::map< std::shared_ptr< Instrument >, std::vector< InstrumentChannel * > > GetAllVisibleChannels()
Get a list of all channels that we are displaying nodes for.
Definition: FilterGraphEditor.cpp:252
void NodeIcon(InstrumentChannel *chan, ImVec2 iconpos, ImVec2 iconsize, ImDrawList *list)
Draws an icon showing the function of a node.
Definition: FilterGraphEditor.cpp:2288
Bijection< std::pair< FlowGraphNode *, int >, ax::NodeEditor::PinId, std::less< std::pair< FlowGraphNode *, int > >, lessID< ax::NodeEditor::PinId > > m_inputIDMap
Map of (channel, input number) to input port IDs.
Definition: FilterGraphEditor.h:246
virtual bool Render()
Renders the dialog and handles UI events.
Definition: FilterGraphEditor.cpp:235
std::pair< FlowGraphNode *, int > m_createInput
Input we're considering hooking a new channel up to.
Definition: FilterGraphEditor.h:295
void SetCanvasManagedDPI()
Use 1.0 as the DPI since the canvas scales independently.
Definition: FilterGraphEditor.cpp:1577
ax::NodeEditor::NodeId GetID(FlowGraphNode *node)
Gets the ID for an arbitrary node.
Definition: FilterGraphEditor.cpp:668
ax::NodeEditor::PinId CanonicalizePin(ax::NodeEditor::PinId port)
Gets the actual source/sink pin given a pin which might be a hierarchical port.
Definition: FilterGraphEditor.cpp:1365
MainWindow * m_parent
Top level window.
Definition: FilterGraphEditor.h:226
uintptr_t AllocateID()
Allocate an ID, avoiding collisions with the session IDTable.
Definition: FilterGraphEditor.cpp:2599
std::map< ax::NodeEditor::NodeId, std::shared_ptr< EmbeddableDialog >, lessID< ax::NodeEditor::NodeId > > m_propertiesDialogs
Properties dialogs for channels to be displayed inside nodes.
Definition: FilterGraphEditor.h:287
void DoNodeForTrigger(Trigger *trig)
Make a node for a trigger.
Definition: FilterGraphEditor.cpp:1906
void OutputPortTooltip(StreamDescriptor stream)
Display tooltips when mousing over interesting stuff.
Definition: FilterGraphEditor.cpp:1044
Session & m_session
Session being manipulated.
Definition: FilterGraphEditor.h:223
void ApplyNodeForces(const std::vector< ax::NodeEditor::NodeId > &nodes, const std::vector< bool > &isgroup, const std::vector< bool > &dragging, const std::vector< ImVec2 > &positions, std::vector< ImVec2 > &forces)
Once forces are calculated, actually move the nodes (unless being dragged)
Definition: FilterGraphEditor.cpp:1184
virtual bool DoRender()
Renders the dialog and handles UI events.
Definition: FilterGraphEditor.cpp:428
static bool IsBackEdge(FlowGraphNode *src, FlowGraphNode *dst)
Determine if a proposed edge in the filter graph is a back edge (one whose creation would lead to a c...
Definition: FilterGraphEditor.cpp:1602
std::map< uintptr_t, std::string > GetGroupIDs()
Return a list of group IDs and names.
Definition: FilterGraphEditor.cpp:2689
ax::NodeEditor::NodeId m_selectedProperties
Node whose properties we're currently interacting with.
Definition: FilterGraphEditor.h:290
static size_t LoadSettingsCallback(char *data, void *pThis)
Definition: FilterGraphEditor.cpp:2673
StreamDescriptor m_newFilterSourceStream
Source stream of the newly created filter.
Definition: FilterGraphEditor.h:281
void DoAddMenu()
Implement the add menu.
Definition: FilterGraphEditor.cpp:2516
void SetImGuiManagedDPI()
Use the ImGui viewport scale as the DPI scale.
Definition: FilterGraphEditor.cpp:1587
uintptr_t m_nextID
Next link/port ID to be allocated.
Definition: FilterGraphEditor.h:259
void ClearOldPropertiesDialogs()
Delete old properties dialogs for no-longer-extant nodes.
Definition: FilterGraphEditor.cpp:1014
void RefreshGroupPorts()
Figure out which source/sink ports are within each group.
Definition: FilterGraphEditor.cpp:684
void HandleLinkDeletionRequests(Filter *&fReconfigure)
Handle requests to delete a link.
Definition: FilterGraphEditor.cpp:1813
std::vector< FlowGraphNode * > GetAllNodes()
Get a list of all objects we're displaying nodes for (channels, filters, triggers,...
Definition: FilterGraphEditor.cpp:333
void FilterSubmenu(StreamDescriptor src, const std::string &name, Filter::Category cat)
Run the submenu for a single filter category.
Definition: FilterGraphEditor.cpp:1762
void FilterMenu(StreamDescriptor src)
Runs the "create filter" menu.
Definition: FilterGraphEditor.cpp:1729
void CalculateNodeForces(const std::vector< ax::NodeEditor::NodeId > &nodes, const std::vector< bool > &isgroup, const std::vector< bool > &dragging, const std::vector< bool > &nocollide, const std::vector< ImVec2 > &positions, const std::vector< ImVec2 > &sizes, std::vector< ImVec2 > &forces)
Calculates the forces applied to each node in the graph based on interaction physics.
Definition: FilterGraphEditor.cpp:1101
void HandleBackgroundContextMenu()
Show add menu when background is right clicked.
Definition: FilterGraphEditor.cpp:2492
ax::NodeEditor::PinId GetSourcePinForLink(StreamDescriptor source, FlowGraphNode *sink)
Gets the source pin we should use for drawing a connection.
Definition: FilterGraphEditor.cpp:371
Bijection< std::pair< ax::NodeEditor::PinId, ax::NodeEditor::PinId >, ax::NodeEditor::LinkId, lessIDPair, lessID< ax::NodeEditor::LinkId > > m_linkMap
Map of (ID, ID) to link IDs.
Definition: FilterGraphEditor.h:253
bool HandleNodeProperties()
Open the properties window when a node is right clicked.
Definition: FilterGraphEditor.cpp:2383
ax::NodeEditor::PinId GetSinkPinForLink(StreamDescriptor source, std::pair< FlowGraphNode *, int > sink)
Gets the sink pin we should use for drawing a connection.
Definition: FilterGraphEditor.cpp:399
void DoInternalLinksForGroup(std::shared_ptr< FilterGraphGroup > group)
Handle links between nodes in a group and the hierarchical ports.
Definition: FilterGraphEditor.cpp:968
void HandleLinkCreationRequests(Filter *&fReconfigure)
Handle requests to create a new link.
Definition: FilterGraphEditor.cpp:1396
Bijection< FlowGraphNode *, std::shared_ptr< FilterGraphGroup > > m_nodeGroupMap
Map of signal sources to the group the source node is in (if there is one)
Definition: FilterGraphEditor.h:256
void HandleOverlaps()
Find nodes that are intersecting, and apply forces to resolve collisions.
Definition: FilterGraphEditor.cpp:1276
void HandleDoubleClicks()
Opens a persistent properties window when a node is double clicked.
Definition: FilterGraphEditor.cpp:2343
Bijection< StreamDescriptor, ax::NodeEditor::PinId, std::less< StreamDescriptor >, lessID< ax::NodeEditor::PinId > > m_streamIDMap
Map of streams to output port IDs.
Definition: FilterGraphEditor.h:239
Definition: FilterGraphEditor.h:78
void MoveBy(ImVec2 displacement)
Moves this node and all of its child nodes.
Definition: FilterGraphEditor.cpp:182
Bijection< StreamDescriptor, ax::NodeEditor::PinId, std::less< StreamDescriptor >, lessID< ax::NodeEditor::PinId > > m_hierOutputMap
Map of streams to hierarchial output port IDs.
Definition: FilterGraphEditor.h:109
void RefreshChildren()
Refreshes the list of child nodes within this node.
Definition: FilterGraphEditor.cpp:63
ax::NodeEditor::NodeId m_id
ID of the group.
Definition: FilterGraphEditor.h:87
ax::NodeEditor::NodeId m_inputId
ID of the dummy node for input ports.
Definition: FilterGraphEditor.h:93
std::string m_name
Display name of the group.
Definition: FilterGraphEditor.h:84
Bijection< std::pair< FlowGraphNode *, int >, ax::NodeEditor::PinId, std::less< std::pair< FlowGraphNode *, int > >, lessID< ax::NodeEditor::PinId > > m_hierInputMap
Map of streams to hierarchial input port IDs.
Definition: FilterGraphEditor.h:130
std::set< ax::NodeEditor::PinId, lessID< ax::NodeEditor::PinId > > m_childSourcePins
List of output pins we contain on our child nodes.
Definition: FilterGraphEditor.h:99
std::set< ax::NodeEditor::PinId, lessID< ax::NodeEditor::PinId > > m_childSinkPins
List of input pins we contain on our child nodes.
Definition: FilterGraphEditor.h:102
void RefreshLinks()
Refreshes the list of links between this group and the outside world.
Definition: FilterGraphEditor.cpp:88
Bijection< std::pair< FlowGraphNode *, int >, ax::NodeEditor::LinkId, std::less< std::pair< FlowGraphNode *, int > >, lessID< ax::NodeEditor::LinkId > > m_hierInputLinkMap
Map of streams to internal link IDs.
Definition: FilterGraphEditor.h:144
Bijection< std::pair< FlowGraphNode *, int >, ax::NodeEditor::PinId, std::less< std::pair< FlowGraphNode *, int > >, lessID< ax::NodeEditor::PinId > > m_hierInputInternalMap
Map of streams to hierarchial input port internal-facing port IDs.
Definition: FilterGraphEditor.h:137
ax::NodeEditor::NodeId m_outputId
ID of the dummy node for output ports.
Definition: FilterGraphEditor.h:90
Bijection< StreamDescriptor, ax::NodeEditor::LinkId, std::less< StreamDescriptor >, lessID< ax::NodeEditor::LinkId > > m_hierOutputLinkMap
Map of streams to internal link IDs.
Definition: FilterGraphEditor.h:123
Bijection< StreamDescriptor, ax::NodeEditor::PinId, std::less< StreamDescriptor >, lessID< ax::NodeEditor::PinId > > m_hierOutputInternalMap
Map of streams to hierarchial output port internal-facing port IDs.
Definition: FilterGraphEditor.h:116
std::set< ax::NodeEditor::NodeId, lessID< ax::NodeEditor::NodeId > > m_children
List of nodes we contain (by ID)
Definition: FilterGraphEditor.h:96
Abstract base class for all filter graph blocks which are not physical instrument channels.
Definition: Filter.h:95
Category
Category the filter should be displayed under in the GUI.
Definition: Filter.h:108
Abstract base class for a node in the signal flow graph.
Definition: FlowGraphNode.h:55
uintptr_t emplace(SerializableObject *p)
Store a new object in the table.
Definition: IDTable.h:66
A single channel of an instrument.
Definition: InstrumentChannel.h:63
Top level application window.
Definition: MainWindow.h:115
Definition: SerializableObject.h:40
A Session stores all of the instrument configuration and other state the user has open.
Definition: Session.h:95
IDTable m_idtable
ID mapping used for serialization.
Definition: Session.h:299
Descriptor for a single stream coming off a channel.
Definition: StreamDescriptor.h:46
Abstract base class for oscilloscope / logic analyzer trigger inputs.
Definition: Trigger.h:46
Definition: FilterGraphEditor.h:58
Definition: FilterGraphEditor.h:51