BottomToolContentViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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
  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. private bool isSingleView;
  39. public bool IsSingleView
  40. {
  41. get { return isSingleView; }
  42. set
  43. {
  44. SetProperty(ref isSingleView, value);
  45. if (value)
  46. {
  47. SetModeView();
  48. }
  49. }
  50. }
  51. private bool isContinue;
  52. public bool IsContinue
  53. {
  54. get { return isContinue; }
  55. set
  56. {
  57. SetProperty(ref isContinue, value);
  58. SetModeView();
  59. }
  60. }
  61. private bool isDoubleView;
  62. public bool IsDoubleView
  63. {
  64. get { return isDoubleView; }
  65. set
  66. {
  67. SetProperty(ref isDoubleView, value);
  68. if (value)
  69. {
  70. SetModeView();
  71. }
  72. }
  73. }
  74. private bool isBookMode;
  75. public bool IsBookMode
  76. {
  77. get { return isBookMode; }
  78. set
  79. {
  80. SetProperty(ref isBookMode, value);
  81. if (value)
  82. {
  83. SetModeView();
  84. }
  85. }
  86. }
  87. private int pageCount;
  88. public int PageCount
  89. {
  90. get { return pageCount; }
  91. set
  92. {
  93. SetProperty(ref pageCount, value);
  94. }
  95. }
  96. private int currentPage;
  97. public int CurrentPage
  98. {
  99. get { return currentPage; }
  100. set
  101. {
  102. SetProperty(ref currentPage, value);
  103. }
  104. }
  105. private double currenZoom;
  106. public double CurrentZoom
  107. {
  108. get { return currenZoom; }
  109. set { SetProperty(ref currenZoom, value); }
  110. }
  111. private int selectedIndex;
  112. public int SelectedIndex
  113. {
  114. get { return selectedIndex; }
  115. set
  116. {
  117. SetProperty(ref selectedIndex, value);
  118. }
  119. }
  120. public CPDFViewer PDFViewer { get; set; }
  121. private IRegionManager region;
  122. private IDialogService dialogs;
  123. public BottomToolContentViewModel(IRegionManager regionManager,IDialogService dialogService)
  124. {
  125. region = regionManager;
  126. dialogs = dialogService;
  127. Load = new DelegateCommand(LoadControl);
  128. PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  129. NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  130. FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  131. LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  132. ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(()=>CurrentZoom);
  133. ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(()=>CurrentZoom);
  134. SetViewModeCommand = new DelegateCommand(SetModeView);
  135. PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
  136. PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
  137. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  138. OpenFullCommand = new DelegateCommand(OpenFullWindow);
  139. }
  140. /// <summary>
  141. /// 进入全屏模式
  142. /// </summary>
  143. private void OpenFullWindow()
  144. {
  145. DialogParameters parameters = new DialogParameters();
  146. //因为全屏模式可能需要设置特定的页面模式,所以只传文件路径,新建一个PDFview对象
  147. parameters.Add(ParameterNames.FilePath,PDFViewer.Document.FilePath);
  148. parameters.Add(ParameterNames.PassWord,PDFViewer.Tag==null?"":PDFViewer.Tag.ToString());
  149. dialogs.ShowDialog(DialogNames.FullScreenDialog,parameters,e=> {
  150. if(e.Result==ButtonResult.Cancel)
  151. {
  152. //TODO:弹窗提示打开全屏模式失败
  153. MessageBox.Show("Error");
  154. }
  155. });
  156. }
  157. private void SelectionChangedEvent(object e)
  158. {
  159. var args = e as SelectionChangedEventArgs;
  160. if(args!=null)
  161. {
  162. if(SelectedIndex<=2)
  163. {
  164. switch (SelectedIndex)
  165. {
  166. case 0:
  167. PDFViewer.ChangeFitMode(FitMode.FitSize);
  168. break;
  169. case 1:
  170. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  171. break;
  172. case 2:
  173. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. else
  180. {
  181. if (args.AddedItems[0] is ComboBoxItem)
  182. {
  183. if ((args.AddedItems[0] as ComboBoxItem).Tag != null)
  184. {
  185. PDFViewer.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. private void PageNumText_PreviewKeyDown(object e)
  192. {
  193. var args = e as KeyEventArgs;
  194. if(args != null)
  195. {
  196. //显示文本框输入内容
  197. 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,
  198. Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  199. if (!NumberKeys.Contains(args.Key))
  200. {
  201. args.Handled = true;
  202. }
  203. }
  204. }
  205. private void PageNumText_KeyDown(object e)
  206. {
  207. if (!(e is KeyEventArgs))
  208. return;
  209. //回车提交输入
  210. if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
  211. {
  212. int pagenum = 0;
  213. string text = ((e as KeyEventArgs).OriginalSource as TextBox).Text.ToString();
  214. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  215. int i = text.LastIndexOfAny(chs);
  216. if (i > 0)
  217. text = text.Substring(0, i - 1);
  218. if (!int.TryParse(text, out pagenum))
  219. {
  220. CurrentPage = PDFViewer.Document.PageCount;
  221. ((e as KeyEventArgs).OriginalSource as TextBox).Text = PDFViewer.Document.PageCount.ToString();
  222. return;
  223. }
  224. if (pagenum < 1)
  225. pagenum = 1;
  226. else if (pagenum > PDFViewer.Document.PageCount)
  227. pagenum = PDFViewer.Document.PageCount;
  228. PDFViewer.GoToPage(pagenum - 1);
  229. CurrentPage = pagenum;
  230. ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString();
  231. }
  232. }
  233. private void GoPrePage()
  234. {
  235. if (PDFViewer != null)
  236. {
  237. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  238. }
  239. }
  240. private bool CanPrePageExcute()
  241. {
  242. if (PDFViewer != null)
  243. {
  244. if (PDFViewer.CurrentIndex <= 0)
  245. return false;
  246. else
  247. return true;
  248. }
  249. return false;
  250. }
  251. private void GoNextPage()
  252. {
  253. if (PDFViewer != null)
  254. {
  255. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  256. if(((int)PDFViewer.Mode%2)==0)
  257. {
  258. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  259. }
  260. }
  261. }
  262. private bool CanNextPageExcute()
  263. {
  264. if (PDFViewer != null)
  265. {
  266. if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1)
  267. return false;
  268. else
  269. return true;
  270. }
  271. return false;
  272. }
  273. private void GoFirstPage()
  274. {
  275. if (PDFViewer != null)
  276. {
  277. PDFViewer.GoToPage(0);
  278. }
  279. }
  280. private void GoLastPage()
  281. {
  282. if (PDFViewer != null)
  283. {
  284. PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1);
  285. }
  286. }
  287. private void ZoomIn()
  288. {
  289. var zoom = PDFViewer.ZoomFactor*100;
  290. if (zoom >= 1 && zoom <= 100)
  291. {
  292. PDFViewer.Zoom((zoom + 5) / 100.0);
  293. }
  294. else if (zoom <= 500)
  295. {
  296. PDFViewer.Zoom((zoom + 50) / 100.0);
  297. }
  298. else
  299. {
  300. PDFViewer.Zoom((zoom + 100) / 100.0);
  301. }
  302. }
  303. private void ZoomOut()
  304. {
  305. var zoom = PDFViewer.ZoomFactor*100;
  306. if (zoom >= 1 && zoom <= 100)
  307. {
  308. PDFViewer.Zoom((zoom - 5)/100.0);
  309. }
  310. else if (zoom <= 500)
  311. {
  312. PDFViewer.Zoom((zoom - 50) / 100.0);
  313. }
  314. else
  315. {
  316. PDFViewer.Zoom((zoom - 100) / 100.0);
  317. }
  318. }
  319. private bool CanZoomIn()
  320. {
  321. if (PDFViewer != null)
  322. {
  323. if (PDFViewer.ZoomFactor*100 >=1000)
  324. return false;
  325. else
  326. return true;
  327. }
  328. return false;
  329. }
  330. private bool CanZoomOut()
  331. {
  332. if (PDFViewer != null)
  333. {
  334. if (PDFViewer.ZoomFactor*100 <=1)
  335. return false;
  336. else
  337. return true;
  338. }
  339. return false;
  340. }
  341. private void LoadControl()
  342. {
  343. if (App.mainWindowViewModel.CurrentPDFViewer != null)
  344. {
  345. PDFViewer = App.mainWindowViewModel.CurrentPDFViewer;
  346. if (PDFViewer != null)
  347. {
  348. //获取页面设置等信息
  349. PageCount = PDFViewer.Document.PageCount;
  350. GetModeView(PDFViewer.ModeView);
  351. CurrentPage = PDFViewer.CurrentIndex+1;
  352. CurrentZoom = PDFViewer.ZoomFactor*100;
  353. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  354. }
  355. }
  356. }
  357. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  358. {
  359. if (e.Key == "PageNum")
  360. {
  361. CurrentPage = (int)e.Value;
  362. }
  363. if (e.Key == "ViewMode")
  364. {
  365. GetModeView((ViewMode)e.Value);
  366. }
  367. if(e.Key=="Zoom")
  368. {
  369. CurrentZoom = (double)e.Value*100;
  370. SelectedIndex = -1;
  371. }
  372. }
  373. private void GetModeView(ViewMode mode)
  374. {
  375. if ((int)mode % 2 == 0)
  376. {
  377. if(!IsContinue)
  378. {
  379. IsContinue = true;
  380. }
  381. }
  382. else
  383. {
  384. if (IsContinue)
  385. {
  386. IsContinue = false;
  387. }
  388. }
  389. if ((int)mode <= 2)
  390. {
  391. if(!isSingleView)
  392. {
  393. IsSingleView = true;
  394. }
  395. }
  396. else if ((int)mode <= 4)
  397. {
  398. if(!isDoubleView)
  399. {
  400. IsDoubleView = true;
  401. }
  402. }
  403. else
  404. {
  405. if(!isBookMode)
  406. {
  407. IsBookMode = true;
  408. }
  409. }
  410. }
  411. private void SetModeView()
  412. {
  413. if (PDFViewer != null)
  414. {
  415. if (IsContinue)
  416. {
  417. if (IsSingleView)
  418. {
  419. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  420. }
  421. else if (IsDoubleView)
  422. {
  423. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  424. }
  425. else
  426. {
  427. PDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  428. }
  429. }
  430. else
  431. {
  432. if (IsSingleView)
  433. {
  434. PDFViewer.ChangeViewMode(ViewMode.Single);
  435. }
  436. else if (IsDoubleView)
  437. {
  438. PDFViewer.ChangeViewMode(ViewMode.Double);
  439. }
  440. else
  441. {
  442. PDFViewer.ChangeViewMode(ViewMode.Book);
  443. }
  444. }
  445. }
  446. }
  447. }
  448. }