Files
DP44/DTS Viewer/DTS.Viewer.Modules/DTS.Viewer.Graph/ViewModel/GraphViewModel.cs
2026-04-17 14:55:32 -04:00

267 lines
12 KiB
C#

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using C1.WPF.C1Chart;
using DTS.Common.Base;
using DTS.Common.Events;
using DTS.Common.Interactivity;
using DTS.Common.Interface;
using Prism.Events;
using Prism.Regions;
using Unity;
// ReSharper disable InconsistentNaming
// ReSharper disable NotAccessedField.Local
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedAutoPropertyAccessor.Local
// ReSharper disable UnusedMember.Local
// ReSharper disable RedundantDefaultMemberInitializer
namespace DTS.Viewer.Graph
{
public class GraphViewModel : BaseViewModel<IGraphViewModel>, IGraphViewModel
{
public IGraphView View { get; private set; }
internal IBaseViewModel Parent { get; set; }
private new IBaseModel Model { get; set; }
public ITestDataSeriesView DataSeriesView { get; set; }
private IEventAggregator _eventAggregator { get; set; }
private IUnityContainer _unityContainer { get; set; }
public InteractionRequest<Notification> NotificationRequest { get; private set; }
public new InteractionRequest<Confirmation> ConfirmationRequest { get; private set; }
/// <summary>
/// Creates a new instance of the GraphViewModel.
/// </summary>
/// <param name="view">The GraphView interface.</param>
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
/// <param name="eventAggregator">The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other.</param>
/// <param name="unityContainer">The unityContainer.</param>
public GraphViewModel(IGraphView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)
: base(regionManager, eventAggregator, unityContainer)
{
View = view;
View.DataContext = this;
NotificationRequest = new InteractionRequest<Notification>();
ConfirmationRequest = new InteractionRequest<Confirmation>();
_eventAggregator = eventAggregator;
_unityContainer = unityContainer;
}
#region Methods
private void Subscribe()
{
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
_eventAggregator.GetEvent<GraphSelectedChannelCountNotification>().Subscribe(OnGraphSelectedCountChanged);
_eventAggregator.GetEvent<TestSummaryCountNotification>().Subscribe(OnTestSelectedCountChanged);
_eventAggregator.GetEvent<TestModificationChangedEvent>().Subscribe(OnTestModificationChanged);
_eventAggregator.GetEvent<GraphChannelsReadCompletedNotification>().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread);
_eventAggregator.GetEvent<GraphChannelReadCalcProgressChangedEvent>().Subscribe(OnGraphChannelReadCalcProgressChangedEvent, ThreadOption.UIThread);
}
private void SubscribeDataSelect()
{
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
_eventAggregator.GetEvent<GraphSelectedChannelCountNotification>().Subscribe(OnGraphSelectedCountChanged);
_eventAggregator.GetEvent<TestSummaryCountNotification>().Subscribe(OnTestSelectedCountChanged);
_eventAggregator.GetEvent<GraphChannelsReadCompletedNotification>().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread);
_eventAggregator.GetEvent<GraphChannelReadCalcProgressChangedEvent>().Subscribe(OnGraphChannelReadCalcProgressChangedEvent, ThreadOption.UIThread);
}
private void SubscribeResult()
{
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
_eventAggregator.GetEvent<GraphSelectedChannelCountNotification>().Subscribe(OnGraphSelectedCountChanged);
_eventAggregator.GetEvent<TestSummaryCountNotification>().Subscribe(OnTestSelectedCountChanged);
_eventAggregator.GetEvent<GraphChannelsReadCompletedNotification>().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread);
_eventAggregator.GetEvent<GraphChannelReadCalcProgressChangedEvent>().Subscribe(OnGraphChannelReadCalcProgressChangedEvent, ThreadOption.UIThread);
}
private void OnTestModificationChanged(ITestModificationModel obj)
{
}
public override void Initialize()
{
}
public override void Initialize(object parameter)
{
if (parameter is IViewerMainViewModel)
{
Subscribe();
Parent = (IBaseViewModel)parameter;
DataSeriesView = GetTestDataSeriesView(this);
}
else if (parameter is Tuple<IBaseViewModel, string> tuParam && tuParam.Item1 is IPSDReportMainViewModel)
{
switch (tuParam.Item2)
{
case "DataSelect":
SubscribeDataSelect();
break;
default:
SubscribeResult();
break;
}
Parent = tuParam.Item1;
DataSeriesView = GetTestDataSeriesView(this, "Graph" == tuParam.Item2, tuParam.Item2);
switch (tuParam.Item2)
{
case "DataSelect":
//kill scrolling, zoom in/out to data onscreen
((AxisScrollBar)((TestDataSeriesView)DataSeriesView).MainChart.View.AxisX.ScrollBar).Visibility = System.Windows.Visibility.Collapsed;
((AxisScrollBar)((TestDataSeriesView)DataSeriesView).MainChart.View.AxisY.ScrollBar).Visibility = System.Windows.Visibility.Collapsed;
break;
default:
//kills mouse selecting too
((TestDataSeriesView)DataSeriesView).MainChart.Actions.Clear();
((AxisScrollBar)((TestDataSeriesView)DataSeriesView).MainChart.View.AxisX.ScrollBar).Visibility = System.Windows.Visibility.Collapsed;
((AxisScrollBar)((TestDataSeriesView)DataSeriesView).MainChart.View.AxisY.ScrollBar).Visibility = System.Windows.Visibility.Collapsed;
break;
}
}
}
private ITestDataSeriesView GetTestDataSeriesView(IBaseViewModel parent, bool initPSD = false, string chartType = "")
{
var view = _unityContainer.Resolve<ITestDataSeriesView>();
var viewModel = _unityContainer.Resolve<ITestDataSeriesViewModel>();
view.DataContext = viewModel;
viewModel.Initialize(parent, chartType);
if (initPSD)
{
((TestDataSeriesViewModel)viewModel).SubscribePSD();
}
return view;
}
#region Change Events
private const string MessageSelection = "Please select {0}(s) to view data";
private void OnTestSelectedCountChanged(TestSummaryCountNotificationArg arg)
{
if (Parent != arg.ParentVM) return;
const string test = "Test";
const string graph = "Graph";
MessageText = string.Format(MessageSelection, arg.SummaryCount == 0 ? test : graph);
}
/// <summary>
/// Private Event handler for RaiseNotification event.
/// </summary>
private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle)
{
// Notification object expects a NotificationContentEventArgsWithoutTitle object and a Title string.
var eventArgsWithoutTitle = new NotificationContentEventArgs(eventArgsWithTitle.Message, eventArgsWithTitle.MessageDetails, eventArgsWithTitle.Image);
NotificationRequest.Raise(new Notification
{
Content = eventArgsWithoutTitle,
Title = eventArgsWithTitle.Title
});
}
private void OnGraphSelectedCountChanged(GraphSelectedChannelCountNotificationArg arg)
{
if (Parent != arg?.ParentVM) return;
MessageVisibility = !(arg.SelectedChannelCount > 0);
GraphInfoVisibility = !MessageVisibility;
}
private void OnGraphChannelsReadCompleted(GraphChannelsReadCompletedNotificationArgs arg)
{
if (null == arg) { return; }
if (this != arg.GraphVM) return;
if (!arg.IsReadCompleted)
{
ProgressText = Resources.StringResources.ReadingChannelData;
ProgressPercent = 0;
}
ProgressVisibility = !arg.IsReadCompleted;
}
private void OnGraphChannelReadCalcProgressChangedEvent(GraphChannelReadCalcProgressChangedEventArgs arg)
{
if (this != arg.GraphVM) return;
if (!string.IsNullOrEmpty(arg.ProgressMessage)) ProgressText = arg.ProgressMessage;
if (arg.ProgressPercent >= 0) ProgressPercent = arg.ProgressPercent;
}
#endregion Change Events
#endregion
#region ContextRegion
public object ContextGraphRegion
{
get => ((GraphView)View).GraphRegion.Content;
set { ((GraphView)View).GraphRegion.Content = value; OnPropertyChanged("ContextGraphRegion"); }
}
#endregion
#region Properties
private string _messageText = "Please select Event(s) to export data";
public string MessageText { get => _messageText; set { _messageText = value; OnPropertyChanged("MessageText"); } }
private bool _messageVisibility = true;
public bool MessageVisibility { get => _messageVisibility; set { _messageVisibility = value; FireVisibilities(); } }
private double _progressPercent = 0D;
public double ProgressPercent { get => _progressPercent; set { _progressPercent = value; OnPropertyChanged("ProgressPercent"); } }
private string _progressText = "Reading channel data...";
public string ProgressText { get => _progressText; set { _progressText = value; OnPropertyChanged("ProgressText"); } }
private bool _progressVisibility = false;
public bool ProgressVisibility { get => _progressVisibility; set { _progressVisibility = value; FireVisibilities(); } }
private bool _graphInfoVisibility = false;
public bool GraphInfoVisibility { get => _graphInfoVisibility; set { _graphInfoVisibility = value; FireVisibilities(); } }
public bool GraphVisibility => !MessageVisibility;
///<summary>
///Occurs when a property value changes.
///</summary>
public new event PropertyChangedEventHandler PropertyChanged;
private new void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void FireVisibilities()
{
OnPropertyChanged("MessageVisibility");
OnPropertyChanged("ProgressVisibility");
OnPropertyChanged("GraphInfoVisibility");
OnPropertyChanged("GraphVisibility");
}
/// <summary>
/// Gets the HeaderInfo.
/// </summary>
public string HeaderInfo => "GraphRegion";
private bool _isBusy = false;
public new bool IsBusy
{
get => _isBusy;
set { _isBusy = value; OnPropertyChanged("IsBusy"); }
}
public new bool IsDirty { get; } = false;
#endregion
}
}