123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- using MeterVision.Config;
- using MeterVision.model;
- using System;
- using System.Collections.Generic;
- 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.Shapes;
- namespace MeterVision.Dlg
- {
- /// <summary>
- /// LogViewerWindow.xaml 的交互逻辑
- /// </summary>
- public partial class LogViewerWindow : Window
- {
- private readonly object DetailItem;
- //public LogViewerWindow(string imagePath,string logFilePath)
- //{
- // InitializeComponent();
- // LoadContent(imagePath, logFilePath);
- //}
- public LogViewerWindow(object detailItem)
- {
- InitializeComponent();
- int width = CfginiItem.GetConfigItem().LogvSize_Width;
- int height = CfginiItem.GetConfigItem().LogvSize_Heigth;
- if(width != 0 && height!= 0)
- {
- this.Width = width;
- this.Height = height;
- }
- int leftWidth = CfginiItem.GetConfigItem().LogvLeftWidth;
- if (leftWidth != 0)
- {
- ColumnDefinition leftColum = gridMid.ColumnDefinitions[0];
- leftColum.Width = new GridLength(leftWidth);
- }
- DetailItem = detailItem;
- InitLoad();
- }
- //加载登录的信息
- private void InitLoad()
- {
- ucImageSrc.ImageName = "原始图片";
- ucImageDst.ImageName = "结果图片";
- if (DetailItem is PatchDetailItem patchDetailItem)
- {
- ucImageSrc.ImageSource = patchDetailItem.SrcImage;
- ucImageDst.ImageSource = patchDetailItem.DstImage;
- LoadLogContent(patchDetailItem.LogPath);
- }
- else if (DetailItem is SingleDetailItem singleDetailItem)
- {
- ucImageSrc.ImageSource = singleDetailItem.SrcImage;
- ucImageDst.ImageSource = singleDetailItem.DstImage;
- LoadLogContent(singleDetailItem.LogPath);
- }
- }
- // 加载图片和日志内容
- private void LoadContent(string imagePath, string logFilePath)
- {
- try
- {
- // 加载并插入图片
- InsertImageAtTop(imagePath);
- // 加载并插入日志内容
- LoadLogContent(logFilePath);
- }
- catch (Exception ex)
- {
- MessageBox.Show($"加载内容时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- // 在 RichTextBox 顶部插入图片
- private void InsertImageAtTop(string imagePath)
- {
- try
- {
- // 创建一个 BitmapImage 并加载图片
- BitmapImage bitmap = new BitmapImage();
- bitmap.BeginInit();
- bitmap.UriSource = new Uri(imagePath, UriKind.Absolute);
- bitmap.EndInit();
- // 创建 Image 并设置图片源
- Image image = new Image
- {
- Source = bitmap,
- Width = 320, // 设置图片宽度
- Height = 320, // 设置图片高度
- Margin = new Thickness(0, 0, 0, 10) // 设置图片底部的间距
- };
- // 将 Image 包裹在 InlineUIContainer 中
- InlineUIContainer container = new InlineUIContainer(image);
- // 创建段落并将图片加入段落
- Paragraph imageParagraph = new Paragraph(container);
- // 将图片段落插入到文档的顶部
- FlowDocument document = RichTxtContentBox.Document;
- if (document.Blocks.Count > 0)
- {
- document.Blocks.InsertBefore(document.Blocks.FirstBlock, imageParagraph);
- }
- else
- {
- document.Blocks.Add(imageParagraph); // 如果文档为空,直接添加
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"插入图片时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- // 加载日志文件内容
- private void LoadLogContent(string filePath)
- {
- try
- {
- if (File.Exists(filePath))
- {
- string content = File.ReadAllText(filePath);
- // 创建段落并将日志内容添加到段落
- Paragraph logParagraph = new Paragraph(new Run(content));
- // 将日志段落添加到 RichTextBox 的文档中
- RichTxtContentBox.Document.Blocks.Add(logParagraph);
- }
- else
- {
- MessageBox.Show("日志文件未找到!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"加载日志文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- //记录位置
- private void Window_Closed(object sender, EventArgs e)
- {
- //最大化后的尺寸也记录
- //if (WindowState != WindowState.Maximized)
- //{
- int width = (int)this.Width;
- int height = (int)this.Height;
- string dialogSize = $"{width},{height}";
- //CfginiItem.GetConfigItem().DialogStandSize = dialogSize;
- CfginiItem.GetConfigItem().LogvSize = dialogSize;
- //}
- //获取左边的宽度
- ColumnDefinition leftColumn = gridMid.ColumnDefinitions[0];
- int leftWidth = (int)leftColumn.ActualWidth;
- if(leftWidth > 0)
- {
- CfginiItem.GetConfigItem().LogvLeftWidth = leftWidth;
- }
- }
- private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- }
- private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
- {
- }
- private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if(e.Key == Key.Escape)
- {
- this.Close();
- }
- }
- private void LogScrollTop_Click(object sender, RoutedEventArgs e)
- {
- RichTxtContentBox.ScrollToHome();
- }
- private void LogScrollBottom_Click(object sender, RoutedEventArgs e)
- {
- RichTxtContentBox.ScrollToEnd();
- }
- private void AiLogSave_Click(object sender, RoutedEventArgs e)
- {
- if (DetailItem == null) return;
- string logPath="";
- if (DetailItem is PatchDetailItem patchDetailItem)
- {
- logPath = patchDetailItem.LogPath;
- }
- else if (DetailItem is SingleDetailItem singleDetailItem)
- {
- logPath = singleDetailItem.LogPath;
- }
- if (!string.IsNullOrEmpty(logPath))
- {
- ThisApp.ExportAiLog(logPath);
- }
- }
- private TextPointer lastSearchPosition = null;
- private void SearchButton_Click(object sender, RoutedEventArgs e)
- {
- string searchText = txtSearchBox.Text;
- if (string.IsNullOrEmpty(searchText))
- return;
- TextRange documentRange = new TextRange(RichTxtContentBox.Document.ContentStart, RichTxtContentBox.Document.ContentEnd);
- documentRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Transparent); // 清除旧高亮
- TextPointer searchStart = lastSearchPosition ?? RichTxtContentBox.Document.ContentStart;
- TextRange foundRange = FindTextInRichTextBox(searchStart, searchText);
- if (foundRange != null)
- {
- foundRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
- RichTxtContentBox.Selection.Select(foundRange.Start, foundRange.End);
- RichTxtContentBox.Focus();
- lastSearchPosition = foundRange.End;
- }
- else
- {
- MessageBox.Show("未找到更多内容", "搜索", MessageBoxButton.OK, MessageBoxImage.Information);
- lastSearchPosition = null; // 重置
- }
- }
- private TextRange FindTextInRichTextBox(TextPointer start, string searchText)
- {
- while (start != null && start.CompareTo(RichTxtContentBox.Document.ContentEnd) < 0)
- {
- if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
- {
- string textRun = start.GetTextInRun(LogicalDirection.Forward);
- int index = textRun.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
- if (index >= 0)
- {
- TextPointer startPos = start.GetPositionAtOffset(index);
- TextPointer endPos = startPos?.GetPositionAtOffset(searchText.Length);
- return new TextRange(startPos, endPos);
- }
- }
- start = start.GetNextContextPosition(LogicalDirection.Forward);
- }
- return null;
- }
- //--------------------------------
- }
- }
|