using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using DTS.Common.Base; using Unity; using DTS.Common.Enums.Viewer; using DTS.Common.Events; using DTS.Common.Interface; using Prism.Events; using Prism.Regions; using DTS.Common.Interactivity; using Prism.Commands; // ReSharper disable UnassignedGetOnlyAutoProperty // ReSharper disable InconsistentNaming // ReSharper disable NotAccessedField.Local // ReSharper disable UnusedAutoPropertyAccessor.Local // ReSharper disable CheckNamespace // ReSharper disable AutoPropertyCanBeMadeGetOnly.Local // ReSharper disable RedundantDefaultMemberInitializer namespace DTS.Viewer.ChartOptions { public class ChartOptionsViewModel : BaseViewModel, IChartOptionsViewModel { public IBaseViewModel Parent { 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; } public IBaseView View { get; set; } private IChartOptionsModel _model; public new IChartOptionsModel Model { get => _model; set { _model = value; OnPropertyChanged("Model"); } } /// /// Creates a new instance of the TestSummaryViewModel. /// /// The TestListView 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 ChartOptionsViewModel(IChartOptionsView 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; _eventAggregator.GetEvent().Subscribe(OnChartAxisChangedEvent); //Model = _unityContainer.Resolve(); //Model.Parent = this; //View.DataContext = Model; } public override void Initialize() { } private string chartType = string.Empty; public override void Initialize(object parameter) { Subscribe(); Model = _unityContainer.Resolve(); Model.Parent = this; View.DataContext = Model; if (parameter is IViewerMainViewModel) { Parent = (IBaseViewModel)parameter; Model.DecimateData = true; } else if (parameter is Tuple tuParam && tuParam.Item1 is IPSDReportMainViewModel) { Parent = tuParam.Item1; chartType = tuParam.Item2; Model.CanPublishChanges = false; if ("Graph" == chartType) { Model.UnitType = ChartUnitTypeEnum.PSD; Model.YRange = YRangeScaleEnum.Fixed; Model.MaxFixedY = 1; Model.MinFixedY = 1e-12; //log scale, can't be 0 Model.DecimateData = false; } else { Model.DecimateData = true; Model.Filter = FilterOptionEnum.Unfiltered; } Model.CanPublishChanges = true; } } private void Subscribe() { _eventAggregator.GetEvent().Subscribe(OnCursorsAlailableChanged); _eventAggregator.GetEvent().Subscribe(OnGraphSelectedChannelsChanged); } private string Directory { get; set; } private void OnGraphSelectedChannelsChanged(GraphSelectedChannelsNotificationArg arg) { if (Parent != arg?.ParentVM) return; var channels = arg.SelectedChannels; Model.Parent = this; Directory = channels.Count > 0 ? channels[0].BinaryFilePath : string.Empty; ChartOptionsVisability = (channels.Count > 0); //whenever channels change, check the status of mV/ADC buttons //18256 Implement StackChannelScaleFactorsEUPerADC Arm/Event Command if (AllChannelsSupportADC(channels)) { Model.SupportsADC = true; } else { Model.SupportsADC = false; if (Model.UnitType == ChartUnitTypeEnum.ADC) { Model.UnitType = ChartUnitTypeEnum.EU; } } if (AllChannelsSupportmV(channels)) { Model.SupportsMV = true; } else { Model.SupportsMV = false; if (Model.UnitType == ChartUnitTypeEnum.mV) { Model.UnitType = ChartUnitTypeEnum.EU; } } PublishChanges(); } /// /// returns true if all channels support ADC /// for now this is determined by looking at the sensor serial number, but we /// can be more sophisticated in the future, as there are other channels that won't support ADC meaningfully as well /// (like calculated channels) /// private bool AllChannelsSupportADC(List channels) { return !channels.Exists(x => Common.Enums.Hardware.HardwareConstants.IsTSRAIRSerialNumber(x.ParentModule.BaseSerialNumber)); } /// /// returns true if all channels support mV /// private bool AllChannelsSupportmV(List channels) { return !channels.Exists(x => Common.Enums.Hardware.HardwareConstants.IsTSRAIRSerialNumber(x.ParentModule.BaseSerialNumber)); } private void OnCursorsAlailableChanged(bool value) { Model.IsCursorsAvailable = value; } private void OnChartAxisChangedEvent(ChartAxisChangedEventArg args) { if (args?.ParentVM != Parent) return; Model.CanPublishChanges = false; switch (args.Axis) { case "Y": Model.MinFixedY = args.MinValue; Model.MaxFixedY = args.MaxValue; break; case "X": Model.MinFixedT = args.MinValue; Model.MaxFixedT = args.MaxValue; break; } Model.CanPublishChanges = true; } #region Properties public object ContextSearchRegion { get; set; } public new bool IsMenuIncluded { get; set; } public new bool IsNavigationIncluded { get; set; } public new bool IsBusy { get; set; } public new bool IsDirty { get; } private bool _chartOptionsVisability = false; public bool ChartOptionsVisability { get => _chartOptionsVisability; set { _chartOptionsVisability = value; OnPropertyChanged("ChartOptionsVisability"); } } public new event PropertyChangedEventHandler PropertyChanged; public new void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion Properties #region Methods public void ShowMinMaxCursor(bool value) { _eventAggregator.GetEvent().Publish(value); } public void ResetZoomMethod() { Model.YRange = YRangeScaleEnum.AutoRange; _eventAggregator.GetEvent().Publish(true); } public void ResetTMethod() { _eventAggregator.GetEvent().Publish(false); } public void SaveToPDFMethod() { _eventAggregator.GetEvent().Publish(Directory); } public void PublishChanges() { _eventAggregator.GetEvent().Publish(new ChartOptionsChangedEventArg() { ParentVM = Parent, Model = Model, ChartType = chartType }); } public void ShowCusor(bool value) { _eventAggregator.GetEvent().Publish(value); } #endregion Methods #region Commands private DelegateCommand _clearMarkersCommand; public DelegateCommand ClearMarkersCommand => _clearMarkersCommand ?? (_clearMarkersCommand = new DelegateCommand(ClearMarkersMethod)); private void ClearMarkersMethod() { _eventAggregator.GetEvent().Publish(true); } #endregion } }