MainWindowViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using Microsoft.Win32;
  2. using PDF_Office.CustomControl.SystemControl;
  3. using PDF_Office.Model;
  4. using PDF_Office.EventAggregators;
  5. using PDF_Office.Views;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Controls;
  17. using ComPDFKitViewer.PdfViewer;
  18. using Dragablz;
  19. using System.Windows;
  20. using Prism.Ioc;
  21. using PDF_Office.CustomControl;
  22. using Prism.Services.Dialogs;
  23. using PDF_Office.Properties;
  24. using PDF_Office.Helper;
  25. using PDFSettings.Settings;
  26. namespace PDF_Office.ViewModels
  27. {
  28. public class MainWindowViewModel : BindableBase
  29. {
  30. private MainContent selectedItem;
  31. public MainContent SelectedItem
  32. {
  33. get { return selectedItem; }
  34. set
  35. {
  36. SetProperty(ref selectedItem, value);
  37. if (SelectedItem != null)
  38. {
  39. var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
  40. if (pdfviewer != null)
  41. {
  42. CurrentPDFViewer = pdfviewer;
  43. }
  44. else
  45. {
  46. CurrentPDFViewer = null;
  47. }
  48. }
  49. }
  50. }
  51. private string progresstitle;
  52. /// <summary>
  53. /// 进度控件标题
  54. /// </summary>
  55. public string ProgressTitle
  56. {
  57. get { return progresstitle; }
  58. set
  59. {
  60. SetProperty(ref progresstitle, value);
  61. }
  62. }
  63. private double processvalue = 0;
  64. /// <summary>
  65. /// 进度条当前值
  66. /// </summary>
  67. public double Value
  68. {
  69. get { return processvalue; }
  70. set
  71. {
  72. SetProperty(ref processvalue, value);
  73. }
  74. }
  75. private double maxValue = 100;
  76. /// <summary>
  77. /// 进度条最大值
  78. /// </summary>
  79. public double MaxValue
  80. {
  81. get { return maxValue; }
  82. set
  83. {
  84. SetProperty(ref maxValue, value);
  85. }
  86. }
  87. /// <summary>
  88. ///提示登录高级功能
  89. /// </summary>
  90. private Visibility _OphVis = Visibility.Collapsed;
  91. public Visibility OphVis
  92. {
  93. get { return _OphVis; }
  94. set
  95. {
  96. SetProperty(ref _OphVis, value);
  97. }
  98. }
  99. private Visibility _RegisterVis = Visibility.Collapsed;
  100. public Visibility RegisterVis
  101. {
  102. get { return _RegisterVis; }
  103. set
  104. {
  105. SetProperty(ref _RegisterVis, value);
  106. }
  107. }
  108. private Visibility _LoginVis = Visibility.Collapsed;
  109. public Visibility LoginVis
  110. {
  111. get { return _LoginVis; }
  112. set
  113. {
  114. SetProperty(ref _LoginVis, value);
  115. }
  116. }
  117. private Visibility _UserVis = Visibility.Collapsed;
  118. public Visibility UserVis
  119. {
  120. get { return _UserVis; }
  121. set
  122. {
  123. SetProperty(ref _UserVis, value);
  124. }
  125. }
  126. private Visibility isProcessVisible = Visibility.Collapsed;
  127. /// <summary>
  128. /// OCR进度条是否显示
  129. /// </summary>
  130. public Visibility IsProcessVisible
  131. {
  132. get { return isProcessVisible; }
  133. set
  134. {
  135. SetProperty(ref isProcessVisible, value);
  136. }
  137. }
  138. /// <summary>
  139. /// 进度条关闭的事件
  140. /// </summary>
  141. public Action ProcessCloseAction;
  142. private bool isCloseAllEnable = false;
  143. public bool IsCloseAllEnable
  144. {
  145. get { return isCloseAllEnable; }
  146. set
  147. {
  148. SetProperty(ref isCloseAllEnable, value);
  149. }
  150. }
  151. public DelegateCommand CloseAllTabCommand { get; set; }
  152. private CPDFViewer pdfViewer;
  153. /// <summary>
  154. /// 当前页签的PdfViewer对象
  155. /// </summary>
  156. public CPDFViewer CurrentPDFViewer
  157. {
  158. get { return pdfViewer; }
  159. set { pdfViewer = value; }
  160. }
  161. public DelegateCommand AddTab { get; set; }
  162. /// <summary>
  163. /// 关闭OCR进度条
  164. /// </summary>
  165. public DelegateCommand CloseOCRCommand { get; set; }
  166. public DelegateCommand OpenRegisterCommand { get; set; }
  167. public DelegateCommand OpenLoginCommand { get; set; }
  168. public DelegateCommand OpenUserCommand { get; set; }
  169. public DelegateCommand LoadCommand { get; set; }
  170. public IRegionManager region;
  171. public IEventAggregator eventer;
  172. public IDialogService dialogs;
  173. private IContainerProvider containerProvider;
  174. public IInterTabClient InterTabClient { get; }
  175. public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container, IDialogService dialogService)
  176. {
  177. if (App.mainWindowViewModel == null)
  178. {
  179. //加载第一个窗体时 先赋值静态对象
  180. App.mainWindowViewModel = this;
  181. }
  182. dialogs = dialogService;
  183. region = regionManager;
  184. eventer = eventAggregator;
  185. containerProvider = container;
  186. InterTabClient = new InterTabClient(container);
  187. AddTab = new DelegateCommand(AddTabItem);
  188. CloseOCRCommand = new DelegateCommand(closeocr);
  189. OpenRegisterCommand = new DelegateCommand(OpenRegister);
  190. OpenLoginCommand = new DelegateCommand(OpenLogin);
  191. OpenUserCommand = new DelegateCommand(OpenUser);
  192. CloseAllTabCommand = new DelegateCommand(() => { if (closeAllTabItem())
  193. {
  194. App.Current.MainWindow.Close();
  195. }
  196. });
  197. //第一次打开时需要自动加载Home页
  198. if (App.IsFirstOpen)
  199. {
  200. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  201. {
  202. if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
  203. {
  204. List<string> files = Settings.Default.AppProperties.NeedToOpenFileList;
  205. foreach (var file in files)
  206. {
  207. this.AddTabItem(file);
  208. }
  209. Settings.Default.AppProperties.NeedToOpenFileList.Clear();
  210. Settings.Default.Save();
  211. return;
  212. }
  213. else
  214. {
  215. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  216. }
  217. try
  218. {
  219. //避免注册弹窗和新手引导弹窗一起弹出
  220. if (App.IsShowRegist && !Settings.Default.IsNewUser)
  221. {
  222. OpenRegister();
  223. }
  224. //新用户弹出引导弹窗
  225. if (Settings.Default.IsNewUser)
  226. {
  227. ShowGuidDialog();
  228. Settings.Default.IsNewUser = false;
  229. Settings.Default.Save();
  230. }
  231. }
  232. catch { }
  233. }
  234. ));
  235. App.IsFirstOpen = false;
  236. if (Settings.Default.AppProperties.LoginToken != "")
  237. {
  238. if(ServiceHelper.GetUser()=="false")
  239. {
  240. LoginVis = Visibility.Visible;
  241. OphVis = Visibility.Visible;
  242. }
  243. else
  244. {
  245. UserVis = Visibility.Visible;
  246. }
  247. }
  248. else
  249. {
  250. LoginVis = Visibility.Visible;
  251. OphVis = Visibility.Visible;
  252. }
  253. }
  254. else
  255. {
  256. if (ServiceHelper.GetUser() == "false")
  257. {
  258. LoginVis = Visibility.Visible;
  259. }
  260. else
  261. {
  262. UserVis = Visibility.Visible;
  263. }
  264. }
  265. }
  266. //显示新手引导弹窗
  267. private void ShowGuidDialog()
  268. {
  269. dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
  270. {
  271. });
  272. }
  273. //打开注册弹窗
  274. public void OpenRegister()
  275. {
  276. dialogs.ShowDialog(DialogNames.RegisterDialog, new DialogParameters(), r =>
  277. {
  278. });
  279. }
  280. //打开登录弹窗
  281. public void OpenLogin()
  282. {
  283. dialogs.ShowDialog(DialogNames.LoginDialog, new DialogParameters(""), r =>
  284. {
  285. });
  286. }
  287. //打开用户弹窗
  288. public void OpenUser()
  289. {
  290. dialogs.ShowDialog(DialogNames.UserDialog, new DialogParameters(""), r =>
  291. {
  292. });
  293. }
  294. /// <summary>
  295. /// 关闭OCR
  296. /// </summary>
  297. private void closeocr()
  298. {
  299. if (ProcessCloseAction != null)
  300. {
  301. ProcessCloseAction.Invoke();
  302. }
  303. }
  304. /// <summary>
  305. /// 单击+号时添加页签
  306. /// </summary>
  307. private void AddTabItem()
  308. {
  309. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  310. CheckViewsCount();
  311. }
  312. /// <summary>
  313. /// 选择多文档打开时
  314. /// </summary>
  315. /// <param name="filePath"></param>
  316. public void AddTabItem(string filePath)
  317. {
  318. NavigationParameters parameters = new NavigationParameters
  319. {
  320. { ParameterNames.FilePath, filePath }
  321. };
  322. region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
  323. CheckViewsCount();
  324. }
  325. /// <summary>
  326. /// 关闭所有页签
  327. /// </summary>
  328. public bool closeAllTabItem()
  329. {
  330. //下一次重新打开未关闭的文档
  331. if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
  332. {
  333. Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
  334. Settings.Default.Save();
  335. }
  336. while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
  337. {
  338. if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
  339. {
  340. ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
  341. if (result == ContentResult.Cancel)
  342. return false;
  343. App.OpenedFileList.Remove(CurrentPDFViewer.Document.FilePath);
  344. CurrentPDFViewer.CloseDocument();
  345. }
  346. if (selectedItem != null)
  347. {
  348. region.Regions[RegionNames.MainRegion].Remove(selectedItem);
  349. }
  350. //多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
  351. if (selectedItem == null)
  352. {
  353. return true;
  354. }
  355. }
  356. return true;
  357. }
  358. public void CloseTabItem(object item)
  359. {
  360. //获取x号所在的maincontentviewmodel对象
  361. var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
  362. //如果已经有打开文档,先切换到目标页签
  363. if (maincontentViewModel.PDFViewer != null)
  364. {
  365. SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
  366. }
  367. if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
  368. {
  369. ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
  370. if (result == ContentResult.Cancel)
  371. {
  372. return;
  373. }
  374. App.OpenedFileList.Remove(maincontentViewModel.PDFViewer.Document.FilePath);
  375. if (maincontentViewModel.PDFViewer.Document != null)
  376. {
  377. maincontentViewModel.PDFViewer.CloseDocument();
  378. }
  379. }
  380. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  381. {
  382. region.Regions[RegionNames.MainRegion].Remove(item);
  383. CheckViewsCount();
  384. }
  385. else
  386. {
  387. if (maincontentViewModel.PDFViewer != null)
  388. {
  389. //如果是打开文档的页签 则回到首页
  390. region.Regions[RegionNames.MainRegion].Remove(item);
  391. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  392. }
  393. else
  394. {
  395. //如果是关闭首页则关闭软件
  396. region.Regions[RegionNames.MainRegion].Remove(item);
  397. App.Current.MainWindow.Close();
  398. }
  399. }
  400. return;
  401. }
  402. /// <summary>
  403. /// 检查页签个数 更新close allcommand 命令状态
  404. /// </summary>
  405. private void CheckViewsCount()
  406. {
  407. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  408. {
  409. IsCloseAllEnable = true;
  410. }
  411. else
  412. {
  413. IsCloseAllEnable = false;
  414. }
  415. }
  416. public void SelectItem(string filepath)
  417. {
  418. var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
  419. if (item == null)
  420. return;
  421. region.Regions[RegionNames.MainRegion].Activate(item);
  422. var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
  423. if (behavior != null)
  424. {
  425. behavior.ActivateView(item);
  426. }
  427. }
  428. /// <summary>
  429. /// 显示关闭询问弹窗
  430. /// </summary>
  431. /// <param name="pdfViewer"></param>
  432. /// <returns></returns>
  433. private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
  434. {
  435. if(Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  436. {
  437. //偏好设置里选择自动保存时
  438. return ContentResult.Ok;
  439. }
  440. AlertsMessage alertsMessage = new AlertsMessage();
  441. alertsMessage.ShowDialog("关闭提示", "当前文档有为保存的操作,是否需要保存?", "Cancel", "No", "Ok");
  442. var result = alertsMessage.result;
  443. if (result == ContentResult.Ok)
  444. {
  445. var isSave = viewContentViewModel.saveFile();
  446. if (isSave == false)
  447. result = ContentResult.Cancel;
  448. }
  449. return result;
  450. }
  451. }
  452. }