ViewModularContentViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Office.Interop.Word;
  5. using PDF_Master.CustomControl;
  6. using PDF_Master.EventAggregators;
  7. using PDF_Master.Helper;
  8. using PDF_Master.Model;
  9. using PDF_Master.Model.BOTA;
  10. using PDF_Master.ViewModels.BOTA;
  11. using PDF_Master.Views;
  12. using PDF_Master.Views.BOTA;
  13. using PDFSettings;
  14. using Prism.Commands;
  15. using Prism.Events;
  16. using Prism.Mvvm;
  17. using Prism.Regions;
  18. using Prism.Services.Dialogs;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Collections.ObjectModel;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using System.Windows;
  26. using System.Windows.Forms;
  27. namespace PDF_Master.ViewModels.PropertyPanel.ViewModular
  28. {
  29. public class ViewModularContentViewModel : BindableBase, INavigationAware
  30. {
  31. #region 文案
  32. private string T_title;
  33. public string T_Title
  34. {
  35. get { return T_title; }
  36. set
  37. {
  38. SetProperty(ref T_title, value);
  39. }
  40. }
  41. private string T_modeTitle;
  42. public string T_ModeTitle
  43. {
  44. get { return T_modeTitle; }
  45. set
  46. {
  47. SetProperty(ref T_modeTitle, value);
  48. }
  49. }
  50. private string T_single;
  51. public string T_Single
  52. {
  53. get { return T_single; }
  54. set
  55. {
  56. SetProperty(ref T_single, value);
  57. }
  58. }
  59. private string T_two;
  60. public string T_Two
  61. {
  62. get { return T_two; }
  63. set
  64. {
  65. SetProperty(ref T_two, value);
  66. }
  67. }
  68. private string T_book;
  69. public string T_Book
  70. {
  71. get { return T_book; }
  72. set
  73. {
  74. SetProperty(ref T_book, value);
  75. }
  76. }
  77. private string T_read;
  78. public string T_Read
  79. {
  80. get { return T_read; }
  81. set
  82. {
  83. SetProperty(ref T_read, value);
  84. }
  85. }
  86. private string T_full;
  87. public string T_Full
  88. {
  89. get { return T_full; }
  90. set
  91. {
  92. SetProperty(ref T_full, value);
  93. }
  94. }
  95. private string T_continuousScroll;
  96. public string T_ContinuousScroll
  97. {
  98. get { return T_continuousScroll; }
  99. set
  100. {
  101. SetProperty(ref T_continuousScroll, value);
  102. }
  103. }
  104. private string T_pageBreak;
  105. public string T_PageBreak
  106. {
  107. get { return T_pageBreak; }
  108. set
  109. {
  110. SetProperty(ref T_pageBreak, value);
  111. }
  112. }
  113. private string T_rotateTitle;
  114. public string T_RotateTitle
  115. {
  116. get { return T_rotateTitle; }
  117. set
  118. {
  119. SetProperty(ref T_rotateTitle, value);
  120. }
  121. }
  122. private void InitString()
  123. {
  124. T_Title = App.MainPageLoader.GetString("ViewProperties_Title");
  125. T_ModeTitle = App.MainPageLoader.GetString("ViewProperties_Mode");
  126. T_Single = App.MainPageLoader.GetString("ViewProperties_Single");
  127. T_Two = App.MainPageLoader.GetString("ViewProperties_Two");
  128. T_Book = App.MainPageLoader.GetString("ViewProperties_Book");
  129. T_Read = App.MainPageLoader.GetString("ViewProperties_Read");
  130. T_Full = App.MainPageLoader.GetString("ViewProperties_Full");
  131. T_ContinuousScroll = App.MainPageLoader.GetString("ViewProperties_ContinuousScroll");
  132. T_PageBreak = App.MainPageLoader.GetString("ViewProperties_PageBreak");
  133. T_RotateTitle = App.MainPageLoader.GetString("ViewProperties_RotateTitle");
  134. }
  135. #endregion 文案
  136. private IRegionManager region;
  137. private IDialogService dialogs;
  138. public CPDFViewer PDFViewer { get; set; }
  139. public CPDFViewer SplitScreenPDFViewer { get; set; }
  140. public BottomToolContentViewModel BottomToolContentViewModel { get; set; }
  141. public ViewContentViewModel ViewContentViewModel { get; set; }
  142. public string SplitScreenViewRegionName { get; set; }
  143. public string ThemesContentName { get; set; }
  144. private Visibility splitScreenViewVisible = Visibility.Collapsed;
  145. public OpenFileInfo OpenFileInfo = null;
  146. private IEventAggregator events;
  147. /// <summary>
  148. /// 用于区分事件的唯一码
  149. /// </summary>
  150. private string unicode;
  151. /// <summary>
  152. /// 控制Content的显示 用于显示分屏的模块
  153. /// </summary>
  154. public Visibility SplitScreenViewVisible
  155. {
  156. get { return splitScreenViewVisible; }
  157. set
  158. {
  159. SetProperty(ref splitScreenViewVisible, value);
  160. }
  161. }
  162. private bool isContinue = true;
  163. public bool IsContinue
  164. {
  165. get { return isContinue; }
  166. set
  167. {
  168. SetProperty(ref isContinue, value);
  169. if (value)
  170. {
  171. SetModeView();
  172. }
  173. }
  174. }
  175. private bool isPagesBreak = true;
  176. public bool IsPagesBreak
  177. {
  178. get { return isPagesBreak; }
  179. set
  180. {
  181. SetProperty(ref isPagesBreak, value);
  182. }
  183. }
  184. private bool isSingleView;
  185. public bool IsSingleView
  186. {
  187. get { return isSingleView; }
  188. set
  189. {
  190. SetProperty(ref isSingleView, value);
  191. if (value)
  192. {
  193. SetModeView();
  194. }
  195. }
  196. }
  197. private bool isTwoPageView;
  198. public bool IsTwoPageView
  199. {
  200. get { return isTwoPageView; }
  201. set
  202. {
  203. SetProperty(ref isTwoPageView, value);
  204. if (value)
  205. {
  206. SetModeView();
  207. }
  208. }
  209. }
  210. private bool isBookModeView;
  211. public bool IsBookModeView
  212. {
  213. get { return isBookModeView; }
  214. set
  215. {
  216. SetProperty(ref isBookModeView, value);
  217. if (value)
  218. {
  219. SetModeView();
  220. }
  221. }
  222. }
  223. private bool rBtnFullScreenIsChecked = false;
  224. public bool IsFullScreen
  225. {
  226. get { return rBtnFullScreenIsChecked; }
  227. set
  228. {
  229. SetProperty(ref rBtnFullScreenIsChecked, value);
  230. }
  231. }
  232. public DelegateCommand<object> SplitScreenCommand { get; set; }
  233. public DelegateCommand<object> DisableCommand { get; set; }
  234. public DelegateCommand SetViewModeCommand { get; set; }
  235. public DelegateCommand<object> ContinueCommand { get; set; }
  236. public DelegateCommand<object> PagesBreakCommand { get; set; }
  237. public DelegateCommand<object> RotateCommand { get; set; }
  238. public DelegateCommand OpenFullCommand { get; set; }
  239. public ViewModularContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  240. {
  241. InitString();
  242. region = regionManager;
  243. dialogs = dialogService;
  244. events = eventAggregator;
  245. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  246. //未显示时无法注册上Region名称,所以需要短暂显示
  247. //SplitScreenViewVisible = Visibility.Visible;
  248. //SplitScreenViewRegionName = RegionNames.SplitScreenViewRegionName;
  249. ThemesContentName = RegionNames.ThemesContentName;
  250. //SplitScreenViewVisible = Visibility.Collapsed;
  251. SplitScreenCommand = new DelegateCommand<object>(SplitScreenEvent);
  252. DisableCommand = new DelegateCommand<object>(DisableEvent);
  253. SetViewModeCommand = new DelegateCommand(SetModeView);
  254. ContinueCommand = new DelegateCommand<object>(ContinueEvent);
  255. PagesBreakCommand = new DelegateCommand<object>(PagesBreakEvent);
  256. RotateCommand = new DelegateCommand<object>(RotateEvent);
  257. OpenFullCommand = new DelegateCommand(OpenFullWindow);
  258. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  259. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  260. {
  261. NavigationParameters parameters = new NavigationParameters();
  262. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  263. parameters.Add(ParameterNames.ViewModularContentViewModel, this);
  264. region.RequestNavigate(RegionNames.ThemesContentName, "ThemesContent", parameters);
  265. }
  266. ));
  267. }
  268. /// <summary>
  269. /// 进入全屏模式
  270. /// </summary>
  271. private void OpenFullWindow()
  272. {
  273. DialogParameters parameters = new DialogParameters();
  274. //因为全屏模式可能需要设置特定的页面模式,所以只传文件路径,新建一个PDFview对象
  275. parameters.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  276. parameters.Add(ParameterNames.PassWord, PDFViewer.Tag == null ? "" : PDFViewer.Tag.ToString());
  277. dialogs.ShowDialog(DialogNames.FullScreenDialog, parameters, e =>
  278. {
  279. if (e.Result == ButtonResult.Cancel)
  280. {
  281. //TODO:弹窗提示打开全屏模式失败
  282. AlertsMessage alertsMessage = new AlertsMessage();
  283. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("OperationFailWarning"), App.ServiceLoader.GetString("Text_ok"));
  284. return;
  285. }
  286. else if (e.Result == ButtonResult.OK)
  287. {
  288. IsFullScreen = false;
  289. IsSingleView = true;
  290. }
  291. });
  292. }
  293. /// <summary>
  294. /// 旋转
  295. /// </summary>
  296. /// <param name="obj"></param>
  297. private void RotateEvent(object obj)
  298. {
  299. bool right = Convert.ToBoolean(obj);
  300. PDFViewer.RotatePage(PageRotate.Rotate90, right, PDFViewer.CurrentIndex);
  301. if (SplitScreenPDFViewer != null)
  302. {
  303. SplitScreenPDFViewer.RotatePage(PageRotate.Rotate90, right, SplitScreenPDFViewer.CurrentIndex);
  304. }
  305. PDFViewer.UndoManager.CanSave = true;
  306. events.GetEvent<ViewContentEvent>().Publish(new ViewContentEventArgs() { type = right ? EventType.RightRotate : EventType.LeftRotate, Unicode = unicode });
  307. //后续需要添加,缩略图旋转改变
  308. if (ViewContentViewModel.OpenBOTA == true)
  309. {
  310. UpdateTabItemAnnotation();
  311. }
  312. }
  313. private void UpdateTabItemAnnotation()
  314. {
  315. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  316. if (isTabItemAnnotation == true && bOTAContent.TabItemAnnotation.IsSelected == true)
  317. {
  318. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  319. if (viewModel != null)
  320. {
  321. if (viewModel.AnnotationListItems.Count > 0)
  322. {
  323. var list = viewModel.AnnotationListItems.Where(q => q.AnnotHandlerEventArgs is FreehandAnnotArgs).ToList();
  324. if (list.Count() > 0)
  325. {
  326. for (int i = 0; i < list.Count(); i++)
  327. {
  328. var item = list[i];
  329. viewModel.UpdateModifiedAnnot(item.PageIndex, item.AnnotIndex);
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }
  336. private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel, out AnnotationContent annotation)
  337. {
  338. AnnotationContentViewModel viewModel = null;
  339. annotation = null;
  340. if (bOTAContentViewModel != null)
  341. {
  342. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  343. {
  344. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  345. foreach (var item in views)
  346. {
  347. if (item is AnnotationContent annotation1)
  348. {
  349. annotation = annotation1;
  350. viewModel = annotation1.DataContext as AnnotationContentViewModel;
  351. break;
  352. }
  353. }
  354. }
  355. }
  356. return viewModel;
  357. }
  358. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  359. {
  360. bool isTabItem = false;
  361. bOTAContentViewModel = null;
  362. bOTAContent = null;
  363. if (region.Regions.ContainsRegionWithName(ViewContentViewModel.BOTARegionName))
  364. {
  365. var views = region.Regions[ViewContentViewModel.BOTARegionName].Views;
  366. var model = views.FirstOrDefault(q => q is BOTAContent);
  367. if (model is BOTAContent bOTAContent1)
  368. {
  369. bOTAContent = bOTAContent1;
  370. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  371. if (bOTAContentViewModel.CurrentBar == tabItemText)
  372. {
  373. isTabItem = true;
  374. }
  375. }
  376. }
  377. return isTabItem;
  378. }
  379. /// <summary>
  380. /// 分页符
  381. /// </summary>
  382. /// <param name="obj"></param>
  383. private void PagesBreakEvent(object obj)
  384. {
  385. if (IsPagesBreak)
  386. {
  387. PDFViewer.SetPageSpacing(8);
  388. IsPagesBreak = true;
  389. }
  390. else
  391. {
  392. PDFViewer.SetPageSpacing(0);
  393. IsPagesBreak = false;
  394. }
  395. SaveMode(IsPagesBreak);
  396. }
  397. /// <summary>
  398. /// 设置显示模式
  399. /// </summary>
  400. private void SetModeView()
  401. {
  402. if (PDFViewer != null)
  403. {
  404. SetUpModeView(PDFViewer);
  405. }
  406. //if (App.SplitScreenPDFViewer != null)
  407. //{
  408. // SetUpModeView(App.SplitScreenPDFViewer);
  409. // SaveMode(App.SplitScreenPDFViewer.ModeView);
  410. //}
  411. SaveMode(PDFViewer.ModeView);
  412. }
  413. private void SetUpModeView(CPDFViewer pDFViewer)
  414. {
  415. if (IsContinue)
  416. {
  417. if (IsSingleView)
  418. {
  419. pDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  420. //App.SplitScreenViewMode = ViewMode.SingleContinuous;
  421. }
  422. else if (IsTwoPageView)
  423. {
  424. pDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  425. //App.SplitScreenViewMode = ViewMode.DoubleContinuous;
  426. }
  427. else
  428. {
  429. pDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  430. //App.SplitScreenViewMode = ViewMode.BookContinuous;
  431. }
  432. }
  433. else
  434. {
  435. if (IsSingleView)
  436. {
  437. pDFViewer.ChangeViewMode(ViewMode.Single);
  438. //App.SplitScreenViewMode = ViewMode.Single;
  439. }
  440. else if (IsTwoPageView)
  441. {
  442. pDFViewer.ChangeViewMode(ViewMode.Double);
  443. //App.SplitScreenViewMode = ViewMode.Double;
  444. }
  445. else
  446. {
  447. pDFViewer.ChangeViewMode(ViewMode.Book);
  448. //App.SplitScreenViewMode = ViewMode.Book;
  449. }
  450. }
  451. }
  452. /// <summary>
  453. /// 连续滚动
  454. /// </summary>
  455. /// <param name="obj"></param>
  456. private void ContinueEvent(object obj)
  457. {
  458. SetModeView();
  459. }
  460. /// <summary>
  461. /// 保存模式到本地
  462. /// </summary>
  463. /// <param name="modeView"></param>
  464. private void SaveMode(object modeView)
  465. {
  466. OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  467. if (fileInfo != null)
  468. {
  469. if (modeView is ComPDFKitViewer.SplitMode)
  470. {
  471. fileInfo.LastSplitMode = PDFViewer.Mode;
  472. }
  473. else if (modeView is ViewMode)
  474. {
  475. fileInfo.LastViewMode = PDFViewer.ModeView;
  476. }
  477. else if (modeView is bool)
  478. {
  479. fileInfo.LastPageSpace = (bool)modeView;
  480. }
  481. SettingHelper.SetFileInfo(fileInfo);
  482. }
  483. }
  484. /// <summary>
  485. /// 分屏视图-单屏
  486. /// </summary>
  487. /// <param name="obj"></param>
  488. private void DisableEvent(object obj)
  489. {
  490. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Single);
  491. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.None;
  492. }
  493. /// <summary>
  494. /// 分屏视图-垂直
  495. /// </summary>
  496. /// <param name="obj"></param>
  497. private void SplitScreenEvent(object obj)
  498. {
  499. if (obj is System.Windows.Controls.RadioButton radioButton)
  500. {
  501. IsContinue = true;
  502. IsPagesBreak = true;
  503. switch (radioButton.Tag.ToString())
  504. {
  505. case "VerticalSplit":
  506. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Vertical);
  507. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.Vertical;
  508. break;
  509. case "HorizontalSplit":
  510. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Horizontal);
  511. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.Horizontal;
  512. break;
  513. }
  514. }
  515. }
  516. public bool IsNavigationTarget(NavigationContext navigationContext)
  517. {
  518. return true;
  519. }
  520. public void OnNavigatedFrom(NavigationContext navigationContext)
  521. {
  522. if (BottomToolContentViewModel != null)
  523. {
  524. IsSingleView = BottomToolContentViewModel.IsSingleView;
  525. IsTwoPageView = BottomToolContentViewModel.IsDoubleView;
  526. IsBookModeView = BottomToolContentViewModel.IsBookMode;
  527. IsContinue = BottomToolContentViewModel.IsContinue;
  528. }
  529. }
  530. public void OnNavigatedTo(NavigationContext navigationContext)
  531. {
  532. CPDFViewer pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  533. BottomToolContentViewModel btnTool = navigationContext.Parameters["BottomToolContentViewModel"] as BottomToolContentViewModel;
  534. if (pdfview != null && btnTool != null)
  535. {
  536. BottomToolContentViewModel = btnTool;
  537. PDFViewer = pdfview;
  538. ViewContentViewModel = BottomToolContentViewModel.ViewContentViewModel;
  539. OpenFileInfo = BottomToolContentViewModel.OpenFileInfo;
  540. }
  541. else
  542. {
  543. return;
  544. }
  545. IsSingleView = BottomToolContentViewModel.IsSingleView;
  546. IsTwoPageView = BottomToolContentViewModel.IsDoubleView;
  547. IsBookModeView = BottomToolContentViewModel.IsBookMode;
  548. IsContinue = BottomToolContentViewModel.IsContinue;
  549. PDFViewer.InfoChanged -= PDFViewer_InfoChanged;
  550. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  551. }
  552. /// <summary>
  553. /// 和底部工具栏联动
  554. /// </summary>
  555. /// <param name="sender"></param>
  556. /// <param name="e"></param>
  557. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  558. {
  559. if (e.Key == "ViewMode")
  560. {
  561. GetModeView((ViewMode)e.Value);
  562. }
  563. }
  564. private void GetModeView(ViewMode mode)
  565. {
  566. if ((int)mode % 2 == 0)
  567. {
  568. if (!IsContinue)
  569. {
  570. IsContinue = true;
  571. }
  572. }
  573. else
  574. {
  575. if (IsContinue)
  576. {
  577. IsContinue = false;
  578. }
  579. }
  580. if ((int)mode <= 2)
  581. {
  582. if (!isSingleView)
  583. {
  584. IsSingleView = true;
  585. }
  586. }
  587. else if ((int)mode <= 4)
  588. {
  589. if (!isTwoPageView)
  590. {
  591. IsTwoPageView = true;
  592. }
  593. }
  594. else
  595. {
  596. if (!isBookModeView)
  597. {
  598. IsBookModeView = true;
  599. }
  600. }
  601. }
  602. }
  603. }