ViewModularContentViewModel.cs 21 KB

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