forked from StockSharp/StockSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagramElementParam.cs
More file actions
271 lines (226 loc) · 7.07 KB
/
DiagramElementParam.cs
File metadata and controls
271 lines (226 loc) · 7.07 KB
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
namespace StockSharp.Diagram;
/// <summary>
/// The diagram element parameter.
/// </summary>
/// <typeparam name="T">Value type.</typeparam>
public class DiagramElementParam<T> : NotifiableObject, IDiagramElementParam
{
/// <summary>
/// The parameter value change start event.
/// </summary>
public event Action<T, T> ValueChanging;
/// <summary>
/// The parameter value change event.
/// </summary>
public event Action<T> ValueChanged;
private Func<T, T, bool> _valueValidating = (o, n) => !EqualityComparer<T>.Default.Equals(o, n);
/// <summary>
/// Validate the parameter value.
/// </summary>
public Func<T, T, bool> ValueValidating
{
get => _valueValidating;
set => _valueValidating = value ?? throw new ArgumentNullException(nameof(value));
}
private string _name;
/// <inheritdoc />
public string Name
{
get => _name;
set
{
_name = value;
NotifyChanged();
}
}
/// <inheritdoc />
public Type Type => typeof(T);
/// <summary>
/// Can change value.
/// </summary>
public bool CanChangeValue { get; set; } = true;
private readonly List<Attribute> _attributes = [];
/// <inheritdoc />
public IList<Attribute> Attributes => _attributes;
private T _value;
private bool _hasValue;
/// <summary>
/// The parameter value.
/// </summary>
public virtual T Value
{
get => _value;
set
{
if (!CanChangeValue)
return;
if (!ValueValidating(_value, value))
return;
if (NotifyOnChanged)
NotifyChanging();
ValueChanging?.Invoke(_value, value);
IgnoreOnSave = false;
_value = value;
_hasValue = true;
ValueChanged?.Invoke(_value);
if (NotifyOnChanged)
NotifyChanged();
}
}
/// <inheritdoc />
public bool IsDefault => !_hasValue;
/// <inheritdoc />
public bool CanOptimize { get; set; }
/// <inheritdoc />
public bool IgnoreOnSave { get; set; }
/// <inheritdoc />
public void SetValueWithIgnoreOnSave(object value)
{
IgnoreOnSave = true;
Value = (T)value;
IgnoreOnSave = true;
}
/// <inheritdoc />
public bool NotifyOnChanged { get; set; } = true;
object IDiagramElementParam.Value
{
get => Value;
set => Value = (T)value;
}
/// <summary>
/// The parameter value saving handler.
/// </summary>
public Func<T, SettingsStorage> SaveHandler { get; set; }
/// <summary>
/// The parameter value loading handler.
/// </summary>
public Func<SettingsStorage, T> LoadHandler { get; set; }
/// <summary>
/// To set the <see cref="ExpandableObjectConverter"/> attribute for the diagram element parameter.
/// </summary>
/// <param name="expandable">Value.</param>
/// <returns>The diagram element parameter.</returns>
public DiagramElementParam<T> SetExpandable(bool expandable)
=> this.SetAttribute(expandable, () => new TypeConverterAttribute(typeof(ExpandableObjectConverter)));
/// <summary>
/// To add the attribute <see cref="Attribute"/> for the diagram element parameter.
/// </summary>
/// <typeparam name="TEditor">Editor type.</typeparam>
/// <param name="editor">Attribute.</param>
/// <returns>The diagram element parameter.</returns>
public DiagramElementParam<T> SetEditor<TEditor>(TEditor editor)
where TEditor : Attribute
{
if (editor == null)
throw new ArgumentNullException(nameof(editor));
return this.SetAttribute(true, () => editor);
}
/// <summary>
/// To set the <see cref="DisplayAttribute"/> attribute for the diagram element parameter.
/// </summary>
/// <param name="groupName">The category of the diagram element parameter.</param>
/// <param name="displayName">The display name.</param>
/// <param name="description">The description of the diagram element parameter.</param>
/// <param name="order">The property order.</param>
/// <returns>The diagram element parameter.</returns>
public DiagramElementParam<T> SetDisplay(string groupName, string displayName, string description, int order)
=> this.SetAttribute(true, () => new DisplayAttribute
{
Name = displayName,
Description = description,
GroupName = groupName,
Order = order,
});
/// <summary>
/// To set the <see cref="ReadOnlyAttribute"/> attribute for the diagram element parameter.
/// </summary>
/// <param name="readOnly">Read-only.</param>
/// <returns>The diagram element parameter.</returns>
public DiagramElementParam<T> SetReadOnly(bool readOnly = true)
=> this.SetAttribute(readOnly, () => new ReadOnlyAttribute(true));
/// <summary>
/// To set the <see cref="BasicSettingAttribute"/> attribute for the diagram element parameter.
/// </summary>
/// <param name="isBasic">Is basic parameter.</param>
/// <returns>The diagram element parameter.</returns>
public DiagramElementParam<T> SetBasic(bool isBasic = true)
=> this.SetAttribute(isBasic, () => new BasicSettingAttribute());
/// <summary>
/// To set the <see cref="BrowsableAttribute"/> attribute for the diagram element parameter.
/// </summary>
/// <param name="nonBrowsable">Hidden parameter.</param>
/// <returns>The diagram element parameter.</returns>
public DiagramElementParam<T> SetNonBrowsable(bool nonBrowsable = true)
=> this.SetAttribute(nonBrowsable, () => new BrowsableAttribute(false));
/// <summary>
/// To modify <see cref="IDiagramElementParam.CanOptimize"/>.
/// </summary>
/// <param name="value">Value.</param>
/// <returns>The diagram element parameter.</returns>
public DiagramElementParam<T> SetCanOptimize(bool value = true)
{
CanOptimize = value;
return this;
}
/// <summary>
/// Load settings.
/// </summary>
/// <param name="storage">Settings storage.</param>
public void Load(SettingsStorage storage)
{
if (LoadHandler != null)
{
storage.SafeGetValue<SettingsStorage>(nameof(Value), s => Value = s == null ? default : LoadHandler(s), true);
}
else if (typeof(T).IsPersistable())
{
storage.SafeGetValue<SettingsStorage>(nameof(Value), s =>
{
if (s == null)
Value = default;
else
{
// 2025-03-27 remove few years later
var type = s.GetValue<string>("type");
if (type == "StockSharp.Algo.Candles.CandleSeries, StockSharp.Algo")
{
type = "StockSharp.Algo.Candles.CandleSeries, StockSharp.BusinessEntities";
var copy = new SettingsStorage();
copy.AddRange(s);
copy.SetValue("type", type);
s = copy;
}
var v = s.LoadEntire<IPersistable>();
#pragma warning disable CS0618 // Type or member is obsolete
if (v is CandleSeries cs && typeof(T) == typeof(DataType))
v = DataType.Create(typeof(TimeFrameCandleMessage), cs.Arg);
#pragma warning restore CS0618 // Type or member is obsolete
Value = (T)v;
}
}, true);
}
else
Value = storage.GetValue<T>(nameof(Value));
}
/// <summary>
/// Save settings.
/// </summary>
/// <param name="storage">Settings storage.</param>
public void Save(SettingsStorage storage)
{
if (SaveHandler != null)
{
storage.SetValue(nameof(Value), SaveHandler(Value));
}
else if (Value is IPersistable pers)
{
if (Value.IsNull())
return;
storage.SetValue(nameof(Value), pers.SaveEntire(false));
}
else
storage.SetValue(nameof(Value), Value);
}
/// <inheritdoc />
public override string ToString() => $"{Name}: {Value}";
}