ViewModularContentViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. //记录 单页 双页 书本模式
  139. private ViewMode viewmode = ViewMode.Single;
  140. public CPDFViewer PDFViewer { get; set; }
  141. public CPDFViewer SplitScreenPDFViewer { get; set; }
  142. public BottomToolContentViewModel BottomToolContentViewModel { get; set; }
  143. public ViewContentViewModel ViewContentViewModel { get; set; }
  144. public string SplitScreenViewRegionName { get; set; }
  145. public string ThemesContentName { get; set; }
  146. private Visibility splitScreenViewVisible = Visibility.Collapsed;
  147. public OpenFileInfo OpenFileInfo = null;
  148. private IEventAggregator events;
  149. /// <summary>
  150. /// 用于区分事件的唯一码
  151. /// </summary>
  152. private string unicode;
  153. /// <summary>
  154. /// 控制Content的显示 用于显示分屏的模块
  155. /// </summary>
  156. public Visibility SplitScreenViewVisible
  157. {
  158. get { return splitScreenViewVisible; }
  159. set
  160. {
  161. SetProperty(ref splitScreenViewVisible, value);
  162. }
  163. }
  164. private bool isContinue = true;
  165. public bool IsContinue
  166. {
  167. get { return isContinue; }
  168. set
  169. {
  170. SetProperty(ref isContinue, value);
  171. if (value)
  172. {
  173. SetModeView();
  174. }
  175. }
  176. }
  177. private bool isPagesBreak = true;
  178. public bool IsPagesBreak
  179. {
  180. get { return isPagesBreak; }
  181. set
  182. {
  183. SetProperty(ref isPagesBreak, value);
  184. }
  185. }
  186. private bool isSingleView;
  187. public bool IsSingleView
  188. {
  189. get { return isSingleView; }
  190. set
  191. {
  192. SetProperty(ref isSingleView, value);
  193. if (value)
  194. {
  195. SetModeView();
  196. }
  197. }
  198. }
  199. private bool isTwoPageView;
  200. public bool IsTwoPageView
  201. {
  202. get { return isTwoPageView; }
  203. set
  204. {
  205. SetProperty(ref isTwoPageView, value);
  206. if (value)
  207. {
  208. SetModeView();
  209. }
  210. }
  211. }
  212. private bool isBookModeView;
  213. public bool IsBookModeView
  214. {
  215. get { return isBookModeView; }
  216. set
  217. {
  218. SetProperty(ref isBookModeView, value);
  219. if (value)
  220. {
  221. SetModeView();
  222. }
  223. }
  224. }
  225. private bool rBtnFullScreenIsChecked = false;
  226. public bool IsFullScreen
  227. {
  228. get { return rBtnFullScreenIsChecked; }
  229. set
  230. {
  231. SetProperty(ref rBtnFullScreenIsChecked, value);
  232. }
  233. }
  234. public DelegateCommand<object> SplitScreenCommand { get; set; }
  235. public DelegateCommand<object> DisableCommand { get; set; }
  236. public DelegateCommand SetViewModeCommand { get; set; }
  237. public DelegateCommand<object> ContinueCommand { get; set; }
  238. public DelegateCommand<object> PagesBreakCommand { get; set; }
  239. public DelegateCommand<object> RotateCommand { get; set; }
  240. public DelegateCommand OpenFullCommand { get; set; }
  241. public ViewModularContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  242. {
  243. InitString();
  244. region = regionManager;
  245. dialogs = dialogService;
  246. events = eventAggregator;
  247. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  248. //未显示时无法注册上Region名称,所以需要短暂显示
  249. //SplitScreenViewVisible = Visibility.Visible;
  250. //SplitScreenViewRegionName = RegionNames.SplitScreenViewRegionName;
  251. ThemesContentName = RegionNames.ThemesContentName;
  252. //SplitScreenViewVisible = Visibility.Collapsed;
  253. SplitScreenCommand = new DelegateCommand<object>(SplitScreenEvent);
  254. DisableCommand = new DelegateCommand<object>(DisableEvent);
  255. SetViewModeCommand = new DelegateCommand(SetModeView);
  256. ContinueCommand = new DelegateCommand<object>(ContinueEvent);
  257. PagesBreakCommand = new DelegateCommand<object>(PagesBreakEvent);
  258. RotateCommand = new DelegateCommand<object>(RotateEvent);
  259. OpenFullCommand = new DelegateCommand(OpenFullWindow);
  260. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  261. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  262. {
  263. NavigationParameters parameters = new NavigationParameters();
  264. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  265. parameters.Add(ParameterNames.ViewModularContentViewModel, this);
  266. region.RequestNavigate(RegionNames.ThemesContentName, "ThemesContent", parameters);
  267. }
  268. ));
  269. }
  270. /// <summary>
  271. /// 进入全屏模式
  272. /// </summary>
  273. private void OpenFullWindow()
  274. {
  275. DialogParameters parameters = new DialogParameters();
  276. //因为全屏模式可能需要设置特定的页面模式,所以只传文件路径,新建一个PDFview对象
  277. parameters.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  278. parameters.Add(ParameterNames.PassWord, PDFViewer.Tag == null ? "" : PDFViewer.Tag.ToString());
  279. dialogs.ShowDialog(DialogNames.FullScreenDialog, parameters, e =>
  280. {
  281. if (e.Result == ButtonResult.Cancel)
  282. {
  283. //TODO:弹窗提示打开全屏模式失败
  284. AlertsMessage alertsMessage = new AlertsMessage();
  285. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("OperationFailWarning"), App.ServiceLoader.GetString("Text_ok"));
  286. return;
  287. }
  288. else if (e.Result == ButtonResult.OK)
  289. {
  290. IsFullScreen = false;
  291. IsSingleView = true;
  292. }
  293. });
  294. }
  295. /// <summary>
  296. /// 旋转
  297. /// </summary>
  298. /// <param name="obj"></param>
  299. private void RotateEvent(object obj)
  300. {
  301. bool right = Convert.ToBoolean(obj);
  302. PDFViewer.RotatePage(PageRotate.Rotate90, right, PDFViewer.CurrentIndex);
  303. if (SplitScreenPDFViewer != null)
  304. {
  305. SplitScreenPDFViewer.RotatePage(PageRotate.Rotate90, right, SplitScreenPDFViewer.CurrentIndex);
  306. }
  307. PDFViewer.UndoManager.CanSave = true;
  308. events.GetEvent<ViewContentEvent>().Publish(new ViewContentEventArgs() { type = right ? EventType.RightRotate : EventType.LeftRotate, Unicode = unicode });
  309. //后续需要添加,缩略图旋转改变
  310. if (ViewContentViewModel.OpenBOTA == true)
  311. {
  312. UpdateTabItemAnnotation();
  313. }
  314. }
  315. private void UpdateTabItemAnnotation()
  316. {
  317. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  318. if (isTabItemAnnotation == true && bOTAContent.TabItemAnnotation.IsSelected == true)
  319. {
  320. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  321. if (viewModel != null)
  322. {
  323. if (viewModel.AnnotationListItems.Count > 0)
  324. {
  325. var list = viewModel.AnnotationListItems.Where(q => q.AnnotHandlerEventArgs is FreehandAnnotArgs).ToList();
  326. if (list.Count() > 0)
  327. {
  328. for (int i = 0; i < list.Count(); i++)
  329. {
  330. var item = list[i];
  331. viewModel.UpdateModifiedAnnot(item.PageIndex, item.AnnotIndex);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel, out AnnotationContent annotation)
  339. {
  340. AnnotationContentViewModel viewModel = null;
  341. annotation = null;
  342. if (bOTAContentViewModel != null)
  343. {
  344. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  345. {
  346. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  347. foreach (var item in views)
  348. {
  349. if (item is AnnotationContent annotation1)
  350. {
  351. annotation = annotation1;
  352. viewModel = annotation1.DataContext as AnnotationContentViewModel;
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. return viewModel;
  359. }
  360. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  361. {
  362. bool isTabItem = false;
  363. bOTAContentViewModel = null;
  364. bOTAContent = null;
  365. if (region.Regions.ContainsRegionWithName(ViewContentViewModel.BOTARegionName))
  366. {
  367. var views = region.Regions[ViewContentViewModel.BOTARegionName].Views;
  368. var model = views.FirstOrDefault(q => q is BOTAContent);
  369. if (model is BOTAContent bOTAContent1)
  370. {
  371. bOTAContent = bOTAContent1;
  372. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  373. if (bOTAContentViewModel.CurrentBar == tabItemText)
  374. {
  375. isTabItem = true;
  376. }
  377. }
  378. }
  379. return isTabItem;
  380. }
  381. /// <summary>
  382. /// 分页符
  383. /// </summary>
  384. /// <param name="obj"></param>
  385. private void PagesBreakEvent(object obj)
  386. {
  387. if (IsPagesBreak)
  388. {
  389. PDFViewer.SetPageSpacing(8);
  390. IsPagesBreak = true;
  391. }
  392. else
  393. {
  394. PDFViewer.SetPageSpacing(0);
  395. IsPagesBreak = false;
  396. }
  397. SaveMode(IsPagesBreak);
  398. }
  399. /// <summary>
  400. /// 设置显示模式
  401. /// </summary>
  402. private void SetModeView()
  403. {
  404. if (PDFViewer != null)
  405. {
  406. SetUpModeView(PDFViewer);
  407. }
  408. //if (App.SplitScreenPDFViewer != null)
  409. //{
  410. // SetUpModeView(App.SplitScreenPDFViewer);
  411. // SaveMode(App.SplitScreenPDFViewer.ModeView);
  412. //}
  413. SaveMode(PDFViewer.ModeView);
  414. }
  415. private void SetUpModeView(CPDFViewer pDFViewer)
  416. {
  417. if (IsContinue)
  418. {
  419. if (IsSingleView)
  420. {
  421. pDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  422. //App.SplitScreenViewMode = ViewMode.SingleContinuous;
  423. }
  424. else if (IsTwoPageView)
  425. {
  426. pDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  427. //App.SplitScreenViewMode = ViewMode.DoubleContinuous;
  428. }
  429. else
  430. {
  431. pDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  432. //App.SplitScreenViewMode = ViewMode.BookContinuous;
  433. }
  434. }
  435. else
  436. {
  437. if (IsSingleView)
  438. {
  439. pDFViewer.ChangeViewMode(ViewMode.Single);
  440. //App.SplitScreenViewMode = ViewMode.Single;
  441. }
  442. else if (IsTwoPageView)
  443. {
  444. pDFViewer.ChangeViewMode(ViewMode.Double);
  445. //App.SplitScreenViewMode = ViewMode.Double;
  446. }
  447. else
  448. {
  449. pDFViewer.ChangeViewMode(ViewMode.Book);
  450. //App.SplitScreenViewMode = ViewMode.Book;
  451. }
  452. }
  453. }
  454. /// <summary>
  455. /// 连续滚动
  456. /// </summary>
  457. /// <param name="obj"></param>
  458. private void ContinueEvent(object obj)
  459. {
  460. SetModeView();
  461. }
  462. /// <summary>
  463. /// 保存模式到本地
  464. /// </summary>
  465. /// <param name="modeView"></param>
  466. private void SaveMode(object modeView)
  467. {
  468. OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  469. if (fileInfo != null)
  470. {
  471. if (modeView is ComPDFKitViewer.SplitMode)
  472. {
  473. fileInfo.LastSplitMode = PDFViewer.Mode;
  474. }
  475. else if (modeView is ViewMode)
  476. {
  477. fileInfo.LastViewMode = PDFViewer.ModeView;
  478. }
  479. else if (modeView is bool)
  480. {
  481. fileInfo.LastPageSpace = (bool)modeView;
  482. }
  483. SettingHelper.SetFileInfo(fileInfo);
  484. }
  485. }
  486. /// <summary>
  487. /// 分屏视图-单屏
  488. /// </summary>
  489. /// <param name="obj"></param>
  490. private void DisableEvent(object obj)
  491. {
  492. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Single);
  493. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.None;
  494. }
  495. /// <summary>
  496. /// 分屏视图-垂直
  497. /// </summary>
  498. /// <param name="obj"></param>
  499. private void SplitScreenEvent(object obj)
  500. {
  501. if (obj is System.Windows.Controls.RadioButton radioButton)
  502. {
  503. IsContinue = true;
  504. IsPagesBreak = true;
  505. switch (radioButton.Tag.ToString())
  506. {
  507. case "VerticalSplit":
  508. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Vertical);
  509. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.Vertical;
  510. break;
  511. case "HorizontalSplit":
  512. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Horizontal);
  513. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.Horizontal;
  514. break;
  515. }
  516. }
  517. }
  518. public bool IsNavigationTarget(NavigationContext navigationContext)
  519. {
  520. return true;
  521. }
  522. public void OnNavigatedFrom(NavigationContext navigationContext)
  523. {
  524. if (BottomToolContentViewModel != null)
  525. {
  526. IsSingleView = BottomToolContentViewModel.IsSingleView;
  527. IsTwoPageView = BottomToolContentViewModel.IsDoubleView;
  528. IsBookModeView = BottomToolContentViewModel.IsBookMode;
  529. IsContinue = BottomToolContentViewModel.IsContinue;
  530. }
  531. }
  532. public void OnNavigatedTo(NavigationContext navigationContext)
  533. {
  534. CPDFViewer pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  535. BottomToolContentViewModel btnTool = navigationContext.Parameters["BottomToolContentViewModel"] as BottomToolContentViewModel;
  536. if (pdfview != null && btnTool != null)
  537. {
  538. BottomToolContentViewModel = btnTool;
  539. PDFViewer = pdfview;
  540. ViewContentViewModel = BottomToolContentViewModel.ViewContentViewModel;
  541. OpenFileInfo = BottomToolContentViewModel.OpenFileInfo;
  542. }
  543. else
  544. {
  545. return;
  546. }
  547. IsSingleView = BottomToolContentViewModel.IsSingleView;
  548. IsTwoPageView = BottomToolContentViewModel.IsDoubleView;
  549. IsBookModeView = BottomToolContentViewModel.IsBookMode;
  550. IsContinue = BottomToolContentViewModel.IsContinue;
  551. PDFViewer.InfoChanged -= PDFViewer_InfoChanged;
  552. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  553. }
  554. /// <summary>
  555. /// 和底部工具栏联动
  556. /// </summary>
  557. /// <param name="sender"></param>
  558. /// <param name="e"></param>
  559. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  560. {
  561. if (e.Key == "ViewMode")
  562. {
  563. GetModeView((ViewMode)e.Value);
  564. }
  565. }
  566. private void GetModeView(ViewMode mode)
  567. {
  568. if ((int)mode % 2 == 0)
  569. {
  570. if (!IsContinue)
  571. {
  572. IsContinue = true;
  573. }
  574. }
  575. else
  576. {
  577. if (IsContinue)
  578. {
  579. IsContinue = false;
  580. }
  581. }
  582. if ((int)mode <= 2)
  583. {
  584. if (!isSingleView)
  585. {
  586. IsSingleView = true;
  587. }
  588. }
  589. else if ((int)mode <= 4)
  590. {
  591. if (!isTwoPageView)
  592. {
  593. IsTwoPageView = true;
  594. }
  595. }
  596. else
  597. {
  598. if (!isBookModeView)
  599. {
  600. IsBookModeView = true;
  601. }
  602. }
  603. }
  604. }
  605. }