[longomatch] Allow deletion of several projects at the same time and clean-up the projects list widget



commit fbb0bc6ce3aba8d3c97b260777b1c04dd5de074c
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Mon Jun 21 23:59:07 2010 +0200

    Allow deletion of several projects at the same time and clean-up the projects list widget

 LongoMatch/Gui/Component/ProjectListWidget.cs |  103 ++++++++++++++-----------
 LongoMatch/Gui/Dialog/OpenProjectDialog.cs    |   15 +++-
 LongoMatch/Gui/Dialog/ProjectsManager.cs      |   92 ++++++++++++++--------
 LongoMatch/Gui/Dialog/TemplatesEditor.cs      |    4 -
 LongoMatch/Gui/MainWindow.cs                  |    2 +-
 LongoMatch/Handlers/Handlers.cs               |    3 +-
 6 files changed, 131 insertions(+), 88 deletions(-)
---
diff --git a/LongoMatch/Gui/Component/ProjectListWidget.cs b/LongoMatch/Gui/Component/ProjectListWidget.cs
index 0beb4ef..75d08ad 100644
--- a/LongoMatch/Gui/Component/ProjectListWidget.cs
+++ b/LongoMatch/Gui/Component/ProjectListWidget.cs
@@ -25,6 +25,7 @@ using Mono.Unix;
 using Gtk;
 using Db4objects.Db4o;
 using LongoMatch.DB;
+using LongoMatch.Handlers;
 using LongoMatch.TimeNodes;
 using LongoMatch.Video.Utils;
 
@@ -33,7 +34,6 @@ using LongoMatch.Video.Utils;
 namespace LongoMatch.Gui.Component
 {
 
-	public delegate void ProjectSelectedHandler(ProjectDescription project);
 
 	[System.ComponentModel.Category("LongoMatch")]
 	[System.ComponentModel.ToolboxItem(true)]
@@ -41,17 +41,15 @@ namespace LongoMatch.Gui.Component
 	{
 
 		private Gtk.ListStore projectsListStore;
+		private List<ProjectDescription> projectsList;
 		private TreeModelFilter filter;
-		public event ProjectSelectedHandler ProjectSelectedEvent;
+		public event ProjectsSelectedHandler ProjectsSelected;
 
 
 		public ProjectListWidget()
 		{
 			this.Build();
 			projectsListStore = new Gtk.ListStore(typeof(Project));
-			filter = new Gtk.TreeModelFilter(projectsListStore, null);
-			filter.VisibleFunc = new Gtk.TreeModelFilterVisibleFunc(FilterTree);
-			treeview.Model = filter;
 
 			Gtk.TreeViewColumn fileDescriptionColumn = new Gtk.TreeViewColumn();
 			fileDescriptionColumn.Title = Catalog.GetString("Filename");
@@ -70,19 +68,54 @@ namespace LongoMatch.Gui.Component
 			treeview.EnableGridLines = TreeViewGridLines.Horizontal;
 			treeview.HeadersVisible = false;
 		}
+		
+		public SelectionMode SelectionMode {
+			set {
+				treeview.Selection.Mode = value;
+			}
+		}
+		
+		public void RemoveProjects(List<ProjectDescription> projects) {
+			/* FIXME: to delete projects from the treeview we need to remove the filter
+			 * and clear everything, otherwise we have seen several crashes trying
+			 * to render cells with an invalid iter. It's not very performant, but
+			 * it's safe. */
+			treeview.Model = projectsListStore;
+			projectsListStore.Clear();
+			foreach (ProjectDescription project in projects)
+				projectsList.Remove(project);
+			Fill(projectsList);
+		}
+		
+		public void Fill(List<ProjectDescription> projects) {
+			projectsList = projects;
+			projectsList.Sort();
+			projectsListStore.Clear();
+			foreach (ProjectDescription project in projectsList)
+				projectsListStore.AppendValues(project);
+			filter = new Gtk.TreeModelFilter(projectsListStore, null);
+			filter.VisibleFunc = new Gtk.TreeModelFilterVisibleFunc(FilterTree);
+			treeview.Model = filter;
+			treeview.Selection.Mode = SelectionMode.Multiple;
+			treeview.Selection.Changed += OnSelectionChanged;
+		}
 
+		public void ClearSearch() {
+			filterEntry.Text="";
+		}
 
 		private void RenderPixbuf(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
 		{
 			ProjectDescription project = (ProjectDescription) model.GetValue(iter, 0);
+			
 			(cell as Gtk.CellRendererPixbuf).Pixbuf= project.Preview;
 		}
 		
 		private void RenderProperties(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
 		{
-			ProjectDescription project = (ProjectDescription) model.GetValue(iter, 0);
 			string text;
-
+			ProjectDescription project = (ProjectDescription) model.GetValue(iter, 0);
+			
 			text = "\n"+"\n"+"\n"+"<b>"+Catalog.GetString("File length")+":</b>  " + project.Length.ToSecondsString();
 			text = text +"\n"+"<b>"+Catalog.GetString("Video codec")+":</b>  " + project.VideoCodec;
 			text = text +"\n"+"<b>"+Catalog.GetString("Audio codec")+":</b>  " + project.AudioCodec;
@@ -93,8 +126,8 @@ namespace LongoMatch.Gui.Component
 		
 		private void RenderName(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
 		{
-			ProjectDescription project = (ProjectDescription) model.GetValue(iter, 0);
 			string text;
+			ProjectDescription project = (ProjectDescription) model.GetValue(iter, 0);
 
 			text = "<b>"+Catalog.GetString("Title")+":</b>  " + project.Title;
 			text = text +"\n"+"<b>"+Catalog.GetString("Local team")+":</b>  " + project.LocalName;
@@ -107,44 +140,6 @@ namespace LongoMatch.Gui.Component
 			(cell as Gtk.CellRendererText).Markup = text;
 		}
 
-		public void Fill(List<ProjectDescription> projectsList) {
-			projectsListStore.Clear();
-			projectsList.Sort();
-			foreach (ProjectDescription project in projectsList) {
-				projectsListStore.AppendValues(project);
-			}
-		}
-
-		public ProjectDescription GetSelection() {
-			TreePath path;
-			TreeViewColumn col;
-			treeview.GetCursor(out path,out col);
-			return this.GetProject(path);
-		}
-
-		public void ClearSearch() {
-			filterEntry.Text="";
-		}
-
-		private ProjectDescription GetProject(TreePath path) {
-			if (path != null) {
-				Gtk.TreeIter iter;
-				filter.GetIter(out iter, path);
-				ProjectDescription project = (ProjectDescription) filter.GetValue(iter, 0);
-				return project;
-			}
-			else return null;
-		}
-
-		protected virtual void OnTreeviewCursorChanged(object sender, System.EventArgs e)
-		{
-			TreeIter iter;
-			this.treeview.Selection.GetSelected(out iter);
-			ProjectDescription selectedProject = (ProjectDescription) filter.GetValue(iter, 0);
-			if (ProjectSelectedEvent!=null)
-				ProjectSelectedEvent(selectedProject);
-		}
-
 		protected virtual void OnFilterentryChanged(object sender, System.EventArgs e)
 		{
 			filter.Refilter();
@@ -173,5 +168,21 @@ namespace LongoMatch.Gui.Component
 			else
 				return false;
 		}
+		
+		protected virtual void OnSelectionChanged (object o, EventArgs args){
+			TreeIter iter;
+			List<ProjectDescription> list;
+			TreePath[] pathArray;
+			
+			list = new List<ProjectDescription>();
+			pathArray = treeview.Selection.GetSelectedRows();
+			
+			for (int i=0; i< pathArray.Length; i++){
+				treeview.Model.GetIterFromString (out iter, pathArray[i].ToString());
+				list.Add((ProjectDescription) treeview.Model.GetValue(iter, 0));
+			}
+			if (ProjectsSelected != null)
+				ProjectsSelected(list);
+		}
 	}
 }
diff --git a/LongoMatch/Gui/Dialog/OpenProjectDialog.cs b/LongoMatch/Gui/Dialog/OpenProjectDialog.cs
index 3802b23..4ea2875 100644
--- a/LongoMatch/Gui/Dialog/OpenProjectDialog.cs
+++ b/LongoMatch/Gui/Dialog/OpenProjectDialog.cs
@@ -19,6 +19,7 @@
 //
 
 using System;
+using System.Collections.Generic;
 using Gtk;
 using LongoMatch.DB;
 
@@ -34,14 +35,22 @@ namespace LongoMatch.Gui.Dialog
 		{
 			this.Build();
 			this.Fill();
+			projectlistwidget.SelectionMode = SelectionMode.Single;
 		}
-
-		public ProjectDescription GetSelection() {
-			return projectlistwidget.GetSelection();
+		
+		public ProjectDescription SelectedProject {
+			get;
+			set;
 		}
 
 		public void Fill() {
 			projectlistwidget.Fill(MainClass.DB.GetAllProjects());
 		}
+		
+		protected virtual void OnProjectlistwidgetProjectsSelected (List<ProjectDescription> projects)
+		{
+			buttonOk.Sensitive =  (projects.Count == 1);
+			SelectedProject = projects[0];			
+		}
 	}
 }
\ No newline at end of file
diff --git a/LongoMatch/Gui/Dialog/ProjectsManager.cs b/LongoMatch/Gui/Dialog/ProjectsManager.cs
index e048964..7ec4190 100644
--- a/LongoMatch/Gui/Dialog/ProjectsManager.cs
+++ b/LongoMatch/Gui/Dialog/ProjectsManager.cs
@@ -35,7 +35,8 @@ namespace LongoMatch.Gui.Dialog
 	{
 
 		private string originalFilePath;
-		private Project openedProject;
+		private Project openedProject;
+		private List<ProjectDescription> selectedProjects;
 
 		public ProjectsManager(Project openedProject)
 		{
@@ -49,15 +50,20 @@ namespace LongoMatch.Gui.Dialog
 		public void Fill() {
 			List<ProjectDescription> projectsList = MainClass.DB.GetAllProjects();
 			projectlistwidget1.Fill(projectsList);
-			projectlistwidget1.ClearSearch();
-			projectdetails.Clear();
-			projectdetails.Sensitive = false;
-			saveButton.Sensitive = false;
-			deleteButton.Sensitive = false;
-			exportbutton.Sensitive = false;
+			projectlistwidget1.ClearSearch();
+			projectlistwidget1.SelectionMode = SelectionMode.Multiple;
+			Clear();
 			originalFilePath=null;
 		}
 		
+		private void Clear(){
+			projectdetails.Clear();
+			projectdetails.Sensitive = false;
+			saveButton.Sensitive = false;
+			deleteButton.Sensitive = false;
+			exportbutton.Sensitive = false;	
+		}
+		
 		private void PromptToSaveEditedProject(){			
 			MessageDialog md = new MessageDialog((Window)this.Toplevel,DialogFlags.Modal,
 			                                     MessageType.Question, ButtonsType.YesNo,
@@ -94,28 +100,32 @@ namespace LongoMatch.Gui.Dialog
 
 
 		protected virtual void OnDeleteButtonPressed(object sender, System.EventArgs e)
-		{
-			ProjectDescription selectedProject = projectlistwidget1.GetSelection();
-			if (selectedProject != null) {
+		{
+		    List<ProjectDescription> deletedProjects = new List<ProjectDescription>();
+			
+			if (selectedProjects == null)
+			    return;
+			
+			foreach (ProjectDescription selectedProject in selectedProjects) {
 				if (openedProject != null && selectedProject.File == openedProject.File.FilePath) {
 					MessagePopup.PopupMessage(this, MessageType.Warning,
 					                          Catalog.GetString("This Project is actually in use.")+"\n"+
-					                          Catalog.GetString("Close it first to allow its removal from the database"));
-				}
-				else {
-					MessageDialog md = new MessageDialog(this,DialogFlags.Modal,
-					                                     MessageType.Question,
-					                                     ButtonsType.YesNo,
-					                                     Catalog.GetString("Do you really want to delete:")+
-					                                     "\n"+selectedProject.Title);
-					if (md.Run()== (int)ResponseType.Yes) {
-						projectdetails.Clear();
-						MainClass.DB.RemoveProject(selectedProject.File);
-						Fill();
-					}
-					md.Destroy();
+					                          Catalog.GetString("Close it first to allow its removal from the database"));
+					continue;
 				}
-			}
+				MessageDialog md = new MessageDialog(this,DialogFlags.Modal,
+				                                     MessageType.Question,
+				                                     ButtonsType.YesNo,
+				                                     Catalog.GetString("Do you really want to delete:")+
+				                                     "\n"+selectedProject.Title);
+				if (md.Run()== (int)ResponseType.Yes) {
+					MainClass.DB.RemoveProject(selectedProject.File);
+					deletedProjects.Add (selectedProject);
+				} 
+				md.Destroy();
+			}
+			projectlistwidget1.RemoveProjects (deletedProjects);
+			Clear();
 		}
 
 		protected virtual void OnSaveButtonPressed(object sender, System.EventArgs e)
@@ -134,21 +144,37 @@ namespace LongoMatch.Gui.Dialog
 			this.Destroy();
 		}
 
-		protected virtual void OnProjectlistwidget1ProjectSelectedEvent(ProjectDescription project)
+		protected virtual void OnProjectlistwidget1ProjectsSelected(List<ProjectDescription> projects)
 		{
+			ProjectDescription project;
+			
+			/* prompt tp save the opened project if has changes */
 			if (projectdetails.Edited) {
 				PromptToSaveEditedProject();
-			}
+			}
+			
+			selectedProjects = projects;
+			
+			/* if no projects are selected clear everything */
+			if (projects.Count == 0){
+				Clear();
+				return;
+			/* if more than one project is selected clear everything but keep 
+			 * the delete button and the export button sensitives */
+			} else if (projects.Count > 1){
+				Clear();
+				deleteButton.Sensitive = true;
+				exportbutton.Sensitive = true;
+				return;
+			} 
+			
+			/* if only one project is selected try to load it in the editor */
+			project = projects[0];
 			
 			if (openedProject != null && project.File == openedProject.File.FilePath) {
-
 				MessagePopup.PopupMessage(this, MessageType.Warning,
 				                          Catalog.GetString("The Project you are trying to load is actually in use.")+"\n" +Catalog.GetString("Close it first to edit it"));
-				projectdetails.Clear();
-				projectdetails.Sensitive = false;
-				saveButton.Sensitive = false;
-				deleteButton.Sensitive = false;
-				exportbutton.Sensitive = false;
+				Clear();
 			}
 			else {
 				projectdetails.Sensitive = true;
diff --git a/LongoMatch/Gui/Dialog/TemplatesEditor.cs b/LongoMatch/Gui/Dialog/TemplatesEditor.cs
index a348dff..3cbd481 100644
--- a/LongoMatch/Gui/Dialog/TemplatesEditor.cs
+++ b/LongoMatch/Gui/Dialog/TemplatesEditor.cs
@@ -114,15 +114,11 @@ namespace LongoMatch.Gui.Dialog
 			SetSensitive(true);
 		}
 
-
-
 		private void UpdateTeamTemplate() {
 			SetTeamTemplate(TeamTemplate.LoadFromFile(templateName));
 			SetSensitive(true);
-
 		}
 
-
 		private void SetSensitive(bool sensitive) {
 			if (useType == UseType.SectionsTemplate)
 				sectionspropertieswidget1.Sensitive = true;
diff --git a/LongoMatch/Gui/MainWindow.cs b/LongoMatch/Gui/MainWindow.cs
index 49024f4..49070c9 100644
--- a/LongoMatch/Gui/MainWindow.cs
+++ b/LongoMatch/Gui/MainWindow.cs
@@ -372,7 +372,7 @@ namespace LongoMatch.Gui
 			opd.TransientFor = this;
 
 			if (opd.Run() == (int)ResponseType.Ok)
-				project = opd.GetSelection();
+				project = opd.SelectedProject;
 			opd.Destroy();
 			if (project != null)
 				SetProject(MainClass.DB.GetProject(project.File), ProjectType.FileProject, new CapturePropertiesStruct());
diff --git a/LongoMatch/Handlers/Handlers.cs b/LongoMatch/Handlers/Handlers.cs
index 61ebd11..347a914 100644
--- a/LongoMatch/Handlers/Handlers.cs
+++ b/LongoMatch/Handlers/Handlers.cs
@@ -21,6 +21,7 @@
 using System;
 using System.Collections.Generic;
 using LongoMatch;
+using LongoMatch.DB;
 using LongoMatch.TimeNodes;
 
 namespace LongoMatch.Handlers
@@ -84,5 +85,5 @@ namespace LongoMatch.Handlers
 	public delegate void SectionHandler(SectionsTimeNode tNode);
 	public delegate void SectionsHandler(List<SectionsTimeNode> tNodesList);
 	
-	
+	public delegate void ProjectsSelectedHandler(List<ProjectDescription> projects);
 }



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