[gnome-subtitles] Improved subtitle splitting (bug #651958). It remains to be decided how text is going to be split.



commit 998c2512bb09ab5553f1bf2cea65c50308a80bac
Author: Pedro Castro <mail pedrocastro org>
Date:   Sat Jun 11 19:39:38 2011 +0100

    Improved subtitle splitting (bug #651958). It remains to be decided how text is going to be split.

 gnome-subtitles.mdp                                |    3 +-
 .../Core/Command/SplitSubtitlesCommand.cs          |   63 ++++++++++++++----
 src/GnomeSubtitles/Core/EventHandlers.cs           |    3 +-
 src/SubLib/Core/Timing/SplitOperator.cs            |   69 ++++++++++++-------
 4 files changed, 96 insertions(+), 42 deletions(-)
---
diff --git a/gnome-subtitles.mdp b/gnome-subtitles.mdp
index 46b2b91..c31a3b0 100644
--- a/gnome-subtitles.mdp
+++ b/gnome-subtitles.mdp
@@ -2,7 +2,6 @@
   <Policies>
     <TextStylePolicy FileWidth="120" TabWidth="4" RemoveTrailingWhitespace="True" EolMarker="Unix" inheritsSet="Mono" inheritsScope="text/plain" />
   </Policies>
-  <Deployment.LinuxDeployData generateScript="False" />
   <Configurations active="Debug">
     <Configuration name="Debug" ctype="DotNetProjectConfiguration">
       <Output directory="build" assembly="gnome-subtitles" assemblyKeyFile="/home/noup/Workspace/gnome-subtitles/." />
@@ -257,6 +256,7 @@
     <File subtype="Code" buildaction="Compile" name="src/GnomeSubtitles/Core/GlobalAccelerators.cs" />
     <File subtype="Code" buildaction="Compile" name="src/GnomeSubtitles/Core/Command/SplitSubtitlesCommand.cs" />
     <File subtype="Code" buildaction="Compile" name="src/SubLib/Core/Timing/SplitOperator.cs" />
+    <File subtype="Code" buildaction="Compile" name="src/SubLib/Core/Timing/MergeOperator.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
@@ -270,5 +270,6 @@
     <ProjectReference type="Gac" localcopy="True" refto="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   </References>
   <LanguageParameters StartupObject="GnomeSubtitles.Execution.Executable" ApplicationIcon="." ctype="CSharpProjectParameters" />
+  <Deployment.LinuxDeployData generateScript="False" />
   <DeploymentInformation strategy="File" />
 </Project>
\ No newline at end of file
diff --git a/src/GnomeSubtitles/Core/Command/SplitSubtitlesCommand.cs b/src/GnomeSubtitles/Core/Command/SplitSubtitlesCommand.cs
index a1903a7..a9d5de9 100644
--- a/src/GnomeSubtitles/Core/Command/SplitSubtitlesCommand.cs
+++ b/src/GnomeSubtitles/Core/Command/SplitSubtitlesCommand.cs
@@ -21,43 +21,80 @@ using GnomeSubtitles.Ui.View;
 using Gtk;
 using Mono.Unix;
 using SubLib.Core.Domain;
+using SubLib.Core.Timing;
+using System.Collections;
 
 namespace GnomeSubtitles.Core.Command {
 
 public class SplitSubtitlesCommand : MultipleSelectionCommand {
 	private static string description = Catalog.GetString("Splitting subtitles");
-	//private Subtitle[] originalSubtitles = null;
+	private Subtitle[] subtitlesBefore = null;
+	private TreePath[] pathsAfter = null;
+	private Subtitle[] subtitlesAfter = null;
 	
 	public SplitSubtitlesCommand () : base(description, false, SelectionIntended.Simple, null) {
-		
 	}
 
 	public override bool Execute () {
-	//	this.originalSubtitles = GetSelectedSubtitles();
+		GnomeSubtitles.Ui.View.Subtitles subtitles = Base.Document.Subtitles;
+		ArrayList pathsBefore = new ArrayList();
+		ArrayList subtitlesBefore = new ArrayList();
+		ArrayList pathsAfter = new ArrayList();
+		
+		SplitOperator splitOperator = new SplitOperator(subtitles, 100); //FIXME 100
 		
+		foreach (TreePath path in Paths) {
+			int subtitleIndex = Util.PathToInt(path) + subtitlesBefore.Count; //need to account for subtitles already added in this loop
+			Subtitle subtitle = subtitles[subtitleIndex];
+			Subtitle subtitleClone = subtitle.Clone(subtitles.Properties);
+			Subtitle subtitle2 = splitOperator.Split(subtitle);
+			if (subtitle2 != null) {
+				pathsAfter.Add(Util.IntToPath(subtitleIndex));
+				pathsAfter.Add(Util.IntToPath(subtitleIndex + 1));
+
+				pathsBefore.Add(path);
+				subtitlesBefore.Add(subtitleClone);
+
+				subtitles.Add(subtitle2, subtitleIndex + 1);
+			}
+		}
 		
-		return true;
+		/* If any subtitle was changed, the command was successful */
+		if (subtitlesBefore.Count == 0)
+			return false;
+		else {
+			this.subtitlesBefore = (Subtitle[])subtitlesBefore.ToArray(typeof(Subtitle));
+			this.Paths = (TreePath[])pathsBefore.ToArray(typeof(TreePath));
+			this.pathsAfter = (TreePath[])pathsAfter.ToArray(typeof(TreePath));
+			Base.Ui.View.Selection.Select(this.pathsAfter, null, true);
+			return true;
+		}
 	}
 	
 	public override void Undo () {
-		
+		if (this.subtitlesAfter == null) {
+			this.subtitlesAfter = GetSubtitlesAfter(Base.Document.Subtitles, this.pathsAfter);
+		}
+		Base.Document.Subtitles.Remove(this.pathsAfter);
+		Base.Ui.View.Insert(this.subtitlesBefore, this.Paths, null);
 	}
 
 	public override void Redo () {
-		Undo();
+		Base.Document.Subtitles.Remove(this.Paths);
+		Base.Ui.View.Insert(this.subtitlesAfter, this.pathsAfter, null);
 	}
 	
 	
 	/* Private members */
 
-	private Subtitle[] GetSelectedSubtitles () {
-		int count = Paths.Length;
-		Subtitle[] subtitles = new Subtitle[count];
-		for (int index = 0 ; index < count ; index++) {
-			TreePath path = Paths[index];
-			subtitles[index] = Base.Document.Subtitles[path];
+	private Subtitle[] GetSubtitlesAfter (GnomeSubtitles.Ui.View.Subtitles subtitles, TreePath[] pathsAfter) {
+		Subtitle[] subtitlesAfter = new Subtitle[pathsAfter.Length];
+		for (int index = 0 ; index < pathsAfter.Length ; index++) {
+			TreePath path = pathsAfter[index];
+			int subtitleIndex = Util.PathToInt(path);
+			subtitlesAfter[index] = subtitles[subtitleIndex];
 		}
-		return subtitles;
+		return subtitlesAfter;
 	}
 
 }
diff --git a/src/GnomeSubtitles/Core/EventHandlers.cs b/src/GnomeSubtitles/Core/EventHandlers.cs
index a2e337e..c637905 100644
--- a/src/GnomeSubtitles/Core/EventHandlers.cs
+++ b/src/GnomeSubtitles/Core/EventHandlers.cs
@@ -137,8 +137,7 @@ public class EventHandlers {
 	
 	public void OnEditSplit (object o, EventArgs args) {
 		Console.WriteLine ("OnEditSplit");
-		
-		
+		Base.CommandManager.Execute(new SplitSubtitlesCommand());
 	}
 	
 	public void OnEditMerge (object o, EventArgs args) {
diff --git a/src/SubLib/Core/Timing/SplitOperator.cs b/src/SubLib/Core/Timing/SplitOperator.cs
index e155399..c7c0e1f 100644
--- a/src/SubLib/Core/Timing/SplitOperator.cs
+++ b/src/SubLib/Core/Timing/SplitOperator.cs
@@ -25,43 +25,60 @@ namespace SubLib.Core.Timing {
 /// <summary>Performs split operations.</summary>
 public class SplitOperator {
 	private Subtitles subtitles = null;
+	private int timeBetweenSubtitles = -1;
 	
-	public SplitOperator (Subtitles subtitles) {
+	public SplitOperator (Subtitles subtitles, int timeBetweenSubtitles) {
 		this.subtitles = subtitles;
+		this.timeBetweenSubtitles = timeBetweenSubtitles;
 	}
 
 	/* Public members */
 	
-	
-	/// <summary>Changes the current frame rate of the subtitles.</summary>
-	/// <param name="frameRate">The new frame rate to be used.</param>
-	/// <remarks>This changes the frame rate currently being used with the subtitles, which is sometimes
-	/// refered to as the output frame rate.</remarks>
-	public void ChangeCurrent (float frameRate) {
-		float currentFrameRate = subtitles.Properties.CurrentFrameRate;
-		if (currentFrameRate != frameRate) {
-			subtitles.Properties.SetCurrentFrameRate(frameRate);
-			subtitles.UpdateFramesFromTimes(frameRate);
-		}
+	public Subtitle Split (Subtitle subtitle) {
+		if (!isOperationValid(subtitle))
+			return null;
+		
+		Subtitle subtitle2 = Split(subtitle, this.subtitles.Properties, this.timeBetweenSubtitles);
+		return subtitle2;
 	}
 	
-	/// <summary>Updates the subtitles' frames using the specified frame rate as the one they had when they were opened.</summary>
-	/// <param name="frameRate">The original subtitles' frame rate.</param>
-	/// <remarks>This results on having the subtitles with the frames they would have if they had been opened with this frame rate.
-	/// The original frame rate is sometimes refered to as the input frame rate.</remarks>
-	public void ChangeOriginal (float frameRate) {
-		SubtitleProperties properties = subtitles.Properties;
-		float originalFrameRate = properties.OriginalFrameRate;
-		float currentFrameRate = properties.CurrentFrameRate;
+	
+	/* Private members */
+	
+	/// <summary>Splits a subtitle in two halves. The subtitle passed as parameter is turned into the first half and the second half is returned.</summary>
+	/// <param name="subtitle">The subtitle to split</param>
+	/// <param name="subtitleProperties">The subtitle properties</param>
+	/// <param name="timeBetweenSubtitles">Time between the 2 subtitles, in milliseconds</param>
+	private Subtitle Split (Subtitle subtitle, SubtitleProperties subtitleProperties, int timeBetweenSubtitles) {
+		Subtitle subtitle2 = subtitle.Clone(subtitleProperties);
+		
+		/* Change timings */
+		int originalStart = (int)subtitle.Times.Start.TotalMilliseconds;
+		int originalEnd = (int)subtitle.Times.End.TotalMilliseconds;
 
-		if (originalFrameRate != frameRate) {
-			float conversionFrameRate = currentFrameRate * originalFrameRate / frameRate;
-			subtitles.UpdateFramesFromTimes(conversionFrameRate);
-			subtitles.UpdateTimesFromFrames(currentFrameRate);
-			properties.SetOriginalFrameRate(frameRate);
+		if ((originalEnd - originalStart) <= timeBetweenSubtitles) {
+			/* Not possible to have the predefined time between subtitle, subtitle 2 will start at subtitle's end time */
+			int originalMiddle = (originalStart + originalEnd) / 2;
+			TimeSpan newSubtitleEnd = TimeSpan.FromMilliseconds(originalMiddle);
+			subtitle.Times.End = newSubtitleEnd;
+			subtitle2.Times.Start = newSubtitleEnd;
+		}
+		else {
+			int newSubtitleEnd = (originalStart + originalEnd - timeBetweenSubtitles) / 2;
+			int subtitle2Start = newSubtitleEnd + timeBetweenSubtitles;
+			subtitle.Times.End = TimeSpan.FromMilliseconds(newSubtitleEnd);
+			subtitle2.Times.Start = TimeSpan.FromMilliseconds(subtitle2Start);
 		}
+		
+		/* Change text */
+		//FIXME divide text in half
+	
+		return subtitle2;
+	}
+	
+	private bool isOperationValid (Subtitle subtitle) {
+		return subtitle.Times.End >= subtitle.Times.Start;
 	}
-
 }
 
 }



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