4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "bcsignals.h"
29 #include "edlsession.h"
31 #include "filesystem.h"
32 #include "localsession.h"
34 #include "mainsession.h"
35 #include "nestededls.h"
36 #include "trackcanvas.h"
38 #include "transition.h"
47 Edit::Edit(EDL *edl, Track *track)
52 if(track) this->edits = track->edits;
56 Edit::Edit(EDL *edl, Edits *edits)
61 if(edits) this->track = edits->track;
67 //printf("Edit::~Edit 1\n");
68 if(transition) delete transition;
69 //printf("Edit::~Edit 2\n");
88 Indexable* Edit::get_source()
90 if(asset) return asset;
91 if(nested_edl) return nested_edl;
95 int Edit::copy(int64_t start,
98 const char *output_path)
101 //printf("Edit::copy 1\n");
103 int64_t endproject = startproject + length;
106 if((startproject >= start && startproject <= end) || // startproject in range
107 (endproject <= end && endproject >= start) || // endproject in range
108 (startproject <= start && endproject >= end)) // range in project
111 int64_t startproject_in_selection = startproject; // start of edit in selection in project
112 int64_t startsource_in_selection = startsource; // start of source in selection in source
113 //int64_t endsource_in_selection = startsource + length; // end of source in selection
114 int64_t length_in_selection = length; // length of edit in selection
115 //printf("Edit::copy 2\n");
117 if(startproject < start)
118 { // start is after start of edit in project
119 int64_t length_difference = start - startproject;
121 startsource_in_selection += length_difference;
122 startproject_in_selection += length_difference;
123 length_in_selection -= length_difference;
125 //printf("Edit::copy 3\n");
128 { // end is before end of edit in project
129 length_in_selection = end - startproject_in_selection;
132 //printf("Edit::copy 4\n");
133 if(file) // only if not counting
135 file->tag.set_title("EDIT");
136 file->tag.set_property("STARTSOURCE", startsource_in_selection);
137 file->tag.set_property("CHANNEL", (int64_t)channel);
138 file->tag.set_property("LENGTH", length_in_selection);
139 if(user_title[0]) file->tag.set_property("USER_TITLE", user_title);
140 //printf("Edit::copy 5\n");
142 copy_properties_derived(file, length_in_selection);
145 // file->append_newline();
146 //printf("Edit::copy 6\n");
150 file->tag.set_title("NESTED_EDL");
151 file->tag.set_property("SRC", nested_edl->path);
157 //printf("Edit::copy 6 %s\n", asset->path);
158 char stored_path[BCTEXTLEN];
159 char asset_directory[BCTEXTLEN];
160 char output_directory[BCTEXTLEN];
163 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
164 fs.extract_dir(asset_directory, asset->path);
165 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
168 fs.extract_dir(output_directory, output_path);
170 output_directory[0] = 0;
171 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
173 if(output_path && !strcmp(asset_directory, output_directory))
174 fs.extract_name(stored_path, asset->path);
176 strcpy(stored_path, asset->path);
178 file->tag.set_title("FILE");
179 file->tag.set_property("SRC", stored_path);
181 file->tag.set_title("/FILE");
187 transition->save_xml(file);
190 //printf("Edit::copy 7\n");
191 file->tag.set_title("/EDIT");
193 file->append_newline();
194 //printf("Edit::copy 8\n");
196 //printf("Edit::copy 9\n");
203 //printf("Edit::copy 10\n");
208 int64_t Edit::get_source_end(int64_t default_)
213 void Edit::insert_transition(char *title)
215 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
217 transition = new Transition(edl,
220 track->to_units(edl->session->default_transition_length, 1));
223 void Edit::detach_transition()
225 if(transition) delete transition;
231 if(asset || nested_edl)
238 void Edit::copy_from(Edit *edit)
240 this->nested_edl = edl->nested_edls->get_copy(edit->nested_edl);
241 this->asset = edl->assets->update(edit->asset);
242 this->startsource = edit->startsource;
243 this->startproject = edit->startproject;
244 this->length = edit->length;
245 strcpy (this->user_title, edit->user_title);
249 if(!transition) transition = new Transition(edl,
251 edit->transition->title,
252 edit->transition->length);
253 *this->transition = *edit->transition;
255 this->channel = edit->channel;
258 void Edit::equivalent_output(Edit *edit, int64_t *result)
260 // End of edit changed
261 if(startproject + length != edit->startproject + edit->length)
263 int64_t new_length = MIN(startproject + length,
264 edit->startproject + edit->length);
265 if(*result < 0 || new_length < *result)
266 *result = new_length;
270 // Different nested EDLs
271 (edit->nested_edl && !nested_edl) ||
272 (!edit->nested_edl && nested_edl) ||
274 (edit->asset == 0 && asset != 0) ||
275 (edit->asset != 0 && asset == 0) ||
276 // different transitions
277 (edit->transition == 0 && transition != 0) ||
278 (edit->transition != 0 && transition == 0) ||
280 (startproject != edit->startproject) ||
281 (startsource != edit->startsource) ||
282 // Transition changed
283 (transition && edit->transition &&
284 !transition->identical(edit->transition)) ||
286 (asset && edit->asset &&
287 !asset->equivalent(*edit->asset, 1, 1)) ||
288 // Nested EDL changed
289 (nested_edl && edit->nested_edl &&
290 strcmp(nested_edl->path, edit->nested_edl->path))
293 // Start of edit changed
294 if(*result < 0 || startproject < *result) *result = startproject;
299 Edit& Edit::operator=(Edit& edit)
301 //printf("Edit::operator= called\n");
306 void Edit::synchronize_params(Edit *edit)
312 // Comparison for ResourcePixmap drawing
313 int Edit::identical(Edit &edit)
315 int result = (this->nested_edl == edit.nested_edl &&
316 this->asset == edit.asset &&
317 this->startsource == edit.startsource &&
318 this->startproject == edit.startproject &&
319 this->length == edit.length &&
320 this->transition == edit.transition &&
321 this->channel == edit.channel);
325 int Edit::operator==(Edit &edit)
327 return identical(edit);
330 double Edit::frames_per_picon()
332 return Units::round(picon_w()) / frame_w();
335 double Edit::frame_w()
337 return track->from_units(1) *
338 edl->session->sample_rate /
339 edl->local_session->zoom_sample;
342 double Edit::picon_w()
349 else if(nested_edl) {
350 w = nested_edl->session->output_w;
351 h = nested_edl->session->output_h;
353 return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
358 return edl->local_session->zoom_track;
362 int Edit::dump(FILE *fp)
364 fprintf(fp," EDIT %p\n", this); fflush(fp);
365 fprintf(fp," nested_edl=%p %s asset=%p %s\n",
367 nested_edl ? nested_edl->path : "",
369 asset ? asset->path : "");
371 fprintf(fp," channel %d\n", channel);
374 fprintf(fp," TRANSITION %p\n", transition);
375 transition->dump(fp);
377 fprintf(fp," startsource %jd startproject %jd length %jd\n",
378 startsource, startproject, length); fflush(fp);
382 int Edit::load_properties(FileXML *file, int64_t &startproject)
384 startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
385 length = file->tag.get_property("LENGTH", (int64_t)0);
387 file->tag.get_property("USER_TITLE", user_title);
388 this->startproject = startproject;
389 load_properties_derived(file);
393 void Edit::shift(int64_t difference)
395 //printf("Edit::shift 1 %p %jd %jd\n", this, startproject, difference);
396 startproject += difference;
397 //printf("Edit::shift 2 %jd %jd\n", startproject, difference);
400 int Edit::shift_start_in(int edit_mode,
409 int64_t cut_length = newposition - oldposition;
410 int64_t end_previous_source, end_source;
412 if(edit_mode == MOVE_ALL_EDITS)
414 if(cut_length < length)
416 edits->clear_recursive(oldposition,
426 edits->clear_recursive(oldposition,
427 startproject + length,
436 if(edit_mode == MOVE_ONE_EDIT)
438 // Paste silence and cut
439 //printf("Edit::shift_start_in 1\n");
442 Edit *new_edit = edits->create_edit();
443 new_edit->startproject = this->startproject;
444 new_edit->length = 0;
445 edits->insert_before(this,
448 //printf("Edit::shift_start_in 2 %p\n", previous);
450 end_previous_source = previous->get_source_end(previous->startsource + previous->length + cut_length);
451 if(end_previous_source > 0 &&
452 previous->startsource + previous->length + cut_length > end_previous_source)
453 cut_length = end_previous_source - previous->startsource - previous->length;
455 if(cut_length < length)
457 startproject += cut_length;
458 startsource += cut_length;
459 length -= cut_length;
460 previous->length += cut_length;
461 //printf("Edit::shift_start_in 2\n");
464 { // Clear entire edit
466 previous->length += cut_length;
467 for(Edit* current_edit = this; current_edit; current_edit = current_edit->next)
469 current_edit->startproject += cut_length;
471 edits->clear_recursive(oldposition + cut_length,
472 startproject + cut_length,
479 //printf("Edit::shift_start_in 3\n");
482 if(edit_mode == MOVE_NO_EDITS)
484 end_source = get_source_end(startsource + length + cut_length);
485 if(end_source > 0 && startsource + length + cut_length > end_source)
486 cut_length = end_source - startsource - length;
488 startsource += cut_length;
493 int Edit::shift_start_out(int edit_mode,
502 int64_t cut_length = oldposition - newposition;
505 if(asset || nested_edl)
507 int64_t end_source = get_source_end(1);
509 //printf("Edit::shift_start_out 1 %jd %jd\n", startsource, cut_length);
510 if(end_source > 0 && startsource < cut_length)
512 cut_length = startsource;
516 if(edit_mode == MOVE_ALL_EDITS)
518 //printf("Edit::shift_start_out 10 %jd\n", cut_length);
519 startsource -= cut_length;
520 length += cut_length;
523 edits->shift_keyframes_recursive(startproject,
526 edits->shift_effects_recursive(startproject,
530 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
532 current_edit->startproject += cut_length;
536 if(edit_mode == MOVE_ONE_EDIT)
540 if(cut_length < previous->length)
541 { // Cut into previous edit
542 previous->length -= cut_length;
543 startproject -= cut_length;
544 startsource -= cut_length;
545 length += cut_length;
546 printf("Edit::shift_start_out 2\n");
549 { // Clear entire previous edit
550 cut_length = previous->length;
551 previous->length = 0;
552 length += cut_length;
553 startsource -= cut_length;
554 startproject -= cut_length;
559 if(edit_mode == MOVE_NO_EDITS)
561 startsource -= cut_length;
564 // Fix infinite length files
565 if(startsource < 0) startsource = 0;
569 int Edit::shift_end_in(int edit_mode,
578 int64_t cut_length = oldposition - newposition;
580 if(edit_mode == MOVE_ALL_EDITS)
582 //printf("Edit::shift_end_in 1\n");
583 if(newposition > startproject)
584 { // clear partial edit
585 //printf("Edit::shift_end_in %p %p\n", track->edits, edits);
586 edits->clear_recursive(newposition,
595 { // clear entire edit
596 edits->clear_recursive(startproject,
606 if(edit_mode == MOVE_ONE_EDIT)
612 int64_t end_source = next->get_source_end(1);
614 if(end_source > 0 && next->startsource - cut_length < 0)
616 cut_length = next->startsource;
620 if(cut_length < length)
622 length -= cut_length;
623 next->startproject -= cut_length;
624 next->startsource -= cut_length;
625 next->length += cut_length;
626 //printf("Edit::shift_end_in 2 %d\n", cut_length);
631 next->length += cut_length;
632 next->startsource -= cut_length;
633 next->startproject -= cut_length;
634 length -= cut_length;
639 if(cut_length < length)
641 length -= cut_length;
646 edits->clear_recursive(startproject,
657 // Does nothing for plugins
658 if(edit_mode == MOVE_NO_EDITS)
660 //printf("Edit::shift_end_in 3\n");
661 int64_t end_source = get_source_end(1);
662 if(end_source > 0 && startsource < cut_length)
664 cut_length = startsource;
666 startsource -= cut_length;
671 int Edit::shift_end_out(int edit_mode,
680 int64_t cut_length = newposition - oldposition;
681 int64_t endsource = get_source_end(startsource + length + cut_length);
683 // check end of edit against end of source file
684 if(endsource > 0 && startsource + length + cut_length > endsource)
685 cut_length = endsource - startsource - length;
687 //printf("Edit::shift_end_out 1 %jd %d %d %d\n", oldposition, newposition, this->length, cut_length);
688 if(edit_mode == MOVE_ALL_EDITS)
691 this->length += cut_length;
693 // Effects are shifted in length extension
695 edits->shift_effects_recursive(oldposition /* startproject */,
699 edits->shift_keyframes_recursive(oldposition /* startproject */,
702 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
704 current_edit->startproject += cut_length;
708 if(edit_mode == MOVE_ONE_EDIT)
712 if(cut_length < next->length)
714 length += cut_length;
715 next->startproject += cut_length;
716 next->startsource += cut_length;
717 next->length -= cut_length;
718 //printf("Edit::shift_end_out 2 %d\n", cut_length);
722 cut_length = next->length;
724 length += cut_length;
729 length += cut_length;
733 if(edit_mode == MOVE_NO_EDITS)
735 startsource += cut_length;
768 int Edit::popup_transition(float view_start, float zoom_units, int cursor_x, int cursor_y)
770 int64_t left, right, left_unit, right_unit;
771 if(!transition) return 0;
772 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
774 if(cursor_x > left && cursor_x < right)
776 // transition->popup_transition(cursor_x, cursor_y);
782 int Edit::select_handle(float view_start, float zoom_units, int cursor_x, int cursor_y, int64_t &selection)
784 int64_t left, right, left_unit, right_unit;
785 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
787 int64_t pixel1, pixel2;
789 pixel2 = pixel1 + 10;
792 // cursor_x is faked in acanvas
793 if(cursor_x >= pixel1 && cursor_x <= pixel2)
795 selection = left_unit;
796 return 1; // left handle
799 //int64_t endproject = startproject + length;
801 pixel1 = pixel2 - 10;
804 if(cursor_x >= pixel1 && cursor_x <= pixel2)
806 selection = right_unit;
807 return 2; // right handle