fasdfsa commited on
Commit
cbf03e6
·
1 Parent(s): a52ca5a

校对模式恢复选框

Browse files
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -86,7 +86,10 @@ namespace WpfEditor
86
  private Canvas _selectionCanvas;
87
  // 添加存储多个矩形选框的集合
88
  private List<Rectangle> _selectionRectangles = new List<Rectangle>();
89
-
 
 
 
90
  // 添加调整大小相关变量
91
  private bool _isResizing = false;
92
  private Rectangle _resizingRectangle = null;
@@ -768,6 +771,7 @@ namespace WpfEditor
768
  if (result == MessageBoxResult.Cancel)
769
  return;
770
  if (result == MessageBoxResult.Yes)
 
771
  SaveFile();
772
  }
773
 
@@ -886,10 +890,24 @@ namespace WpfEditor
886
  // File.WriteAllText(pth_json, jsn_str);
887
  //}
888
 
889
- OnOpenImage_Click(openFileDialog.FileName);
890
 
891
  var (jsn, m5, pth_img, pth_json) = Global.img_md5_jsn(openFileDialog.FileName);
892
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
893
  Editor._pthimg = openFileDialog.FileName;
894
  Editor._jsn_OpenImg = jsn;
895
  Editor._md5_OpenImg = m5;
@@ -915,14 +933,77 @@ namespace WpfEditor
915
  {
916
  // 整理校对模式要手动框选才出字
917
 
918
- string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
919
- byte[] img_bytes = Base64.Base64Decode(b64_str);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
920
 
921
- Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
922
 
923
- BitmapSource source = TextEditorControl.MatToBitmapSource(im_orig);
924
 
925
- this.ShowImageInPanel(source);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926
  }
927
  }
928
  }
@@ -1564,14 +1645,35 @@ namespace WpfEditor
1564
  }
1565
  }
1566
 
1567
- // 将Image_SizeChanged方法移动到这里(MainWindow类内部)
1568
  private void Image_SizeChanged(object sender, SizeChangedEventArgs e)
1569
  {
1570
- if (ViewModel.Image == null || ViewModel.Rectangles.Count == 0) return;
1571
 
1572
  var newWidth = e.NewSize.Width;
1573
  var newHeight = e.NewSize.Height;
1574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1575
  // 如果是首次设置尺寸,记录初始值
1576
  if (_previousImageWidth == 0 || _previousImageHeight == 0)
1577
  {
 
86
  private Canvas _selectionCanvas;
87
  // 添加存储多个矩形选框的集合
88
  private List<Rectangle> _selectionRectangles = new List<Rectangle>();
89
+
90
+ // 在类中添加一个字段来存储待处理的矩形数据
91
+ private List<JObject> _pendingRectangles = new List<JObject>(); // 等图片显示后才能正确进行坐标转换
92
+
93
  // 添加调整大小相关变量
94
  private bool _isResizing = false;
95
  private Rectangle _resizingRectangle = null;
 
771
  if (result == MessageBoxResult.Cancel)
772
  return;
773
  if (result == MessageBoxResult.Yes)
774
+ _textChanged = false;
775
  SaveFile();
776
  }
777
 
 
890
  // File.WriteAllText(pth_json, jsn_str);
891
  //}
892
 
893
+ //OnOpenImage_Click(openFileDialog.FileName);
894
 
895
  var (jsn, m5, pth_img, pth_json) = Global.img_md5_jsn(openFileDialog.FileName);
896
 
897
+ // 加载原图
898
+ string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
899
+ byte[] img_bytes = Base64.Base64Decode(b64_str);
900
+ Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
901
+ BitmapSource source = TextEditorControl.MatToBitmapSource(im_orig);
902
+ ViewModel.Image = source;
903
+ ViewModel.ClearAllRectangles();
904
+
905
+
906
+ // 重置图片尺寸记录
907
+ _previousImageWidth = 0;
908
+ _previousImageHeight = 0;
909
+
910
+
911
  Editor._pthimg = openFileDialog.FileName;
912
  Editor._jsn_OpenImg = jsn;
913
  Editor._md5_OpenImg = m5;
 
933
  {
934
  // 整理校对模式要手动框选才出字
935
 
936
+ try
937
+ {
938
+ //string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
939
+ //byte[] img_bytes = Base64.Base64Decode(b64_str);
940
+
941
+ //Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
942
+
943
+ //BitmapSource source = TextEditorControl.MatToBitmapSource(im_orig);
944
+
945
+ //this.ShowImageInPanel(source);
946
+
947
+
948
+ //Editor.SetText(jsn, m5);
949
+
950
+
951
+ Editor._lines.Clear();
952
+
953
+ Editor._lines = Global.loadJsonReviseEdit_FromOriginalPath(pth_json);
954
+
955
+ // 恢复框选
956
+ ViewModel.ClearAllRectangles();
957
+ //ViewModel.AddRectangle(int Id, double left, double top, double width, double height)
958
+
959
+ foreach (var item in Editor._lines)
960
+ {
961
+ if ( item.Characters.Count == 0 )
962
+ {
963
+ continue;
964
+ }
965
+
966
+ JObject selected_rectange = item.selected_rectange;
967
+ if (selected_rectange == null || !selected_rectange.HasValues)
968
+ {
969
+ continue;
970
+ }
971
+ int Id = selected_rectange["Id"].Value<int>();
972
+ int lft = selected_rectange["lft"].Value<int>();
973
+ int tp = selected_rectange["tp"].Value<int>();
974
+ int wdth = selected_rectange["wdth"].Value<int>();
975
+ int hght = selected_rectange["hght"].Value<int>();
976
 
977
+ _pendingRectangles.Add(selected_rectange); // 等图片显示后再处理 see Image_SizeChanged
978
 
 
979
 
980
+ //var displayPos = Editor.ConvertOriginalToDisplayCoordinates(lft, tp);
981
+ //var displaySize = Editor.ConvertOriginalToDisplaySize(wdth, hght);
982
+
983
+ //ViewModel.AddRectangle(Id, displayPos.X, displayPos.Y, displaySize.Width, displaySize.Height);
984
+
985
+ }
986
+
987
+
988
+ Editor._undoBuffer.Clear();
989
+ Editor._undoIndex = -1;
990
+ Editor._cursorLine = 0;
991
+ Editor._cursorColumn = 0;
992
+ Editor.ClearSelection();
993
+ Editor._textChanged = true;
994
+ Editor.InvalidateVisual();
995
+
996
+ // 触发文本改变事件
997
+ Editor.OnTextChanged();
998
+
999
+ UpdateTitle();
1000
+ StatusText.Text = $"已打开: {_currentFilePath}";
1001
+ }
1002
+ catch (Exception ex)
1003
+ {
1004
+ MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
1005
+ }
1006
+
1007
  }
1008
  }
1009
  }
 
1645
  }
1646
  }
1647
 
 
1648
  private void Image_SizeChanged(object sender, SizeChangedEventArgs e)
1649
  {
1650
+ if (ViewModel.Image == null) return; // ViewModel.Rectangles.Count == 0
1651
 
1652
  var newWidth = e.NewSize.Width;
1653
  var newHeight = e.NewSize.Height;
1654
 
1655
+ // 校对模式打开图片需要恢复选框
1656
+ if (_pendingRectangles.Count > 0 && newWidth > 0 && newHeight > 0)
1657
+ {
1658
+ foreach(var selected_rectange in _pendingRectangles)
1659
+ {
1660
+ int Id = selected_rectange["Id"].Value<int>();
1661
+ int lft = selected_rectange["lft"].Value<int>();
1662
+ int tp = selected_rectange["tp"].Value<int>();
1663
+ int wdth = selected_rectange["wdth"].Value<int>();
1664
+ int hght = selected_rectange["hght"].Value<int>();
1665
+
1666
+
1667
+ var displayPos = Editor.ConvertOriginalToDisplayCoordinates(lft, tp);
1668
+ var displaySize = Editor.ConvertOriginalToDisplaySize(wdth, hght);
1669
+
1670
+ ViewModel.AddRectangle(Id, displayPos.X, displayPos.Y, displaySize.Width, displaySize.Height);
1671
+ }
1672
+
1673
+ _pendingRectangles.Clear();
1674
+ return;
1675
+ }
1676
+
1677
  // 如果是首次设置尺寸,记录初始值
1678
  if (_previousImageWidth == 0 || _previousImageHeight == 0)
1679
  {
src/WpfEditor/TextEditorControl.cs CHANGED
@@ -391,14 +391,14 @@ namespace WpfEditor
391
  public List<TextLine> _lines = new List<TextLine>();
392
  public int _cursorLine = 0;
393
  public int _cursorColumn = 0;
394
- private int _selectionStartLine = -1;
395
- private int _selectionStartColumn = -1;
396
- private int _selectionEndLine = -1;
397
- private int _selectionEndColumn = -1;
398
- private List<UndoAction> _undoBuffer = new List<UndoAction>();
399
- private int _undoIndex = -1;
400
- private bool _textChanged = false;
401
- private int _scrollOffset = 0; // 添加滚动偏移量变量
402
 
403
  #endregion
404
 
@@ -2064,7 +2064,7 @@ namespace WpfEditor
2064
  return false;
2065
  }
2066
 
2067
- private void ClearSelection()
2068
  {
2069
  // 清除选择
2070
  _selectionStartLine = -1;
 
391
  public List<TextLine> _lines = new List<TextLine>();
392
  public int _cursorLine = 0;
393
  public int _cursorColumn = 0;
394
+ public int _selectionStartLine = -1;
395
+ public int _selectionStartColumn = -1;
396
+ public int _selectionEndLine = -1;
397
+ public int _selectionEndColumn = -1;
398
+ public List<UndoAction> _undoBuffer = new List<UndoAction>();
399
+ public int _undoIndex = -1;
400
+ public bool _textChanged = false;
401
+ public int _scrollOffset = 0; // 添加滚动偏移量变量
402
 
403
  #endregion
404
 
 
2064
  return false;
2065
  }
2066
 
2067
+ public void ClearSelection()
2068
  {
2069
  // 清除选择
2070
  _selectionStartLine = -1;
src/WpfEditor/ViewModel.cs CHANGED
@@ -101,6 +101,14 @@ namespace WpfEditor
101
  return RectangleCounter;
102
  }
103
 
 
 
 
 
 
 
 
 
104
  // 清除单个临时矩形(绘制过程中使用)
105
  public void ClearRectangle()
106
  {
 
101
  return RectangleCounter;
102
  }
103
 
104
+ public int AddRectangle(int Id, double left, double top, double width, double height)
105
+ {
106
+ var newRectangle = new RectangleItem(Id, left, top, width, height);
107
+ Rectangles.Add(newRectangle);
108
+ RectangleCounter = Rectangles.Count;
109
+ return RectangleCounter;
110
+ }
111
+
112
  // 清除单个临时矩形(绘制过程中使用)
113
  public void ClearRectangle()
114
  {
src/WpfEditor/global.cs CHANGED
@@ -371,7 +371,9 @@ namespace WpfEditor
371
  {
372
  if (!File.Exists(pth_json_reviseEdit))
373
  {
374
- throw new FileNotFoundException($"JSON 文件不存在: {pth_json_reviseEdit}");
 
 
375
  }
376
 
377
  string jsonString = File.ReadAllText(pth_json_reviseEdit, Encoding.UTF8);
@@ -407,14 +409,24 @@ namespace WpfEditor
407
  }
408
  }
409
 
410
- // 重载方法:根据原始 JSON 路径自动构建 reviseEdit JSON 路径
411
- public static List<TextLine> loadJsonReviseEdit_FromOriginalPath(string pth_json)
412
  {
413
  var dir = Path.GetDirectoryName(Path.GetDirectoryName(pth_json));
414
  var name_json = Path.GetFileName(pth_json);
415
  var dir_new = Path.Combine(dir, "json2");
416
  var pth_json_reviseEdit = Path.Combine(dir_new, name_json);
417
-
 
 
 
 
 
 
 
 
 
 
 
418
  return loadJsonReviseEdit(pth_json_reviseEdit);
419
  }
420
 
 
371
  {
372
  if (!File.Exists(pth_json_reviseEdit))
373
  {
374
+ //throw new FileNotFoundException($"JSON 文件不存在: {pth_json_reviseEdit}");
375
+
376
+ return new List<TextLine>();
377
  }
378
 
379
  string jsonString = File.ReadAllText(pth_json_reviseEdit, Encoding.UTF8);
 
409
  }
410
  }
411
 
412
+ public static string get_json2_path(string pth_json)
 
413
  {
414
  var dir = Path.GetDirectoryName(Path.GetDirectoryName(pth_json));
415
  var name_json = Path.GetFileName(pth_json);
416
  var dir_new = Path.Combine(dir, "json2");
417
  var pth_json_reviseEdit = Path.Combine(dir_new, name_json);
418
+ return pth_json_reviseEdit;
419
+ }
420
+
421
+ // 重载方法:根据原始 JSON 路径自动构建 reviseEdit JSON 路径
422
+ public static List<TextLine> loadJsonReviseEdit_FromOriginalPath(string pth_json)
423
+ {
424
+ //var dir = Path.GetDirectoryName(Path.GetDirectoryName(pth_json));
425
+ //var name_json = Path.GetFileName(pth_json);
426
+ //var dir_new = Path.Combine(dir, "json2");
427
+ //var pth_json_reviseEdit = Path.Combine(dir_new, name_json);
428
+ var pth_json_reviseEdit = get_json2_path(pth_json);
429
+
430
  return loadJsonReviseEdit(pth_json_reviseEdit);
431
  }
432
 
src/WpfEditor/readme.txt CHANGED
@@ -64,3 +64,30 @@ WpfEditor\TextEditorControl.cs
64
  public bool saveJson(string pth_img_)
65
 
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  public bool saveJson(string pth_img_)
65
 
66
 
67
+ 校对模式恢复选框
68
+ src\WpfEditor\MainWindow.xaml.cs
69
+ private void Image_SizeChanged(object sender, SizeChangedEventArgs e)
70
+ // 校对模式打开图片需要恢复选框
71
+ if (_pendingRectangles.Count > 0 && newWidth > 0 && newHeight > 0)
72
+ {
73
+ foreach(var selected_rectange in _pendingRectangles)
74
+ {
75
+ int Id = selected_rectange["Id"].Value<int>();
76
+ int lft = selected_rectange["lft"].Value<int>();
77
+ int tp = selected_rectange["tp"].Value<int>();
78
+ int wdth = selected_rectange["wdth"].Value<int>();
79
+ int hght = selected_rectange["hght"].Value<int>();
80
+
81
+
82
+ var displayPos = Editor.ConvertOriginalToDisplayCoordinates(lft, tp);
83
+ var displaySize = Editor.ConvertOriginalToDisplaySize(wdth, hght);
84
+
85
+ ViewModel.AddRectangle(Id, displayPos.X, displayPos.Y, displaySize.Width, displaySize.Height);
86
+ }
87
+
88
+ _pendingRectangles.Clear();
89
+ return;
90
+ }
91
+
92
+
93
+