123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- 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
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- 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<LabelGroup>(), new List<LabelGroup>(), new List<LabelAssociation>());
- 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<ImageLabel>());
- 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<ImageLabel>());
- 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<ImageLabel>());
- 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<ImageLabel>());
- 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();
- }
- }
- }
- }
|