PageRenderTime 47ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/HistoryFunctions/FlattenFunction.cs

https://bitbucket.org/tuldok89/openpdn
C# | 68 lines | 48 code | 11 blank | 9 comment | 3 complexity | 3c4319fc818ce6636d705758f253415c MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using PaintDotNet.HistoryMementos;
  10. using System.Collections.Generic;
  11. namespace PaintDotNet.HistoryFunctions
  12. {
  13. internal sealed class FlattenFunction
  14. : HistoryFunction
  15. {
  16. public static string StaticName
  17. {
  18. get
  19. {
  20. return PdnResources.GetString("FlattenFunction.Name");
  21. }
  22. }
  23. public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
  24. {
  25. object savedSelection = null;
  26. var actions = new List<HistoryMemento>();
  27. if (!historyWorkspace.Selection.IsEmpty)
  28. {
  29. savedSelection = historyWorkspace.Selection.Save();
  30. var da = new DeselectFunction();
  31. HistoryMemento hm = da.Execute(historyWorkspace);
  32. actions.Add(hm);
  33. }
  34. var rdha = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace);
  35. actions.Add(rdha);
  36. var chm = new CompoundHistoryMemento(
  37. StaticName,
  38. PdnResources.GetImageResource("Icons.MenuImageFlattenIcon.png"),
  39. actions);
  40. // TODO: we can save memory here by serializing, then flattening on to an existing layer
  41. Document flat = historyWorkspace.Document.Flatten();
  42. EnterCriticalRegion();
  43. historyWorkspace.Document = flat;
  44. if (savedSelection != null)
  45. {
  46. var shm = new SelectionHistoryMemento(null, null, historyWorkspace);
  47. historyWorkspace.Selection.Restore(savedSelection);
  48. chm.PushNewAction(shm);
  49. }
  50. return chm;
  51. }
  52. public FlattenFunction()
  53. : base(ActionFlags.None)
  54. {
  55. }
  56. }
  57. }