ViewModularContentViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Interop.Word;
  4. using PDF_Office.CustomControl;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.ViewModels.BOTA;
  8. using PDF_Office.Views;
  9. using PDFSettings.Settings;
  10. using Prism.Commands;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using Prism.Services.Dialogs;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Forms;
  21. namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
  22. {
  23. public class ViewModularContentViewModel : BindableBase, INavigationAware
  24. {
  25. private IRegionManager region;
  26. private IDialogService dialogs;
  27. //记录 单页 双页 书本模式
  28. private ViewMode viewmode = ViewMode.Single;
  29. public CPDFViewer PDFViewer { get; set; }
  30. public CPDFViewer SplitScreenPDFViewer { get; set; }
  31. public BottomToolContentViewModel BottomToolContentViewModel { get; set; }
  32. public ViewContentViewModel ViewContentViewModel { get; set; }
  33. public string SplitScreenViewRegionName { get; set; }
  34. public string ThemesContentName { get; set; }
  35. private Visibility splitScreenViewVisible = Visibility.Collapsed;
  36. public OpenFileInfo OpenFileInfo = null;
  37. /// <summary>
  38. /// 控制Content的显示 用于显示分屏的模块
  39. /// </summary>
  40. public Visibility SplitScreenViewVisible
  41. {
  42. get { return splitScreenViewVisible; }
  43. set
  44. {
  45. SetProperty(ref splitScreenViewVisible, value);
  46. }
  47. }
  48. private bool isContinue = true;
  49. public bool IsContinue
  50. {
  51. get { return isContinue; }
  52. set
  53. {
  54. SetProperty(ref isContinue, value);
  55. if (value)
  56. {
  57. SetModeView();
  58. }
  59. }
  60. }
  61. private bool isPagesBreak = true;
  62. public bool IsPagesBreak
  63. {
  64. get { return isPagesBreak; }
  65. set
  66. {
  67. SetProperty(ref isPagesBreak, value);
  68. }
  69. }
  70. private bool isSingleView;
  71. public bool IsSingleView
  72. {
  73. get { return isSingleView; }
  74. set
  75. {
  76. SetProperty(ref isSingleView, value);
  77. if (value)
  78. {
  79. SetModeView();
  80. }
  81. }
  82. }
  83. private bool isTwoPageView;
  84. public bool IsTwoPageView
  85. {
  86. get { return isTwoPageView; }
  87. set
  88. {
  89. SetProperty(ref isTwoPageView, value);
  90. if (value)
  91. {
  92. SetModeView();
  93. }
  94. }
  95. }
  96. private bool isBookModeView;
  97. public bool IsBookModeView
  98. {
  99. get { return isBookModeView; }
  100. set
  101. {
  102. SetProperty(ref isBookModeView, value);
  103. if (value)
  104. {
  105. SetModeView();
  106. }
  107. }
  108. }
  109. private bool rBtnFullScreenIsChecked = false;
  110. public bool IsFullScreen
  111. {
  112. get { return rBtnFullScreenIsChecked; }
  113. set
  114. {
  115. SetProperty(ref rBtnFullScreenIsChecked, value);
  116. }
  117. }
  118. public DelegateCommand<object> SplitScreenCommand { get; set; }
  119. public DelegateCommand<object> DisableCommand { get; set; }
  120. public DelegateCommand SetViewModeCommand { get; set; }
  121. public DelegateCommand<object> ContinueCommand { get; set; }
  122. public DelegateCommand<object> PagesBreakCommand { get; set; }
  123. public DelegateCommand<object> RotateCommand { get; set; }
  124. public DelegateCommand OpenFullCommand { get; set; }
  125. public ViewModularContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  126. {
  127. region = regionManager;
  128. dialogs = dialogService;
  129. //未显示时无法注册上Region名称,所以需要短暂显示
  130. //SplitScreenViewVisible = Visibility.Visible;
  131. //SplitScreenViewRegionName = RegionNames.SplitScreenViewRegionName;
  132. ThemesContentName = RegionNames.ThemesContentName;
  133. //SplitScreenViewVisible = Visibility.Collapsed;
  134. SplitScreenCommand = new DelegateCommand<object>(SplitScreenEvent);
  135. DisableCommand = new DelegateCommand<object>(DisableEvent);
  136. SetViewModeCommand = new DelegateCommand(SetModeView);
  137. ContinueCommand = new DelegateCommand<object>(ContinueEvent);
  138. PagesBreakCommand = new DelegateCommand<object>(PagesBreakEvent);
  139. RotateCommand = new DelegateCommand<object>(RotateEvent);
  140. OpenFullCommand = new DelegateCommand(OpenFullWindow);
  141. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  142. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  143. {
  144. NavigationParameters parameters = new NavigationParameters();
  145. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  146. parameters.Add(ParameterNames.ViewModularContentViewModel, this);
  147. region.RequestNavigate(RegionNames.ThemesContentName, "ThemesContent", parameters);
  148. }
  149. ));
  150. }
  151. /// <summary>
  152. /// 进入全屏模式
  153. /// </summary>
  154. private void OpenFullWindow()
  155. {
  156. DialogParameters parameters = new DialogParameters();
  157. //因为全屏模式可能需要设置特定的页面模式,所以只传文件路径,新建一个PDFview对象
  158. parameters.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  159. parameters.Add(ParameterNames.PassWord, PDFViewer.Tag == null ? "" : PDFViewer.Tag.ToString());
  160. dialogs.ShowDialog(DialogNames.FullScreenDialog, parameters, e =>
  161. {
  162. if (e.Result == ButtonResult.Cancel)
  163. {
  164. //TODO:弹窗提示打开全屏模式失败
  165. AlertsMessage alertsMessage = new AlertsMessage();
  166. alertsMessage.ShowDialog("", "Error", "OK");
  167. return;
  168. }
  169. else if (e.Result == ButtonResult.OK)
  170. {
  171. IsFullScreen = false;
  172. IsSingleView = true;
  173. }
  174. });
  175. }
  176. /// <summary>
  177. /// 旋转
  178. /// </summary>
  179. /// <param name="obj"></param>
  180. private void RotateEvent(object obj)
  181. {
  182. bool right = Convert.ToBoolean(obj);
  183. PDFViewer.RotatePage(PageRotate.Rotate90, right, PDFViewer.CurrentIndex);
  184. if (SplitScreenPDFViewer != null)
  185. {
  186. SplitScreenPDFViewer.RotatePage(PageRotate.Rotate90, right, SplitScreenPDFViewer.CurrentIndex);
  187. }
  188. //后续需要添加,缩略图旋转改变
  189. PDFViewer.UndoManager.CanSave = true;
  190. }
  191. /// <summary>
  192. /// 分页符
  193. /// </summary>
  194. /// <param name="obj"></param>
  195. private void PagesBreakEvent(object obj)
  196. {
  197. if (IsPagesBreak)
  198. {
  199. PDFViewer.SetPageSpacing(8);
  200. IsPagesBreak = true;
  201. }
  202. else
  203. {
  204. PDFViewer.SetPageSpacing(0);
  205. IsPagesBreak = false;
  206. }
  207. SaveMode(IsPagesBreak);
  208. }
  209. /// <summary>
  210. /// 设置显示模式
  211. /// </summary>
  212. private void SetModeView()
  213. {
  214. if (PDFViewer != null)
  215. {
  216. SetUpModeView(PDFViewer);
  217. }
  218. //if (App.SplitScreenPDFViewer != null)
  219. //{
  220. // SetUpModeView(App.SplitScreenPDFViewer);
  221. // SaveMode(App.SplitScreenPDFViewer.ModeView);
  222. //}
  223. SaveMode(PDFViewer.ModeView);
  224. }
  225. private void SetUpModeView(CPDFViewer pDFViewer)
  226. {
  227. if (IsContinue)
  228. {
  229. if (IsSingleView)
  230. {
  231. pDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  232. //App.SplitScreenViewMode = ViewMode.SingleContinuous;
  233. }
  234. else if (IsTwoPageView)
  235. {
  236. pDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  237. //App.SplitScreenViewMode = ViewMode.DoubleContinuous;
  238. }
  239. else
  240. {
  241. pDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  242. //App.SplitScreenViewMode = ViewMode.BookContinuous;
  243. }
  244. }
  245. else
  246. {
  247. if (IsSingleView)
  248. {
  249. pDFViewer.ChangeViewMode(ViewMode.Single);
  250. //App.SplitScreenViewMode = ViewMode.Single;
  251. }
  252. else if (IsTwoPageView)
  253. {
  254. pDFViewer.ChangeViewMode(ViewMode.Double);
  255. //App.SplitScreenViewMode = ViewMode.Double;
  256. }
  257. else
  258. {
  259. pDFViewer.ChangeViewMode(ViewMode.Book);
  260. //App.SplitScreenViewMode = ViewMode.Book;
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// 连续滚动
  266. /// </summary>
  267. /// <param name="obj"></param>
  268. private void ContinueEvent(object obj)
  269. {
  270. SetModeView();
  271. }
  272. /// <summary>
  273. /// 保存模式到本地
  274. /// </summary>
  275. /// <param name="modeView"></param>
  276. private void SaveMode(object modeView)
  277. {
  278. OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  279. if (fileInfo != null)
  280. {
  281. if (modeView is SplitMode)
  282. {
  283. fileInfo.LastSplitMode = PDFViewer.Mode;
  284. }
  285. else if (modeView is ViewMode)
  286. {
  287. fileInfo.LastViewMode = PDFViewer.ModeView;
  288. }
  289. else if (modeView is bool)
  290. {
  291. fileInfo.LastPageSpace = (bool)modeView;
  292. }
  293. SettingHelper.SetFileInfo(fileInfo);
  294. }
  295. }
  296. /// <summary>
  297. /// 分屏视图-单屏
  298. /// </summary>
  299. /// <param name="obj"></param>
  300. private void DisableEvent(object obj)
  301. {
  302. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Single);
  303. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.None;
  304. }
  305. /// <summary>
  306. /// 分屏视图-垂直
  307. /// </summary>
  308. /// <param name="obj"></param>
  309. private void SplitScreenEvent(object obj)
  310. {
  311. if (obj is System.Windows.Controls.RadioButton radioButton)
  312. {
  313. IsContinue = true;
  314. IsPagesBreak = true;
  315. switch (radioButton.Tag.ToString())
  316. {
  317. case "VerticalSplit":
  318. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Vertical);
  319. OpenFileInfo.LastSplitMode= ComPDFKitViewer.SplitMode.Vertical;
  320. break;
  321. case "HorizontalSplit":
  322. ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Horizontal);
  323. OpenFileInfo.LastSplitMode = ComPDFKitViewer.SplitMode.Horizontal;
  324. break;
  325. }
  326. }
  327. }
  328. public bool IsNavigationTarget(NavigationContext navigationContext)
  329. {
  330. return true;
  331. }
  332. public void OnNavigatedFrom(NavigationContext navigationContext)
  333. {
  334. }
  335. public void OnNavigatedTo(NavigationContext navigationContext)
  336. {
  337. CPDFViewer pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  338. BottomToolContentViewModel btnTool = navigationContext.Parameters["BottomToolContentViewModel"] as BottomToolContentViewModel;
  339. if (pdfview != null && btnTool != null)
  340. {
  341. BottomToolContentViewModel = btnTool;
  342. PDFViewer = pdfview;
  343. ViewContentViewModel = BottomToolContentViewModel.ViewContentViewModel;
  344. OpenFileInfo = BottomToolContentViewModel.OpenFileInfo;
  345. }
  346. else
  347. {
  348. return;
  349. }
  350. IsSingleView = BottomToolContentViewModel.IsSingleView;
  351. IsTwoPageView = BottomToolContentViewModel.IsDoubleView;
  352. IsBookModeView = BottomToolContentViewModel.IsBookMode;
  353. IsContinue = BottomToolContentViewModel.IsContinue;
  354. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  355. }
  356. /// <summary>
  357. /// 和底部工具栏联动
  358. /// </summary>
  359. /// <param name="sender"></param>
  360. /// <param name="e"></param>
  361. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  362. {
  363. if (e.Key == "ViewMode")
  364. {
  365. GetModeView((ViewMode)e.Value);
  366. }
  367. }
  368. private void GetModeView(ViewMode mode)
  369. {
  370. if ((int)mode % 2 == 0)
  371. {
  372. if (!IsContinue)
  373. {
  374. IsContinue = true;
  375. }
  376. }
  377. else
  378. {
  379. if (IsContinue)
  380. {
  381. IsContinue = false;
  382. }
  383. }
  384. if ((int)mode <= 2)
  385. {
  386. if (!isSingleView)
  387. {
  388. IsSingleView = true;
  389. }
  390. }
  391. else if ((int)mode <= 4)
  392. {
  393. if (!isTwoPageView)
  394. {
  395. IsTwoPageView = true;
  396. }
  397. }
  398. else
  399. {
  400. if (!isBookModeView)
  401. {
  402. IsBookModeView = true;
  403. }
  404. }
  405. }
  406. }
  407. }