123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- using Microsoft.Win32;
- using PDF_Office.CustomControl.SystemControl;
- using PDF_Office.Model;
- using PDF_Office.EventAggregators;
- using PDF_Office.Views;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using ComPDFKitViewer.PdfViewer;
- using Dragablz;
- using System.Windows;
- using Prism.Ioc;
- using PDF_Office.CustomControl;
- using Prism.Services.Dialogs;
- using PDF_Office.Properties;
- using PDF_Office.Helper;
- using PDFSettings.Settings;
- namespace PDF_Office.ViewModels
- {
- public class MainWindowViewModel : BindableBase
- {
- private MainContent selectedItem;
- public MainContent SelectedItem
- {
- get { return selectedItem; }
- set
- {
- SetProperty(ref selectedItem, value);
- if (SelectedItem != null)
- {
- var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
- if (pdfviewer != null)
- {
- CurrentPDFViewer = pdfviewer;
- }
- else
- {
- CurrentPDFViewer = null;
- }
- }
- }
- }
- private string progresstitle;
- /// <summary>
- /// 进度控件标题
- /// </summary>
- public string ProgressTitle
- {
- get { return progresstitle; }
- set
- {
- SetProperty(ref progresstitle, value);
- }
- }
- private double processvalue = 0;
- /// <summary>
- /// 进度条当前值
- /// </summary>
- public double Value
- {
- get { return processvalue; }
- set
- {
- SetProperty(ref processvalue, value);
- }
- }
- private double maxValue = 100;
- /// <summary>
- /// 进度条最大值
- /// </summary>
- public double MaxValue
- {
- get { return maxValue; }
- set
- {
- SetProperty(ref maxValue, value);
- }
- }
- /// <summary>
- ///提示登录高级功能
- /// </summary>
- private Visibility _OphVis = Visibility.Collapsed;
- public Visibility OphVis
- {
- get { return _OphVis; }
- set
- {
- SetProperty(ref _OphVis, value);
- }
- }
- private Visibility _RegisterVis = Visibility.Collapsed;
- public Visibility RegisterVis
- {
- get { return _RegisterVis; }
- set
- {
- SetProperty(ref _RegisterVis, value);
- }
- }
- private Visibility _LoginVis = Visibility.Collapsed;
- public Visibility LoginVis
- {
- get { return _LoginVis; }
- set
- {
- SetProperty(ref _LoginVis, value);
- }
- }
- private Visibility _UserVis = Visibility.Collapsed;
- public Visibility UserVis
- {
- get { return _UserVis; }
- set
- {
- SetProperty(ref _UserVis, value);
- }
- }
- private Visibility isProcessVisible = Visibility.Collapsed;
- /// <summary>
- /// OCR进度条是否显示
- /// </summary>
- public Visibility IsProcessVisible
- {
- get { return isProcessVisible; }
- set
- {
- SetProperty(ref isProcessVisible, value);
- }
- }
- /// <summary>
- /// 进度条关闭的事件
- /// </summary>
- public Action ProcessCloseAction;
- private bool isCloseAllEnable = false;
- public bool IsCloseAllEnable
- {
- get { return isCloseAllEnable; }
- set
- {
- SetProperty(ref isCloseAllEnable, value);
- }
- }
- public DelegateCommand CloseAllTabCommand { get; set; }
- private CPDFViewer pdfViewer;
- /// <summary>
- /// 当前页签的PdfViewer对象
- /// </summary>
- public CPDFViewer CurrentPDFViewer
- {
- get { return pdfViewer; }
- set { pdfViewer = value; }
- }
- public DelegateCommand AddTab { get; set; }
- /// <summary>
- /// 关闭OCR进度条
- /// </summary>
- public DelegateCommand CloseOCRCommand { get; set; }
- public DelegateCommand OpenRegisterCommand { get; set; }
- public DelegateCommand OpenLoginCommand { get; set; }
- public DelegateCommand OpenUserCommand { get; set; }
- public DelegateCommand LoadCommand { get; set; }
- public IRegionManager region;
- public IEventAggregator eventer;
- public IDialogService dialogs;
- private IContainerProvider containerProvider;
- public IInterTabClient InterTabClient { get; }
- public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container, IDialogService dialogService)
- {
- if (App.mainWindowViewModel == null)
- {
- //加载第一个窗体时 先赋值静态对象
- App.mainWindowViewModel = this;
- }
- dialogs = dialogService;
- region = regionManager;
- eventer = eventAggregator;
- containerProvider = container;
- InterTabClient = new InterTabClient(container);
- AddTab = new DelegateCommand(AddTabItem);
- CloseOCRCommand = new DelegateCommand(closeocr);
- OpenRegisterCommand = new DelegateCommand(OpenRegister);
- OpenLoginCommand = new DelegateCommand(OpenLogin);
- OpenUserCommand = new DelegateCommand(OpenUser);
- CloseAllTabCommand = new DelegateCommand(() => { if (closeAllTabItem())
- {
- App.Current.MainWindow.Close();
- }
- });
- //第一次打开时需要自动加载Home页
- if (App.IsFirstOpen)
- {
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
- {
- List<string> files = Settings.Default.AppProperties.NeedToOpenFileList;
- foreach (var file in files)
- {
- this.AddTabItem(file);
- }
- Settings.Default.AppProperties.NeedToOpenFileList.Clear();
- Settings.Default.Save();
- return;
- }
- else
- {
- region.RequestNavigate(RegionNames.MainRegion, "MainContent");
- }
- try
- {
- //避免注册弹窗和新手引导弹窗一起弹出
- if (App.IsShowRegist && !Settings.Default.IsNewUser)
- {
- OpenRegister();
- }
- //新用户弹出引导弹窗
- if (Settings.Default.IsNewUser)
- {
- ShowGuidDialog();
- Settings.Default.IsNewUser = false;
- Settings.Default.Save();
- }
- }
- catch { }
- }
- ));
- App.IsFirstOpen = false;
- if (Settings.Default.AppProperties.LoginToken != "")
- {
-
- if(ServiceHelper.GetUser()=="false")
- {
-
- LoginVis = Visibility.Visible;
- OphVis = Visibility.Visible;
- }
- else
- {
- UserVis = Visibility.Visible;
- }
- }
- else
- {
- LoginVis = Visibility.Visible;
- OphVis = Visibility.Visible;
- }
- }
- else
- {
- if (ServiceHelper.GetUser() == "false")
- {
- LoginVis = Visibility.Visible;
- }
- else
- {
- UserVis = Visibility.Visible;
- }
- }
-
- }
- //显示新手引导弹窗
- private void ShowGuidDialog()
- {
- dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
- {
- });
- }
- //打开注册弹窗
- public void OpenRegister()
- {
- dialogs.ShowDialog(DialogNames.RegisterDialog, new DialogParameters(), r =>
- {
- });
- }
- //打开登录弹窗
- public void OpenLogin()
- {
- dialogs.ShowDialog(DialogNames.LoginDialog, new DialogParameters(""), r =>
- {
- });
- }
- //打开用户弹窗
- public void OpenUser()
- {
- dialogs.ShowDialog(DialogNames.UserDialog, new DialogParameters(""), r =>
- {
- });
- }
- /// <summary>
- /// 关闭OCR
- /// </summary>
- private void closeocr()
- {
- if (ProcessCloseAction != null)
- {
- ProcessCloseAction.Invoke();
- }
- }
- /// <summary>
- /// 单击+号时添加页签
- /// </summary>
- private void AddTabItem()
- {
- region.RequestNavigate(RegionNames.MainRegion, "MainContent");
- CheckViewsCount();
- }
- /// <summary>
- /// 选择多文档打开时
- /// </summary>
- /// <param name="filePath"></param>
- public void AddTabItem(string filePath)
- {
- NavigationParameters parameters = new NavigationParameters
- {
- { ParameterNames.FilePath, filePath }
- };
- region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
- CheckViewsCount();
- }
- /// <summary>
- /// 关闭所有页签
- /// </summary>
- public bool closeAllTabItem()
- {
- //下一次重新打开未关闭的文档
- if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
- {
- Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
- Settings.Default.Save();
- }
- while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
- {
- if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
- {
- ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
- if (result == ContentResult.Cancel)
- return false;
- App.OpenedFileList.Remove(CurrentPDFViewer.Document.FilePath);
- CurrentPDFViewer.CloseDocument();
- }
- if (selectedItem != null)
- {
- region.Regions[RegionNames.MainRegion].Remove(selectedItem);
- }
- //多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
- if (selectedItem == null)
- {
- return true;
- }
- }
- return true;
- }
- public void CloseTabItem(object item)
- {
- //获取x号所在的maincontentviewmodel对象
- var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
- //如果已经有打开文档,先切换到目标页签
- if (maincontentViewModel.PDFViewer != null)
- {
- SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
- }
- if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
- {
- ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
- if (result == ContentResult.Cancel)
- {
- return;
- }
- App.OpenedFileList.Remove(maincontentViewModel.PDFViewer.Document.FilePath);
- if (maincontentViewModel.PDFViewer.Document != null)
- {
- maincontentViewModel.PDFViewer.CloseDocument();
- }
- }
- if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
- {
- region.Regions[RegionNames.MainRegion].Remove(item);
- CheckViewsCount();
- }
- else
- {
- if (maincontentViewModel.PDFViewer != null)
- {
- //如果是打开文档的页签 则回到首页
- region.Regions[RegionNames.MainRegion].Remove(item);
- region.RequestNavigate(RegionNames.MainRegion, "MainContent");
- }
- else
- {
- //如果是关闭首页则关闭软件
- region.Regions[RegionNames.MainRegion].Remove(item);
- App.Current.MainWindow.Close();
- }
- }
- return;
- }
- /// <summary>
- /// 检查页签个数 更新close allcommand 命令状态
- /// </summary>
- private void CheckViewsCount()
- {
- if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
- {
- IsCloseAllEnable = true;
- }
- else
- {
- IsCloseAllEnable = false;
- }
- }
- public void SelectItem(string filepath)
- {
- var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
- if (item == null)
- return;
- region.Regions[RegionNames.MainRegion].Activate(item);
- var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
- if (behavior != null)
- {
- behavior.ActivateView(item);
- }
- }
- /// <summary>
- /// 显示关闭询问弹窗
- /// </summary>
- /// <param name="pdfViewer"></param>
- /// <returns></returns>
- private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
- {
- if(Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
- {
- //偏好设置里选择自动保存时
- return ContentResult.Ok;
- }
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("关闭提示", "当前文档有为保存的操作,是否需要保存?", "Cancel", "No", "Ok");
- var result = alertsMessage.result;
- if (result == ContentResult.Ok)
- {
- var isSave = viewContentViewModel.saveFile();
- if (isSave == false)
- result = ContentResult.Cancel;
- }
- return result;
- }
- }
- }
|