-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWaveFormViewModel.cs
More file actions
336 lines (276 loc) · 9.02 KB
/
Copy pathWaveFormViewModel.cs
File metadata and controls
336 lines (276 loc) · 9.02 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
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
using System.Collections.ObjectModel;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using DynamicData.Binding;
using OneWare.Essentials.Helpers;
using OneWare.Vcd.Parser.Data;
using OneWare.WaveFormViewer.Models;
namespace OneWare.WaveFormViewer.ViewModels;
public class WaveFormViewModel : ObservableObject
{
private static readonly IBrush[] WaveColors =
[Brushes.Lime, Brushes.Cyan, Brushes.Magenta, Brushes.Yellow];
private long _cursorOffset;
private bool _extendSignals;
private long _loadingMarkerOffset = long.MaxValue;
private long _markerOffset = long.MaxValue;
private long _max;
private long _offset;
private long _secondMarkerOffset = long.MaxValue;
private WaveModel? _selectedSignal;
private long _timeScale = 1;
private int _zoomLevel;
private double _zoomMultiply = 1;
public int MinZoomLevel { get; } = -2;
public int MaxZoomLevel { get; private set; } = 1;
public ObservableCollection<WaveModel> Signals { get; } = [];
public WaveModel? SelectedSignal
{
get => _selectedSignal;
set => SetProperty(ref _selectedSignal, value);
}
/// <summary>
/// 1 = 1 fs
/// </summary>
public long TimeScale
{
get => _timeScale;
set
{
SetProperty(ref _timeScale, value);
TimeScaleUnit = TimeHelper.GetTimeScaleFromUnit(TimeScale);
}
}
public string TimeScaleUnit { get; private set; } = string.Empty;
public bool ExtendSignals
{
get => _extendSignals;
set => SetProperty(ref _extendSignals, value);
}
public long Offset
{
get => _offset;
set => SetProperty(ref _offset, value > 0 ? value : 0);
}
public long ViewPortWidth => (long)(Max / ZoomMultiply);
public long Max
{
get => _max;
set
{
SetProperty(ref _max, value);
OnPropertyChanged(nameof(ViewPortWidth));
OnPropertyChanged(nameof(MaxScroll));
MaxZoomLevel = Max > 0 ? (int)Math.Log2(Max) : 0;
}
}
public long MaxScroll => Max - ViewPortWidth;
public int ZoomLevel
{
get => _zoomLevel;
set
{
if (value < MinZoomLevel) value = MinZoomLevel;
if (value > MaxZoomLevel) value = MaxZoomLevel;
SetProperty(ref _zoomLevel, value);
ZoomMultiply = Math.Pow(2, value);
if (ZoomMultiply >= 1)
{
var currentOffsetWidth = Max / (long)ZoomMultiply;
if (MarkerOffset != long.MaxValue)
Offset = MarkerOffset - currentOffsetWidth / 2;
}
}
}
public double ZoomMultiply
{
get => _zoomMultiply;
private set
{
SetProperty(ref _zoomMultiply, value);
OnPropertyChanged(nameof(ViewPortWidth));
OnPropertyChanged(nameof(MaxScroll));
}
}
public long CursorOffset
{
get => _cursorOffset;
set
{
SetProperty(ref _cursorOffset, value >= 0 ? value : 0);
OnPropertyChanged(nameof(CursorText));
}
}
public long MarkerOffset
{
get => _markerOffset;
set
{
SetProperty(ref _markerOffset, value >= 0 ? value : 0);
OnPropertyChanged(nameof(MarkerText));
OnPropertyChanged(nameof(MarkerTextOriginal));
SetSignalMarkerValues(MarkerOffset);
}
}
public long SecondMarkerOffset
{
get => _secondMarkerOffset;
set
{
SetProperty(ref _secondMarkerOffset, value >= 0 ? value : 0);
OnPropertyChanged(nameof(MarkerText));
OnPropertyChanged(nameof(MarkerTextOriginal));
SetSignalMarkerValues(SecondMarkerOffset);
}
}
public long LoadingMarkerOffset
{
get => _loadingMarkerOffset;
set
{
SetProperty(ref _loadingMarkerOffset, value >= 0 ? value : 0);
if (MarkerOffset == long.MaxValue) SetSignalMarkerValues(_loadingMarkerOffset);
}
}
public string MarkerTextOriginal
{
get
{
if (LoadingMarkerOffset != long.MaxValue)
{
if (MarkerOffset != long.MaxValue) return $"{MarkerOffset} {TimeScaleUnit}";
return "?";
}
if (SecondMarkerOffset == long.MaxValue)
{
if (MarkerOffset != long.MaxValue) return $"{MarkerOffset} {TimeScaleUnit}";
return "?";
}
if (MarkerOffset == long.MaxValue) return "?";
var dist = SecondMarkerOffset - MarkerOffset;
return (dist > 0 ? "+" : "") + $"{dist} {TimeScaleUnit}";
}
}
public string MarkerText
{
get
{
if (MarkerOffset != long.MaxValue &&
(LoadingMarkerOffset != long.MaxValue || SecondMarkerOffset == long.MaxValue))
return TimeHelper.FormatTime(MarkerOffset, TimeScale, ViewPortWidth);
if (MarkerOffset == long.MaxValue) return "?";
var dist = SecondMarkerOffset - MarkerOffset;
return (dist > 0 ? "+" : "") + TimeHelper.FormatTime(dist, TimeScale, ViewPortWidth);
}
}
public string CursorText => TimeHelper.FormatTime(CursorOffset, TimeScale, ViewPortWidth);
private void SetSignalMarkerValues(long offset)
{
foreach (var s in Signals) SetSignalMarkerValue(s, offset);
}
private void SetSignalMarkerValue(WaveModel model, long offset)
{
model.MarkerValue = SignalConverter.ConvertSignal(model.Signal.GetValueFromOffset(offset) ?? StdLogic.U, model);
}
public void ZoomIn()
{
ZoomLevel++;
}
public void ZoomOut()
{
ZoomLevel--;
}
public void ResetView()
{
ZoomLevel = 0;
Offset = 0;
}
public void XOffsetPlus()
{
var plus = (long)(Max / ZoomMultiply / 10);
Offset += plus >= 1 ? plus : 1;
}
public void XOffsetMinus()
{
var minus = (long)(Max / ZoomMultiply / 10);
Offset -= minus >= 1 ? minus : 1;
}
/// <summary>
/// Clears both markers so that no delta is displayed.
/// </summary>
public void ClearMarkers()
{
MarkerOffset = long.MaxValue;
SecondMarkerOffset = long.MaxValue;
}
/// <summary>
/// Moves the primary marker to the next change-time of the selected signal
/// that is strictly greater than the current marker position. If no signal
/// is selected, or there is no further edge, nothing happens.
/// </summary>
public void JumpToNextEdge()
{
JumpToEdge(1);
}
/// <summary>
/// Moves the primary marker to the previous change-time of the selected
/// signal that is strictly less than the current marker position. If no
/// signal is selected, or there is no previous edge, nothing happens.
/// </summary>
public void JumpToPreviousEdge()
{
JumpToEdge(-1);
}
private void JumpToEdge(int direction)
{
var selected = SelectedSignal;
if (selected is null) return;
var signal = selected.Signal;
var current = MarkerOffset == long.MaxValue ? 0 : MarkerOffset;
// FindIndex returns i such that changeTime[i] <= current < changeTime[i+1],
// or -1 if current is before the first change-time.
var index = signal.FindIndex(current);
long target;
if (direction > 0)
{
var nextIndex = index < 0 ? 0 : index + 1;
target = signal.GetChangeTimeFromIndex(nextIndex);
if (target == long.MaxValue || target <= current) return;
}
else
{
if (index < 0) return;
var changeAtIndex = signal.GetChangeTimeFromIndex(index);
var prevIndex = changeAtIndex < current ? index : index - 1;
if (prevIndex < 0) return;
target = signal.GetChangeTimeFromIndex(prevIndex);
if (target == long.MaxValue || target >= current) return;
}
MarkerOffset = target;
EnsureMarkerVisible(target);
}
private void EnsureMarkerVisible(long position)
{
if (ViewPortWidth <= 0) return;
if (position < Offset)
{
Offset = Math.Max(0, position - ViewPortWidth / 4);
}
else if (position > Offset + ViewPortWidth)
{
Offset = Math.Max(0, position - (long)(ViewPortWidth * 0.75));
}
}
public WaveModel AddSignal(IVcdSignal signal)
{
var waveModel = new WaveModel(signal, WaveColors[Signals.Count % WaveColors.Length]);
SetSignalMarkerValue(waveModel, MarkerOffset);
Signals.Add(waveModel);
waveModel.WhenValueChanged(x => x.DataType).Subscribe(_ => { SetSignalMarkerValue(waveModel, MarkerOffset); });
return waveModel;
}
public void RemoveSignal(WaveModel model)
{
Signals.Remove(model);
}
}