ViewModularContentViewModel.cs 14 KB

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