LogViewerWindow.xaml.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using MeterVision.Config;
  2. using MeterVision.model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. namespace MeterVision.Dlg
  18. {
  19. /// <summary>
  20. /// LogViewerWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class LogViewerWindow : Window
  23. {
  24. private readonly object DetailItem;
  25. //public LogViewerWindow(string imagePath,string logFilePath)
  26. //{
  27. // InitializeComponent();
  28. // LoadContent(imagePath, logFilePath);
  29. //}
  30. public LogViewerWindow(object detailItem)
  31. {
  32. InitializeComponent();
  33. int width = CfginiItem.GetConfigItem().LogvSize_Width;
  34. int height = CfginiItem.GetConfigItem().LogvSize_Heigth;
  35. if(width != 0 && height!= 0)
  36. {
  37. this.Width = width;
  38. this.Height = height;
  39. }
  40. int leftWidth = CfginiItem.GetConfigItem().LogvLeftWidth;
  41. if (leftWidth != 0)
  42. {
  43. ColumnDefinition leftColum = gridMid.ColumnDefinitions[0];
  44. leftColum.Width = new GridLength(leftWidth);
  45. }
  46. DetailItem = detailItem;
  47. InitLoad();
  48. }
  49. //加载登录的信息
  50. private void InitLoad()
  51. {
  52. ucImageSrc.ImageName = "原始图片";
  53. ucImageDst.ImageName = "结果图片";
  54. if (DetailItem is PatchDetailItem patchDetailItem)
  55. {
  56. ucImageSrc.ImageSource = patchDetailItem.SrcImage;
  57. ucImageDst.ImageSource = patchDetailItem.DstImage;
  58. LoadLogContent(patchDetailItem.LogPath);
  59. }
  60. else if (DetailItem is SingleDetailItem singleDetailItem)
  61. {
  62. ucImageSrc.ImageSource = singleDetailItem.SrcImage;
  63. ucImageDst.ImageSource = singleDetailItem.DstImage;
  64. LoadLogContent(singleDetailItem.LogPath);
  65. }
  66. }
  67. // 加载图片和日志内容
  68. private void LoadContent(string imagePath, string logFilePath)
  69. {
  70. try
  71. {
  72. // 加载并插入图片
  73. InsertImageAtTop(imagePath);
  74. // 加载并插入日志内容
  75. LoadLogContent(logFilePath);
  76. }
  77. catch (Exception ex)
  78. {
  79. MessageBox.Show($"加载内容时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  80. }
  81. }
  82. // 在 RichTextBox 顶部插入图片
  83. private void InsertImageAtTop(string imagePath)
  84. {
  85. try
  86. {
  87. // 创建一个 BitmapImage 并加载图片
  88. BitmapImage bitmap = new BitmapImage();
  89. bitmap.BeginInit();
  90. bitmap.UriSource = new Uri(imagePath, UriKind.Absolute);
  91. bitmap.EndInit();
  92. // 创建 Image 并设置图片源
  93. Image image = new Image
  94. {
  95. Source = bitmap,
  96. Width = 320, // 设置图片宽度
  97. Height = 320, // 设置图片高度
  98. Margin = new Thickness(0, 0, 0, 10) // 设置图片底部的间距
  99. };
  100. // 将 Image 包裹在 InlineUIContainer 中
  101. InlineUIContainer container = new InlineUIContainer(image);
  102. // 创建段落并将图片加入段落
  103. Paragraph imageParagraph = new Paragraph(container);
  104. // 将图片段落插入到文档的顶部
  105. FlowDocument document = RichTxtContentBox.Document;
  106. if (document.Blocks.Count > 0)
  107. {
  108. document.Blocks.InsertBefore(document.Blocks.FirstBlock, imageParagraph);
  109. }
  110. else
  111. {
  112. document.Blocks.Add(imageParagraph); // 如果文档为空,直接添加
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. MessageBox.Show($"插入图片时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  118. }
  119. }
  120. // 加载日志文件内容
  121. private void LoadLogContent(string filePath)
  122. {
  123. try
  124. {
  125. if (File.Exists(filePath))
  126. {
  127. string content = File.ReadAllText(filePath);
  128. // 创建段落并将日志内容添加到段落
  129. Paragraph logParagraph = new Paragraph(new Run(content));
  130. // 将日志段落添加到 RichTextBox 的文档中
  131. RichTxtContentBox.Document.Blocks.Add(logParagraph);
  132. }
  133. else
  134. {
  135. MessageBox.Show("日志文件未找到!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  136. }
  137. }
  138. catch (Exception ex)
  139. {
  140. MessageBox.Show($"加载日志文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  141. }
  142. }
  143. //记录位置
  144. private void Window_Closed(object sender, EventArgs e)
  145. {
  146. //最大化后的尺寸也记录
  147. //if (WindowState != WindowState.Maximized)
  148. //{
  149. int width = (int)this.Width;
  150. int height = (int)this.Height;
  151. string dialogSize = $"{width},{height}";
  152. //CfginiItem.GetConfigItem().DialogStandSize = dialogSize;
  153. CfginiItem.GetConfigItem().LogvSize = dialogSize;
  154. //}
  155. //获取左边的宽度
  156. ColumnDefinition leftColumn = gridMid.ColumnDefinitions[0];
  157. int leftWidth = (int)leftColumn.ActualWidth;
  158. if(leftWidth > 0)
  159. {
  160. CfginiItem.GetConfigItem().LogvLeftWidth = leftWidth;
  161. }
  162. }
  163. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  164. {
  165. }
  166. private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
  167. {
  168. }
  169. private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  170. {
  171. if(e.Key == Key.Escape)
  172. {
  173. this.Close();
  174. }
  175. }
  176. private void LogScrollTop_Click(object sender, RoutedEventArgs e)
  177. {
  178. RichTxtContentBox.ScrollToHome();
  179. }
  180. private void LogScrollBottom_Click(object sender, RoutedEventArgs e)
  181. {
  182. RichTxtContentBox.ScrollToEnd();
  183. }
  184. private void AiLogSave_Click(object sender, RoutedEventArgs e)
  185. {
  186. if (DetailItem == null) return;
  187. string logPath="";
  188. if (DetailItem is PatchDetailItem patchDetailItem)
  189. {
  190. logPath = patchDetailItem.LogPath;
  191. }
  192. else if (DetailItem is SingleDetailItem singleDetailItem)
  193. {
  194. logPath = singleDetailItem.LogPath;
  195. }
  196. if (!string.IsNullOrEmpty(logPath))
  197. {
  198. ThisApp.ExportAiLog(logPath);
  199. }
  200. }
  201. private TextPointer lastSearchPosition = null;
  202. private void SearchButton_Click(object sender, RoutedEventArgs e)
  203. {
  204. string searchText = txtSearchBox.Text;
  205. if (string.IsNullOrEmpty(searchText))
  206. return;
  207. TextRange documentRange = new TextRange(RichTxtContentBox.Document.ContentStart, RichTxtContentBox.Document.ContentEnd);
  208. documentRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Transparent); // 清除旧高亮
  209. TextPointer searchStart = lastSearchPosition ?? RichTxtContentBox.Document.ContentStart;
  210. TextRange foundRange = FindTextInRichTextBox(searchStart, searchText);
  211. if (foundRange != null)
  212. {
  213. foundRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
  214. RichTxtContentBox.Selection.Select(foundRange.Start, foundRange.End);
  215. RichTxtContentBox.Focus();
  216. lastSearchPosition = foundRange.End;
  217. }
  218. else
  219. {
  220. MessageBox.Show("未找到更多内容", "搜索", MessageBoxButton.OK, MessageBoxImage.Information);
  221. lastSearchPosition = null; // 重置
  222. }
  223. }
  224. private TextRange FindTextInRichTextBox(TextPointer start, string searchText)
  225. {
  226. while (start != null && start.CompareTo(RichTxtContentBox.Document.ContentEnd) < 0)
  227. {
  228. if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
  229. {
  230. string textRun = start.GetTextInRun(LogicalDirection.Forward);
  231. int index = textRun.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
  232. if (index >= 0)
  233. {
  234. TextPointer startPos = start.GetPositionAtOffset(index);
  235. TextPointer endPos = startPos?.GetPositionAtOffset(searchText.Length);
  236. return new TextRange(startPos, endPos);
  237. }
  238. }
  239. start = start.GetNextContextPosition(LogicalDirection.Forward);
  240. }
  241. return null;
  242. }
  243. //--------------------------------
  244. }
  245. }