FileViewModel.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using Microsoft.Win32;
  7. using PixiEditor.Exceptions;
  8. using PixiEditor.Helpers;
  9. using PixiEditor.Models.DataHolders;
  10. using PixiEditor.Models.Dialogs;
  11. using PixiEditor.Models.Enums;
  12. using PixiEditor.Models.IO;
  13. using PixiEditor.Models.UserPreferences;
  14. namespace PixiEditor.ViewModels.SubViewModels.Main
  15. {
  16. public class FileViewModel : SubViewModel<ViewModelMain>
  17. {
  18. public RelayCommand OpenNewFilePopupCommand { get; set; }
  19. public RelayCommand SaveDocumentCommand { get; set; }
  20. public RelayCommand OpenFileCommand { get; set; }
  21. public RelayCommand ExportFileCommand { get; set; } // Command that is used to save file
  22. public FileViewModel(ViewModelMain owner)
  23. : base(owner)
  24. {
  25. OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
  26. SaveDocumentCommand = new RelayCommand(SaveDocument, Owner.DocumentIsNotNull);
  27. OpenFileCommand = new RelayCommand(Open);
  28. ExportFileCommand = new RelayCommand(ExportFile, CanSave);
  29. Owner.OnStartupEvent += Owner_OnStartupEvent;
  30. }
  31. /// <summary>
  32. /// Generates new Layer and sets it as active one.
  33. /// </summary>
  34. /// <param name="parameter">CommandParameter.</param>
  35. public void OpenNewFilePopup(object parameter)
  36. {
  37. NewFileDialog newFile = new NewFileDialog();
  38. if (newFile.ShowDialog())
  39. {
  40. NewDocument(newFile.Width, newFile.Height);
  41. }
  42. }
  43. public void NewDocument(int width, int height, bool addBaseLayer = true)
  44. {
  45. Owner.BitmapManager.ActiveDocument = new Document(width, height);
  46. if (addBaseLayer)
  47. {
  48. Owner.BitmapManager.AddNewLayer("Base Layer");
  49. }
  50. Owner.ResetProgramStateValues();
  51. }
  52. /// <summary>
  53. /// Opens file from path.
  54. /// </summary>
  55. /// <param name="path">Path to file.</param>
  56. public void OpenFile(string path)
  57. {
  58. ImportFileDialog dialog = new ImportFileDialog();
  59. if (path != null && File.Exists(path))
  60. {
  61. dialog.FilePath = path;
  62. }
  63. if (dialog.ShowDialog())
  64. {
  65. NewDocument(dialog.FileWidth, dialog.FileHeight, false);
  66. Owner.BitmapManager.AddNewLayer("Image", Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight));
  67. }
  68. }
  69. public void SaveDocument(bool asNew)
  70. {
  71. SaveDocument(parameter: asNew ? "asnew" : null);
  72. }
  73. private void Owner_OnStartupEvent(object sender, System.EventArgs e)
  74. {
  75. var lastArg = Environment.GetCommandLineArgs().Last();
  76. if (Importer.IsSupportedFile(lastArg) && File.Exists(lastArg))
  77. {
  78. Open(lastArg);
  79. }
  80. else
  81. {
  82. if (PreferencesSettings.GetPreference("ShowNewFilePopupOnStartup", true))
  83. {
  84. OpenNewFilePopup(null);
  85. }
  86. }
  87. }
  88. private void Open(string path)
  89. {
  90. if (Owner.DocumentSubViewModel.UnsavedDocumentModified)
  91. {
  92. var result = ConfirmationDialog.Show(DocumentViewModel.ConfirmationDialogMessage);
  93. if (result == ConfirmationType.Yes)
  94. {
  95. SaveDocument(null);
  96. }
  97. else if (result == ConfirmationType.Canceled)
  98. {
  99. return;
  100. }
  101. }
  102. try
  103. {
  104. Owner.ResetProgramStateValues();
  105. if (path.EndsWith(".pixi"))
  106. {
  107. OpenDocument(path);
  108. }
  109. else
  110. {
  111. OpenFile(path);
  112. }
  113. }
  114. catch (CorruptedFileException ex)
  115. {
  116. MessageBox.Show(ex.Message, "Failed to open file.", MessageBoxButton.OK, MessageBoxImage.Error);
  117. }
  118. }
  119. private void Open(object property)
  120. {
  121. OpenFileDialog dialog = new OpenFileDialog
  122. {
  123. Filter = "All Files|*.*|PixiEditor Files | *.pixi|PNG Files|*.png",
  124. DefaultExt = "pixi"
  125. };
  126. if ((bool)dialog.ShowDialog())
  127. {
  128. if (Importer.IsSupportedFile(dialog.FileName))
  129. {
  130. Open(dialog.FileName);
  131. }
  132. Owner.ViewportSubViewModel.CenterViewport();
  133. }
  134. }
  135. private void OpenDocument(string path)
  136. {
  137. Owner.BitmapManager.ActiveDocument = Importer.ImportDocument(path);
  138. Exporter.SaveDocumentPath = path;
  139. Owner.DocumentSubViewModel.UnsavedDocumentModified = false;
  140. }
  141. private void SaveDocument(object parameter)
  142. {
  143. bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
  144. if (paramIsAsNew || Exporter.SaveDocumentPath == null)
  145. {
  146. var saved = Exporter.SaveAsEditableFileWithDialog(Owner.BitmapManager.ActiveDocument, !paramIsAsNew);
  147. Owner.DocumentSubViewModel.UnsavedDocumentModified = Owner.DocumentSubViewModel.UnsavedDocumentModified && !saved;
  148. }
  149. else
  150. {
  151. Exporter.SaveAsEditableFile(Owner.BitmapManager.ActiveDocument, Exporter.SaveDocumentPath);
  152. Owner.DocumentSubViewModel.UnsavedDocumentModified = false;
  153. }
  154. }
  155. /// <summary>
  156. /// Generates export dialog or saves directly if save data is known.
  157. /// </summary>
  158. /// <param name="parameter">CommandProperty.</param>
  159. private void ExportFile(object parameter)
  160. {
  161. WriteableBitmap bitmap = Owner.BitmapManager.GetCombinedLayersBitmap();
  162. Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
  163. }
  164. /// <summary>
  165. /// Returns true if file save is possible.
  166. /// </summary>
  167. /// <param name="property">CommandProperty.</param>
  168. /// <returns>True if active document is not null.</returns>
  169. private bool CanSave(object property)
  170. {
  171. return Owner.BitmapManager.ActiveDocument != null;
  172. }
  173. }
  174. }