MeasureControl.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.Tool;
  4. using Compdfkit_Tools.Helper;
  5. using Compdfkit_Tools.PDFControl;
  6. using ComPDFKitViewer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Globalization;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. using ComPDFKit.Tool.Help;
  24. using ComPDFKit.Viewer.Annot;
  25. using ComPDFKitViewer.BaseObject;
  26. namespace Compdfkit_Tools.Measure
  27. {
  28. /// <summary>
  29. /// MeasureControl.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class MeasureControl : UserControl
  32. {
  33. public MeasurePropertyControl measurePropertyControl = new MeasurePropertyControl();
  34. private CPDFDisplaySettingsControl displaySettingsControl;
  35. private PDFViewControl PdfViewControl = new PDFViewControl();
  36. private PanelState panelState = PanelState.GetInstance();
  37. private CPDFAnnotation currentAnnot = null;
  38. public event EventHandler ExpandEvent;
  39. public MeasureControl()
  40. {
  41. InitializeComponent();
  42. //MeasureSetting.MeasureChanged += MeasureSetting_MeasureChanged;
  43. }
  44. //private void MeasureSetting_MeasureChanged(object sender, MeasureEventArgs e)
  45. //{
  46. // InfoPanel.SetMeasureType(e.Type);
  47. // InfoPanel.SetMeasureInfo(e);
  48. // switch (e.Type)
  49. // {
  50. // case CPDFMeasureType.CPDF_DISTANCE_MEASURE:
  51. // case CPDFMeasureType.CPDF_PERIMETER_MEASURE:
  52. // SettingPanel.ShowAreaAndLength(Visibility.Collapsed);
  53. // break;
  54. // case CPDFMeasureType.CPDF_AREA_MEASURE:
  55. // SettingPanel.ShowAreaAndLength(Visibility.Visible);
  56. // break;
  57. // default:
  58. // break;
  59. // }
  60. // SettingPanel.BindMeasureSetting(e);
  61. //}
  62. #region Init PDFViewer
  63. public void InitWithPDFViewer(PDFViewControl pdfViewControl)
  64. {
  65. PdfViewControl = pdfViewControl;
  66. //PdfViewControl.PDFView = pdfViewer;
  67. PDFMeasureTool.InitWithPDFViewer(pdfViewControl, measurePropertyControl, this);
  68. FloatPageTool.InitWithPDFViewer(pdfViewControl);
  69. PDFGrid.Child = PdfViewControl;
  70. panelState.PropertyChanged -= PanelState_PropertyChanged;
  71. panelState.PropertyChanged += PanelState_PropertyChanged;
  72. PdfViewControl.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  73. PdfViewControl.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  74. PdfViewControl.MouseMoveHandler -= PDFToolManager_MouseMoveHandler;
  75. PdfViewControl.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  76. PdfViewControl.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  77. PdfViewControl.MouseMoveHandler += PDFToolManager_MouseMoveHandler;
  78. //pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  79. //pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  80. //pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  81. //pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  82. SetInfoPanelVisble(false, false);
  83. SettingPanel.PdfViewControl= pdfViewControl;
  84. }
  85. private void PDFToolManager_MouseMoveHandler(object sender, MouseEventObject e)
  86. {
  87. }
  88. private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  89. {
  90. }
  91. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  92. {
  93. BaseAnnot baseAnnot = PdfViewControl.GetCacheHitTestAnnot();
  94. if (baseAnnot != null)
  95. {
  96. AnnotData annotData = baseAnnot.GetAnnotData();
  97. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  98. PdfViewControl.GetCPDFViewer().GetDocument(), annotData.PageIndex, annotData.Annot);
  99. measurePropertyControl.SetPropertyForMeasureCreate(annotParam, annotData.Annot, PdfViewControl);
  100. SetMeasureInfoPanel(annotData.Annot, annotParam);
  101. currentAnnot = annotData.Annot;
  102. }
  103. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  104. // measurePropertyControl.SetPropertyForMeasureCreate(LineArgs, e);
  105. SetInfoPanelVisble(true, false);
  106. }
  107. //private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  108. //{
  109. // switch (e.CommandType)
  110. // {
  111. // case CommandType.Context:
  112. // if (e.CommandTarget == TargetType.Annot && e.PressOnAnnot)
  113. // {
  114. // e.Handle = true;
  115. // e.PopupMenu = new ContextMenu();
  116. // e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  117. // MenuItem menuItem = new MenuItem();
  118. // menuItem.Header = "Measurement Settings";
  119. // menuItem.Click += (item, param) =>
  120. // {
  121. // SettingPanel.UpdateArgsList = e.AnnotEventArgsList;
  122. // SettingPanel.BindMeasureSetting();
  123. // SetInfoPanelVisble(false, true);
  124. // };
  125. // MenuItem propertyItem = new MenuItem();
  126. // propertyItem.Header = "Properties";
  127. // propertyItem.Click += (item, param) =>
  128. // {
  129. // ExpandEvent?.Invoke(this, new EventArgs());
  130. // };
  131. // e.PopupMenu.Items.Add(menuItem);
  132. // e.PopupMenu.Items.Add(propertyItem);
  133. // }
  134. // break;
  135. // case CommandType.Delete:
  136. // e.DoCommand();
  137. // break;
  138. // default:
  139. // break;
  140. // }
  141. //}
  142. //private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  143. //{
  144. // if (e == null || e.IsAnnotCreateReset)
  145. // {
  146. // if (e == null)
  147. // {
  148. // measurePropertyControl?.ClearMeasurePanel();
  149. // if(PDFMeasureTool.ToolChecked()==false)
  150. // {
  151. // SetInfoPanelVisble(false, false);
  152. // }
  153. // }
  154. // return;
  155. // }
  156. // else
  157. // {
  158. // switch (e.GetAnnotTypes())
  159. // {
  160. // case AnnotArgsType.LineMeasure:
  161. // LineMeasureArgs LineArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.LineMeasure).First() as LineMeasureArgs;
  162. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  163. // measurePropertyControl.SetPropertyForMeasureCreate(LineArgs, e);
  164. // SetInfoPanelVisble(true, false);
  165. // SetMeasureInfoPanel(LineArgs.GetPDFAnnot(), LineArgs);
  166. // break;
  167. // case AnnotArgsType.PolygonMeasure:
  168. // PolygonMeasureArgs polygonArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.PolygonMeasure).First() as PolygonMeasureArgs;
  169. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  170. // measurePropertyControl.SetPropertyForMeasureCreate(polygonArgs, e);
  171. // SetInfoPanelVisble(true, false);
  172. // SetMeasureInfoPanel(polygonArgs.GetPDFAnnot(),polygonArgs);
  173. // break;
  174. // case AnnotArgsType.PolyLineMeasure:
  175. // PolyLineMeasureArgs polyLineArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.PolyLineMeasure).First() as PolyLineMeasureArgs;
  176. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  177. // measurePropertyControl.SetPropertyForMeasureCreate(polyLineArgs, e);
  178. // SetInfoPanelVisble(true, false);
  179. // SetMeasureInfoPanel(polyLineArgs.GetPDFAnnot(),polyLineArgs);
  180. // break;
  181. // }
  182. // }
  183. //}
  184. private void SetMeasureInfoPanel(CPDFAnnotation annot,AnnotParam param = null)
  185. {
  186. if (annot == null)
  187. {
  188. return;
  189. }
  190. try
  191. {
  192. if (annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  193. {
  194. CPDFLineAnnotation lineAnnot = annot as CPDFLineAnnotation;
  195. if (lineAnnot.IsMersured() && lineAnnot.Points != null && lineAnnot.Points.Count() == 2)
  196. {
  197. InfoPanel.SetMeasureInfo(lineAnnot);
  198. SetMeasureInfoType(CPDFMeasureType.CPDF_DISTANCE_MEASURE);
  199. }
  200. }
  201. if (annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE)
  202. {
  203. CPDFPolylineAnnotation polylineAnnot = annot as CPDFPolylineAnnotation;
  204. if (polylineAnnot.IsMersured() && polylineAnnot.Points != null && polylineAnnot.Points.Count() >= 2)
  205. {
  206. InfoPanel.SetMeasureInfo(polylineAnnot);
  207. SetMeasureInfoType(CPDFMeasureType.CPDF_PERIMETER_MEASURE);
  208. }
  209. }
  210. if(annot.Type== C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON)
  211. {
  212. CPDFPolygonAnnotation Annot = annot as CPDFPolygonAnnotation;
  213. CPDFAreaMeasure polygonMeasure = Annot.GetAreaMeasure();
  214. CPDFMeasureInfo measureInfo = polygonMeasure.MeasureInfo;
  215. CPDFCaptionType CaptionType = measureInfo.CaptionType;
  216. bool IsArea = false;
  217. bool IsLength = false;
  218. if ((CaptionType& CPDFCaptionType.CPDF_CAPTION_AREA)== CPDFCaptionType.CPDF_CAPTION_AREA)
  219. {
  220. IsArea = true;
  221. }
  222. if ((CaptionType & CPDFCaptionType.CPDF_CAPTION_LENGTH) == CPDFCaptionType.CPDF_CAPTION_LENGTH)
  223. {
  224. IsLength = true;
  225. }
  226. SettingPanel.ChangedCheckBoxIsChecked(IsArea, IsLength);
  227. InfoPanel.SetMeasureInfo(Annot);
  228. SetMeasureInfoType(CPDFMeasureType.CPDF_AREA_MEASURE);
  229. }
  230. }
  231. catch (Exception e)
  232. {
  233. }
  234. }
  235. private double GetMeasureRatio(string baseUnit)
  236. {
  237. if (baseUnit == CPDFMeasure.CPDF_PT)
  238. {
  239. return 1 / 72;
  240. }
  241. if (baseUnit == CPDFMeasure.CPDF_IN)
  242. {
  243. return 1;
  244. }
  245. if (baseUnit == CPDFMeasure.CPDF_MM)
  246. {
  247. return 1 / 25.4;
  248. }
  249. if (baseUnit == CPDFMeasure.CPDF_CM)
  250. {
  251. return 1 / 2.54;
  252. }
  253. if (baseUnit == CPDFMeasure.CPDF_M)
  254. {
  255. return 1 / 0.0254;
  256. }
  257. if (baseUnit == CPDFMeasure.CPDFO_KM)
  258. {
  259. return 1 / 0.0254 / 1000;
  260. }
  261. if (baseUnit == CPDFMeasure.CPDF_FT)
  262. {
  263. return 12;
  264. }
  265. if (baseUnit == CPDFMeasure.CPDF_YD)
  266. {
  267. return 36;
  268. }
  269. if (baseUnit == CPDFMeasure.CPDF_MI)
  270. {
  271. return 63360;
  272. }
  273. return 0;
  274. }
  275. public void SetSettingsControl(CPDFDisplaySettingsControl cPDFDisplaySettingsControl)
  276. {
  277. displaySettingsControl = cPDFDisplaySettingsControl;
  278. }
  279. public void ClearAllToolState()
  280. {
  281. PDFMeasureTool.ClearAllToolState();
  282. }
  283. public void ClearViewerControl()
  284. {
  285. PDFGrid.Child = null;
  286. BotaContainer.Child = null;
  287. PropertyContainer.Child = null;
  288. displaySettingsControl = null;
  289. }
  290. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  291. {
  292. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  293. {
  294. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  295. }
  296. else if (e.PropertyName == nameof(PanelState.RightPanel))
  297. {
  298. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  299. {
  300. ExpandRightPropertyPanel(measurePropertyControl, Visibility.Visible);
  301. }
  302. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  303. {
  304. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  305. }
  306. else
  307. {
  308. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  309. }
  310. }
  311. }
  312. #endregion
  313. #region Expand and collapse Panel
  314. public void ExpandRightPropertyPanel(Visibility visible)
  315. {
  316. ExpandRightPropertyPanel(measurePropertyControl, visible);
  317. }
  318. public void ExpandNullRightPropertyPanel(Visibility visible)
  319. {
  320. ExpandRightPropertyPanel(null, visible);
  321. }
  322. public void ExpandViewSettings(Visibility visible)
  323. {
  324. SetViewSettings(displaySettingsControl, visible);
  325. }
  326. private void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  327. {
  328. PropertyContainer.Width = 260;
  329. PropertyContainer.Child = propertytPanel;
  330. PropertyContainer.Visibility = visible;
  331. }
  332. private void SetViewSettings(CPDFDisplaySettingsControl displaySettingsControl, Visibility visibility)
  333. {
  334. PropertyContainer.Child = displaySettingsControl;
  335. PropertyContainer.Visibility = visibility;
  336. }
  337. public void ExpandLeftPanel(bool isExpand)
  338. {
  339. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  340. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  341. if (isExpand)
  342. {
  343. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  344. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  345. }
  346. else
  347. {
  348. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  349. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  350. }
  351. }
  352. #endregion
  353. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  354. {
  355. }
  356. private void MeasureInfoPanel_SettingClick(object sender, EventArgs e)
  357. {
  358. if (sender is MeasureInfoPanel)
  359. {
  360. switch ((sender as MeasureInfoPanel).MeasureType)
  361. {
  362. case CPDFMeasureType.CPDF_DISTANCE_MEASURE:
  363. case CPDFMeasureType.CPDF_PERIMETER_MEASURE:
  364. SettingPanel.ShowAreaAndLength(Visibility.Collapsed);
  365. break;
  366. case CPDFMeasureType.CPDF_AREA_MEASURE:
  367. SettingPanel.ShowAreaAndLength(Visibility.Visible);
  368. break;
  369. default:
  370. break;
  371. }
  372. }
  373. SettingPanel.ReturnToInfoPanel = true;
  374. SetInfoPanelVisble(false, true);
  375. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  376. PdfViewControl.GetCPDFViewer().GetDocument(), currentAnnot.Page.PageIndex, currentAnnot);
  377. SettingPanel.BindMeasureSetting(GetMeasureInfoFromParam(annotParam));
  378. }
  379. private CPDFMeasureInfo GetMeasureInfoFromParam(AnnotParam param)
  380. {
  381. if(param is LineMeasureParam lineParam)
  382. {
  383. return lineParam.measureInfo;
  384. }
  385. if (param is PolyLineMeasureParam polyLineParam)
  386. {
  387. return polyLineParam.measureInfo;
  388. }
  389. if (param is PolygonMeasureParam polygonParam)
  390. {
  391. return polygonParam.measureInfo;
  392. }
  393. return null;
  394. }
  395. private void SettingPanel_CancelEvent(object sender, EventArgs e)
  396. {
  397. SetInfoPanelVisble(SettingPanel.ReturnToInfoPanel, false);
  398. }
  399. private void SettingPanel_DoneEvent(object sender, EventArgs e)
  400. {
  401. SetInfoPanelVisble(SettingPanel.ReturnToInfoPanel, false);
  402. SettingPanel.SaveMeasureSetting(currentAnnot);
  403. }
  404. public void SetInfoPanelVisble(bool measureInfo, bool measureSetting)
  405. {
  406. InfoPanel.Visibility = measureInfo ? Visibility.Visible : Visibility.Collapsed;
  407. SettingPanel.Visibility = measureSetting ? Visibility.Visible : Visibility.Collapsed;
  408. }
  409. public void SetMeasureInfoType(CPDFMeasureType measureType)
  410. {
  411. InfoPanel?.SetMeasureType(measureType);
  412. }
  413. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  414. {
  415. this.BotaContainer.Child = botaControl;
  416. }
  417. public void SetMeasureScale(CPDFMeasureType measureType, string scale)
  418. {
  419. InfoPanel?.SetMeasureScale(measureType,scale);
  420. }
  421. }
  422. }