PdfView.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using ivf_tl_Manage.Converts;
  2. using ivf_tl_Manage.ViewModels;
  3. using ivf_tl_Manage.Win;
  4. using QuestPDF.Fluent;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Effects;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace ivf_tl_Manage.Views
  21. {
  22. /// <summary>
  23. /// PdfView.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class PdfView : UserControl
  26. {
  27. private PdfViewModel vm;
  28. public PdfView()
  29. {
  30. InitializeComponent();
  31. Loaded += PdfView_Loaded;
  32. Unloaded += PdfView_Unloaded;
  33. }
  34. private void PdfView_Unloaded(object sender, RoutedEventArgs e)
  35. {
  36. Canvas_Root.Children.Clear();
  37. vm = null;
  38. DataContext = null;
  39. }
  40. private void PdfView_Loaded(object sender, RoutedEventArgs e)
  41. {
  42. vm = (PdfViewModel)this.DataContext;
  43. if (vm == null) return;
  44. InitNav();
  45. ImageTransformGroup();
  46. if (vm.IsUpdateAi)
  47. {
  48. Task.Factory.StartNew(() =>
  49. {
  50. var reportData = vm.dishProvider.GetCultureRecordDetailByIdApi(vm.Dish.id);
  51. if (reportData == null)
  52. {
  53. //Dispatcher.Invoke(() => MessageShow("没有可以导出的数据"));
  54. Dispatcher.Invoke(() => MessageShow(KeyToStringConvert.GetLanguageStringByKey("0614")));
  55. return;
  56. }
  57. vm.Document = vm.GenerateReportEN(reportData);
  58. vm.PdfImageList = vm.Document.GenerateImages().ToList();
  59. Dispatcher.Invoke(() => PdfShow());
  60. });
  61. }
  62. else
  63. {
  64. PdfShow();
  65. }
  66. }
  67. private void PdfShow()
  68. {
  69. try
  70. {
  71. DropShadowEffect dropShadowEffect = new DropShadowEffect();
  72. dropShadowEffect.BlurRadius = 15;
  73. dropShadowEffect.Direction = 0;
  74. dropShadowEffect.ShadowDepth = 0;
  75. dropShadowEffect.Opacity = 0.5;
  76. this.image.Children.Clear();
  77. foreach (var item in vm.PdfImageList)
  78. {
  79. //Rectangle pdfImage = new Rectangle();
  80. //pdfImage.Fill = new SolidColorBrush(Colors.LightBlue);
  81. Image pdfImage = new Image();
  82. pdfImage.Source = AppData.Instance.ByteToBitmapImage(item);
  83. if (pdfImage.Source == null) continue;
  84. this.image.Children.Add(pdfImage);
  85. if (item == vm.PdfImageList.Last())
  86. {
  87. pdfImage.Width = 1560;
  88. pdfImage.Height = 1102;
  89. }
  90. else
  91. {
  92. pdfImage.Width = 1200;
  93. pdfImage.Height = 1698;
  94. }
  95. pdfImage.Margin = new Thickness(5);
  96. pdfImage.Effect = dropShadowEffect;
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. return;
  102. }
  103. }
  104. private void ImageTransformGroup()
  105. {
  106. TransformGroup transformGroup = this.image.RenderTransform as TransformGroup;
  107. ScaleTransform sfr = transformGroup.Children[0] as ScaleTransform;
  108. TranslateTransform ttf = transformGroup.Children[1] as TranslateTransform;
  109. image.MouseWheel += (s, arg) =>
  110. {
  111. if (Keyboard.Modifiers != ModifierKeys.Control) return;
  112. Point centerPoint = arg.GetPosition(image);
  113. var delta = arg.Delta * 0.001;
  114. if (sfr.ScaleX + delta < 0.5 || sfr.ScaleX + delta > 4.0)
  115. {
  116. arg.Handled = true;
  117. return;
  118. }
  119. if (true)
  120. {
  121. //Point pointNew = transformGroup.Inverse.Transform(centerPoint);
  122. sfr.CenterX = centerPoint.X;
  123. sfr.CenterY = centerPoint.Y;
  124. sfr.ScaleX += delta;
  125. sfr.ScaleY += delta;
  126. }
  127. else
  128. {
  129. Point pointNew = transformGroup.Inverse.Transform(centerPoint);
  130. sfr.ScaleX += delta;
  131. sfr.ScaleY += delta;
  132. ttf.X = -1 * ((pointNew.X * sfr.ScaleX) - centerPoint.X);
  133. ttf.Y = -1 * ((pointNew.Y * sfr.ScaleY) - centerPoint.Y);
  134. }
  135. arg.Handled = true;
  136. };
  137. Point StartPosition = new Point();
  138. Point EndPosition = new Point();
  139. Point ControlPoint = new Point();
  140. bool isStart = false;
  141. image.MouseDown += (s, arg) =>
  142. {
  143. Mouse.Capture((IInputElement)s);
  144. StartPosition = arg.GetPosition(parent);
  145. ControlPoint = new Point(ttf.X, ttf.Y);
  146. isStart = true;
  147. };
  148. image.MouseMove += (s, arg) =>
  149. {
  150. if (!isStart) return;
  151. if (arg.LeftButton != MouseButtonState.Pressed) return;
  152. EndPosition = arg.GetPosition(parent);
  153. var X = (EndPosition.X - StartPosition.X);
  154. var Y = EndPosition.Y - StartPosition.Y;
  155. ttf.X = ControlPoint.X + X;
  156. ttf.Y = ControlPoint.Y + Y;
  157. };
  158. image.MouseUp += (s, arg) =>
  159. {
  160. Mouse.Capture(null);
  161. isStart = false;
  162. };
  163. image.MouseLeave += (s, arg) =>
  164. {
  165. isStart = false;
  166. };
  167. }
  168. private void InitNav()
  169. {
  170. _nav_StackPanel.Children.Clear();
  171. for (int i = 0; i < vm.NavList.Count; i++)
  172. {
  173. var item = vm.NavList[i];
  174. if (i == vm.NavList.Count - 1)
  175. {
  176. TextBlock textBlock1 = new TextBlock();
  177. textBlock1.Margin = new Thickness(8, 0, 0, 0);
  178. textBlock1.FontWeight = FontWeights.Bold;
  179. textBlock1.Foreground = new SolidColorBrush(Colors.Black);
  180. textBlock1.FontSize = 32d;
  181. textBlock1.VerticalAlignment = VerticalAlignment.Center;
  182. textBlock1.Text = item.NavName;
  183. _nav_StackPanel.Children.Add(textBlock1);
  184. return;
  185. }
  186. Button button = new Button();
  187. button.Content = item.NavName;
  188. button.FontWeight = FontWeights.Bold;
  189. button.FontSize = 28d;
  190. button.BorderThickness = new Thickness(0);
  191. button.Background = new SolidColorBrush(Colors.Transparent);
  192. button.Style = App.Current.FindResource("ButtonStyle1") as Style;
  193. if (i == 0)
  194. {
  195. button.Margin = new Thickness(0, 0, 0, 0);
  196. }
  197. else
  198. {
  199. button.Margin = new Thickness(8, 0, 0, 0);
  200. }
  201. button.Click += (s, e) =>
  202. {
  203. if (vm != null)
  204. {
  205. vm.NavList.Clear();
  206. vm.LogoBytes = null;
  207. vm.Document = null;
  208. vm.PdfImageList.Clear();
  209. vm.Dish = null;
  210. vm.dishProvider = null;
  211. vm.QuickButtons.Clear();
  212. vm.DishPicAndVideoList.Clear();
  213. }
  214. AppData.Instance.MainWindowViewModel.CurrentViewModle = item;
  215. };
  216. TextBlock textBlock = new TextBlock();
  217. textBlock.Margin = new Thickness(8, 0, 0, 0);
  218. textBlock.VerticalAlignment = VerticalAlignment.Center;
  219. textBlock.FontWeight = FontWeights.Bold;
  220. textBlock.FontSize = 28d;
  221. textBlock.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#9B9B9B"));
  222. textBlock.Text = ">";
  223. _nav_StackPanel.Children.Add(button);
  224. _nav_StackPanel.Children.Add(textBlock);
  225. }
  226. }
  227. public void MessageShow(string message)
  228. {
  229. new ToastMessageWindow(AppData.Instance.MainWindow, 1920, 65, message).Show();
  230. }
  231. private void Toast(bool success)
  232. {
  233. new ToastWindow(AppData.Instance.MainWindow, 1920, 65, success).Show();
  234. }
  235. private void Export_Click(object sender, RoutedEventArgs e)
  236. {
  237. try
  238. {
  239. if (vm == null || vm.Document == null || vm.Dish == null)
  240. {
  241. //MessageShow("没有可以导出的数据")
  242. MessageShow(KeyToStringConvert.GetLanguageStringByKey("0614"));
  243. return;
  244. }
  245. string fileName = AppData.Instance.SaveFileName(vm.Dish.wife);
  246. if (fileName == null) return;
  247. vm.Document.GeneratePdf(fileName);
  248. MessageShow(KeyToStringConvert.GetLanguageStringByKey("0585"));
  249. //MessageShow("AI报告导出成功");
  250. }
  251. catch (Exception ex)
  252. {
  253. Toast(false);
  254. }
  255. }
  256. private void Mark_Click(object sender, RoutedEventArgs e)
  257. {
  258. vm.MarkEmbryo();
  259. }
  260. private void ScrollViewer_ManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
  261. {
  262. e.Handled = true;
  263. }
  264. private void ZoomMax_Click(object sender, RoutedEventArgs e)
  265. {
  266. double viewportCenterX = parent.HorizontalOffset + parent.ViewportWidth / 2;
  267. double viewportCenterY = parent.VerticalOffset + parent.ViewportHeight / 2;
  268. Point centerInImage = parent.TranslatePoint(new Point(viewportCenterX, viewportCenterY), image);
  269. TransformGroup transformGroup = this.image.RenderTransform as TransformGroup;
  270. ScaleTransform scaleTransform = transformGroup.Children[0] as ScaleTransform;
  271. scaleTransform.CenterX = centerInImage.X;
  272. scaleTransform.CenterY = centerInImage.Y;
  273. scaleTransform.ScaleX += 0.1;
  274. scaleTransform.ScaleY += 0.1;
  275. }
  276. private void ZoomMin_Click(object sender, RoutedEventArgs e)
  277. {
  278. double viewportCenterX = parent.HorizontalOffset + parent.ViewportWidth / 2;
  279. double viewportCenterY = parent.VerticalOffset + parent.ViewportHeight / 2;
  280. Point centerInImage = parent.TranslatePoint(new Point(viewportCenterX, viewportCenterY), image);
  281. TransformGroup transformGroup = this.image.RenderTransform as TransformGroup;
  282. ScaleTransform scaleTransform = transformGroup.Children[0] as ScaleTransform;
  283. scaleTransform.CenterX = centerInImage.X;
  284. scaleTransform.CenterY = centerInImage.Y;
  285. if (scaleTransform.ScaleX > 0.1)
  286. {
  287. scaleTransform.ScaleX -= 0.1;
  288. scaleTransform.ScaleY -= 0.1;
  289. }
  290. }
  291. }
  292. }