2026-01-26 18:56:53 +09:00

168 lines
6.0 KiB
C#

/*
* Copyright (c) 2025 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using Aurora.Api;
using System.Windows;
using System.Windows.Controls;
using VisionCommon;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Reactive.Linq;
using VisionCommonUI.Resources;
using System.Text.RegularExpressions;
using System.Windows.Input;
namespace VisionUI.Setup {
/// <summary>
/// 캘리브레이션 파라미터 뷰
/// CalParamView.xaml behind code
/// </summary>
public partial class CalParamView : UserControl, IUpdateRecipe, IDisposable {
private const int PATTERN_NUM_1 = 1;
private const int PATTERN_MAX_COUNT = 3;
private const string PATTERN_UNDEFINED = "Undefined";
private Logger _Logger;
private FovInfo _FovInfo;
private FovRecipe _FovRecipe;
private IDisposable _EventDisposable;
private PatternAgentBase _PatternAgent = null;
private List<PatternInfo> _PatternInfos = null;
private Dictionary<int, ComboBox> _CbCalPatterns;
private List<PatternIndex> _PatternIndexes = new List<PatternIndex>();
/// <summary>
/// 생성자
/// </summary>
/// <param name="fovInfo">Fov 정보</param>
public CalParamView(FovInfo fovInfo) {
_FovInfo = fovInfo;
_FovRecipe = VisionRecipeManager.GetFovRecipe(fovInfo);
_Logger = new Logger($"CalParamView[{_FovRecipe.FovName}]");
_PatternAgent = PatternAgentManager.GetAgent(fovInfo);
InitializeComponent();
CreateComboBoxes();
}
private void InitPatternList() {
try {
_PatternInfos = _PatternAgent.GetMainPatterns(_FovInfo);
ClearComboBoxItems(_CbCalPatterns);
AddComboBoxeItems(_CbCalPatterns);
SelectComboBoxItem(_CbCalPatterns);
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
}
private void ClearComboBoxItems(Dictionary<int, ComboBox> cbPatterns) {
foreach (var cbPatternPair in cbPatterns) {
ComboBox cb = cbPatternPair.Value;
cb.Items.Clear();
}
}
private void CreateComboBoxes() {
_CbCalPatterns = new Dictionary<int, ComboBox>(PATTERN_MAX_COUNT);
_CbCalPatterns.Add(PATTERN_NUM_1, cbCalPattern);
}
private void AddComboBoxeItems(Dictionary<int, ComboBox> cbPatterns) {
try {
foreach (var cbPatternPair in cbPatterns) {
ComboBox cb = cbPatternPair.Value;
for (int i=0; i< _PatternInfos.Count(); i++) {
_PatternIndexes.Add(new PatternIndex(_FovInfo, _FovRecipe.FovName, _PatternInfos[i].GetPatternId()));
cb.Items.Add(PatternUtil.GetPatternDescription(_PatternInfos[i]));
}
}
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
}
private void SelectComboBoxItem(Dictionary<int, ComboBox> cbPatterns) {
try {
if (VisionSettingManager.VisionSetting.UseCamPattern) {
var startIndex = 0;
var endIndex = _PatternInfos.Count();
if (!_FovRecipe.CalFovRecipe.IsFovPattern) {
startIndex = _PatternInfos.Count();
endIndex = _PatternIndexes.Count();
}
for (int i = startIndex; i < endIndex; i++) {
if (_PatternIndexes[i].PatternId == _FovRecipe.CalFovRecipe.PatternId) {
cbPatterns[PATTERN_NUM_1].SelectedIndex = i;
}
}
}
else {
for (int i = 0; i < _PatternIndexes.Count(); i++) {
if (_PatternIndexes[i].PatternId == _FovRecipe.CalFovRecipe.PatternId) {
cbPatterns[PATTERN_NUM_1].SelectedIndex = i;
}
}
}
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
}
private void Button_Save_Click(object sender, RoutedEventArgs e) {
UpdateRecipeFromUI();
RecipeFileManager.SaveVisionRecipe();
VisionCommonUI.MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_SaveCompleted);
}
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
bool isVisible = (bool)e.NewValue;
if (isVisible == true) {
RefreshVisionRecipes();
UpdateRecipeFromFile();
}
}
public void RefreshVisionRecipes() {
_FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo);
(DataContext as CalParamViewModel).RefreshVisionRecipes();
}
public void UpdateRecipeFromFile() {
(DataContext as CalParamViewModel).UpdateRecipeFromFile();
InitPatternList();
}
public void UpdateRecipeFromUI() {
(DataContext as CalParamViewModel).CalFovRecipe.PatternId = _PatternIndexes[_CbCalPatterns[PATTERN_NUM_1].SelectedIndex].PatternId;
if (VisionSettingManager.VisionSetting.UseCamPattern) {
(DataContext as CalParamViewModel).CalFovRecipe.IsFovPattern = false;
}
else {
(DataContext as CalParamViewModel).CalFovRecipe.IsFovPattern = true;
}
(DataContext as CalParamViewModel).UpdateRecipeFromUI();
}
private void IsDecimalNumber(object sender, TextCompositionEventArgs e) {
decimal d;
if (!decimal.TryParse(e.Text, out d)) {
e.Handled = true;
}
}
public void Dispose() {
_EventDisposable?.Dispose();
_EventDisposable = null;
}
}
}