BottomToolContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using Prism.Mvvm;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using ComPDFKitViewer.PdfViewer;
  8. using Prism.Commands;
  9. using Prism.Regions;
  10. using System.Windows;
  11. using PDF_Office.Views;
  12. using ComPDFKitViewer;
  13. using System.Windows.Input;
  14. using System.Windows.Controls;
  15. using Prism.Services.Dialogs;
  16. using PDF_Office.CustomControl;
  17. using PDF_Office.Model;
  18. namespace PDF_Office.ViewModels
  19. {
  20. public class BottomToolContentViewModel : BindableBase, INavigationAware
  21. {
  22. public DelegateCommand Load { get; set; }
  23. public DelegateCommand FirstPageCommand { get; set; }
  24. public DelegateCommand LastPageCommand { get; set; }
  25. public DelegateCommand PrePageCommand { get; set; }
  26. public DelegateCommand NextPageCommand { get; set; }
  27. public DelegateCommand MyPropertyCommand { get; set; }
  28. public DelegateCommand FullScreenCommand { get; set; }
  29. public DelegateCommand ReadModeCommand { get; set; }
  30. public DelegateCommand OpenViewCommand { get; set; }
  31. public DelegateCommand ZoomInCommand { get; set; }
  32. public DelegateCommand ZoomOutCommand { get; set; }
  33. public DelegateCommand SetViewModeCommand { get; set; }
  34. public DelegateCommand OpenFullCommand { get; set; }
  35. public DelegateCommand<object> PageTextKeyDownCommand { get; set; }
  36. public DelegateCommand<object> PageTextPreviewKeyDownCommand { get; set; }
  37. public DelegateCommand<object> SelectionChangedCommand { get; set; }
  38. public DelegateCommand<object> ShowViewModularCommand { get; set; }
  39. private bool isShowViewModular = false;
  40. /// <summary>
  41. /// 属性栏是否展开
  42. /// </summary>
  43. public bool IsShowViewModular
  44. {
  45. get { return isShowViewModular; }
  46. set
  47. {
  48. SetProperty(ref isShowViewModular, value);
  49. }
  50. }
  51. private bool isSingleView;
  52. public bool IsSingleView
  53. {
  54. get { return isSingleView; }
  55. set
  56. {
  57. SetProperty(ref isSingleView, value);
  58. if (value)
  59. {
  60. SetModeView();
  61. }
  62. }
  63. }
  64. private bool isContinue;
  65. public bool IsContinue
  66. {
  67. get { return isContinue; }
  68. set
  69. {
  70. SetProperty(ref isContinue, value);
  71. SetModeView();
  72. }
  73. }
  74. private bool isDoubleView;
  75. public bool IsDoubleView
  76. {
  77. get { return isDoubleView; }
  78. set
  79. {
  80. SetProperty(ref isDoubleView, value);
  81. if (value)
  82. {
  83. SetModeView();
  84. }
  85. }
  86. }
  87. private bool isBookMode;
  88. public bool IsBookMode
  89. {
  90. get { return isBookMode; }
  91. set
  92. {
  93. SetProperty(ref isBookMode, value);
  94. if (value)
  95. {
  96. SetModeView();
  97. }
  98. }
  99. }
  100. private int pageCount;
  101. public int PageCount
  102. {
  103. get { return pageCount; }
  104. set
  105. {
  106. SetProperty(ref pageCount, value);
  107. }
  108. }
  109. private int currentPage;
  110. public int CurrentPage
  111. {
  112. get { return currentPage; }
  113. set
  114. {
  115. SetProperty(ref currentPage, value);
  116. }
  117. }
  118. private double currenZoom;
  119. public double CurrentZoom
  120. {
  121. get { return currenZoom; }
  122. set { SetProperty(ref currenZoom, value); }
  123. }
  124. private int selectedIndex;
  125. public int SelectedIndex
  126. {
  127. get { return selectedIndex; }
  128. set
  129. {
  130. SetProperty(ref selectedIndex, value);
  131. }
  132. }
  133. public CPDFViewer PDFViewer { get; set; }
  134. private ViewContentViewModel viewContentViewModel { get; set; }
  135. private IRegionManager region;
  136. private IDialogService dialogs;
  137. public BottomToolContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  138. {
  139. region = regionManager;
  140. dialogs = dialogService;
  141. PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  142. NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  143. FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  144. LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  145. ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(() => CurrentZoom);
  146. ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(() => CurrentZoom);
  147. SetViewModeCommand = new DelegateCommand(SetModeView);
  148. PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
  149. PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
  150. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  151. OpenFullCommand = new DelegateCommand(OpenFullWindow);
  152. ShowViewModularCommand = new DelegateCommand<object>(ShowViewModularEvent);
  153. }
  154. private void ShowViewModularEvent(object obj)
  155. {
  156. if (viewContentViewModel.IsPropertyOpen == false)
  157. {
  158. viewContentViewModel.IsPropertyOpen = true;
  159. NavigationParameters param = new NavigationParameters();
  160. param.Add(ParameterNames.PDFViewer, PDFViewer);
  161. param.Add(ParameterNames.BottomToolContentViewModel, this);
  162. region.RequestNavigate(RegionNames.PropertyRegionName, "ViewModularContent", param);
  163. }
  164. else
  165. {
  166. viewContentViewModel.IsPropertyOpen = false;
  167. }
  168. IsShowViewModular = viewContentViewModel.IsPropertyOpen;
  169. }
  170. /// <summary>
  171. /// 进入全屏模式
  172. /// </summary>
  173. private void OpenFullWindow()
  174. {
  175. DialogParameters parameters = new DialogParameters();
  176. //因为全屏模式可能需要设置特定的页面模式,所以只传文件路径,新建一个PDFview对象
  177. parameters.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  178. parameters.Add(ParameterNames.PassWord, PDFViewer.Tag == null ? "" : PDFViewer.Tag.ToString());
  179. dialogs.ShowDialog(DialogNames.FullScreenDialog, parameters, e =>
  180. {
  181. if (e.Result == ButtonResult.Cancel)
  182. {
  183. //TODO:弹窗提示打开全屏模式失败
  184. AlertsMessage alertsMessage = new AlertsMessage();
  185. alertsMessage.ShowDialog("", "Error", "OK");
  186. return;
  187. }
  188. });
  189. }
  190. private void SelectionChangedEvent(object e)
  191. {
  192. var args = e as SelectionChangedEventArgs;
  193. if (args != null)
  194. {
  195. if (SelectedIndex <= 2)
  196. {
  197. switch (SelectedIndex)
  198. {
  199. case 0:
  200. PDFViewer.ChangeFitMode(FitMode.FitSize);
  201. break;
  202. case 1:
  203. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  204. break;
  205. case 2:
  206. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. else
  213. {
  214. if (args.AddedItems[0] is ComboBoxItem)
  215. {
  216. if ((args.AddedItems[0] as ComboBoxItem).Tag != null)
  217. {
  218. PDFViewer.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. private void PageNumText_PreviewKeyDown(object e)
  225. {
  226. var args = e as KeyEventArgs;
  227. if (args != null)
  228. {
  229. //显示文本框输入内容
  230. List<Key> NumberKeys = new List<Key>() { Key.D0,Key.D1,Key.D2,Key.D3,Key.D4,Key.D5,Key.D6,Key.D7,Key.D8,Key.D9,Key.NumPad0,Key.NumPad1,Key.NumPad2,
  231. Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  232. if (!NumberKeys.Contains(args.Key))
  233. {
  234. args.Handled = true;
  235. }
  236. }
  237. }
  238. private void PageNumText_KeyDown(object e)
  239. {
  240. if (!(e is KeyEventArgs))
  241. return;
  242. //回车提交输入
  243. if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
  244. {
  245. int pagenum = 0;
  246. string text = ((e as KeyEventArgs).OriginalSource as TextBox).Text.ToString();
  247. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  248. int i = text.LastIndexOfAny(chs);
  249. if (i > 0)
  250. text = text.Substring(0, i - 1);
  251. if (!int.TryParse(text, out pagenum))
  252. {
  253. CurrentPage = PDFViewer.Document.PageCount;
  254. ((e as KeyEventArgs).OriginalSource as TextBox).Text = PDFViewer.Document.PageCount.ToString();
  255. return;
  256. }
  257. if (pagenum < 1)
  258. pagenum = 1;
  259. else if (pagenum > PDFViewer.Document.PageCount)
  260. pagenum = PDFViewer.Document.PageCount;
  261. PDFViewer.GoToPage(pagenum - 1);
  262. CurrentPage = pagenum;
  263. ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString();
  264. }
  265. }
  266. private void GoPrePage()
  267. {
  268. if (PDFViewer != null)
  269. {
  270. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  271. }
  272. }
  273. private bool CanPrePageExcute()
  274. {
  275. if (PDFViewer != null)
  276. {
  277. if (PDFViewer.CurrentIndex <= 0)
  278. return false;
  279. else
  280. return true;
  281. }
  282. return false;
  283. }
  284. private void GoNextPage()
  285. {
  286. if (PDFViewer != null)
  287. {
  288. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  289. if (((int)PDFViewer.Mode % 2) == 0)
  290. {
  291. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  292. }
  293. }
  294. }
  295. private bool CanNextPageExcute()
  296. {
  297. if (PDFViewer != null)
  298. {
  299. if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1)
  300. return false;
  301. else
  302. return true;
  303. }
  304. return false;
  305. }
  306. private void GoFirstPage()
  307. {
  308. if (PDFViewer != null)
  309. {
  310. PDFViewer.GoToPage(0);
  311. }
  312. }
  313. private void GoLastPage()
  314. {
  315. if (PDFViewer != null)
  316. {
  317. PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1);
  318. }
  319. }
  320. private void ZoomIn()
  321. {
  322. double zoom = SetPageZoomFactor(true);
  323. PDFViewer.Zoom(zoom / 100);
  324. }
  325. private void ZoomOut()
  326. {
  327. double zoom = SetPageZoomFactor(false);
  328. PDFViewer.Zoom(zoom / 100);
  329. }
  330. private double SetPageZoomFactor(bool flag)
  331. {
  332. double zoom = PDFViewer.ZoomFactor * 100;
  333. if (flag)
  334. {
  335. if (zoom >= 25 && zoom <= 300)
  336. {
  337. zoom = zoom + 25;
  338. }
  339. else if (zoom >= 301 && zoom <= 1000)
  340. {
  341. zoom = zoom + 40;
  342. }
  343. else if (zoom >= 1001 && zoom <= 10000)
  344. {
  345. zoom = zoom + 100;
  346. }
  347. }
  348. else
  349. {
  350. if (zoom >= 25 && zoom <= 300)
  351. {
  352. zoom = zoom - 25;
  353. }
  354. else if (zoom >= 301 && zoom <= 1000)
  355. {
  356. zoom = zoom - 40;
  357. }
  358. else if (zoom >= 1001 && zoom <= 10000)
  359. {
  360. zoom = zoom - 100;
  361. }
  362. }
  363. if (zoom < 25)
  364. {
  365. zoom = 25;
  366. }
  367. if (zoom > 10000)
  368. {
  369. zoom = 10000;
  370. }
  371. return zoom;
  372. }
  373. private bool CanZoomIn()
  374. {
  375. if (PDFViewer != null)
  376. {
  377. if (PDFViewer.ZoomFactor * 100 >= 10000)
  378. return false;
  379. else
  380. return true;
  381. }
  382. return false;
  383. }
  384. private bool CanZoomOut()
  385. {
  386. if (PDFViewer != null)
  387. {
  388. if (PDFViewer.ZoomFactor * 100 <= 25)
  389. return false;
  390. else
  391. return true;
  392. }
  393. return false;
  394. }
  395. public void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  396. {
  397. if (e.Key == "PageNum")
  398. {
  399. CurrentPage = (int)e.Value;
  400. PageCount = PDFViewer.Document.PageCount;
  401. }
  402. if (e.Key == "ViewMode")
  403. {
  404. GetModeView((ViewMode)e.Value);
  405. }
  406. if (e.Key == "Zoom")
  407. {
  408. CurrentZoom = (double)e.Value * 100;
  409. SelectedIndex = -1;
  410. }
  411. }
  412. private void GetModeView(ViewMode mode)
  413. {
  414. if ((int)mode % 2 == 0)
  415. {
  416. if (!IsContinue)
  417. {
  418. IsContinue = true;
  419. }
  420. }
  421. else
  422. {
  423. if (IsContinue)
  424. {
  425. IsContinue = false;
  426. }
  427. }
  428. if ((int)mode <= 2)
  429. {
  430. if (!isSingleView)
  431. {
  432. IsSingleView = true;
  433. }
  434. }
  435. else if ((int)mode <= 4)
  436. {
  437. if (!isDoubleView)
  438. {
  439. IsDoubleView = true;
  440. }
  441. }
  442. else
  443. {
  444. if (!isBookMode)
  445. {
  446. IsBookMode = true;
  447. }
  448. }
  449. }
  450. public void SetModeView()
  451. {
  452. if (PDFViewer != null)
  453. {
  454. if (IsContinue)
  455. {
  456. if (IsSingleView)
  457. {
  458. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  459. }
  460. else if (IsDoubleView)
  461. {
  462. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  463. }
  464. else
  465. {
  466. PDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  467. }
  468. }
  469. else
  470. {
  471. if (IsSingleView)
  472. {
  473. PDFViewer.ChangeViewMode(ViewMode.Single);
  474. }
  475. else if (IsDoubleView)
  476. {
  477. PDFViewer.ChangeViewMode(ViewMode.Double);
  478. }
  479. else
  480. {
  481. PDFViewer.ChangeViewMode(ViewMode.Book);
  482. }
  483. }
  484. }
  485. }
  486. #region Navigation
  487. public void OnNavigatedTo(NavigationContext navigationContext)
  488. {
  489. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  490. if (viewContentViewModel != null)
  491. {
  492. this.viewContentViewModel = viewContentViewModel;
  493. IsShowViewModular = viewContentViewModel.IsPropertyOpen;
  494. }
  495. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  496. if (pdfview != null)
  497. {
  498. //获取页面设置等信息
  499. PDFViewer = pdfview;
  500. PageCount = PDFViewer.Document.PageCount;
  501. GetModeView(PDFViewer.ModeView);
  502. CurrentPage = PDFViewer.CurrentIndex + 1;
  503. CurrentZoom = PDFViewer.ZoomFactor * 100;
  504. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  505. }
  506. }
  507. public bool IsNavigationTarget(NavigationContext navigationContext)
  508. {
  509. return true;
  510. }
  511. public void OnNavigatedFrom(NavigationContext navigationContext)
  512. {
  513. }
  514. #endregion Navigation
  515. }
  516. }