Files
DP44/DTS Viewer/DTS.Viewer/.svn/pristine/87/87397076eb1781af23d15103ed3ea9aa62a219d7.svn-base
2026-04-17 14:55:32 -04:00

527 lines
23 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Input;
using DTS.Common.Base;
using DTS.Common.Classes.Viewer.Commands;
using DTS.Common.Enums;
using DTS.Common.Enums.Sensors;
using DTS.Common.Events;
using DTS.Common.Interface;
using DTS.Common.Utils;
using DTS.Viewer.Resources;
using Xceed.Wpf.AvalonDock.Layout.Serialization;
using System.Globalization;
using DTS.Common.Interactivity;
using Prism.Regions;
using Prism.Events;
using Unity;
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable NotAccessedField.Local
// ReSharper disable UnusedMember.Local
// ReSharper disable RedundantDefaultMemberInitializer
namespace DTS.Viewer
{
public class ExportMainViewModel : BaseViewModel<IExportMainViewModel>, IExportMainViewModel
{
public IBaseView View { get; set; }
public bool Standalone { get; set; }
public string SelectedTest { get; set; }
public string SelectedDTSFile { get; set; }
private List<ITestEvent> _selectedEventList = new List<ITestEvent>();
public List<ITestEvent> SelectedEventList
{
get => _selectedEventList;
set { _selectedEventList = value; OnPropertyChanged("SelectedEventList"); }
}
public List<string> AvailableTestIds { get; set; }
private IBaseWindowModel Parent { 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 ExportMainViewModel.
/// </summary>
/// <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 Unity container.</param>
public ExportMainViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)
: base(regionManager, eventAggregator, unityContainer)
{
NotificationRequest = new InteractionRequest<Notification>();
ConfirmationRequest = new InteractionRequest<Confirmation>();
_eventAggregator = eventAggregator;
_unityContainer = unityContainer;
View = _unityContainer.Resolve<IExportMainViewGrid>();
View.DataContext = this;
View.DataContext = this;
}
#region Methods
public void AddSelectedEvents(string groupName, List<ITestEvent> events)
{
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>().Publish(true);
foreach (var e in events)
{
SelectedEventList.Add(e);
}
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>().Publish(false);
}
/// <summary>
/// sets SelectedDataFile and marks it as selected in viewer/model
/// 16158 Browse button on View Data tab not functiona
/// </summary>
public void SelectAndIncludeDataFile(string value)
{
if (string.IsNullOrEmpty(value)) return;
_selectedDataFile = value;
_eventAggregator.GetEvent<DataFolderChangedEvent>().Publish(new DataFolderSelectionArg
{ Path = string.Empty, File = _selectedDataFile, SetSelected = true, SelectAll = true, ParentVM = this });
OnPropertyChanged("SelectedDataFile");
}
private void OnDataFileSelected(DataFileSelectionArg arg)
{
if (this != arg.ParentVM) return;
SelectAndIncludeDataFile(arg?.File);
}
private void Subscribe()
{
_eventAggregator.GetEvent<LoadExportModuleEvent>().Subscribe(OnLoadExportModule, ThreadOption.PublisherThread, true);
_eventAggregator.GetEvent<TestLoadedCountNotification>().Subscribe(OnTestLoadedChanged);
_eventAggregator.GetEvent<TestSummaryCountNotification>().Subscribe(OnTestSelectedCountChanged);
_eventAggregator.GetEvent<GraphLoadedCountNotification>().Subscribe(OnGraphLoadedCountChanged);
_eventAggregator.GetEvent<ShiftT0Event>().Subscribe(OnShiftT0Event);
_eventAggregator.GetEvent<GraphSelectedEventCountNotification>().Subscribe(OnGraphSelectedCountChanged);
_eventAggregator.GetEvent<GraphChannelsReadCompletedNotification>().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread);
_eventAggregator.GetEvent<ViewerSettingsVisibilityChangedEvent>().Subscribe(OnViewerSettingsVisibilityChanged);
_eventAggregator.GetEvent<DataFileSelectedEvent>().Subscribe(OnDataFileSelected);
_eventAggregator.GetEvent<SaveToPDFRequestedEvent>().Subscribe(OnSaveToPDFRequested);
}
private void OnViewerSettingsVisibilityChanged(Visibility settingsVisibility)
{
SettingsVisibility = settingsVisibility;
}
private Visibility _settingsVisibility = Visibility.Visible;
public Visibility SettingsVisibility
{
get => _settingsVisibility;
private set
{
_settingsVisibility = value;
OnPropertyChanged("SettingsVisibility");
}
}
/// <summary>
/// Initialize without parameter
/// </summary>
public override void Initialize()
{
Subscribe();
}
/// <summary>
/// Initialize with parameter - Parent
/// </summary>
public override void Initialize(object parameter)
{
Parent = (IBaseWindowModel)parameter;
Parent.IsMenuIncluded = _isMenuIncluded;
Parent.IsNavigationIncluded = _isNavigationIncluded;
Subscribe();
View.DataContext = this;
}
public override void Activated()
{
}
private void OnGraphLoadedCountChanged(GraphLoadedCountNotificationArg arg)
{
if (this != arg?.ParentVM) return;
TotalLoadedGraphs = arg.LoadedCount;
}
private void OnGraphSelectedCountChanged(GraphSelectedEventCountNotificationArg arg)
{
if (this != arg?.ParentVM) return;
TotalSelectedGraphs = arg.SelectedEventCount;
}
private void OnGraphChannelsReadCompleted(GraphChannelsReadCompletedNotificationArgs arg)
{
if (null == arg) { return; }
if ((IGraphView)ContextGraphRegion == null) { return; }
if (((IGraphView)ContextGraphRegion).DataContext != arg?.GraphVM) return;
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>().Publish(!arg.IsReadCompleted);
}
private void OnTestLoadedChanged(TestLoadedCountNotificationArg arg)
{
if (this != arg?.ParentVM) return;
TotalLoadedTests = arg.LoadedCount;
}
private void OnTestSelectedCountChanged(TestSummaryCountNotificationArg arg)
{
if (this != arg?.ParentVM) return;
TotalSelectedTests = arg.SummaryCount;
}
private void OnLoadExportModule(LoadExportModuleArg arg)
{
IsBusy = true;
IsBusyMessage = "Loading export list";
if (null == ContextGraphRegion) ContextGraphRegion = GetGraphView(this);
if (null == ContextTestsRegion) ContextTestsRegion = GetTestDefinitionView(this);
if (null == ContextGraphListRegion) ContextGraphListRegion = GetGraphListView(this);
IsBusy = false;
IsBusyMessage = string.Empty;
}
public void ZoomReset()
{
_eventAggregator.GetEvent<ResetZoomChangedEvent>().Publish(true);
}
public void LeftKeyPress()
{
//inform any other modules and models of a T0 shift
_eventAggregator.GetEvent<ShiftT0Event>().Publish(new ShiftT0EventArguments(-1, false, true));
}
public void RightKeyPress()
{
//inform any other modules and models of a T0 shift
_eventAggregator.GetEvent<ShiftT0Event>().Publish(new ShiftT0EventArguments(1, false, true));
}
private void OnShiftT0Event(ShiftT0EventArguments args)
{
if (args.IsInitialization) { return; }
if (args.T0Time != 0) { ShowModifications = true; }
if (args.T0Steps != 0) { ShowModifications = true; }
}
#endregion
#region Views
private IGraphView GetGraphView(IBaseViewModel parent)
{
var view = _unityContainer.Resolve<IGraphView>();
var viewModel = _unityContainer.Resolve<IGraphViewModel>();
view.DataContext = viewModel;
viewModel.Initialize(parent);
return view;
}
private IExportGraphMainView GetGraphListView(IBaseViewModel parent)
{
var view = _unityContainer.Resolve<IExportGraphMainView>();
var viewModel = _unityContainer.Resolve<IExportGraphMainViewModel>();
view.DataContext = viewModel;
viewModel.Initialize(parent);
return view;
}
private ITestSummaryListView GetTestDefinitionView(IBaseViewModel parent)
{
var view = _unityContainer.Resolve<ITestSummaryListView>();
var viewModel = _unityContainer.Resolve<ITestSummaryListViewModel>();
view.DataContext = viewModel;
viewModel.Initialize(parent);
return view;
}
#endregion Views
#region ContextRegion
private IBaseViewModel _activeContent;
public IBaseViewModel ActiveContent { get => _activeContent; set { _activeContent = value; OnPropertyChanged("ActiveContent"); } }
public object ContextNavigationRegion
{
get => Standalone ? ((ExportMainView)View).NavigationRegion.Content : ((ExportMainViewGrid)View).NavigationRegion.Content;
set { if (Standalone) ((ExportMainView)View).NavigationRegion.Content = value; else ((ExportMainViewGrid)View).NavigationRegion.Content = value; OnPropertyChanged("ContextNavigationRegion"); }
}
#region Graph
public object ContextGraphRegion
{
get => Standalone ? ((ExportMainView)View).GraphRegion.Content : ((ExportMainViewGrid)View).GraphRegion.Content;
set { if (Standalone) ((ExportMainView)View).GraphRegion.Content = value; else ((ExportMainViewGrid)View).GraphRegion.Content = value; OnPropertyChanged("ContextGraphRegion"); }
}
public object ContextGraphListRegion
{
get => Standalone ? ((ExportMainView)View).GraphListRegion.Content : ((ExportMainViewGrid)View).GraphListRegion.Content;
set { if (Standalone) ((ExportMainView)View).GraphListRegion.Content = value; else ((ExportMainViewGrid)View).GraphListRegion.Content = value; OnPropertyChanged("ContextGraphListRegion"); }
}
#endregion Graph
public object ContextTestsRegion
{
get => Standalone ? ((ExportMainView)View).TestsRegion.Content : ((ExportMainViewGrid)View).TestsRegion.Content;
set { if (Standalone) ((ExportMainView)View).TestsRegion.Content = value; else ((ExportMainViewGrid)View).TestsRegion.Content = value; OnPropertyChanged("ContextTestsRegion"); }
}
public object ContextGraphsRegion
{
get => Standalone ? ((ExportMainView)View).GraphListRegion.Content : ((ExportMainViewGrid)View).GraphListRegion.Content;
set { if (Standalone) ((ExportMainView)View).GraphListRegion.Content = value; else ((ExportMainViewGrid)View).GraphListRegion.Content = value; OnPropertyChanged("ContextGraphsRegion"); }
}
public object ContextPropertyRegion { get; set; }
public List<FrameworkElement> GetRegions()
{
var items = new List<FrameworkElement>();
if (Standalone) Utils.GetChildrenByName(((ExportMainView)View).MainShell, "Region", ref items);
else Utils.GetChildrenByName(((ExportMainViewGrid)View).MainShell, "Region", ref items);
return items;
}
private readonly object saveLock = new object();
private void OnSaveToPDFRequested(string directory)
{
if (null == (ExportMainViewGrid)View) return;
lock (saveLock)
{
var pdfFileName = DateTime.Now.ToString("M'/'d'/'yyyy", CultureInfo.InvariantCulture).Replace("/", "_") + "_" +
DateTime.Now.ToString("h':'mm' 'tt", CultureInfo.InvariantCulture).Replace(":", "_") + "_" + DateTime.Now.Second + "_" +
DateTime.Now.Millisecond + ".pdf";
var intResult = ((ExportMainViewGrid)View).SaveToPDF(directory, pdfFileName);
var output = string.Empty;
if (intResult > 0)
{
//Success
output = string.Format(StringResources.SavePDFSuccess, Environment.NewLine,
Path.Combine(directory, pdfFileName), Environment.NewLine,
Path.Combine(directory, pdfFileName.Replace(".pdf", ".png")));
_eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(new string[] { output }, null));
}
else if (intResult < 0)
{
//Failure
output = StringResources.SavePDFError;
_eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(new string[] { output }, null));
}
}
}
public string ConfigPath { get; set; }
#endregion
#region Properties
#region Test Title
private string _titleTests = StringResources.TestsDefaultTitle;
public string TitleTests
{
get => _titleTests;
set
{
_titleTests = value;
if (Standalone) ((ExportMainView)View).TestRegionPanel.Title = _titleTests;
OnPropertyChanged("TitleTests");
}
}
private int _totalSelectedTests = 0;
public int TotalSelectedTests
{
get => _totalSelectedTests;
set
{
_totalSelectedTests = value;
TitleTests = StringResources.TestsDefaultTitle + TotalSelectedTests + "/" + TotalLoadedTests;
if (_totalSelectedTests == 0) { TitleTestIDs = StringResources.TestIDsDefaultTitle; }
OnPropertyChanged("TotalSelectedTests");
}
}
private int _totalLoadedTests = 0;
public int TotalLoadedTests
{
get => _totalLoadedTests;
set { _totalLoadedTests = value; TitleTests = StringResources.TestsDefaultTitle + TotalSelectedTests + "/" + TotalLoadedTests; OnPropertyChanged("TotalLoadedTests"); }
}
#endregion Test Title
#region Test ID Title
private string _titleTestIDs = StringResources.GraphsDefaultTitle;
public string TitleTestIDs
{
get => _titleTestIDs;
set
{
_titleTestIDs = value;
if (Standalone) ((ExportMainView)View).GraphListPanel.Title = _titleTestIDs;
OnPropertyChanged("TitleTestIDs");
}
}
private int _totalSelectedGraphs = 0;
public int TotalSelectedGraphs
{
get => _totalSelectedGraphs;
set { _totalSelectedGraphs = value; TitleTestIDs = StringResources.GraphsDefaultTitle + TotalSelectedGraphs + "/" + TotalLoadedGraphs; OnPropertyChanged("TotalSelectedGraphs"); }
}
private int _totalLoadedGraphs = 0;
public int TotalLoadedGraphs
{
get => _totalLoadedGraphs;
set { _totalLoadedGraphs = value; TitleTestIDs = StringResources.GraphsDefaultTitle + TotalSelectedGraphs + "/" + TotalLoadedGraphs; OnPropertyChanged("TotalLoadedGraphs"); }
}
#endregion Graph Title
private string _selectedDataFolder = string.Empty;
public string SelectedDataFolder
{
get => _selectedDataFolder;
set
{
if (string.IsNullOrEmpty(value)) return;
_selectedDataFolder = value; _eventAggregator.GetEvent<DataFolderChangedEvent>().Publish(new DataFolderSelectionArg { Path = _selectedDataFolder, File = string.Empty, SelectAll = true, ParentVM = this }); OnPropertyChanged("SelectedDataFolder");
}
}
public bool DoesUserHaveEditPermission { get; set; } = false;
private string _selectedDataFile = string.Empty;
public string SelectedDataFile
{
get => _selectedDataFile;
set
{
if (string.IsNullOrEmpty(value)) return;
_selectedDataFile = value; _eventAggregator.GetEvent<DataFolderChangedEvent>().Publish(new DataFolderSelectionArg { Path = string.Empty, File = _selectedDataFile, SelectAll = true, ParentVM = this }); OnPropertyChanged("SelectedDataFile");
}
}
public bool _showModifications = false;
public bool ShowModifications
{
get => _showModifications;
set { _showModifications = value; OnPropertyChanged("ShowModifications"); }
}
private IsoViewMode _channelCodeViewMode = IsoViewMode.ISOAndUserCode;
public IsoViewMode ChannelCodeViewMode
{
get => _channelCodeViewMode;
set { _channelCodeViewMode = value; _eventAggregator.GetEvent<ChannelCodesViewChangedEvent>().Publish(value); OnPropertyChanged("ChannelCodeViewMode"); }
}
private CalibrationBehaviors _calibrationBehaviorSetting = CalibrationBehaviors.NonLinearIfAvailable;
public CalibrationBehaviors CalibrationBehaviorSetting
{
get => _calibrationBehaviorSetting;
set { _calibrationBehaviorSetting = value; _eventAggregator.GetEvent<ExportCalibrationBehaviorSettingChangedEvent>().Publish(value); OnPropertyChanged("CalibrationBehaviorSetting"); }
}
private bool _calibrationBehaviorSettableInViewer = true;
public bool CalibrationBehaviorSettableInViewer
{
get => _calibrationBehaviorSettableInViewer;
set
{
_calibrationBehaviorSettableInViewer = value;
_eventAggregator.GetEvent<CalibrationBehaviorSettableInViewerChangedEvent>().Publish(value);
OnPropertyChanged("CalibrationBehaviorSettableInViewer");
((ExportMainViewGrid)View).graphsTab.IsSelected = true;// !value;
}
}
///<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 bool _isBusy = false;
/// <summary>
/// Display or hide Busy Indicator
/// </summary>
public new bool IsBusy { get => _isBusy; set { _isBusy = value; OnPropertyChanged("IsBusy"); } }
private string _isBusyMessage = string.Empty;
/// <summary>
/// Busy Message text
/// </summary>
public new string IsBusyMessage { get => _isBusyMessage; set { _isBusyMessage = value; OnPropertyChanged("IsBusyMessage"); } }
private bool _isMenuIncluded = false;
public new bool IsMenuIncluded { get => _isMenuIncluded; set { _isMenuIncluded = value; OnPropertyChanged("IsMenuIncluded"); } }
private bool _isNavigationIncluded = false;
public new bool IsNavigationIncluded { get => _isNavigationIncluded; set { _isNavigationIncluded = value; OnPropertyChanged("IsNavigationIncluded"); } }
/// <summary>
/// Gets the HeaderInfo.
/// </summary>
public string HeaderInfo => "MainRegion";
#endregion
#region Commands
#region LoadLayoutCommand
RelayCommand _loadLayoutCommand = null;
public ICommand LoadLayoutCommand => _loadLayoutCommand ?? (_loadLayoutCommand = new RelayCommand(OnLoadLayout, CanLoadLayout));
private bool CanLoadLayout(object parameter)
{
return File.Exists(@".\DataProViewerAvalonDock.config");
}
private void OnLoadLayout(object parameter)
{
var layoutSerializer = new XmlLayoutSerializer(((ExportMainView)View).DockManager);
//Here I've implemented the LayoutSerializationCallback just to show
// a way to feed layout desarialization with content loaded at runtime
//Actually I could in this case let AvalonDock to attach the contents
//from current layout using the content ids
//LayoutSerializationCallback should anyway be handled to attach contents
//not currently loaded
layoutSerializer.LayoutSerializationCallback += (s, e) =>
{
};
layoutSerializer.Deserialize(@".\DataProViewerAvalonDock.config");
}
#endregion
#region SaveLayoutCommand
RelayCommand _saveLayoutCommand = null;
public ICommand SaveLayoutCommand => _saveLayoutCommand ?? (_saveLayoutCommand = new RelayCommand(OnSaveLayout, CanSaveLayout));
private bool CanSaveLayout(object parameter)
{
return true;
}
private void OnSaveLayout(object parameter)
{
var layoutSerializer = new XmlLayoutSerializer(((ExportMainView)View).DockManager);
layoutSerializer.Serialize(@".\AvalonDock.config");
}
#endregion
#endregion
}
}