[gnome-subtitles] Command to perform timings synchronize
- From: Pedro Daniel da Rocha Melo e Castro <pcastro src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnome-subtitles] Command to perform timings synchronize
- Date: Sat, 23 May 2009 07:44:04 -0400 (EDT)
commit 8b79b128947d7979e52bf6e036bf6a4bab23874f
Author: Pedro Castro <mail pedrocastro org>
Date: Sat May 23 12:40:18 2009 +0100
Command to perform timings synchronize
---
.../Core/Command/SynchronizeTimingsCommand.cs | 104 ++++++++++++++++++++
1 files changed, 104 insertions(+), 0 deletions(-)
diff --git a/src/GnomeSubtitles/Core/Command/SynchronizeTimingsCommand.cs b/src/GnomeSubtitles/Core/Command/SynchronizeTimingsCommand.cs
new file mode 100644
index 0000000..dfe28ec
--- /dev/null
+++ b/src/GnomeSubtitles/Core/Command/SynchronizeTimingsCommand.cs
@@ -0,0 +1,104 @@
+/*
+ * This file is part of Gnome Subtitles.
+ * Copyright (C) 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Gnome Subtitles is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+using GnomeSubtitles.Ui.View;
+using Gtk;
+using Mono.Unix;
+using SubLib.Core.Domain;
+using SubLib.Core.Timing;
+using System;
+
+namespace GnomeSubtitles.Core.Command {
+
+public class SynchronizeTimingsCommand : FixedMultipleSelectionCommand {
+ private static string description = Catalog.GetString("Synchronizing timings");
+
+ private SyncPoints syncPoints = null;
+ private bool toSyncAll = false;
+ private TimeSpan[] lastTimes = null;
+
+ public SynchronizeTimingsCommand (SyncPoints syncPoints, bool toSyncAll, SelectionIntended selectionIntended, TreePath[] pathRange) : base(description, false, selectionIntended, pathRange, true) {
+ this.syncPoints = syncPoints;
+ this.toSyncAll = toSyncAll;
+ }
+
+ protected override bool ChangeValues () {
+ Ui.View.Subtitles subtitles = Base.Document.Subtitles;
+
+ if (lastTimes == null) {
+ int[] subtitleRange = GetSubtitleRange(subtitles);
+ TimeSpan[] timesToStore = GetCurrentTimes(subtitleRange, subtitles);
+ SynchronizeOperator syncOp = new SynchronizeOperator(subtitles);
+ if (!syncOp.Sync(syncPoints.Collection, toSyncAll))
+ return false;
+
+ lastTimes = timesToStore;
+ }
+ else {
+ int[] subtitleRange = GetSubtitleRange(subtitles);
+ TimeSpan[] timesToStore = GetCurrentTimes(subtitleRange, subtitles);
+
+ if (subtitleRange == null)
+ return false;
+
+ int subtitleFrom = subtitleRange[0];
+ int subtitleTo = subtitleRange[1];
+ for (int index = subtitleFrom; index <= subtitleTo ; index++) {
+ Subtitle subtitle = subtitles[index];
+ subtitle.Times.Start = lastTimes[index - subtitleFrom];
+ }
+ lastTimes = timesToStore;
+ }
+ return true;
+ }
+
+ private TimeSpan[] GetCurrentTimes (int[] subtitleRange, Ui.View.Subtitles subtitles) {
+ int subtitleFrom = subtitleRange[0];
+ int subtitleTo = subtitleRange[1];
+ TimeSpan[] currentTimes = new TimeSpan[subtitleTo - subtitleFrom + 1];
+
+ for (int index = subtitleFrom; index <= subtitleTo ; index++) {
+ Subtitle subtitle = subtitles[index];
+ currentTimes[index - subtitleFrom] = subtitle.Times.Start;
+ }
+
+ return currentTimes;
+ }
+
+ private int[] GetSubtitleRange (Ui.View.Subtitles subtitles) {
+ if (SelectionType == SelectionType.Range) {
+ TreePath[] paths = Paths;
+ int pathFrom = Util.PathToInt(paths[0]);
+ int pathTo = Util.PathToInt(paths[1]);
+ return new int[] {pathFrom, pathTo};
+ }
+ else if (SelectionType == SelectionType.All) {
+ if (subtitles.Count < 2)
+ return null;
+
+ int pathFrom = 0;
+ int pathTo = subtitles.Count - 1;
+ return new int[] {pathFrom, pathTo};
+ }
+ return null;
+ }
+
+}
+
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]