[ease] [api] internalized slide and element add/remove undo.



commit 273c47699b65043a124f38dc6e16a2573ec15e76
Author: Nate Stedman <natesm gmail com>
Date:   Wed Jul 28 22:39:52 2010 -0400

    [api] internalized slide and element add/remove undo.
    
    Widgets no longer need to create undo actions when
    adding or removing a Slide or Element. It's automagic now.
    
    Outside classes now only have access to UndoAction and the
    base UndoItem.

 ease-core/ease-document.vala             |   41 ++++++++++++++++++++---------
 ease-core/ease-slide.vala                |   32 +++++++++++++++++------
 ease-core/ease-theme.vala                |   10 +++---
 ease-core/ease-undo-action.vala          |    4 +-
 ease-core/ease-undo-actions-element.vala |   12 ++++----
 ease-core/ease-undo-actions-slide.vala   |   16 ++++++------
 ease-core/ease-undo-controller.vala      |   32 ++++++++++++++++++++++-
 src/ease-editor-embed.vala               |    1 -
 src/ease-editor-window.vala              |   22 +++-------------
 src/ease-main.vala                       |    6 ++++
 10 files changed, 113 insertions(+), 63 deletions(-)
---
diff --git a/ease-core/ease-document.vala b/ease-core/ease-document.vala
index 31dc92f..d8aea79 100644
--- a/ease-core/ease-document.vala
+++ b/ease-core/ease-document.vala
@@ -112,13 +112,6 @@ public class Ease.Document : GLib.Object, UndoSource
 	 * Emitted when a { link Slide} is added to the Document.
 	 */
 	public signal void slide_added(Slide slide, int index);
-
-	/**
-	 * Default constructor, creates an empty Document.
-	 * 
-	 * Creates a new, empty document with no slides. Sets up base properties
-	 */
-	public Document() { }
 	
 	public Document.from_saved(string file_path) throws GLib.Error
 	{
@@ -225,17 +218,27 @@ public class Ease.Document : GLib.Object, UndoSource
 	/**
 	 * Inserts a new { link Slide} into the Document
 	 *
-	 * @param s The { link Slide} to insert.
+	 * @param slide The { link Slide} to insert.
 	 * @param index The position of the new { link Slide} in the Document.
 	 */
-	public void add_slide(int index, Slide s)
+	public void add_slide(int index, Slide slide)
 	{
-		s.parent = this;
+		add_slide_actual(index, slide, true);
+	}
+	
+	/**
+	 * Does the actual addition of a new Slide.
+	 */
+	internal void add_slide_actual(int index, Slide slide, bool emit_undo)
+	{
+		slide.parent = this;
 		Gtk.TreeIter itr;
 		slides.insert(out itr, index);
-		slides.set(itr, COL_SLIDE, s);
-		slide_added(s, index);
-		listen(s);
+		slides.set(itr, COL_SLIDE, slide);
+		slide_added(slide, index);
+		listen(slide);
+		
+		if (emit_undo) undo(new SlideAddUndoAction(slide));
 	}
 	
 	/**
@@ -254,6 +257,17 @@ public class Ease.Document : GLib.Object, UndoSource
 	 */
 	public Slide remove_slide(Slide slide)
 	{
+		return remove_slide_actual(slide, true);
+	}
+	
+	/**
+	 * Actually removes a Slide.
+	 */
+	internal Slide remove_slide_actual(Slide slide, bool emit_undo)
+	{
+		// emit an undo action if needed
+		if (emit_undo) undo(new SlideRemoveUndoAction(slide));
+		
 		Slide s;
 		var index = 0;
 		foreach (var itr in slides)
@@ -278,6 +292,7 @@ public class Ease.Document : GLib.Object, UndoSource
 		
 		// retrieve and return the slide
 		slides.get(itr, COL_SLIDE, out ret);
+		
 		return ret;
 	}
 	
diff --git a/ease-core/ease-slide.vala b/ease-core/ease-slide.vala
index 5a1740c..a4335f2 100644
--- a/ease-core/ease-slide.vala
+++ b/ease-core/ease-slide.vala
@@ -276,7 +276,7 @@ public class Ease.Slide : GLib.Object, UndoSource
 				e = new TextElement.from_json(node);
 			}
 			e.element_type = type;
-			add_element(slide.count, e);
+			append(e);
 		}
 	}
 	
@@ -336,12 +336,21 @@ public class Ease.Slide : GLib.Object, UndoSource
 	 * @param index The index to add the { link Element} at.
 	 * @param e The { link Element} to add.
 	 */
-	public void add_element(int index, Element e)
+	public void add(int index, Element e)
+	{
+		add_actual(index, e, true);
+	}
+	
+	/**
+	 * Actual adds an Element.
+	 */
+	internal void add_actual(int index, Element e, bool emit_undo)
 	{
 		e.parent = this;
 		elements.insert(index, e);
 		element_added(this, e, index);
 		listen(e);
+		if (emit_undo) undo(new ElementAddUndoAction(e));
 	}
 	
 	/**
@@ -349,16 +358,25 @@ public class Ease.Slide : GLib.Object, UndoSource
 	 * 
 	 * @param e The element to add;.
 	 */
-	public void add(Element e)
+	public void append(Element e)
 	{
-		add_element(count, e);
+		add(count, e);
 	}
 	
 	/**
 	 * Removes an { link Element} from this slide.
 	 */
-	public void remove_element(Element e)
+	public void remove(Element e)
+	{
+		remove_actual(e, true);
+	}
+	
+	/**
+	 * Actually removes an Element.
+	 */
+	internal void remove_actual(Element e, bool emit_undo)
 	{
+		if (emit_undo) undo(new ElementRemoveUndoAction(e));
 		var index = index_of(e);
 		elements.remove(e);
 		element_removed(this, e, index);
@@ -371,9 +389,7 @@ public class Ease.Slide : GLib.Object, UndoSource
 	public void remove_at(int index)
 	{
 		var e = elements.get(index);
-		elements.remove_at(index);
-		element_removed(this, e, index);
-		silence(e);
+		remove(e);
 	}
 	
 	/**
diff --git a/ease-core/ease-theme.vala b/ease-core/ease-theme.vala
index 36e4911..12a6009 100644
--- a/ease-core/ease-theme.vala
+++ b/ease-core/ease-theme.vala
@@ -358,7 +358,7 @@ public class Ease.Theme : GLib.Object
 				// create the presentation's title
 				int left = element_get(TITLE_TEXT, PAD_LEFT).to_int(),
 				    h = element_get(TITLE_TEXT, HEIGHT).to_int();
-				slide.add(create_text(
+				slide.append(create_text(
 					TITLE_TEXT,
 					left,
 					height / 2 - h - element_get(TITLE_TEXT, PAD_BOTTOM).to_int(),
@@ -368,7 +368,7 @@ public class Ease.Theme : GLib.Object
 				
 				// create the presentation's author field
 				left = element_get(AUTHOR_TEXT, PAD_LEFT).to_int();
-				slide.add(create_text(
+				slide.append(create_text(
 					AUTHOR_TEXT,
 					left,
 					height / 2 + element_get(AUTHOR_TEXT, PAD_TOP).to_int(),
@@ -381,7 +381,7 @@ public class Ease.Theme : GLib.Object
 				int left = element_get(CONTENT_TEXT, PAD_LEFT).to_int(),
 				    top = element_get(CONTENT_TEXT, PAD_TOP).to_int();
 				
-				slide.add(create_text(
+				slide.append(create_text(
 					CONTENT_TEXT,
 					left,
 					top,
@@ -395,7 +395,7 @@ public class Ease.Theme : GLib.Object
 				int left = element_get(HEADER_TEXT, PAD_LEFT).to_int(),
 				    top = element_get(HEADER_TEXT, PAD_TOP).to_int();
 				
-				slide.add(create_text(
+				slide.append(create_text(
 					HEADER_TEXT,
 					left,
 					top,
@@ -408,7 +408,7 @@ public class Ease.Theme : GLib.Object
 				top += element_get(HEADER_TEXT, HEIGHT).to_int() +
 				       element_get(HEADER_TEXT, PAD_BOTTOM).to_int() +
 				       element_get(CONTENT_TEXT, PAD_TOP).to_int();
-				slide.add(create_text(
+				slide.append(create_text(
 					CONTENT_TEXT,
 					left,
 					top,
diff --git a/ease-core/ease-undo-action.vala b/ease-core/ease-undo-action.vala
index 689981c..7c044ec 100644
--- a/ease-core/ease-undo-action.vala
+++ b/ease-core/ease-undo-action.vala
@@ -20,7 +20,7 @@
  */
 public class Ease.UndoAction : UndoItem
 {
-	private Gee.LinkedList<UndoPair> pairs = new Gee.LinkedList<UndoPair>();
+	internal Gee.LinkedList<UndoPair> pairs = new Gee.LinkedList<UndoPair>();
 	
 	/**
 	 * Creates an UndoAction.
@@ -86,7 +86,7 @@ public class Ease.UndoAction : UndoItem
 	/**
 	 * Embedded class for storing object/property pairs in undo actions.
 	 */
-	private class UndoPair
+	internal class UndoPair
 	{
 		public string property;
 		public GLib.Object object;
diff --git a/ease-core/ease-undo-actions-element.vala b/ease-core/ease-undo-actions-element.vala
index 39e7bb2..a797e5f 100644
--- a/ease-core/ease-undo-actions-element.vala
+++ b/ease-core/ease-undo-actions-element.vala
@@ -18,7 +18,7 @@
 /**
  * Undos the addition of an { link Element} to a { link Slide}.
  */
-public class Ease.ElementAddUndoAction : UndoItem
+internal class Ease.ElementAddUndoAction : UndoItem
 {
 	/**
 	 * The { link Element} that was added.
@@ -30,7 +30,7 @@ public class Ease.ElementAddUndoAction : UndoItem
 	 *
 	 * @param e The element that was added.
 	 */
-	public ElementAddUndoAction(Element e)
+	internal ElementAddUndoAction(Element e)
 	{
 		element = e;
 	}
@@ -41,7 +41,7 @@ public class Ease.ElementAddUndoAction : UndoItem
 	internal override UndoItem apply()
 	{
 		var action = new ElementRemoveUndoAction(element);
-		element.parent.remove_element(element);
+		element.parent.remove_actual(element, false);
 		return action;
 	}
 }
@@ -49,7 +49,7 @@ public class Ease.ElementAddUndoAction : UndoItem
 /**
  * Undos the removal of an { link Element} from a { link Slide}.
  */
-public class Ease.ElementRemoveUndoAction : UndoItem
+internal class Ease.ElementRemoveUndoAction : UndoItem
 {
 	/**
 	 * The { link Element} that was removed.
@@ -73,7 +73,7 @@ public class Ease.ElementRemoveUndoAction : UndoItem
 	 *
 	 * @param e The element that was added.
 	 */
-	public ElementRemoveUndoAction(Element e)
+	internal ElementRemoveUndoAction(Element e)
 	{
 		element = e;
 		slide = e.parent;
@@ -85,7 +85,7 @@ public class Ease.ElementRemoveUndoAction : UndoItem
 	 */
 	internal override UndoItem apply()
 	{
-		slide.add_element(index, element);
+		slide.add_actual(index, element, false);
 		return new ElementAddUndoAction(element);
 	}
 }
diff --git a/ease-core/ease-undo-actions-slide.vala b/ease-core/ease-undo-actions-slide.vala
index bb14620..746cc9a 100644
--- a/ease-core/ease-undo-actions-slide.vala
+++ b/ease-core/ease-undo-actions-slide.vala
@@ -18,7 +18,7 @@
 /**
  * Undos the addition of an { link Slide} to a { link Document}.
  */
-public class Ease.SlideAddUndoAction : UndoItem
+internal class Ease.SlideAddUndoAction : UndoItem
 {
 	/**
 	 * The { link Slide} that was added.
@@ -30,7 +30,7 @@ public class Ease.SlideAddUndoAction : UndoItem
 	 *
 	 * @param s The slide that was added.
 	 */
-	public SlideAddUndoAction(Slide s)
+	internal SlideAddUndoAction(Slide s)
 	{
 		slide = s;
 	}
@@ -38,10 +38,10 @@ public class Ease.SlideAddUndoAction : UndoItem
 	/**
 	 * Applies the action, removing the { link Slide}.
 	 */
-	public override UndoItem apply()
+	internal override UndoItem apply()
 	{
 		var action = new SlideRemoveUndoAction(slide);
-		slide.parent.remove_slide(slide);
+		slide.parent.remove_slide_actual(slide, false);
 		return action;
 	}
 }
@@ -49,7 +49,7 @@ public class Ease.SlideAddUndoAction : UndoItem
 /**
  * Undos the removal of an { link Slide} from a { link Document}.
  */
-public class Ease.SlideRemoveUndoAction : UndoItem
+internal class Ease.SlideRemoveUndoAction : UndoItem
 {
 	/**
 	 * The { link Slide} that was removed.
@@ -73,7 +73,7 @@ public class Ease.SlideRemoveUndoAction : UndoItem
 	 *
 	 * @param s The slide that was added.
 	 */
-	public SlideRemoveUndoAction(Slide s)
+	internal SlideRemoveUndoAction(Slide s)
 	{
 		slide = s;
 		document = s.parent;
@@ -83,9 +83,9 @@ public class Ease.SlideRemoveUndoAction : UndoItem
 	/**
 	 * Applies the action, restoring the { link Slide}.
 	 */
-	public override UndoItem apply()
+	internal override UndoItem apply()
 	{
-		document.add_slide(index, slide);
+		document.add_slide_actual(index, slide, false);
 		return new SlideAddUndoAction(slide);
 	}
 }
diff --git a/ease-core/ease-undo-controller.vala b/ease-core/ease-undo-controller.vala
index 96fe220..84c1663 100644
--- a/ease-core/ease-undo-controller.vala
+++ b/ease-core/ease-undo-controller.vala
@@ -33,6 +33,11 @@ public class Ease.UndoController : Object
 	private Gee.LinkedList<UndoItem> redos = new Gee.LinkedList<UndoItem>();
 	
 	/**
+	 * Allows debug messages to be printed every time an action is added.
+	 */
+	public static bool enable_debug { get; set; default = false; }
+	
+	/**
 	 * Creates an UndoController. Used by EditorWindow.
 	 */
 	public UndoController() { }
@@ -84,7 +89,18 @@ public class Ease.UndoController : Object
 	 */
 	public void add_action(UndoItem action)
 	{
-		debug("adding an action");
+		if (enable_debug)
+		{
+			if (action.get_type() == typeof(UndoAction))
+			{
+				stdout.printf("UNDO ACTION ADDED WITH THESE PROPERTIES:\n");
+				foreach (var pair in (action as UndoAction).pairs)
+				{
+					stdout.printf("\t%s\n", pair.property);
+				}
+				stdout.printf("\n");
+			}
+		}
 		undos.offer_head(action);
 	}
 	
@@ -95,7 +111,19 @@ public class Ease.UndoController : Object
 	 */
 	private void add_redo_action(UndoItem action)
 	{
-		debug("adding a redo action");
+		if (enable_debug)
+		{
+			if (action.get_type() == typeof(UndoAction))
+			{
+				stdout.printf("REDO ACTION ADDED WITH THESE PROPERTIES:\n");
+				foreach (var pair in (action as UndoAction).pairs)
+				{
+					stdout.printf("\t%s\n", pair.property);
+				}
+				stdout.printf("\n");
+			}
+		}
+		
 		redos.offer_head(action);
 	}
 }
diff --git a/src/ease-editor-embed.vala b/src/ease-editor-embed.vala
index a954393..11dfbd1 100644
--- a/src/ease-editor-embed.vala
+++ b/src/ease-editor-embed.vala
@@ -781,7 +781,6 @@ public class Ease.EditorEmbed : ScrollableEmbed, UndoSource
 				
 				var slide = slide_actor.slide;
 				var i = slide.index_of(selected.element);
-				undo(new ElementRemoveUndoAction(slide.element_at(i)));
 				slide.remove_at(i);
 				element_deselected(null);
 				
diff --git a/src/ease-editor-window.vala b/src/ease-editor-window.vala
index 4168897..4438e56 100644
--- a/src/ease-editor-window.vala
+++ b/src/ease-editor-window.vala
@@ -274,7 +274,6 @@ public class Ease.EditorWindow : Gtk.Window
 		if (embed.selected == null) return;
 		
 		var i = slide.index_of(embed.selected.element);
-		add_undo_action(new ElementRemoveUndoAction(slide.element_at(i)));
 		slide.remove_at(i);
 	}
 
@@ -287,9 +286,6 @@ public class Ease.EditorWindow : Gtk.Window
 		
 		var index = document.index_of(slide) + 1;
 		
-		// add an undo action
-		add_undo_action(new SlideAddUndoAction(s));
-		
 		document.add_slide(index, s);
 	}
 	
@@ -302,9 +298,6 @@ public class Ease.EditorWindow : Gtk.Window
 		
 		var index = document.index_of(slide) + 1;
 		
-		// add an undo action
-		add_undo_action(new SlideAddUndoAction(s));
-		
 		document.add_slide(index, s);
 	}
 	
@@ -314,9 +307,6 @@ public class Ease.EditorWindow : Gtk.Window
 		// don't remove the last slide in a document
 		if (document.length < 2) return;
 		
-		// add an undo action
-		add_undo_action(new SlideRemoveUndoAction(slide));
-		
 		// set the slide to something safe
 		slide_button_panel.select_slide(document.remove_slide(slide));
 	}
@@ -359,8 +349,7 @@ public class Ease.EditorWindow : Gtk.Window
 		var text = document.theme.create_custom_text();
 		text.x = slide.width / 2 - text.width / 2;
 		text.y = slide.height / 2 - text.height / 2;
-		slide.add(text);
-		add_undo_action(new ElementAddUndoAction(text));
+		slide.append(text);
 		embed.select_element(text);
 	}
 	
@@ -397,8 +386,7 @@ public class Ease.EditorWindow : Gtk.Window
 				e.source_filename = dialog.get_filename();
 				
 				// add the element
-				slide.add(e);
-				add_undo_action(new ElementAddUndoAction(e));
+				slide.append(e);
 				embed.select_element(e);
 			}
 			catch (Error e)
@@ -417,8 +405,7 @@ public class Ease.EditorWindow : Gtk.Window
 		rect.height = 300;
 		rect.x = document.width / 2 - rect.width / 2;
 		rect.y = document.height / 2 - rect.height / 2;
-		slide.add(rect);
-		add_undo_action(new ElementAddUndoAction(rect));
+		slide.append(rect);
 		embed.select_element(rect);
 	}
 	
@@ -430,8 +417,7 @@ public class Ease.EditorWindow : Gtk.Window
 		oval.height = 300;
 		oval.x = document.width / 2 - oval.width / 2;
 		oval.y = document.height / 2 - oval.height / 2;
-		slide.add(oval);
-		add_undo_action(new ElementAddUndoAction(oval));
+		slide.append(oval);
 		embed.select_element(oval);
 	}
 	
diff --git a/src/ease-main.vala b/src/ease-main.vala
index 470b1f9..892fbdd 100644
--- a/src/ease-main.vala
+++ b/src/ease-main.vala
@@ -24,12 +24,15 @@ public class Ease.Main : GLib.Object
 	static string play_filename;
 	static string[] filenames;
 	public static bool presentation_windowed = false;
+	private static bool debug_undo = false;
 	
 	private const OptionEntry[] options = {
 		{ "play", 'p', 0, OptionArg.FILENAME, ref play_filename,
 		   "Play the specified file", "FILE" },
 		{ "window", 'w', 0, OptionArg.NONE, ref presentation_windowed,
 		  "Display presentations in a window", null},
+		{ "debug-undo", 0, 0, OptionArg.NONE, ref debug_undo,
+		  "Display debugging messages about undo actions", null },
 		{ "", 0, 0, OptionArg.FILENAME_ARRAY, ref filenames, null, "FILE..." },
 		{ null } };
 	
@@ -68,6 +71,9 @@ public class Ease.Main : GLib.Object
 			stdout.printf(_("error parsing options: %s\n"), e.message);
 			return 1;
 		}
+		
+		// react to command line flags
+		UndoController.enable_debug = debug_undo;
 
 		// initalize static classes
 		windows = new Gee.ArrayList<EditorWindow>();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]