[gnome-subtitles] Fix bug #648461 - Insert subtitle at video position, based on a patch from Michał (Saviq) Sawicz.



commit 8e4b6847f56fdbb7467d15bf5976ca24cbdd96ce
Author: Pedro Castro <mail pedrocastro org>
Date:   Fri Apr 22 13:20:46 2011 +0100

    Fix bug #648461 - Insert subtitle at video position, based on a patch from MichaÅ? (Saviq) Sawicz.

 src/Glade/MainWindow.glade                         |   18 +++++++++++
 .../Core/Command/InsertSubtitleCommand.cs          |   31 +++++++++++++++++++-
 src/GnomeSubtitles/Core/EventHandlers.cs           |    4 ++
 src/GnomeSubtitles/Ui/Menus.cs                     |    3 +-
 src/GnomeSubtitles/Ui/View/SubtitleView.cs         |   16 +++++++++-
 src/GnomeSubtitles/Ui/View/Subtitles.cs            |    8 ++++-
 src/GnomeSubtitles/Ui/WidgetNames.cs               |    3 +-
 src/SubLib/Core/Domain/SubtitleCollection.cs       |   21 +++++++++++++-
 8 files changed, 98 insertions(+), 6 deletions(-)
---
diff --git a/src/Glade/MainWindow.glade b/src/Glade/MainWindow.glade
index f38a59f..a9c457c 100644
--- a/src/Glade/MainWindow.glade
+++ b/src/Glade/MainWindow.glade
@@ -381,6 +381,24 @@
                                 </child>
                               </widget>
                             </child>
+                            <child>
+                              <widget class="GtkImageMenuItem" id="editInsertSubtitleAVideoPosition">
+                                <property name="label" translatable="yes">At _Video Position</property>
+                                <property name="visible">True</property>
+                                <property name="sensitive">False</property>
+                                <property name="use_underline">True</property>
+                                <property name="use_stock">False</property>
+                                <signal name="activate" handler="OnEditInsertSubtitleAtVideoPosition"/>
+                                <accelerator key="D" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
+                                <child internal-child="image">
+                                  <widget class="GtkImage" id="editInsertSubtitleAtVideoPosition">
+                                    <property name="visible">True</property>
+                                    <property name="stock">gtk-go-forward</property>
+                                    <property name="icon-size">1</property>
+                                  </widget>
+                                </child>
+                              </widget>
+                            </child>
                           </widget>
                         </child>
                       </widget>
diff --git a/src/GnomeSubtitles/Core/Command/InsertSubtitleCommand.cs b/src/GnomeSubtitles/Core/Command/InsertSubtitleCommand.cs
index b1d96c8..6eb2501 100644
--- a/src/GnomeSubtitles/Core/Command/InsertSubtitleCommand.cs
+++ b/src/GnomeSubtitles/Core/Command/InsertSubtitleCommand.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2010 Pedro Castro
+ * Copyright (C) 2006-2011 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
@@ -20,6 +20,7 @@
 using Gtk;
 using Mono.Unix;
 using SubLib.Core.Domain;
+using System;
 
 namespace GnomeSubtitles.Core.Command {
 
@@ -122,4 +123,32 @@ public class InsertLastSubtitleCommand : InsertSubtitleCommand {
 
 }
 
+public class InsertSubtitleAtVideoPositionCommand : InsertSubtitleCommand {
+	private TimeSpan subtitleTime = TimeSpan.Zero;
+
+	public InsertSubtitleAtVideoPositionCommand () : base(null) {
+	}
+
+	protected override TreePath GetNewPath () {
+		subtitleTime = Base.Ui.Video.Position.CurrentTime;
+		if (Base.Ui.Video.IsStatePlaying && Base.Config.PrefsVideoApplyReactionDelay) {
+			subtitleTime -= TimeSpan.FromMilliseconds(Base.Config.PrefsVideoReactionDelay);
+		}
+	
+		if (Base.Document.Subtitles.Count == 0)
+			return TreePath.NewFirst();
+			
+		int index = Base.Ui.Video.Tracker.FindSubtitleNearPosition(subtitleTime);
+		Subtitle nearestSubtitle = Base.Document.Subtitles[index];
+		if (subtitleTime < nearestSubtitle.Times.Start)
+			return Util.IntToPath(index);
+		else
+			return Util.PathNext(Util.IntToPath(index));
+	}
+
+	protected override void InsertNew () {
+		Base.Ui.View.InsertNewAt(NewPath, subtitleTime);
+	}
+}
+
 }
diff --git a/src/GnomeSubtitles/Core/EventHandlers.cs b/src/GnomeSubtitles/Core/EventHandlers.cs
index ab658b8..d9dfb5f 100644
--- a/src/GnomeSubtitles/Core/EventHandlers.cs
+++ b/src/GnomeSubtitles/Core/EventHandlers.cs
@@ -149,6 +149,10 @@ public class EventHandlers {
 			Base.CommandManager.Execute(new InsertLastSubtitleCommand());
 	}
 	
+	public void OnEditInsertSubtitleAtVideoPosition (object o, EventArgs args) {
+		Base.CommandManager.Execute(new InsertSubtitleAtVideoPositionCommand());
+	}
+	
 	public void OnEditDeleteSubtitles (object o, EventArgs args) {
 		if (Base.Ui.View.Selection.Count > 0)
 			Base.CommandManager.Execute(new DeleteSubtitlesCommand());
diff --git a/src/GnomeSubtitles/Ui/Menus.cs b/src/GnomeSubtitles/Ui/Menus.cs
index d2506a3..d06e8ef 100644
--- a/src/GnomeSubtitles/Ui/Menus.cs
+++ b/src/GnomeSubtitles/Ui/Menus.cs
@@ -357,6 +357,7 @@ public class Menus {
 		SetSensitivity(WidgetNames.VideoSelectNearestSubtitle, sensitivity);
 		SetSensitivity(WidgetNames.VideoLoopSelectionPlayback, sensitivity);
 		SetSensitivity(WidgetNames.VideoAutoSelectSubtitles, sensitivity);
+		SetSensitivity(WidgetNames.EditInsertSubtitleAtVideoPosition, sensitivity);
 	}
 	
 	private void SetCheckMenuItemActivity (string menuItemName, bool isActive) {
@@ -367,7 +368,7 @@ public class Menus {
 		CheckMenuItem menuItem = Base.GetWidget(menuItemName) as CheckMenuItem;
 		menuItem.Toggled -= handler;
 		menuItem.Active = isActive;
-		menuItem.Toggled += handler;		
+		menuItem.Toggled += handler;
 	}
 	
 	private void SetToggleToolButtonActivity (string toggleToolButtonName, bool isActive, EventHandler handler) {
diff --git a/src/GnomeSubtitles/Ui/View/SubtitleView.cs b/src/GnomeSubtitles/Ui/View/SubtitleView.cs
index b1d216b..e985367 100644
--- a/src/GnomeSubtitles/Ui/View/SubtitleView.cs
+++ b/src/GnomeSubtitles/Ui/View/SubtitleView.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2010 Pedro Castro
+ * Copyright (C) 2006-2011 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
@@ -133,6 +133,20 @@ public class SubtitleView {
 		return true;
 	}
 	
+	/// <summary>Inserts a new subtitle at the specified <see cref="TreePath" />, with the specified start time.</summary>
+	/// <param name="path">The path at which the new subtitle will be inserted.</param>
+	/// <param name="start">The time at which the new subtitle should start.</para>
+	/// <returns>Whether insertion was successful.</returns>
+	public bool InsertNewAt (TreePath path, TimeSpan start) {  //TODO merge nicely with existing subtitles (subtract times accordingly)
+		int index = Util.PathToInt(path);
+		if (!(subtitles.Collection.Contains(index) || (index == subtitles.Count)))
+			return false;
+
+		subtitles.AddNewAt(index, start);
+		selection.Select(path, true, true);
+		return true;
+	}
+	
 	/// <summary>Inserts a subtitle in the specified path.</summary>
 	public void Insert (Subtitle subtitle, TreePath path) {
 		int index = Util.PathToInt(path);
diff --git a/src/GnomeSubtitles/Ui/View/Subtitles.cs b/src/GnomeSubtitles/Ui/View/Subtitles.cs
index c1bd71b..7ec8879 100644
--- a/src/GnomeSubtitles/Ui/View/Subtitles.cs
+++ b/src/GnomeSubtitles/Ui/View/Subtitles.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2010 Pedro Castro
+ * Copyright (C) 2006-2011 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
@@ -93,6 +93,12 @@ public class Subtitles : SubLib.Core.Domain.Subtitles {
 		model.SetValue(model.Insert(index), 0, newSubtitle);
 	}
 	
+	public void AddNewAt (int index, TimeSpan start) {
+		Collection.AddNewAt(index, Properties, start);
+		Subtitle newSubtitle = this[index];
+		model.SetValue(model.Insert(index), 0, newSubtitle);
+	}
+	
 	/// <summary>Removes a subtitle from the collection, given its <see cref="TreePath" />.</summary>
 	/// <returns>Whether the subtitle could be removed.</returns>
 	public bool Remove (TreePath path) {
diff --git a/src/GnomeSubtitles/Ui/WidgetNames.cs b/src/GnomeSubtitles/Ui/WidgetNames.cs
index f2cea75..bae661e 100644
--- a/src/GnomeSubtitles/Ui/WidgetNames.cs
+++ b/src/GnomeSubtitles/Ui/WidgetNames.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2010 Pedro Castro
+ * Copyright (C) 2006-2011 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
@@ -49,6 +49,7 @@ public class WidgetNames {
 	public const string EditFormatItalic = "editFormatItalic";
 	public const string EditFormatUnderline = "editFormatUnderline";
 	public const string EditInsertSubtitleMenu = "editInsertSubtitleMenu";
+	public const string EditInsertSubtitleAtVideoPosition = "editInsertSubtitleAtVideoPosition";
 	public const string EditDeleteSubtitles = "editDeleteSubtitles";
 	
 	/* View Menu */
diff --git a/src/SubLib/Core/Domain/SubtitleCollection.cs b/src/SubLib/Core/Domain/SubtitleCollection.cs
index f8f19e6..4d36081 100644
--- a/src/SubLib/Core/Domain/SubtitleCollection.cs
+++ b/src/SubLib/Core/Domain/SubtitleCollection.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2010 Pedro Castro
+ * Copyright (C) 2005-2011 Pedro Castro
  *
  * SubLib is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -152,6 +152,25 @@ public class SubtitleCollection {
 		return true;
 	}
 	
+	/// <summary>Creates a subtitle and adds it to the collection, inserting it at the specified index
+	/// and with the specified start time.</summary>
+	/// <remarks>The newly created subtitle's duration will be
+	/// <see cref="SubtitleConstants.MaxSingleLineSubtitleDuration" />.</remarks>
+	/// <param name="index">The zero-based index at which the subtitle should be inserted.</param>
+	/// <param name="subtitleProperties">The SubtitleProperties of the subtitles.</param>
+	/// <param name="start">The time at which the new subtitle will start</para>
+	/// <returns>True if the subtitle could be added, false otherwise.</returns>
+	public bool AddNewAt (int index, SubtitleProperties subtitleProperties, TimeSpan start) {
+		if ((index < 0) || (index > Count))
+			return false;
+
+		TimeSpan subtitleStart = start;
+		TimeSpan subtitleEnd = subtitleStart + TimeSpan.FromSeconds(SubtitleConstants.MaxSingleLineSubtitleDuration);
+		Subtitle subtitle = new Subtitle(subtitleProperties, subtitleStart, subtitleEnd);
+		Add(subtitle, index);
+		return true;
+	}
+	
 	/// <summary>Checks whether a subtitle with the specified index exists in the collection.</summary>
 	/// <param name="index">The zero-based index.</param>
 	/// <returns>Whether the index is contained within the collection.</returns>



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