Просмотр исходного кода

其他-增加保存文件逻辑

ZhouJieSheng 2 лет назад
Родитель
Сommit
2dcf086934

+ 1 - 1
PDF Office/Styles/WindowsStyle.xaml

@@ -8,7 +8,7 @@
                 <WindowChrome
                     CaptionHeight="32"
                     CornerRadius="10"
-                    GlassFrameThickness="-1"
+                    GlassFrameThickness="0"
                     ResizeBorderThickness="5"
                     UseAeroCaptionButtons="False" />
             </Setter.Value>

+ 4 - 0
PDF Office/ViewModels/MainContentViewModel.cs

@@ -203,6 +203,10 @@ namespace PDF_Office.ViewModels
                 {
                     FileChanged = Visibility.Visible;
                 }
+                else
+                {
+                    FileChanged = Visibility.Collapsed;
+                }
             }
         }
 

+ 314 - 24
PDF Office/ViewModels/ViewContentViewModel.cs

@@ -15,6 +15,7 @@ using PDF_Office.CustomControl;
 using PDF_Office.Model;
 using System.Windows;
 using System.Windows.Controls;
+using System.IO;
 
 namespace PDF_Office.ViewModels
 {
@@ -40,6 +41,19 @@ namespace PDF_Office.ViewModels
 
         public string ToolsBarContentRegionName { get; set; }
 
+        /// <summary>
+        /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
+        /// </summary>
+        public bool isInPageEdit = false;
+
+        public Action PageEditUndo { get; set; }
+
+        public Action PageEditRedo { get; set; }
+
+        /// <summary>
+        /// 提示对话框对象
+        /// </summary>
+        private AlertsMessage alertsMessage = new AlertsMessage();
 
 
         private int gridToolRow = 1;
@@ -109,6 +123,41 @@ namespace PDF_Office.ViewModels
             }
         }
 
+        private bool canSave;
+        /// <summary>
+        /// 是否可以保存
+        /// </summary>
+        public bool CanSave
+        {
+            get { return canSave; }
+            set
+            {
+                SetProperty(ref canSave, value);
+            }
+        }
+
+        private bool canUndo;
+
+        public bool CanUndo
+        {
+            get { return canUndo; }
+            set
+            {
+                SetProperty(ref canUndo, value);
+            }
+        }
+
+        private bool canRedo;
+
+        public bool CanRedo
+        {
+            get { return canRedo; }
+            set
+            {
+                SetProperty(ref canRedo, value);
+            }
+        }
+
 
         private Dictionary<string, string> regionNameByTabItem;
 
@@ -118,8 +167,6 @@ namespace PDF_Office.ViewModels
 
         private string currentBar = "";
 
-
-
         /// <summary>
         /// 用来避免重复触发导航事件的标志符
         /// </summary>
@@ -138,6 +185,14 @@ namespace PDF_Office.ViewModels
         public DelegateCommand Load { get; set; }
 
         public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
+
+        public DelegateCommand SaveFile { get; set; }
+
+        public DelegateCommand SaveAsFile { get; set; }
+
+        public DelegateCommand UndoCommand { get; set; }
+
+        public DelegateCommand RedoCommand { get; set; }
         #endregion
 
         public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService)
@@ -147,6 +202,10 @@ namespace PDF_Office.ViewModels
 
             LoadFile = new DelegateCommand(loadFile);
             Load = new DelegateCommand(LoadControl);
+            SaveFile = new DelegateCommand(()=> { saveFile(); });
+            SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
+            UndoCommand = new DelegateCommand(Undo);
+            RedoCommand = new DelegateCommand(Redo);
             TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
 
             ViwerRegionName = Guid.NewGuid().ToString();
@@ -191,24 +250,26 @@ namespace PDF_Office.ViewModels
             dictionary.Add("TabItemFill", "");
         }
 
-        private void LoadControl()
+
+
+        private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
         {
-            //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
-            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
+            if (e.PropertyName == "CanSave")
             {
-                region.RequestNavigate(BOTARegionName, "BOTAContent");
+                CanSave = PDFViewer.UndoManager.CanSave;
+            }
+            if(e.PropertyName == "CanUndo")
+            {
+                CanUndo = PDFViewer.UndoManager.CanUndo;
+            }
+            if(e.PropertyName =="CanRedo")
+            {
+                CanRedo = PDFViewer.UndoManager.CanRedo;
             }
-            ));
         }
 
-        /// <summary>
-        /// 将PDFViwer添加到Region
-        /// </summary>
-        private void loadFile()
-        {
-            PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
-            region.AddToRegion(ViwerRegionName, PDFViewer);
-        }
+
+
 
         /// <summary>
         /// 选项卡切换事件
@@ -223,15 +284,17 @@ namespace PDF_Office.ViewModels
                 currentBar = item.Name;
                 if (previousBar != currentBar)
                 {
-                        if(currentBar== "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
-                        {
-                            EnterToolMode(barContentByTabItem[currentBar]);
-                        }
-                        else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
-                        {
-                            EnterSelectedBar(currentBar);
-                        }
-                        previousBar = currentBar;
+                    if(currentBar== "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
+                    {
+                        EnterToolMode(barContentByTabItem[currentBar]);
+                        isInPageEdit = true;
+                    }
+                    else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
+                    {
+                        EnterSelectedBar(currentBar);
+                        isInPageEdit = false;
+                    }
+                    previousBar = currentBar;
                 }
 
             }
@@ -308,6 +371,233 @@ namespace PDF_Office.ViewModels
         #endregion
 
         #region 方法
+        private void Undo()
+        {
+            if(isInPageEdit)
+            {
+                //执行页面编辑的Undo
+                PageEditUndo?.Invoke();
+            }
+            else
+            {
+                PDFViewer.UndoManager.Undo();
+            }
+        }
+
+        private void Redo()
+        {
+            if(isInPageEdit)
+            {
+                //执行页面编辑的Redo
+                PageEditRedo?.Invoke();
+            }
+            else
+            {
+                PDFViewer.UndoManager.Redo();
+            }
+        }
+
+        private void LoadControl()
+        {
+            //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
+            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
+            {
+                region.RequestNavigate(BOTARegionName, "BOTAContent");
+            }
+            ));
+        }
+
+        /// <summary>
+        /// 将PDFViwer添加到Region
+        /// </summary>
+        private void loadFile()
+        {
+            PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
+            PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
+            CanSave = PDFViewer.UndoManager.CanSave;
+            CanUndo = PDFViewer.UndoManager.CanUndo;
+            CanRedo = PDFViewer.UndoManager.CanRedo;
+            region.AddToRegion(ViwerRegionName, PDFViewer);
+        }
+
+        /// <summary>
+        /// 已有路径文档的保存逻辑
+        /// </summary>
+        private bool saveFile()
+        {
+            try
+            {
+                if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
+                    return saveAsFile();
+
+                //文档已被修复时 提示另存为
+                if (PDFViewer.Document.HasRepaired)
+                {
+
+                    alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
+                    if (alertsMessage.result == ContentResult.Ok)
+                        return saveAsFile();
+                    else
+                        return false;
+                }
+
+                //文件路径无法存在时
+                if (!File.Exists(PDFViewer.Document.FilePath))
+                {
+                    alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
+                    if (alertsMessage.result == ContentResult.Ok)
+                        return saveAsFile();
+                    else
+                        return false;
+                }
+
+                //只读文件无法写入时,提示另存为
+                FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
+                if (fileInfo.IsReadOnly)
+                {
+                    alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
+                    if (alertsMessage.result == ContentResult.Ok)
+                        return saveAsFile();
+                    else
+                        return false;
+                }
+
+                bool result = PDFViewer.Document.WriteToLoadedPath();
+                if (result)
+                {
+                    PDFViewer.UndoManager.CanSave = false;
+                    App.Current.Dispatcher.Invoke(() =>
+                    {
+                        //TODO:更新缩略图
+                        //OpenFileInfo info = SettingHelper.GetFileInfo(PdfViewer.Document.FilePath);
+                        //try
+                        //{
+                        //    if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PdfViewer.Document.IsEncrypted)//加密的文档不获取缩略图
+                        //    {
+                        //        var size = PdfViewer.Document.GetPageSize(0);
+                        //        System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PdfViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
+                        //        string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
+                        //        if (File.Exists(folderPath))
+                        //            File.Delete(folderPath);
+                        //        DirectoryInfo folder = new DirectoryInfo(folderPath);
+                        //        if (!folder.Exists)
+                        //            folder.Create();
+                        //        string imagePath = info.ThumbImgPath;
+                        //        if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
+                        //        {
+                        //            string imageName = Guid.NewGuid().ToString();
+                        //            imagePath = System.IO.Path.Combine(folderPath, imageName);
+                        //            using (FileStream stream = new FileStream(imagePath, FileMode.Create))
+                        //            {
+                        //                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
+                        //            }
+                        //        }
+                        //        else
+                        //        {
+                        //            using (FileStream stream = new FileStream(imagePath, FileMode.Open))
+                        //            {
+                        //                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
+                        //            }
+                        //        }
+                        //        info.ThumbImgPath = imagePath;
+                        //        SettingHelper.SetFileInfo(info);
+                        //    }
+                        //}
+                        //catch
+                        //{
+                        //    info.ThumbImgPath = null;
+                        //    SettingHelper.SetFileInfo(info);
+                        //}
+                    });
+                }
+                else
+                {
+                    //文件被占用 保存失败时
+                    alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
+                    if (alertsMessage.result == ContentResult.Ok)
+                        return saveAsFile();
+                    else
+                        return false;
+                }
+
+                return result;
+            }
+            catch { return false; }
+        }
+
+        /// <summary>
+        /// 另存为或新文档保存逻辑
+        /// </summary>
+        private bool saveAsFile()
+        {
+            var dlg = new SaveFileDialog();
+            dlg.Filter = Properties.Resources.OpenDialogFilter;
+            dlg.FileName = PDFViewer.Document.FileName;
+            if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
+            {
+                bool result = false;
+                if (App.OpenedFileList.Contains(dlg.FileName))
+                {
+                    //提示文件已经被打开
+                }
+                else
+                {
+                    result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
+                    if (result)
+                    {
+                        DoAfterSaveAs(dlg.FileName);
+                    }
+                    else
+                    {
+                        //提示文件被其他软件占用 无法保存
+                        //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
+                    }
+                      ;
+                }
+                return result;
+            }
+            else
+                return false;
+        }
+
+        /// <summary>
+        /// 另存为后进行的操作
+        /// 重新打开新文档
+        /// </summary>
+        /// <param name="targetPath"></param>
+        public void DoAfterSaveAs(string targetPath)
+        {
+            App.OpenedFileList.Remove(targetPath);
+            string oldFilePath = targetPath;
+
+            PDFViewer.UndoManager.CanSave = false;
+            PDFViewer.CloseDocument();
+            //var result = OpenFile(targetPath, true);
+
+            //if (result)
+            //{
+            //    FileChanged.Invoke(this, null);
+            //    Zoomer.PdfViewer = PdfViewer;
+            //    PageSelector.PdfViewer = PdfViewer;
+            //    ViewModeSelector.PdfViewer = PdfViewer;
+
+            //    OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
+            //    if (fileInfo != null)
+            //    {
+            //        PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
+            //        PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
+            //        if (fileInfo.LastFitMode == FitMode.FitFree)
+            //            PdfViewer.Zoom(fileInfo.LastZoom);
+
+            //        PdfViewer.GoToPage(fileInfo.LastPageIndex);
+            //        if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
+            //            PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
+            //        else
+            //            PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
+            //    }
+            //}
+        }
+
         /// <summary>
         /// 显示前添加内容到Region
         /// </summary>

+ 1 - 0
PDF Office/Views/PageEdit/PageEditContent.xaml

@@ -255,6 +255,7 @@
                 Grid.Row="1"
                 Padding="0"
                 AllowDrop="True"
+                Background="#E7E9EC"
                 ItemTemplate="{StaticResource PageEditListBoxItemTemplate}"
                 ItemsSource="{Binding PageEditItems}"
                 ScrollBar.Scroll="ListPageEdit_Scroll"

+ 6 - 0
PDF Office/Views/ViewContent.xaml

@@ -45,18 +45,24 @@
             </Button>
             <Button
                 Margin="8"
+                Command="{Binding SaveFile}"
+                IsEnabled="{Binding CanSave}"
                 Style="{StaticResource MenuButtonStyle}"
                 ToolTip="Save">
                 <Path Data="M0 0H10.7071L14 3.29289V14H0V0ZM13 3.707L10.293 1H1V13H3V7H11V13H13V3.707ZM10 13H4V8H10V13ZM6 5V4H3V5H6Z" Fill="#333333" />
             </Button>
             <Button
                 Margin="8"
+                Command="{Binding UndoCommand}"
+                IsEnabled="{Binding CanUndo}"
                 Style="{StaticResource MenuButtonStyle}"
                 ToolTip="Undo">
                 <Path Data="M3.02773 1.12105L2 1.1197L2.00703 6.05868L7.14571 6.06544L7.1443 5.07764L3.95607 5.07345L5.96547 3.14214L6.1139 3.0063C7.72531 1.60263 10.2166 1.64791 11.7713 3.14214C13.3745 4.68306 13.3745 7.18139 11.7713 8.72231L5.96547 14.3025L6.69119 15L12.497 9.41983L12.651 9.26532C14.4996 7.33014 14.4483 4.32008 12.497 2.44461C10.4929 0.518462 7.24377 0.518462 5.23974 2.44461L3.03264 4.56595L3.02773 1.12105Z" Fill="#333333" />
             </Button>
             <Button
                 Margin="8"
+                Command="{Binding RedoCommand}"
+                IsEnabled="{Binding CanRedo}"
                 Style="{StaticResource MenuButtonStyle}"
                 ToolTip="Redo">
                 <Path Data="M10.9737 0.118216L12 0.116865L11.993 5.05009L6.86172 5.05685L6.86312 4.0702L10.0425 4.06602L8.03792 2.13876L7.88969 2.00309C6.28039 0.601253 3.78921 0.649819 2.23256 2.1464C0.627259 3.68974 0.623702 6.18858 2.22461 7.72771L8.02203 13.3014L7.29536 14L1.49795 8.42633L1.34413 8.27199C-0.501568 6.33885 -0.445937 3.3281 1.50788 1.44969C3.51451 -0.479494 6.76344 -0.483769 8.76458 1.44014L10.9688 3.55934L10.9737 0.118216Z" Fill="#333333" />