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"
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");
93 Indexable* Edit::get_source()
95 if(asset) return asset;
96 if(nested_edl) return nested_edl;
100 int Edit::copy(int64_t start,
103 const char *output_path)
106 //printf("Edit::copy 1\n");
108 int64_t endproject = startproject + length;
111 if((startproject >= start && startproject <= end) || // startproject in range
112 (endproject <= end && endproject >= start) || // endproject in range
113 (startproject <= start && endproject >= end)) // range in project
116 int64_t startproject_in_selection = startproject; // start of edit in selection in project
117 int64_t startsource_in_selection = startsource; // start of source in selection in source
118 //int64_t endsource_in_selection = startsource + length; // end of source in selection
119 int64_t length_in_selection = length; // length of edit in selection
120 //printf("Edit::copy 2\n");
122 if(startproject < start)
123 { // start is after start of edit in project
124 int64_t length_difference = start - startproject;
126 startsource_in_selection += length_difference;
127 startproject_in_selection += length_difference;
128 length_in_selection -= length_difference;
130 //printf("Edit::copy 3\n");
133 { // end is before end of edit in project
134 length_in_selection = end - startproject_in_selection;
137 //printf("Edit::copy 4\n");
138 if(file) // only if not counting
140 file->tag.set_title("EDIT");
141 file->tag.set_property("STARTSOURCE", startsource_in_selection);
142 file->tag.set_property("CHANNEL", (int64_t)channel);
143 file->tag.set_property("LENGTH", length_in_selection);
144 file->tag.set_property("HARD_LEFT", hard_left);
145 file->tag.set_property("HARD_RIGHT", hard_right);
146 file->tag.set_property("COLOR", color);
147 file->tag.set_property("GROUP_ID", group_id);
148 if(user_title[0]) file->tag.set_property("USER_TITLE", user_title);
149 //printf("Edit::copy 5\n");
151 copy_properties_derived(file, length_in_selection);
154 // file->append_newline();
155 //printf("Edit::copy 6\n");
159 file->tag.set_title("NESTED_EDL");
160 file->tag.set_property("SRC", nested_edl->path);
162 file->tag.set_title("/NESTED_EDL");
164 file->append_newline();
169 //printf("Edit::copy 6 %s\n", asset->path);
170 char stored_path[BCTEXTLEN];
171 char asset_directory[BCTEXTLEN];
172 char output_directory[BCTEXTLEN];
175 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
176 fs.extract_dir(asset_directory, asset->path);
177 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
180 fs.extract_dir(output_directory, output_path);
182 output_directory[0] = 0;
183 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
185 if(output_path && !strcmp(asset_directory, output_directory))
186 fs.extract_name(stored_path, asset->path);
188 strcpy(stored_path, asset->path);
190 file->tag.set_title("FILE");
191 file->tag.set_property("SRC", stored_path);
193 file->tag.set_title("/FILE");
197 if(transition && startsource_in_selection == startsource)
199 transition->save_xml(file);
202 //printf("Edit::copy 7\n");
203 file->tag.set_title("/EDIT");
205 file->append_newline();
206 //printf("Edit::copy 8\n");
208 //printf("Edit::copy 9\n");
215 //printf("Edit::copy 10\n");
220 int64_t Edit::get_source_end(int64_t default_)
225 void Edit::insert_transition(char *title)
227 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
229 transition = new Transition(edl, this, title,
230 track->to_units(edl->session->default_transition_length, 1));
233 void Edit::detach_transition()
235 if(transition) delete transition;
241 return (track->data_type != TRACK_SUBTITLE ?
242 asset || nested_edl :
243 *((SEdit *)this)->get_text()) ? 0 : 1;
246 void Edit::set_selected(int v)
249 edl->tracks->set_group_selected(group_id, v);
251 is_selected = v >= 0 ? v : !is_selected ? 1 : 0;
254 void Edit::copy_from(Edit *edit)
256 this->nested_edl = edl->nested_edls.get_nested(edit->nested_edl);
257 this->asset = edl->assets->update(edit->asset);
258 this->startsource = edit->startsource;
259 this->startproject = edit->startproject;
260 this->length = edit->length;
261 this->hard_left = edit->hard_left;
262 this->hard_right = edit->hard_right;
263 this->color = edit->color;
264 this->group_id = edit->group_id;
265 strcpy (this->user_title, edit->user_title);
269 if(!transition) transition = new Transition(edl,
271 edit->transition->title,
272 edit->transition->length);
273 *this->transition = *edit->transition;
275 this->channel = edit->channel;
278 void Edit::equivalent_output(Edit *edit, int64_t *result)
280 // End of edit changed
281 if(startproject + length != edit->startproject + edit->length)
283 int64_t new_length = MIN(startproject + length,
284 edit->startproject + edit->length);
285 if(*result < 0 || new_length < *result)
286 *result = new_length;
290 // Different nested EDLs
291 (edit->nested_edl && !nested_edl) ||
292 (!edit->nested_edl && nested_edl) ||
294 (edit->asset == 0 && asset != 0) ||
295 (edit->asset != 0 && asset == 0) ||
296 // different transitions
297 (edit->transition == 0 && transition != 0) ||
298 (edit->transition != 0 && transition == 0) ||
300 (startproject != edit->startproject) ||
301 (startsource != edit->startsource) ||
302 // Transition changed
303 (transition && edit->transition &&
304 !transition->identical(edit->transition)) ||
306 (asset && edit->asset &&
307 !asset->equivalent(*edit->asset, 1, 1, edl)) ||
308 // Nested EDL changed
309 (nested_edl && edit->nested_edl &&
310 strcmp(nested_edl->path, edit->nested_edl->path))
313 // Start of edit changed
314 if(*result < 0 || startproject < *result) *result = startproject;
319 Edit& Edit::operator=(Edit& edit)
321 //printf("Edit::operator= called\n");
326 void Edit::synchronize_params(Edit *edit)
332 // Comparison for ResourcePixmap drawing
333 int Edit::identical(Edit &edit)
335 int result = (this->nested_edl == edit.nested_edl &&
336 this->asset == edit.asset &&
337 this->startsource == edit.startsource &&
338 this->startproject == edit.startproject &&
339 this->length == edit.length &&
340 this->transition == edit.transition &&
341 this->channel == edit.channel);
345 int Edit::operator==(Edit &edit)
347 return identical(edit);
350 double Edit::frames_per_picon()
352 return Units::round(picon_w()) / frame_w();
355 double Edit::frame_w()
357 return track->from_units(1) *
358 edl->session->sample_rate /
359 edl->local_session->zoom_sample;
362 double Edit::picon_w()
369 else if(nested_edl) {
370 w = nested_edl->session->output_w;
371 h = nested_edl->session->output_h;
373 return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
378 return edl->local_session->zoom_track;
382 int Edit::dump(FILE *fp)
384 fprintf(fp," EDIT %p\n", this); fflush(fp);
385 fprintf(fp," nested_edl=%p %s asset=%p %s\n",
387 nested_edl ? nested_edl->path : "",
389 asset ? asset->path : "");
391 fprintf(fp," channel %d, color %08x, group_id %d, is_selected %d\n",
392 channel, color, group_id, is_selected);
395 fprintf(fp," TRANSITION %p\n", transition);
396 transition->dump(fp);
398 fprintf(fp," startsource %jd startproject %jd hard lt/rt %d/%d length %jd\n",
399 startsource, startproject, hard_left, hard_right, length); fflush(fp);
403 int Edit::load_properties(FileXML *file, int64_t &startproject)
405 startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
406 length = file->tag.get_property("LENGTH", (int64_t)0);
407 hard_left = file->tag.get_property("HARD_LEFT", (int64_t)0);
408 hard_right = file->tag.get_property("HARD_RIGHT", (int64_t)0);
409 color = file->tag.get_property("COLOR", 0);
410 group_id = file->tag.get_property("GROUP_ID", group_id);
412 file->tag.get_property("USER_TITLE", user_title);
413 this->startproject = startproject;
414 load_properties_derived(file);
418 void Edit::shift(int64_t difference)
420 //printf("Edit::shift 1 %p %jd %jd\n", this, startproject, difference);
421 startproject += difference;
422 //printf("Edit::shift 2 %jd %jd\n", startproject, difference);
425 int Edit::shift_start_in(int edit_mode,
434 int64_t cut_length = newposition - oldposition;
435 int64_t end_previous_source, end_source;
437 if(edit_mode == MOVE_ALL_EDITS)
439 if(cut_length < length)
441 edits->clear_recursive(oldposition,
451 edits->clear_recursive(oldposition,
452 startproject + length,
461 if(edit_mode == MOVE_ONE_EDIT)
463 // Paste silence and cut
464 //printf("Edit::shift_start_in 1\n");
467 Edit *new_edit = edits->create_edit();
468 new_edit->startproject = this->startproject;
469 new_edit->length = 0;
470 edits->insert_before(this,
473 //printf("Edit::shift_start_in 2 %p\n", previous);
475 end_previous_source = previous->get_source_end(previous->startsource + previous->length + cut_length);
476 if(end_previous_source > 0 &&
477 previous->startsource + previous->length + cut_length > end_previous_source)
478 cut_length = end_previous_source - previous->startsource - previous->length;
480 if(cut_length < length)
482 startproject += cut_length;
483 startsource += cut_length;
484 length -= cut_length;
485 previous->length += cut_length;
486 //printf("Edit::shift_start_in 2\n");
489 { // Clear entire edit
491 previous->length += cut_length;
492 for(Edit* current_edit = this; current_edit; current_edit = current_edit->next)
494 current_edit->startproject += cut_length;
496 edits->clear_recursive(oldposition + cut_length,
497 startproject + cut_length,
504 //printf("Edit::shift_start_in 3\n");
507 if(edit_mode == MOVE_NO_EDITS)
509 end_source = get_source_end(startsource + length + cut_length);
510 if(end_source > 0 && startsource + length + cut_length > end_source)
511 cut_length = end_source - startsource - length;
513 startsource += cut_length;
518 int Edit::shift_start_out(int edit_mode,
527 int64_t cut_length = oldposition - newposition;
530 if(asset || nested_edl)
532 int64_t end_source = get_source_end(1);
534 //printf("Edit::shift_start_out 1 %jd %jd\n", startsource, cut_length);
535 if(end_source > 0 && startsource < cut_length)
537 cut_length = startsource;
541 if(edit_mode == MOVE_ALL_EDITS)
543 //printf("Edit::shift_start_out 10 %jd\n", cut_length);
544 startsource -= cut_length;
545 length += cut_length;
548 edits->shift_keyframes_recursive(startproject,
551 edits->shift_effects_recursive(startproject,
555 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
557 current_edit->startproject += cut_length;
561 if(edit_mode == MOVE_ONE_EDIT)
565 if(cut_length < previous->length)
566 { // Cut into previous edit
567 previous->length -= cut_length;
568 startproject -= cut_length;
569 startsource -= cut_length;
570 length += cut_length;
573 { // Clear entire previous edit
574 cut_length = previous->length;
575 previous->length = 0;
576 length += cut_length;
577 startsource -= cut_length;
578 startproject -= cut_length;
583 if(edit_mode == MOVE_NO_EDITS)
585 startsource -= cut_length;
588 // Fix infinite length files
589 if(startsource < 0) startsource = 0;
593 int Edit::shift_end_in(int edit_mode,
602 int64_t cut_length = oldposition - newposition;
604 if(edit_mode == MOVE_ALL_EDITS)
606 //printf("Edit::shift_end_in 1\n");
607 if(newposition > startproject)
608 { // clear partial edit
609 //printf("Edit::shift_end_in %p %p\n", track->edits, edits);
610 edits->clear_recursive(newposition,
619 { // clear entire edit
620 edits->clear_recursive(startproject,
630 if(edit_mode == MOVE_ONE_EDIT)
636 int64_t end_source = next->get_source_end(1);
638 if(end_source > 0 && next->startsource - cut_length < 0)
640 cut_length = next->startsource;
644 if(cut_length < length)
646 length -= cut_length;
647 next->startproject -= cut_length;
648 next->startsource -= cut_length;
649 next->length += cut_length;
650 //printf("Edit::shift_end_in 2 %d\n", cut_length);
655 next->length += cut_length;
656 next->startsource -= cut_length;
657 next->startproject -= cut_length;
658 length -= cut_length;
663 if(cut_length < length)
665 length -= cut_length;
670 edits->clear_recursive(startproject,
681 // Does nothing for plugins
682 if(edit_mode == MOVE_NO_EDITS)
684 //printf("Edit::shift_end_in 3\n");
685 int64_t end_source = get_source_end(1);
686 if(end_source > 0 && startsource < cut_length)
688 cut_length = startsource;
690 startsource -= cut_length;
695 int Edit::shift_end_out(int edit_mode,
704 int64_t cut_length = newposition - oldposition;
705 int64_t endsource = get_source_end(startsource + length + cut_length);
707 // check end of edit against end of source file
708 if(endsource > 0 && startsource + length + cut_length > endsource)
709 cut_length = endsource - startsource - length;
711 //printf("Edit::shift_end_out 1 %jd %d %d %d\n", oldposition, newposition, this->length, cut_length);
712 if(edit_mode == MOVE_ALL_EDITS)
715 this->length += cut_length;
717 // Effects are shifted in length extension
719 edits->shift_effects_recursive(oldposition /* startproject */,
723 edits->shift_keyframes_recursive(oldposition /* startproject */,
726 for(Edit* current_edit = next; current_edit; current_edit = current_edit->next)
728 current_edit->startproject += cut_length;
732 if(edit_mode == MOVE_ONE_EDIT)
736 if(cut_length < next->length)
738 length += cut_length;
739 next->startproject += cut_length;
740 next->startsource += cut_length;
741 next->length -= cut_length;
742 //printf("Edit::shift_end_out %d cut_length=%d\n", __LINE__, cut_length);
746 //printf("Edit::shift_end_out %d cut_length=%d next->length=%d\n", __LINE__, cut_length, next->length);
747 cut_length = next->length;
748 next->startproject += next->length;
749 next->startsource += next->length;
751 length += cut_length;
757 length += cut_length;
761 if(edit_mode == MOVE_NO_EDITS)
763 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
812 void Edit::get_title(char *title)
814 if( user_title[0] ) {
815 strcpy(title, user_title);
818 Indexable *idxbl = asset ? (Indexable*)asset : (Indexable*)nested_edl;
824 fs.extract_name(title, idxbl->path);
825 if( asset || track->data_type == TRACK_AUDIO ) {
826 char number[BCSTRLEN];
827 sprintf(number, " #%d", channel + 1);
828 strcat(title, number);