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 { 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 NotificationRequest { get; private set; } public new InteractionRequest ConfirmationRequest { get; private set; } /// /// Creates a new instance of the GraphViewModel. /// /// The GraphView interface. /// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed. /// The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other. /// The unityContainer. public GraphViewModel(IGraphView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) : base(regionManager, eventAggregator, unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; _unityContainer = unityContainer; } #region Methods private void Subscribe() { _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent().Subscribe(OnGraphSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnTestSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnTestModificationChanged); _eventAggregator.GetEvent().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread); _eventAggregator.GetEvent().Subscribe(OnGraphChannelReadCalcProgressChangedEvent, ThreadOption.UIThread); } private void SubscribeDataSelect() { _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent().Subscribe(OnGraphSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnTestSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread); _eventAggregator.GetEvent().Subscribe(OnGraphChannelReadCalcProgressChangedEvent, ThreadOption.UIThread); } private void SubscribeResult() { _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent().Subscribe(OnGraphSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnTestSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread); _eventAggregator.GetEvent().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 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(); var viewModel = _unityContainer.Resolve(); 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); } /// /// Private Event handler for RaiseNotification event. /// 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; /// ///Occurs when a property value changes. /// 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"); } /// /// Gets the HeaderInfo. /// 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 } }