Merging the SubLib project with Gnome Subtitles
[gn-sub.git] / src / SubLib / Core / Timing / ShiftOperator.cs
blobbef5b17d19b2c5710e3743dda68c319630b6bd6f
1 /*
2 * This file is part of SubLib.
3 * Copyright (C) 2006-2008 Pedro Castro
5 * SubLib is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * SubLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 using SubLib.Core.Domain;
21 using System;
23 namespace SubLib.Core.Timing {
25 /// <summary>Performs shift operations.</summary>
26 public class ShiftOperator {
27 private Subtitles subtitles = null;
29 public ShiftOperator (Subtitles subtitles) {
30 this.subtitles = subtitles;
33 /* Public members */
35 /// <summary>Shifts the subtitles a specified amount of time.</summary>
36 /// <param name="time">The time to use, which can be positive or negative.</param>
37 public void Shift (TimeSpan time) {
38 foreach (Subtitle subtitle in subtitles.Collection)
39 subtitle.Times.Shift(time);
42 /// <summary>Shifts a range of subtitles a specified amount of time.</summary>
43 /// <param name="time">The time to use, which can be positive or negative.</param>
44 /// <param name="startIndex">The subtitle index the range begins with.</param>
45 /// <param name="endIndex">The subtitle index the range ends with.</param>
46 public void Shift (TimeSpan time, int startIndex, int endIndex) {
47 if (!AreShiftArgsValid(startIndex, endIndex))
48 return;
50 for (int index = startIndex ; index <= endIndex ; index++) {
51 Subtitle subtitle = subtitles.Collection.Get(index);
52 subtitle.Times.Shift(time);
56 /// <summary>Shifts the subtitles a specified amount of frames.</summary>
57 /// <param name="frames">The frames to use, which can be positive or negative.</param>
58 public void Shift (int frames) {
59 foreach (Subtitle subtitle in subtitles.Collection)
60 subtitle.Frames.Shift(frames);
63 /// <summary>Shifts a range of subtitles a specified amount of frames.</summary>
64 /// <param name="frames">The frames to use, which can be positive or negative.</param>
65 /// <param name="startIndex">The subtitle index the range begins with.</param>
66 /// <param name="endIndex">The subtitle index the range ends with.</param>
67 public void Shift (int frames, int startIndex, int endIndex) {
68 if (!AreShiftArgsValid(startIndex, endIndex))
69 return;
71 for (int index = startIndex ; index <= endIndex ; index++) {
72 Subtitle subtitle = subtitles.Collection.Get(index);
73 subtitle.Frames.Shift(frames);
78 /* Private members */
80 private bool AreShiftArgsValid (int startIndex, int endIndex) {
81 int subtitleCount = subtitles.Collection.Count;
82 if (subtitleCount == 0)
83 return false;
84 else if (!(startIndex <= endIndex))
85 return false;
86 else if ((startIndex < 0) || (startIndex >= subtitleCount))
87 return false;
88 else if (endIndex >= subtitleCount)
89 return false;
90 else
91 return true;