-
Notifications
You must be signed in to change notification settings - Fork 635
/
PortModel.cs
593 lines (523 loc) · 17.5 KB
/
PortModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Xml;
using Dynamo.Configuration;
using Dynamo.Engine;
using Dynamo.Graph.Connectors;
using Dynamo.Graph.Nodes.ZeroTouch;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Utilities;
using Newtonsoft.Json;
using ProtoCore.AST.AssociativeAST;
namespace Dynamo.Graph.Nodes
{
/// <summary>
/// Interaction logic for dynPort.xaml
/// </summary>
public enum PortType { Input, Output };
/// <summary>
/// PortModel represents Dynamo ports.
/// </summary>
public class PortModel : ModelBase, IEquatable<PortModel>
{
#region private fields
ObservableCollection<ConnectorModel> connectors = new ObservableCollection<ConnectorModel>();
private bool usingDefaultValue;
private bool isEnabled = true;
private bool useLevels = false;
private bool keepListStructure = false;
private int level = 1;
private string toolTip;
#endregion
#region public members
/// <summary>
/// ID of the PortModel, which is unique within the graph.
/// </summary>
[JsonProperty("Id")]
[JsonConverter(typeof(IdToGuidConverter))]
public override Guid GUID
{
get
{
return base.GUID;
}
set
{
base.GUID = value;
}
}
/// <summary>
/// Returns the connectors between the specified ports.
/// </summary>
[JsonIgnore]
public ObservableCollection<ConnectorModel> Connectors
{
get { return connectors; }
set { connectors = value; }
}
/// <summary>
/// Name of the port.
/// </summary>
public string Name
{
get;
internal set;
}
/// <summary>
/// Tooltip of the port.
/// </summary>
[JsonProperty("Description")]
public string ToolTip
{
get
{
string useDefaultArgument = string.Empty;
if (!UsingDefaultValue && DefaultValue != null)
useDefaultArgument = " " + Properties.Resources.DefaultValueDisabled;
return toolTip.Contains(Properties.Resources.DefaultValueDisabled)? toolTip: toolTip + useDefaultArgument;
}
set
{
toolTip = value;
RaisePropertyChanged("ToolTip");
}
}
/// <summary>
/// Type of the port.
/// It can be incoming or outcoming.
/// </summary>
[JsonIgnore]
public PortType PortType
{
get;
internal set;
}
/// <summary>
/// Returns the Node.
/// </summary>
[JsonIgnore]
public NodeModel Owner
{
get;
internal set;
}
/// <summary>
/// Index of the port.
/// </summary>
[JsonIgnore]
public int Index
{
get { return Owner.GetPortModelIndex(this); }
}
/// <summary>
/// Returns the LineIndex of that port. The vertical position of PortModel is dependent on LineIndex.
/// </summary>
[JsonIgnore]
public int LineIndex
{
get;
private set;
}
/// <summary>
/// Indicates whether the port is enabled or not.
/// </summary>
[JsonIgnore]
public bool IsEnabled
{
get
{
return isEnabled;
}
set
{
isEnabled = value;
RaisePropertyChanged("IsEnabled");
}
}
private Point2D center = new Point2D();
/// <summary>
/// Center is used by connected connectors to update their shape
/// The "center" of a port is derived from the type of port and
/// offsets from the node origin based on the port's index in the
/// ports collection.
/// </summary>
[JsonIgnore]
public Point2D Center
{
get
{
// If it is a proxy port, return the center as it is already set in the Annotion Model.
if (IsProxyPort)
{
return center;
}
// If it is a node port, calculate the center based on that node position.
else
{
double halfHeight = Height * 0.5;
double offset = Owner.GetPortVerticalOffset(this);
double y = Owner.Y + NodeModel.HeaderHeight + halfHeight + offset + 9;
switch (PortType)
{
case PortType.Input:
return new Point2D(Owner.X, y);
case PortType.Output:
if (Owner is CodeBlockNodeModel)
{
// Special case because code block outputs are smaller than regular outputs.
// This ensures the output port of the first code block output aligns with
// the first input port of any node.
return new Point2D(Owner.X + Owner.Width, y + 9);
}
return new Point2D(Owner.X + Owner.Width, y);
}
return new Point2D();
}
}
internal set
{
if (center.Equals(value)) return;
center = value;
RaisePropertyChanged(nameof(Center));
}
}
/// <summary>
/// Controls whether this port is set to use it's default value (true) or yield a closure (false).
/// </summary>
[DefaultValue(true)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public bool UsingDefaultValue
{
get { return usingDefaultValue; }
set
{
if (usingDefaultValue != value)
{
usingDefaultValue = value;
RaisePropertyChanged("UsingDefaultValue");
RaisePropertyChanged("ToolTip");
}
}
}
/// <summary>
/// Default value for port.
/// </summary>
[JsonIgnore]
public AssociativeNode DefaultValue
{
get;
set;
}
/// <summary>
/// Controls the space between successive output ports
/// </summary>
[JsonIgnore]
public Thickness MarginThickness
{
get;
private set;
}
/// <summary>
/// Based on extensionEdges port is aligned in UI.
/// </summary>
[JsonIgnore]
public SnapExtensionEdges extensionEdges { get; set; }
/// <summary>
/// The Level at which objects will be
/// extracted from a nested list. The deepest
/// level of a nested list is -1.
/// </summary>
public int Level
{
get
{
return level;
}
set
{
if (level != value)
{
level = value;
RaisePropertyChanged("Level");
}
}
}
/// <summary>
/// A flag which determines whether this Port will
/// extract data from a specific level in a nested list.
/// </summary>
public bool UseLevels
{
get
{
return useLevels;
}
set
{
if (useLevels != value)
{
useLevels = value;
if (!useLevels) KeepListStructure = useLevels;
RaisePropertyChanged("UseLevels");
}
}
}
/// <summary>
/// A flag which determines whether data from this
/// node will be re-aligned into the original structure
/// of the nested list.
/// </summary>
public bool KeepListStructure
{
get
{
return keepListStructure;
}
set
{
if (keepListStructure != value)
{
keepListStructure = value;
RaisePropertyChanged(nameof(KeepListStructure));
}
}
}
/// <summary>
/// Returns true if the port has connectors or if the
/// default value is enabled and not null. Otherwise, returns false.
/// </summary>
[JsonIgnore]
public bool IsConnected
{
get
{
return Connectors.Any() || (UsingDefaultValue && DefaultValue != null);
}
}
#endregion
internal bool IsProxyPort { get; set; } = false;
[JsonConstructor]
internal PortModel(string name, string toolTip)
{
UseLevels = false;
KeepListStructure = false;
Level = 2;
Height = 0.0;
DefaultValue = null;
LineIndex = -1;
this.toolTip = toolTip;
this.Name = name;
MarginThickness = new Thickness(0);
Height = Math.Abs(Height) < 0.001 ? Configurations.PortHeightInPixels : Height;
}
/// <summary>
/// Creates PortModel.
/// </summary>
/// <param name="portType">Type of the Port</param>
/// <param name="owner">Parent Node</param>
/// <param name="data">Information about port</param>
public PortModel(PortType portType, NodeModel owner, PortData data)
{
PortType = portType;
Owner = owner;
UseLevels = false;
KeepListStructure = false;
Level = 2;
Height = data.Height;
DefaultValue = data.DefaultValue;
UsingDefaultValue = DefaultValue != null;
LineIndex = data.LineIndex;
toolTip = data.ToolTipString;
Name = data.Name;
MarginThickness = new Thickness(0);
Height = Math.Abs(data.Height) < 0.001 ? Configurations.PortHeightInPixels : data.Height;
}
internal void RaisePortIsConnectedChanged()
{
RaisePropertyChanged(nameof(IsConnected));
}
/// <summary>
/// Deletes all connectors attached to this PortModel.
/// </summary>
internal void DestroyConnectors()
{
if (Owner == null)
return;
while (Connectors.Any())
{
ConnectorModel connector = Connectors[0];
connector.Delete();
}
}
#region Serialization/Deserialization Methods
protected override void SerializeCore(XmlElement element, SaveContext context)
{
// We are not deserializing the ports.
throw new NotImplementedException();
}
protected override void DeserializeCore(XmlElement nodeElement, SaveContext context)
{
// We are not deserializing the ports.
throw new NotImplementedException();
}
#endregion
public bool Equals(PortModel other)
{
if (other == null) return false;
if (this == other) return true;
if (GUID == other.GUID) return true;
return false;
}
public override int GetHashCode()
{
return GUID.GetHashCode();
}
/// <summary>
/// Returns the string representation of the fully qualified typename
/// where possible for the port if it's an input port. This method currently
/// returns a valid type for only Zero Touch, Builtin and NodeModel nodes,
/// and returns null otherwise. The string representation of the type also
/// contains the rank information of the type, e.g. Point[], or var[]..[].
/// </summary>
/// <returns>input port type</returns>
internal string GetInputPortType()
{
if (PortType == PortType.Output) return null;
if (Owner is DSFunction ztNode)
{
var fd = ztNode.Controller.Definition;
string type;
// In the case of a node for an instance method, the first port
// type is the declaring class type of the method itself.
if (fd.Type == FunctionType.InstanceMethod || fd.Type == FunctionType.InstanceProperty)
{
if (Index > 0)
{
var param = fd.Parameters.ElementAt(Index - 1);
type = param.Type.ToString();
}
else
{
type = fd.ClassName;
}
}
else
{
var param = fd.Parameters.ElementAt(Index);
type = param.Type.ToString();
}
return type;
}
if (Owner is CustomNodes.Function cusNode)
{
var cd = cusNode.Controller.Definition;
var param = cd.Parameters.ElementAt(Index);
string type = param.Type.ToString();
return type;
}
if (Owner is NodeModel nmNode)
{
var classType = nmNode.GetType();
var inPortAttribute = classType.GetCustomAttributes().OfType<InPortTypesAttribute>().FirstOrDefault();
try
{
return inPortAttribute?.PortTypes.ElementAt(Index);
}
catch (Exception e)
{
Log(e.Message);
}
}
return null;
}
/// <summary>
/// Returns the string representation of the fully qualified typename
/// where possible for the port if it's an output port. This method currently
/// returns a valid type for only Zero Touch, Builtin and NodeModel nodes,
/// and returns null otherwise. The string representation of the type also
/// contains the rank information of the type, e.g. Point[], or var[]..[].
/// </summary>
/// <returns>output port type</returns>
internal string GetOutPortType()
{
if (PortType == PortType.Input) return null;
if (Owner is DSFunction ztNode)
{
var fd = ztNode.Controller.Definition;
string type = fd.ReturnType.ToString();
return type;
}
if (Owner is CustomNodes.Function cusNode)
{
var cd = cusNode.Controller.Definition;
string type = cd.Returns.ElementAt(Index).Item1;
return type;
}
if (Owner is NodeModel nmNode)
{
var classType = nmNode.GetType();
var outPortAttribute = classType.GetCustomAttributes().OfType<OutPortTypesAttribute>().FirstOrDefault();
try
{
return outPortAttribute?.PortTypes.ElementAt(Index);
}
catch(Exception e)
{
Log(e.Message);
}
}
return null;
}
}
/// <summary>
/// PortData stores information for port. It's used for constructing PortModel.
/// </summary>
public class PortData
{
/// <summary>
/// Name of the port.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Tooltip of the port.
/// </summary>
public string ToolTipString { get; set; }
/// <summary>
/// Default value of the port.
/// </summary>
public AssociativeNode DefaultValue { get; set; }
/// <summary>
/// This property is used in code block nodes.
/// </summary>
public int LineIndex { get; set; }
/// <summary>
/// Height of the port.
/// </summary>
public double Height { get; set; }
/// <summary>
/// Creates PortData.
/// </summary>
/// <param name="name">name of the port</param>
/// <param name="toolTipString">Tooltip of the port</param>
public PortData(string name, string toolTipString) : this(name, toolTipString, null) { }
/// <summary>
/// Creates PortData.
/// </summary>
/// <param name="name">name of the port</param>
/// <param name="toolTipString">Tooltip of the port</param>
/// <param name="defaultValue">Default value of the port</param>
[JsonConstructor]
public PortData(string name, string toolTipString, AssociativeNode defaultValue)
{
Name = name;
ToolTipString = toolTipString;
DefaultValue = defaultValue;
LineIndex = -1;
Height = 0;
}
}
}