MainWindowViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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(() => { closeAllTabItem(); });
  193. //第一次打开时需要自动加载Home页
  194. if (App.IsFirstOpen)
  195. {
  196. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  197. {
  198. if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
  199. {
  200. List<string> files = Settings.Default.AppProperties.NeedToOpenFileList;
  201. foreach (var file in files)
  202. {
  203. this.AddTabItem(file);
  204. }
  205. Settings.Default.AppProperties.NeedToOpenFileList.Clear();
  206. Settings.Default.Save();
  207. return;
  208. }
  209. else
  210. {
  211. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  212. }
  213. try
  214. {
  215. //避免注册弹窗和新手引导弹窗一起弹出
  216. if (App.IsShowRegist && !Settings.Default.IsNewUser)
  217. {
  218. OpenRegister();
  219. }
  220. //新用户弹出引导弹窗
  221. if (Settings.Default.IsNewUser)
  222. {
  223. ShowGuidDialog();
  224. Settings.Default.IsNewUser = false;
  225. Settings.Default.Save();
  226. }
  227. }
  228. catch { }
  229. }
  230. ));
  231. App.IsFirstOpen = false;
  232. if (Settings.Default.AppProperties.LoginToken != "")
  233. {
  234. if(ServiceHelper.GetUser()=="false")
  235. {
  236. LoginVis = Visibility.Visible;
  237. OphVis = Visibility.Visible;
  238. }
  239. else
  240. {
  241. UserVis = Visibility.Visible;
  242. }
  243. }
  244. else
  245. {
  246. LoginVis = Visibility.Visible;
  247. OphVis = Visibility.Visible;
  248. }
  249. }
  250. }
  251. //显示新手引导弹窗
  252. private void ShowGuidDialog()
  253. {
  254. dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
  255. {
  256. });
  257. }
  258. //打开注册弹窗
  259. public void OpenRegister()
  260. {
  261. dialogs.ShowDialog(DialogNames.RegisterDialog, new DialogParameters(), r =>
  262. {
  263. });
  264. }
  265. //打开登录弹窗
  266. public void OpenLogin()
  267. {
  268. dialogs.ShowDialog(DialogNames.LoginDialog, new DialogParameters(""), r =>
  269. {
  270. });
  271. }
  272. //打开用户弹窗
  273. public void OpenUser()
  274. {
  275. dialogs.ShowDialog(DialogNames.UserDialog, new DialogParameters(""), r =>
  276. {
  277. });
  278. }
  279. /// <summary>
  280. /// 关闭OCR
  281. /// </summary>
  282. private void closeocr()
  283. {
  284. if (ProcessCloseAction != null)
  285. {
  286. ProcessCloseAction.Invoke();
  287. }
  288. }
  289. /// <summary>
  290. /// 单击+号时添加页签
  291. /// </summary>
  292. private void AddTabItem()
  293. {
  294. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  295. CheckViewsCount();
  296. }
  297. /// <summary>
  298. /// 选择多文档打开时
  299. /// </summary>
  300. /// <param name="filePath"></param>
  301. public void AddTabItem(string filePath)
  302. {
  303. NavigationParameters parameters = new NavigationParameters
  304. {
  305. { ParameterNames.FilePath, filePath }
  306. };
  307. region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
  308. CheckViewsCount();
  309. }
  310. /// <summary>
  311. /// 关闭所有页签
  312. /// </summary>
  313. public bool closeAllTabItem()
  314. {
  315. //下一次重新打开未关闭的文档
  316. if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
  317. {
  318. Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
  319. Settings.Default.Save();
  320. }
  321. while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
  322. {
  323. if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
  324. {
  325. ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
  326. if (result == ContentResult.Cancel)
  327. return false;
  328. App.OpenedFileList.Remove(CurrentPDFViewer.Document.FilePath);
  329. CurrentPDFViewer.CloseDocument();
  330. }
  331. if (selectedItem != null)
  332. {
  333. region.Regions[RegionNames.MainRegion].Remove(selectedItem);
  334. }
  335. //多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
  336. if (selectedItem == null)
  337. {
  338. return true;
  339. }
  340. }
  341. return true;
  342. }
  343. public void CloseTabItem(object item)
  344. {
  345. //获取x号所在的maincontentviewmodel对象
  346. var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
  347. //如果已经有打开文档,先切换到目标页签
  348. if (maincontentViewModel.PDFViewer != null)
  349. {
  350. SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
  351. }
  352. if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
  353. {
  354. ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
  355. if (result == ContentResult.Cancel)
  356. {
  357. return;
  358. }
  359. App.OpenedFileList.Remove(maincontentViewModel.PDFViewer.Document.FilePath);
  360. if (maincontentViewModel.PDFViewer.Document != null)
  361. {
  362. maincontentViewModel.PDFViewer.CloseDocument();
  363. }
  364. }
  365. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  366. {
  367. region.Regions[RegionNames.MainRegion].Remove(item);
  368. CheckViewsCount();
  369. }
  370. else
  371. {
  372. if (maincontentViewModel.PDFViewer != null)
  373. {
  374. //如果是打开文档的页签 则回到首页
  375. region.Regions[RegionNames.MainRegion].Remove(item);
  376. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  377. }
  378. else
  379. {
  380. //如果是关闭首页则关闭软件
  381. region.Regions[RegionNames.MainRegion].Remove(item);
  382. App.Current.MainWindow.Close();
  383. }
  384. }
  385. return;
  386. }
  387. /// <summary>
  388. /// 检查页签个数 更新close allcommand 命令状态
  389. /// </summary>
  390. private void CheckViewsCount()
  391. {
  392. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  393. {
  394. IsCloseAllEnable = true;
  395. }
  396. else
  397. {
  398. IsCloseAllEnable = false;
  399. }
  400. }
  401. public void SelectItem(string filepath)
  402. {
  403. var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
  404. if (item == null)
  405. return;
  406. region.Regions[RegionNames.MainRegion].Activate(item);
  407. var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
  408. if (behavior != null)
  409. {
  410. behavior.ActivateView(item);
  411. }
  412. }
  413. /// <summary>
  414. /// 显示关闭询问弹窗
  415. /// </summary>
  416. /// <param name="pdfViewer"></param>
  417. /// <returns></returns>
  418. private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
  419. {
  420. if(Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  421. {
  422. //偏好设置里选择自动保存时
  423. return ContentResult.Ok;
  424. }
  425. AlertsMessage alertsMessage = new AlertsMessage();
  426. alertsMessage.ShowDialog("关闭提示", "当前文档有为保存的操作,是否需要保存?", "Cancel", "No", "Ok");
  427. var result = alertsMessage.result;
  428. if (result == ContentResult.Ok)
  429. {
  430. var isSave = viewContentViewModel.saveFile();
  431. if (isSave == false)
  432. result = ContentResult.Cancel;
  433. }
  434. return result;
  435. }
  436. }
  437. }