[gnome-subtitles] Update the dialog based on internal and external events, added the sync operation



commit b18dee98ebef8df48118236660df47fb9720dc42
Author: Pedro Castro <mail pedrocastro org>
Date:   Sat May 23 12:26:17 2009 +0100

    Update the dialog based on internal and external events, added the sync operation
    
    Update the dialog buttons and status message on sync point count change.
    Update the dialog based on the subtitle selection in the main window.
    Issue the synchronization commmand with the sync button.
---
 .../Dialog/TimingsSynchronizeDialog.cs             |  181 ++++++++++++++++----
 1 files changed, 147 insertions(+), 34 deletions(-)

diff --git a/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs b/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs
index 1cae614..bd632ae 100644
--- a/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs
+++ b/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2008 Pedro Castro
+ * Copyright (C) 2008-2009 Pedro Castro
  *
  * Gnome Subtitles is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,10 +19,13 @@
 
 using Glade;
 using GnomeSubtitles.Core;
+using GnomeSubtitles.Core.Command;
+using GnomeSubtitles.Ui.View;
 using Gtk;
 using Mono.Unix;
 using SubLib.Core.Domain;
 using System;
+using System.Collections;
 
 namespace GnomeSubtitles.Dialog {
 
@@ -37,37 +40,122 @@ public class TimingsSynchronizeDialog : GladeDialog {
 	private const string gladeFilename = "TimingsSynchronizeDialog.glade";
 	
 	/* Widgets */
-	[WidgetAttribute] private TreeView tree = null;
+	[WidgetAttribute] private TreeView syncPointsTree = null;
+	[WidgetAttribute] private Button buttonAdd = null;
 	[WidgetAttribute] private Button buttonRemove = null;
+	[WidgetAttribute] private Button buttonSynchronize = null;
+	[WidgetAttribute] private CheckButton syncAllSubtitlesCheckButton = null;
+	[WidgetAttribute] private Label statusMessageLabel = null;
 
-	public TimingsSynchronizeDialog () : base(gladeFilename){
+	public TimingsSynchronizeDialog () : base(gladeFilename, true){
 		this.timingMode = Base.TimingMode;
 	
 		CreateColumns();
 		SetModel();
+		InitWidgetSensitivity();
+		
 		ConnectHandlers();
+		
+		UpdateStatusMessage();
+	}
+	
+	public override void Destroy () {
+		Base.Ui.View.Selection.Changed -= OnUiViewSelectionChanged;
+		base.Destroy();
 	}
 	
 	/* Private methods */
 	
 	private void CreateColumns() {
     	/* Number column */
-    	numberCol = Core.Util.CreateTreeViewColumn(Catalog.GetString("No."), Core.Util.ColumnWidth(tree, "000"), new CellRendererText(), RenderSubtitleNumberCell);
+    	numberCol = Core.Util.CreateTreeViewColumn(Catalog.GetString("Subtitle No."), -1, new CellRendererText(), RenderSubtitleNumberCell);
     	
     	/* Start (current and correct) columns */
-    	int timeWidth = Core.Util.ColumnWidth(tree, "00:00:00.000");
-    	currentStartCol = Core.Util.CreateTreeViewColumn(Catalog.GetString("Current Start"), timeWidth, new CellRendererText(), RenderCurrentStartCell);
-    	correctStartCol = Core.Util.CreateTreeViewColumn(Catalog.GetString("Correct Start"), timeWidth, new CellRendererText(), RenderCorrectStartCell);
+    	currentStartCol = Core.Util.CreateTreeViewColumn(Catalog.GetString("Current Start"), -1, new CellRendererText(), RenderCurrentStartCell);
+    	correctStartCol = Core.Util.CreateTreeViewColumn(Catalog.GetString("Correct Start"), -1, new CellRendererText(), RenderCorrectStartCell);
 
-    	tree.AppendColumn(numberCol);
-    	tree.AppendColumn(currentStartCol);
-    	tree.AppendColumn(correctStartCol);
+    	syncPointsTree.AppendColumn(numberCol);
+    	syncPointsTree.AppendColumn(currentStartCol);
+    	syncPointsTree.AppendColumn(correctStartCol);
 
-    	tree.AppendColumn(new TreeViewColumn()); //Appending to leave empty space to the right
+    	syncPointsTree.AppendColumn(new TreeViewColumn()); //Appending to leave empty space to the right
     }
     
     private void SetModel () {
-		tree.Model = syncPoints.Model;
+		syncPointsTree.Model = syncPoints.Model;
+	}
+	
+	private void UpdateFromSyncPointCountChanged () {
+		UpdateStatusMessage();
+		UpdateSynchronizeButtonSensitivity();
+	}
+	
+	private void UpdateStatusMessage () {
+		String message = String.Empty;
+		switch (syncPoints.Collection.Count) {
+			case 0:
+				message = Catalog.GetString("Add sync points by selecting subtitles and adjusting the video to their correct position. At least 2 points are needed.");
+				break;
+			case 1:
+				message = Catalog.GetString("Add 1 more sync point to start synchronizing. Adding more points will improve accuracy.");
+				break;
+			default:
+				String allSubtitlesSyncMessage = Catalog.GetString("Synchronization is ready. All subtitles will be synchronized.");
+				if (syncAllSubtitlesCheckButton.Active)
+					message = allSubtitlesSyncMessage;
+				else {
+					ArrayList intervals = GetOutOfRangeIntervals();
+					switch (intervals.Count) {
+						case 0:
+							message = allSubtitlesSyncMessage;
+							break;
+						case 1:
+							message = String.Format(Catalog.GetString("Synchronization is ready. The following subtitles will not be synchronized: {0}."), intervals[0]);
+							break;
+						case 2:
+							message = String.Format(Catalog.GetString("Synchronization is ready. The following subtitles will not be synchronized: {0} and {1}."), intervals[0], intervals[1]);
+							break;
+						default:
+							break;
+					}
+				}
+				break;
+		}
+		statusMessageLabel.Text = message;
+	}
+	
+	private void UpdateSynchronizeButtonSensitivity () {
+		buttonSynchronize.Sensitive = (syncPoints.Collection.Count >= 2);
+	}
+	
+	private void SelectPath (TreePath path) {
+		syncPointsTree.SetCursor(path, null, false);
+	}
+	
+	private void InitWidgetSensitivity () {
+		buttonAdd.Sensitive = (Base.Ui.View.Selection.Count == 1);
+	}
+	
+	private ArrayList GetOutOfRangeIntervals () {
+		Ui.View.Subtitles subtitles = Base.Document.Subtitles;
+		ArrayList intervals = new ArrayList();
+		if (syncPoints.Collection.Count == 0)
+			return intervals;
+		
+		SyncPoint first = syncPoints.Collection.Get(0);
+		if (first.SubtitleNumber > 0) {
+			String firstInterval = "1" + (first.SubtitleNumber > 1 ? "-" + first.SubtitleNumber : String.Empty);
+			intervals.Add(firstInterval);
+		}
+		
+		SyncPoint last = syncPoints.Collection.Get(syncPoints.Collection.Count - 1);
+		int lastSubtitleNumber = subtitles.Count - 1;
+		if (last.SubtitleNumber < lastSubtitleNumber) {
+			String lastInterval = (last.SubtitleNumber < lastSubtitleNumber - 1 ? (last.SubtitleNumber + 2) + "-" : String.Empty) + (lastSubtitleNumber + 1);
+			intervals.Add(lastInterval);
+		}
+		
+		return intervals;
 	}
     
     /* Cell Renderers */
@@ -97,7 +185,10 @@ public class TimingsSynchronizeDialog : GladeDialog {
 	#pragma warning disable 169		//Disables warning about handlers not being used
 	
 	private void ConnectHandlers () {
-		tree.Selection.Changed += OnSelectionChanged;
+		syncPointsTree.Selection.Changed += OnSelectionChanged;
+		
+		/* External event handlers */
+		Base.Ui.View.Selection.Changed += OnUiViewSelectionChanged;
 	}
 	
 	private void OnSelectionChanged (object o, EventArgs args) {
@@ -111,13 +202,12 @@ public class TimingsSynchronizeDialog : GladeDialog {
 			return;
 	
 		/* Get selected subtitle */
-		TreePath[] selectedPaths = Base.Ui.View.Selection.Paths;
-		if (selectedPaths.Length != 1)
+		TreePath path = Base.Ui.View.Selection.Path;
+		if (path == null)
 			return;
 		
-		TreePath path = selectedPaths[0];
 		int subtitleNumber = Core.Util.PathToInt(path);
-		Subtitle subtitle = Base.Ui.View.Selection.Subtitle; //TODO refactor: change to FirstSubtitle?
+		Subtitle subtitle = Base.Ui.View.Selection.Subtitle;
 		
 		/* Get current start */
 		Timing currentTiming = new Timing(subtitle.Frames.Start, subtitle.Times.Start);
@@ -127,31 +217,54 @@ public class TimingsSynchronizeDialog : GladeDialog {
 
 		/* Create and add the sync point */
 		SyncPoint syncPoint = new SyncPoint(subtitleNumber, currentTiming, correctTiming);
-		syncPoints.InsertSorted(syncPoint);
+		int syncPointIndex = syncPoints.Add(syncPoint);
+		TreePath syncPointPath = Core.Util.IntToPath(syncPointIndex);
+		SelectPath(syncPointPath);		
+		
+		UpdateFromSyncPointCountChanged();
 	}
 	
 	private void OnRemove (object o, EventArgs args) {
-		System.Console.WriteLine("Remove");
+		TreePath[] paths = syncPointsTree.Selection.GetSelectedRows();
+		if (paths.Length == 0)
+			return;
+
+		syncPoints.Remove(paths);
+		int syncPointCount = syncPoints.Collection.Count;
+		if (syncPointCount != 0) {
+			int firstDeletedSubtitle = Core.Util.PathToInt(paths[0]);
+			int subtitleToDelete = (firstDeletedSubtitle < syncPointCount ? firstDeletedSubtitle : syncPointCount - 1);
+			syncPointsTree.SetCursor(Core.Util.IntToPath(subtitleToDelete), null, false);
+		}
+		
+		UpdateFromSyncPointCountChanged();
+	}
+	
+	private void OnSynchronizeAllSubtitlesToggled (object o, EventArgs args) {
+		UpdateStatusMessage();
 	}
 	
-	private void OnResponse (object o, ResponseArgs args) {
-		if (args.ResponseId == ResponseType.Ok) {
-		/*	SelectionIntended selectionIntended = (allSubtitlesRadioButton.Active ? SelectionIntended.All : SelectionIntended.Range);
+	private void OnUiViewSelectionChanged (TreePath[] paths, Subtitle subtitle) {
+		buttonAdd.Sensitive = (subtitle != null);
+	}
+
+	protected override bool ProcessResponse (ResponseType response) {
+		if (response == ResponseType.Ok) {
+			bool toSyncAll = syncAllSubtitlesCheckButton.Active;
+			SelectionIntended selectionIntended = (toSyncAll ? SelectionIntended.All : SelectionIntended.Range);
 			
-			if (timingMode == TimingMode.Times) {
-				TimeSpan firstTime = TimeSpan.Parse(firstSubtitleNewStartSpinButton.Text);
-				TimeSpan lastTime = TimeSpan.Parse(lastSubtitleNewStartSpinButton.Text);
-				Base.CommandManager.Execute(new AdjustTimingsCommand(firstTime, lastTime, selectionIntended));
+			TreePath[] pathRange = null;
+			if (selectionIntended == SelectionIntended.Range) {
+				pathRange = new TreePath[2];
+				pathRange[0] = Core.Util.IntToPath(syncPoints.Collection[0].SubtitleNumber);
+				pathRange[1] = Core.Util.IntToPath(syncPoints.Collection[syncPoints.Collection.Count - 1].SubtitleNumber);
 			}
-			else {
-				int firstFrame = (int)firstSubtitleNewStartSpinButton.Value;
-				int lastFrame = (int)lastSubtitleNewStartSpinButton.Value;
-				Base.CommandManager.Execute(new AdjustTimingsCommand(firstFrame, lastFrame, selectionIntended));
-			}*/
-		}
-		else {
-			Close();
+			
+			Base.CommandManager.Execute(new SynchronizeTimingsCommand(syncPoints, toSyncAll, selectionIntended, pathRange));
+			return true;
 		}
+		else
+			return false;
 	}
 
 }



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