ViewModularContentViewModel.cs 14 KB

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