[longomatch/redesign: 18/82] Make deletion work with the new API



commit 47ee6faa5cf04ad1d66a8abca57110485dd8a5c0
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Sat Nov 20 21:08:09 2010 +0100

    Make deletion work with the new API

 LongoMatch/DB/Project.cs                          |    7 +-
 LongoMatch/Gui/Component/PlayersListTreeWidget.cs |   66 +++++++++++++--------
 LongoMatch/Gui/Component/PlaysListTreeWidget.cs   |   51 ++++++++++------
 LongoMatch/Gui/Component/TagsTreeWidget.cs        |   33 ++++++-----
 LongoMatch/Gui/Component/TimeLineWidget.cs        |   12 ++--
 LongoMatch/Gui/Component/TimeScale.cs             |    4 +-
 LongoMatch/Gui/TreeView/ListTreeViewBase.cs       |   34 +++++-----
 LongoMatch/Handlers/EventsManager.cs              |   17 +++---
 LongoMatch/Handlers/Handlers.cs                   |    4 +-
 9 files changed, 131 insertions(+), 97 deletions(-)
---
diff --git a/LongoMatch/DB/Project.cs b/LongoMatch/DB/Project.cs
index 29969d0..d6de7b6 100644
--- a/LongoMatch/DB/Project.cs
+++ b/LongoMatch/DB/Project.cs
@@ -161,8 +161,9 @@ namespace LongoMatch.DB
 		/// <param name="section">
 		/// A <see cref="System.Int32"/>: category the play belongs to
 		/// </param>
-		public void RemovePlay(Play play) {
-			playsList.Remove(play);
+		public void RemovePlays(List<Play> plays) {
+			foreach (Play play in plays)
+				playsList.Remove(play);
 		}
 
 		/// <summary>
@@ -182,7 +183,7 @@ namespace LongoMatch.DB
 					where play.Category.UUID == category.UUID
 					select play;
 			/* Delete them */
-			foreach (var play in playsList)
+			foreach (var play in plays)
 				playsList.Remove(play);
 		}
 		
diff --git a/LongoMatch/Gui/Component/PlayersListTreeWidget.cs b/LongoMatch/Gui/Component/PlayersListTreeWidget.cs
index 11040e0..574b0d9 100644
--- a/LongoMatch/Gui/Component/PlayersListTreeWidget.cs
+++ b/LongoMatch/Gui/Component/PlayersListTreeWidget.cs
@@ -17,6 +17,7 @@
 //
 
 using System;
+using System.Collections.Generic;
 using Gtk;
 using Mono.Unix;
 using LongoMatch.DB;
@@ -61,39 +62,52 @@ namespace LongoMatch.Gui.Component
 			}
 		}
 
-		public void RemovePlay(Play tNode, Player player) {
-			/*if (template != null) {
+		public void RemovePlays(List<Play> plays) {
+			TreeIter iter, child;
+			TreeStore model;
+			List<TreeIter> removeIters;
+			
+			if (template == null)
+				return;
+				
+			removeIters = new List<TreeIter>();
+			model = (TreeStore)playerstreeview.Model;
+			model.GetIterFirst(out iter);
+			do{
+				if (!model.IterHasChild(iter))
+					continue;
 				
-				TreeIter iter;
-				TreeStore model = (TreeStore)playerstreeview.Model;
-				model.GetIterFromString(out iter, player.ToString());
-				TreeIter child;
 				model.IterChildren(out child, iter);
-				// Searching the TimeNode to remove it
-				while (model.IterIsValid(child)) {
-					Play mtn = (Play) model.GetValue(child,0);
-					if (mtn == tNode) {
-						model.Remove(ref child);
-						break;
+				do {
+					Play play = (Play) model.GetValue(child,0);
+					if (plays.Contains(play)) {
+						removeIters.Add(child);
 					}
-					TreeIter prev = child;
-					model.IterNext(ref child);
-					if (prev.Equals(child))
-						break;
-				}
-			}*/
+				} while (model.IterNext(ref child)); 
+			} while (model.IterNext(ref iter));
+			
+			for (int i=0; i < removeIters.Count; i++){
+				iter = removeIters[i];
+				model.Remove(ref iter);
+			}
 		}
 
 
 		public void AddPlay(Play play, Player player) {
-			/*if (template != null) {
-				TreeIter iter;
-				TreeStore model = (TreeStore)playerstreeview.Model;
-				model.GetIterFromString(out iter, playerindex.ToString());
-				Player player = (Player)model.GetValue(iter,0);
-				if (template.GetPlayer(playerindex) == player)
-					model.AppendValues(iter,tNode);
-			}*/
+			TreeIter iter;
+			TreeStore model;
+			bool found;
+				
+			if (template == null)
+				return;
+			model = (TreeStore)playerstreeview.Model;
+			model.GetIterFirst(out iter);
+			do{
+				if (model.GetValue(iter, 0) == player){
+					model.AppendValues(iter, player);
+					break;
+				}
+			} while (model.IterNext(ref iter));
 		}
 
 		public void SetTeam(TeamTemplate template, TreeStore model) {
diff --git a/LongoMatch/Gui/Component/PlaysListTreeWidget.cs b/LongoMatch/Gui/Component/PlaysListTreeWidget.cs
index e9f2dc6..257c9f5 100644
--- a/LongoMatch/Gui/Component/PlaysListTreeWidget.cs
+++ b/LongoMatch/Gui/Component/PlaysListTreeWidget.cs
@@ -19,6 +19,7 @@
 //
 
 using System;
+using System.Collections.Generic;
 using Gtk;
 using Mono.Unix;
 using LongoMatch.DB;
@@ -57,27 +58,37 @@ namespace LongoMatch.Gui.Component
             treeview.TagPlay += OnTagPlay;
 		}
 
-		public void RemovePlay(Play play) {
-			if (project != null) {
-				TreeIter iter;
-				TreeIter child;
+		public void RemovePlays(List<Play> plays) {
+			TreeIter iter, child;
+			TreeStore model;
+			List<TreeIter> removeIters;
+			
+			if (project == null)
+				return;
+			
+			removeIters = new List<TreeIter>();
+			model = (TreeStore)treeview.Model;
+			model.GetIterFirst(out iter);
+			/* Scan all the tree and store the iter of each play 
+			 * we need to delete, but don't delete it yet so that
+			 * we don't alter the tree */
+			do{
+				if (!model.IterHasChild(iter))
+					continue;
 				
-				var category = play.Category;
-				var model = (TreeStore)treeview.Model;
-				model.GetIterFromString(out iter, CategoryPath(category));
 				model.IterChildren(out child, iter);
-				// Searching the TimeNode to remove it
-				while (model.IterIsValid(child)) {
-					Play mtn = (Play) model.GetValue(child,0);
-					if (mtn == play) {
-						model.Remove(ref child);
-						break;
+				do {
+					Play play = (Play) model.GetValue(child,0);
+					if (plays.Contains(play)) {
+						removeIters.Add(child);
 					}
-					TreeIter prev = child;
-					model.IterNext(ref child);
-					if (prev.Equals(child))
-						break;
-				}
+				} while (model.IterNext(ref child)); 
+			} while (model.IterNext(ref iter));
+			
+			/* Remove the selected iters now */
+			for (int i=0; i < removeIters.Count; i++){
+				iter = removeIters[i];
+				model.Remove(ref iter);
 			}
 		}
 
@@ -138,9 +149,9 @@ namespace LongoMatch.Gui.Component
 				TimeNodeSelected(tNode);
 		}
 
-		protected virtual void OnTimeNodeDeleted(Play tNode){
+		protected virtual void OnTimeNodeDeleted(List<Play> plays){
 			if (TimeNodeDeleted != null)
-				TimeNodeDeleted(tNode);
+				TimeNodeDeleted(plays);
 		}
 
 		protected virtual void OnPlayListNodeAdded(Play tNode)
diff --git a/LongoMatch/Gui/Component/TagsTreeWidget.cs b/LongoMatch/Gui/Component/TagsTreeWidget.cs
index 4736cfb..75a6374 100644
--- a/LongoMatch/Gui/Component/TagsTreeWidget.cs
+++ b/LongoMatch/Gui/Component/TagsTreeWidget.cs
@@ -78,21 +78,26 @@ namespace LongoMatch.Gui.Component
 			filter.Refilter();
 		}
 
-		public void DeletePlay(Play play) {
-			if (project != null) {
-				TreeIter iter;
-				model.GetIterFirst(out iter);
-				while (model.IterIsValid(iter)) {
-					Play mtn = (Play) model.GetValue(iter,0);
-					if (mtn == play) {
-						model.Remove(ref iter);
-						break;
-					}
-					TreeIter prev = iter;
-					model.IterNext(ref iter);
-					if (prev.Equals(iter))
-						break;
+		public void RemovePlays(List<Play> plays) {
+			TreeIter iter;
+			List<TreeIter> removeIters;
+			
+			if (project == null)
+				return;
+			
+			removeIters = new List<TreeIter>();
+			model.GetIterFirst(out iter);
+			
+			do {
+				Play play = (Play) model.GetValue(iter,0);
+				if (plays.Contains(play)) {
+					removeIters.Add(iter);
 				}
+			} while (model.IterNext(ref iter)); 
+			
+			for (int i=0; i < removeIters.Count; i++){
+				iter = removeIters[i];
+				model.Remove(ref iter);
 			}
 		}
 
diff --git a/LongoMatch/Gui/Component/TimeLineWidget.cs b/LongoMatch/Gui/Component/TimeLineWidget.cs
index e4081cf..6abad27 100644
--- a/LongoMatch/Gui/Component/TimeLineWidget.cs
+++ b/LongoMatch/Gui/Component/TimeLineWidget.cs
@@ -183,10 +183,12 @@ namespace LongoMatch.Gui.Component {
 				ts.AddPlay(play);
 		}
 		
-		public void RemovePlay (Play play){
+		public void RemovePlays (List<Play> plays){
 			TimeScale ts;
-			if (tsList.TryGetValue(play.Category, out ts))
-				ts.RemovePlay(play);
+			foreach (Play play in plays){
+				if (tsList.TryGetValue(play.Category, out ts))
+					ts.RemovePlay(play);
+			}
 			
 		}
 		private void ResetGui() {
@@ -211,9 +213,9 @@ namespace LongoMatch.Gui.Component {
 			if (TimeNodeSelected != null)
 				TimeNodeSelected(tn);
 		}
-		protected virtual void OnTimeNodeDeleted(Play tn) {
+		protected virtual void OnTimeNodeDeleted(List<Play> plays) {
 			if (TimeNodeDeleted != null)
-				TimeNodeDeleted(tn);
+				TimeNodeDeleted(plays);
 		}
 
 		protected virtual void OnFitbuttonClicked(object sender, System.EventArgs e)
diff --git a/LongoMatch/Gui/Component/TimeScale.cs b/LongoMatch/Gui/Component/TimeScale.cs
index a49c43b..a6aae2d 100644
--- a/LongoMatch/Gui/Component/TimeScale.cs
+++ b/LongoMatch/Gui/Component/TimeScale.cs
@@ -292,7 +292,9 @@ namespace LongoMatch.Gui.Component
 			Play tNode;
 			dic.TryGetValue((MenuItem)obj, out tNode);
 			if (TimeNodeDeleted != null && tNode != null) {
-				TimeNodeDeleted(tNode);
+				var list = new List<Play>();
+				list.Add(tNode);
+				TimeNodeDeleted(list);
 			}
 		}
 
diff --git a/LongoMatch/Gui/TreeView/ListTreeViewBase.cs b/LongoMatch/Gui/TreeView/ListTreeViewBase.cs
index e9c23c9..7387a0a 100644
--- a/LongoMatch/Gui/TreeView/ListTreeViewBase.cs
+++ b/LongoMatch/Gui/TreeView/ListTreeViewBase.cs
@@ -207,11 +207,6 @@ namespace LongoMatch.Gui.Component
 			tag.Sensitive = !enabled;
 		}
 		
-		protected int GetSectionFromIter(TreeIter iter) {
-			TreePath path = Model.GetPath(iter);
-			return int.Parse(path.ToString().Split(':')[0]);
-		}
-		
 		protected object GetValueFromPath(TreePath path){
 			Gtk.TreeIter iter;
 			Model.GetIter(out iter, path);
@@ -311,20 +306,25 @@ namespace LongoMatch.Gui.Component
 		}
 
 		protected void OnDeleted(object obj, EventArgs args) {
-			if (TimeNodeDeleted == null)
-				return;
-			List<Play> list = new List<Play>();
+			List <Play> playsList = new List<Play>();
+			List <TreeIter> iters = new List<TreeIter>();
 			TreePath[] paths = Selection.GetSelectedRows();
-			for (int i=0; i<paths.Length; i++){	
-				list.Add((Play)GetValueFromPath(paths[i]));
+			
+			/* Get the iter for all of the paths first, because the path changes
+			 * each time a row is deleted */
+			foreach (var path in paths) {
+				TreeIter iter;
+				Model.GetIter(out iter, path);
+				playsList.Add((Play)Model.GetValue(iter, 0));
+				iters.Add(iter);
 			}
-			// When a TimeNode is deleted from the tree the path changes.
-			// We need first to retrieve all the TimeNodes to delete using the 
-			// current path of each one and then send the TimeNodeDeleted event
-			for (int i=0; i<paths.Length; i++){	
-				/*FIXME*/
-				//TimeNodeDeleted(list[i], int.Parse(paths[i].ToString().Split(':')[0]));
-			}			
+			/* Delete all the iters now */
+			for (int i=0; i< iters.Count; i++){
+				TreeIter iter = iters[i];
+				(Model as TreeStore).Remove(ref iter);
+			}
+			if (TimeNodeDeleted != null)
+				TimeNodeDeleted(playsList);
 		}
 
 		protected void OnDeleteKeyFrame(object obj, EventArgs args) {
diff --git a/LongoMatch/Handlers/EventsManager.cs b/LongoMatch/Handlers/EventsManager.cs
index 5e8f6b4..1975c5d 100644
--- a/LongoMatch/Handlers/EventsManager.cs
+++ b/LongoMatch/Handlers/EventsManager.cs
@@ -19,6 +19,7 @@
 //
 
 using System;
+using System.Collections.Generic;
 using LongoMatch.Common;
 using LongoMatch.Gui.Component;
 using LongoMatch.Gui.Dialog;
@@ -308,18 +309,16 @@ namespace LongoMatch
 			}
 		}
 
-		protected virtual void OnTimeNodeDeleted(Play play)
+		protected virtual void OnTimeNodeDeleted(List<Play> plays)
 		{
-			treewidget.RemovePlay(play);
-			timeline.RemovePlay(play);
+			treewidget.RemovePlays(plays);
+			timeline.RemovePlays(plays);
+			tagsTreeWidget.RemovePlays(plays);
 
-			foreach (var player in play.LocalPlayers)
-				localPlayersList.RemovePlay(play ,player);
+			localPlayersList.RemovePlays(plays);
+			visitorPlayersList.RemovePlays(plays);
 
-			foreach (var player in play.VisitorPlayers)
-				visitorPlayersList.RemovePlay(play ,player);
-
-			openedProject.RemovePlay(play);
+			openedProject.RemovePlays(plays);
 			if (projectType == ProjectType.FileProject){
 				this.player.CloseActualSegment();
 				MainClass.DB.UpdateProject(openedProject);
diff --git a/LongoMatch/Handlers/Handlers.cs b/LongoMatch/Handlers/Handlers.cs
index e2b444e..b3f62e1 100644
--- a/LongoMatch/Handlers/Handlers.cs
+++ b/LongoMatch/Handlers/Handlers.cs
@@ -43,8 +43,8 @@ namespace LongoMatch.Handlers
 	public delegate void NewMarkAtFrameEventHandler(Category category,int frame);
 	//A play was edited
 	public delegate void TimeNodeChangedHandler(TimeNode tNode, object val);
-	//A play was deleted
-	public delegate void TimeNodeDeletedHandler(Play play);
+	//A list of plays was deleted
+	public delegate void TimeNodeDeletedHandler(List<Play> plays);
 	//Players needs to be tagged
 	public delegate void PlayersTaggedHandler(Play play, Team team);
 	//Tag a play



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