Initial commit
[fillmore] / src / marina / command.vala
1 /* Copyright 2009 Yorba Foundation
2  *
3  * This software is licensed under the GNU Lesser General Public License
4  * (version 2.1 or later).  See the COPYING file in this distribution. 
5  */
6
7 namespace Model {
8 public abstract class Command {
9     public abstract void apply();
10     public abstract void undo();
11     public abstract bool merge(Command command);
12     public abstract string description();
13 }
14
15 public enum Parameter { PAN, VOLUME }
16
17 public class ParameterCommand : Command {
18     AudioTrack target;
19     Parameter parameter;
20     double delta;
21     
22     public ParameterCommand(AudioTrack target, Parameter parameter, 
23         double new_value, double old_value) {
24         this.target = target;
25         this.parameter = parameter;
26         this.delta = new_value - old_value;
27     }
28
29     void change_parameter(double amount) {
30         switch (parameter) {
31             case Parameter.PAN:
32                 target._set_pan(target.get_pan() + amount);
33                 break;
34             case Parameter.VOLUME:
35                 target._set_volume(target.get_volume() + amount);
36                 break;
37         }
38     }
39     
40     public override void apply() {
41         change_parameter(delta);
42     }
43     
44     public override void undo() {
45         change_parameter(-delta);
46     }
47     
48     public override bool merge(Command command) {
49         ParameterCommand parameter_command = command as ParameterCommand;
50         if (parameter_command != null && parameter_command.parameter == parameter) {
51             delta = delta + parameter_command.delta;
52             return true;
53         }
54         return false;
55     }
56     
57     public override string description() {
58         switch (parameter) {
59             case Parameter.PAN:
60                 return "Adjust Pan";
61             case Parameter.VOLUME:
62                 return "Adjust Level";
63             default:
64                 assert(false);
65                 return "";
66         }
67     }
68 }
69
70 public class ClipCommand : Command {
71     public enum Action { APPEND, DELETE }
72     Track track;
73     Clip clip;
74     int64 time;
75     Action action;
76     int index;
77     bool select;
78     
79     public ClipCommand(Action action, Track track, Clip clip, int64 time, bool select) {
80         this.track = track;
81         this.clip = clip;
82         this.time = time;
83         this.action = action;
84         this.select = select;
85         this.index = track.get_clip_index(clip);
86     }
87     
88     public override void apply() {
89         switch(action) {
90             case Action.APPEND:
91                 track._append_at_time(clip, time, select);
92                 break;
93             case Action.DELETE:
94                 track._delete_clip(clip);
95                 break;
96             default:
97                 assert(false);
98                 break;
99         }
100     }
101     
102     public override void undo() {
103         switch(action) {
104             case Action.APPEND:
105                 track._delete_clip(clip);
106                 break;
107             case Action.DELETE:
108                 track.add(clip, time, false);
109                 break;
110             default:
111                 assert(false);
112                 break;
113         }
114     }
115     
116     public override bool merge(Command command) {
117         return false;
118     }
119     
120     public override string description() {
121         switch(action) {
122             case Action.APPEND:
123                 return "Create Clip";
124             case Action.DELETE:
125                 return "Delete Clip";
126             default:
127                 assert(false);
128                 return "";
129         }
130     }
131 }
132
133 public class ClipAddCommand : Command {
134     Track track;
135     Clip clip;
136     int64 delta;
137     
138     public ClipAddCommand(Track track, Clip clip, int64 original_time, 
139         int64 new_start) {
140         this.track = track;
141         this.clip = clip;
142         this.delta = new_start - original_time;
143     }
144     
145     public override void apply() {
146         track._move(clip, clip.start);
147     }
148     
149     public override void undo() {
150         track.remove_clip_from_array(clip);     
151         track._move(clip, clip.start - delta);
152     }
153     
154     public override bool merge(Command command) {
155         return false;
156     }
157     
158     public override string description() {
159         return "Move Clip";
160     }
161 }
162
163 public class ClipSplitCommand : Command {
164     Track track;
165     int64 time;
166     bool do_split;
167     
168     public enum Action { SPLIT, JOIN }
169
170     public ClipSplitCommand(Action action, Track track, int64 time) {
171         this.track = track;
172         this.time = time;
173         do_split = action == Action.SPLIT;
174     }
175     
176     public override void apply() {
177         if (do_split) {
178             track._split_at(time);
179         } else {
180             track._join(time);
181         }
182     }
183     
184     public override void undo() {
185         if (do_split) {
186             track._join(time);
187         } else {
188             track._split_at(time);
189         }
190     }
191     
192     public override bool merge(Command command) {
193         return false;
194     }
195     
196     public override string description() {
197         if (do_split) {
198             return "Split Clip";
199         } else {
200             return "Join Clip";
201         }
202     }
203 }
204
205 public class ClipFileDeleteCommand : Command {
206     ClipFile clipfile;
207     Project project;
208     
209     public ClipFileDeleteCommand(Project p, ClipFile cf) {
210         clipfile = cf;
211         project = p;
212     }
213     
214     public override void apply() {
215         project._remove_clipfile(clipfile);
216     }
217     
218     public override void undo() {
219         try {
220             project._add_clipfile(clipfile);
221         } catch (Error e) {
222             project.error_occurred("Could not add clipfile.", e.message);
223         }
224     }
225     
226     public override bool merge(Command command) {
227         return false;
228     }
229     
230     public override string description() {
231         return "Delete from Library";
232     }
233 }
234
235 public class ClipTrimCommand : Command {
236     Track track;
237     Clip clip;
238     int64 delta;
239     Gdk.WindowEdge edge;
240     
241     public ClipTrimCommand(Track track, Clip clip, int64 delta, Gdk.WindowEdge edge) {
242         this.track = track;
243         this.clip = clip;
244         this.delta = delta;
245         this.edge = edge;
246     }
247     
248     public override void apply() {
249         track._trim(clip, delta, edge);
250     }
251     
252     public override void undo() {
253         track._trim(clip, -delta, edge);
254     }
255     
256     public override bool merge(Command command) {
257         return false;
258     }
259     
260     public override string description() {
261         return "Trim To Playhead";
262     }
263 }
264
265 public class ClipRevertCommand : Command {
266     Track track;
267     Clip clip;
268     int64 left_delta;
269     int64 right_delta;
270     
271     public ClipRevertCommand(Track track, Clip clip) {
272         this.track = track;
273         this.clip = clip;
274     }
275
276     public override void apply() {
277         right_delta = clip.end;
278         left_delta = clip.media_start;
279         track._revert_to_original(clip);
280         left_delta -= clip.media_start;
281         right_delta = clip.end - right_delta - left_delta;
282     }
283     
284     public override void undo() {
285         track._trim(clip, -left_delta, Gdk.WindowEdge.WEST);
286         track._trim(clip, -right_delta, Gdk.WindowEdge.EAST);
287     }
288     
289     public override bool merge(Command command) {
290         return false;
291     }
292     
293     public override string description() {
294         return "Revert To Original";
295     }
296 }
297
298 public class TimeSignatureCommand : Command {
299     Fraction new_time_signature;
300     Fraction old_time_signature;
301     Project project;
302     
303     public TimeSignatureCommand(Project project, Fraction new_time_signature) {
304         this.project = project;
305         this.new_time_signature = new_time_signature;
306         this.old_time_signature = project.get_time_signature();
307     }
308
309     public override void apply() {
310         project._set_time_signature(new_time_signature);
311     }
312
313     public override void undo() {
314         project._set_time_signature(old_time_signature);
315     }
316     
317     public override bool merge(Command command) {
318         return false;
319     }
320     
321     public override string description() {
322         return "Set Time Signature";
323     }
324 }
325
326 public class BpmCommand : Command {
327     int delta;
328     Project project;
329     
330     public BpmCommand(Project project, int new_bpm) {
331         delta = new_bpm - project.get_bpm();
332         this.project = project;
333     }
334     
335     public override void apply() {
336         project._set_bpm(project.get_bpm() + delta);
337     }
338     
339     public override void undo() {
340         project._set_bpm(project.get_bpm() - delta);
341     }
342     
343     public override bool merge(Command command) {
344         return false;
345     }
346     
347     public override string description() {
348         return "Set Tempo";
349     }
350 }
351
352 public class AddClipCommand : Command {
353     ClipFile clip_file;
354     Project project;
355
356     public AddClipCommand(Project project, ClipFile clip_file) {
357         this.project = project;
358         this.clip_file = clip_file;
359     }
360
361     public override void apply() {
362         try {
363             project._add_clipfile(clip_file);
364         } catch (GLib.Error error) {
365             project.error_occurred("Error importing", "An error occurred importing this file.");
366         }
367     }
368
369     public override void undo() {
370         project._remove_clipfile(clip_file);
371     }
372
373     public override bool merge(Command command) {
374         return false;
375     }
376
377     public override string description() {
378         return "Add Clip To Library";
379     }
380 }
381
382 public class TransactionCommand : Command {
383     bool open;
384     string transaction_description;
385     
386     public TransactionCommand(bool open, string transaction_description) {
387         this.open = open;
388         this.transaction_description = transaction_description;
389     }
390     
391     public bool in_transaction() {
392         return open;
393     }
394     
395     public override void apply() {
396     }
397     
398     public override void undo() {
399     }
400     
401     public override bool merge(Command command) {
402         return false;
403     }
404     
405     public override string description() {
406         return transaction_description;
407     }
408 }
409 }