MainWindow.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using AIPractice.Shared.Labels;
  18. using Microsoft.Win32;
  19. namespace AIPractice.LabelEditor
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. private string _oldContent;
  27. private string _openedFile;
  28. public string OpenedFile
  29. {
  30. get { return _openedFile; }
  31. set
  32. {
  33. if (_openedFile != value)
  34. {
  35. _openedFile = value;
  36. Title = $"标签编辑器 - [{_openedFile}]";
  37. }
  38. }
  39. }
  40. public MainWindow()
  41. {
  42. InitializeComponent();
  43. var container = new LabelContainer();
  44. App.Container = new LabelContainerViewModel(container);
  45. _oldContent = container.ToXml();
  46. DataContext = App.Container;
  47. Closing += OnClosing;
  48. }
  49. private void OnClosing(object sender, CancelEventArgs e)
  50. {
  51. if (_oldContent != App.Container.Container.ToXml())
  52. {
  53. var result = MessageBox.Show(this, "是否保存当前标签?", "标签编辑器", MessageBoxButton.YesNoCancel,
  54. MessageBoxImage.Question);
  55. if (result == MessageBoxResult.Yes)
  56. {
  57. OnSaveClick(this, null);
  58. }
  59. if (result == MessageBoxResult.Cancel)
  60. {
  61. e.Cancel = true;
  62. }
  63. }
  64. }
  65. private void OnOpenLabelClick(object sender, RoutedEventArgs e)
  66. {
  67. var ofd = new OpenFileDialog();
  68. ofd.Filter = "标签文件|*.lb|XML文件|*.xml";
  69. if (ofd.ShowDialog() == true)
  70. {
  71. var targetFile = ofd.FileName;
  72. try
  73. {
  74. var container = LabelContainer.Open(targetFile);
  75. App.Container = new LabelContainerViewModel(container);
  76. DataContext = null;
  77. DataContext = App.Container;
  78. if (App.Container.Labels.Count > 0)
  79. {
  80. App.Container.SelectedRootLabel = App.Container.Labels[0];
  81. }
  82. OpenedFile = targetFile;
  83. _oldContent = App.Container.Container.ToXml();
  84. }
  85. catch (Exception)
  86. {
  87. MessageBox.Show(this,"打开标签文件失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  88. }
  89. }
  90. }
  91. private string GetUniqueRootLabelName(string prefix)
  92. {
  93. var index = 1;
  94. while (App.Container.Labels.Any(x=>x.Title == $"{prefix}{index}"))
  95. {
  96. index++;
  97. }
  98. return $"{prefix}{index}";
  99. }
  100. private string GetUniqueDescriptionGroupName(string prefix)
  101. {
  102. var index = 1;
  103. while (App.Container.SelectedRootLabel.Descriptions.Any(x => x.Title == $"{prefix}{index}"))
  104. {
  105. index++;
  106. }
  107. return $"{prefix}{index}";
  108. }
  109. private string GetUniqueConclusionGroupName(string prefix)
  110. {
  111. var index = 1;
  112. while (App.Container.SelectedRootLabel.Conclusions.Any(x => x.Title == $"{prefix}{index}"))
  113. {
  114. index++;
  115. }
  116. return $"{prefix}{index}";
  117. }
  118. private void OnAddRootLabelClick(object sender, RoutedEventArgs e)
  119. {
  120. var id = Guid.NewGuid().ToString("N").ToUpper();
  121. var name = GetUniqueRootLabelName("新建部位");
  122. var rootLabel = new RootLabel(id, name, new List<LabelGroup>(), new List<LabelGroup>(), new List<LabelAssociation>());
  123. App.Container.Labels.Add(new RootLabelViewModel(rootLabel));
  124. }
  125. private void OnAddMultiDescriptionGroupClick(object sender, RoutedEventArgs e)
  126. {
  127. if (App.Container.SelectedRootLabel != null)
  128. {
  129. var id = Guid.NewGuid().ToString("N").ToUpper();
  130. var name = GetUniqueDescriptionGroupName("新建描述标签组");
  131. var multiSelectionLabelGroup =
  132. new MultiSelectionLabelGroup(id, name, GroupType.All, new List<ImageLabel>());
  133. var multiSelectionGroupViewModel = new MultiSelectionLabelGroupViewModel(multiSelectionLabelGroup);
  134. App.Container.SelectedRootLabel.Descriptions.Add(multiSelectionGroupViewModel);
  135. DescriptionScroller.ScrollToBottom();
  136. }
  137. }
  138. private void OnAddSingleDescriptionGroupClick(object sender, RoutedEventArgs e)
  139. {
  140. if (App.Container.SelectedRootLabel != null)
  141. {
  142. var id = Guid.NewGuid().ToString("N").ToUpper();
  143. var name = GetUniqueDescriptionGroupName("新建描述标签组");
  144. var singleSelectionLabelGroup =
  145. new SingleSelectionLabelGroup(id, name, GroupType.All, new List<ImageLabel>());
  146. var singleSelectionGroupViewModel = new SingleSelectionLabelGroupViewModel(singleSelectionLabelGroup);
  147. App.Container.SelectedRootLabel.Descriptions.Add(singleSelectionGroupViewModel);
  148. DescriptionScroller.ScrollToBottom();
  149. }
  150. }
  151. private void OnAddMultiConclusionGroupClick(object sender, RoutedEventArgs e)
  152. {
  153. if (App.Container.SelectedRootLabel != null)
  154. {
  155. var id = Guid.NewGuid().ToString("N").ToUpper();
  156. var name = GetUniqueConclusionGroupName("新建结论标签组");
  157. var multiSelectionLabelGroup =
  158. new MultiSelectionLabelGroup(id, name, GroupType.All, new List<ImageLabel>());
  159. var multiSelectionGroupViewModel = new MultiSelectionLabelGroupViewModel(multiSelectionLabelGroup);
  160. App.Container.SelectedRootLabel.Conclusions.Add(multiSelectionGroupViewModel);
  161. ConclusionScroller.ScrollToBottom();
  162. }
  163. }
  164. private void OnAddSingleConclusionGroupClick(object sender, RoutedEventArgs e)
  165. {
  166. if (App.Container.SelectedRootLabel != null)
  167. {
  168. var id = Guid.NewGuid().ToString("N").ToUpper();
  169. var name = GetUniqueConclusionGroupName("新建结论标签组");
  170. var singleSelectionLabelGroup =
  171. new SingleSelectionLabelGroup(id, name, GroupType.All, new List<ImageLabel>());
  172. var singleSelectionGroupViewModel = new SingleSelectionLabelGroupViewModel(singleSelectionLabelGroup);
  173. App.Container.SelectedRootLabel.Conclusions.Add(singleSelectionGroupViewModel);
  174. ConclusionScroller.ScrollToBottom();
  175. }
  176. }
  177. private void OnSaveClick(object sender, RoutedEventArgs e)
  178. {
  179. foreach(var root in App.Container.Labels)
  180. {
  181. root.RootLabel.ClearLabelAssociations();
  182. foreach(var group in root.Descriptions)
  183. {
  184. foreach(var label in group.Labels)
  185. {
  186. if(label.Linked != null)
  187. {
  188. var left = (DescriptionLabelViewModel)label;
  189. var right = (ConclusionLabelViewModel)label.Linked;
  190. root.RootLabel.AddLabelAssociation(new LabelAssociation(left.Description, right.Conclusion));
  191. }
  192. }
  193. }
  194. }
  195. if (_openedFile != null)
  196. {
  197. App.Container.Container.Save(_openedFile);
  198. _oldContent = App.Container.Container.ToXml();
  199. }
  200. else
  201. {
  202. var sfd = new SaveFileDialog
  203. {
  204. Filter = "标签文件|*.lb|XML文件|*.xml"
  205. };
  206. if (sfd.ShowDialog() == true)
  207. {
  208. var targetFile = sfd.FileName;
  209. App.Container.Container.Save(targetFile);
  210. OpenedFile = targetFile;
  211. _oldContent = App.Container.Container.ToXml();
  212. }
  213. }
  214. }
  215. private void OnSaveAsClick(object sender, RoutedEventArgs e)
  216. {
  217. foreach (var root in App.Container.Labels)
  218. {
  219. root.RootLabel.ClearLabelAssociations();
  220. foreach (var group in root.Descriptions)
  221. {
  222. foreach (var label in group.Labels)
  223. {
  224. if (label.Linked != null)
  225. {
  226. var left = (DescriptionLabelViewModel)label;
  227. var right = (ConclusionLabelViewModel)label.Linked;
  228. root.RootLabel.AddLabelAssociation(new LabelAssociation(left.Description, right.Conclusion));
  229. }
  230. }
  231. }
  232. }
  233. var sfd = new SaveFileDialog
  234. {
  235. Filter = "标签文件|*.lb|XML文件|*.xml"
  236. };
  237. if (sfd.ShowDialog() == true)
  238. {
  239. var targetFile = sfd.FileName;
  240. App.Container.Container.Save(targetFile);
  241. OpenedFile = targetFile;
  242. _oldContent = App.Container.Container.ToXml();
  243. }
  244. }
  245. }
  246. }