using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using AIPractice.Shared.Labels; using Microsoft.Win32; namespace AIPractice.LabelEditor { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private string _oldContent; private string _openedFile; public string OpenedFile { get { return _openedFile; } set { if (_openedFile != value) { _openedFile = value; Title = $"标签编辑器 - [{_openedFile}]"; } } } public MainWindow() { InitializeComponent(); var container = new LabelContainer(); App.Container = new LabelContainerViewModel(container); _oldContent = container.ToXml(); DataContext = App.Container; Closing += OnClosing; } private void OnClosing(object sender, CancelEventArgs e) { if (_oldContent != App.Container.Container.ToXml()) { var result = MessageBox.Show(this, "是否保存当前标签?", "标签编辑器", MessageBoxButton.YesNoCancel, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { OnSaveClick(this, null); } if (result == MessageBoxResult.Cancel) { e.Cancel = true; } } } private void OnOpenLabelClick(object sender, RoutedEventArgs e) { var ofd = new OpenFileDialog(); ofd.Filter = "标签文件|*.lb|XML文件|*.xml"; if (ofd.ShowDialog() == true) { var targetFile = ofd.FileName; try { var container = LabelContainer.Open(targetFile); App.Container = new LabelContainerViewModel(container); DataContext = null; DataContext = App.Container; if (App.Container.Labels.Count > 0) { App.Container.SelectedRootLabel = App.Container.Labels[0]; } OpenedFile = targetFile; _oldContent = App.Container.Container.ToXml(); } catch (Exception) { MessageBox.Show(this,"打开标签文件失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } private string GetUniqueRootLabelName(string prefix) { var index = 1; while (App.Container.Labels.Any(x=>x.Title == $"{prefix}{index}")) { index++; } return $"{prefix}{index}"; } private string GetUniqueDescriptionGroupName(string prefix) { var index = 1; while (App.Container.SelectedRootLabel.Descriptions.Any(x => x.Title == $"{prefix}{index}")) { index++; } return $"{prefix}{index}"; } private string GetUniqueConclusionGroupName(string prefix) { var index = 1; while (App.Container.SelectedRootLabel.Conclusions.Any(x => x.Title == $"{prefix}{index}")) { index++; } return $"{prefix}{index}"; } private void OnAddRootLabelClick(object sender, RoutedEventArgs e) { var id = Guid.NewGuid().ToString("N").ToUpper(); var name = GetUniqueRootLabelName("新建部位"); var rootLabel = new RootLabel(id, name, new List(), new List(), new List()); App.Container.Labels.Add(new RootLabelViewModel(rootLabel)); } private void OnAddMultiDescriptionGroupClick(object sender, RoutedEventArgs e) { if (App.Container.SelectedRootLabel != null) { var id = Guid.NewGuid().ToString("N").ToUpper(); var name = GetUniqueDescriptionGroupName("新建描述标签组"); var multiSelectionLabelGroup = new MultiSelectionLabelGroup(id, name, GroupType.All, new List()); var multiSelectionGroupViewModel = new MultiSelectionLabelGroupViewModel(multiSelectionLabelGroup); App.Container.SelectedRootLabel.Descriptions.Add(multiSelectionGroupViewModel); DescriptionScroller.ScrollToBottom(); } } private void OnAddSingleDescriptionGroupClick(object sender, RoutedEventArgs e) { if (App.Container.SelectedRootLabel != null) { var id = Guid.NewGuid().ToString("N").ToUpper(); var name = GetUniqueDescriptionGroupName("新建描述标签组"); var singleSelectionLabelGroup = new SingleSelectionLabelGroup(id, name, GroupType.All, new List()); var singleSelectionGroupViewModel = new SingleSelectionLabelGroupViewModel(singleSelectionLabelGroup); App.Container.SelectedRootLabel.Descriptions.Add(singleSelectionGroupViewModel); DescriptionScroller.ScrollToBottom(); } } private void OnAddMultiConclusionGroupClick(object sender, RoutedEventArgs e) { if (App.Container.SelectedRootLabel != null) { var id = Guid.NewGuid().ToString("N").ToUpper(); var name = GetUniqueConclusionGroupName("新建结论标签组"); var multiSelectionLabelGroup = new MultiSelectionLabelGroup(id, name, GroupType.All, new List()); var multiSelectionGroupViewModel = new MultiSelectionLabelGroupViewModel(multiSelectionLabelGroup); App.Container.SelectedRootLabel.Conclusions.Add(multiSelectionGroupViewModel); ConclusionScroller.ScrollToBottom(); } } private void OnAddSingleConclusionGroupClick(object sender, RoutedEventArgs e) { if (App.Container.SelectedRootLabel != null) { var id = Guid.NewGuid().ToString("N").ToUpper(); var name = GetUniqueConclusionGroupName("新建结论标签组"); var singleSelectionLabelGroup = new SingleSelectionLabelGroup(id, name, GroupType.All, new List()); var singleSelectionGroupViewModel = new SingleSelectionLabelGroupViewModel(singleSelectionLabelGroup); App.Container.SelectedRootLabel.Conclusions.Add(singleSelectionGroupViewModel); ConclusionScroller.ScrollToBottom(); } } private void OnSaveClick(object sender, RoutedEventArgs e) { foreach(var root in App.Container.Labels) { root.RootLabel.ClearLabelAssociations(); foreach(var group in root.Descriptions) { foreach(var label in group.Labels) { if(label.Linked != null) { var left = (DescriptionLabelViewModel)label; var right = (ConclusionLabelViewModel)label.Linked; root.RootLabel.AddLabelAssociation(new LabelAssociation(left.Description, right.Conclusion)); } } } } if (_openedFile != null) { App.Container.Container.Save(_openedFile); _oldContent = App.Container.Container.ToXml(); } else { var sfd = new SaveFileDialog { Filter = "标签文件|*.lb|XML文件|*.xml" }; if (sfd.ShowDialog() == true) { var targetFile = sfd.FileName; App.Container.Container.Save(targetFile); OpenedFile = targetFile; _oldContent = App.Container.Container.ToXml(); } } } private void OnSaveAsClick(object sender, RoutedEventArgs e) { foreach (var root in App.Container.Labels) { root.RootLabel.ClearLabelAssociations(); foreach (var group in root.Descriptions) { foreach (var label in group.Labels) { if (label.Linked != null) { var left = (DescriptionLabelViewModel)label; var right = (ConclusionLabelViewModel)label.Linked; root.RootLabel.AddLabelAssociation(new LabelAssociation(left.Description, right.Conclusion)); } } } } var sfd = new SaveFileDialog { Filter = "标签文件|*.lb|XML文件|*.xml" }; if (sfd.ShowDialog() == true) { var targetFile = sfd.FileName; App.Container.Container.Save(targetFile); OpenedFile = targetFile; _oldContent = App.Container.Container.ToXml(); } } } }