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"
33 #include "localsession.h"
35 #include "mainsession.h"
37 #include "trackcanvas.h"
39 #include "transition.h"
48 Edit::Edit(EDL *edl, Track *track)
53 if(track) this->edits = track->edits;
58 Edit::Edit(EDL *edl, Edits *edits)
63 if(edits) this->track = edits->track;
70 //printf("Edit::~Edit 1\n");
71 if(transition) delete transition;
72 //printf("Edit::~Edit 2\n");
95 Indexable* Edit::get_source()
97 if(asset) return asset;
98 if(nested_edl) return nested_edl;
102 int Edit::copy(int64_t start,
105 const char *output_path)
108 //printf("Edit::copy 1\n");
110 int64_t endproject = startproject + length;
113 if((startproject >= start && startproject <= end) || // startproject in range
114 (endproject <= end && endproject >= start) || // endproject in range
115 (startproject <= start && endproject >= end)) // range in project
118 int64_t startproject_in_selection = startproject; // start of edit in selection in project
119 int64_t startsource_in_selection = startsource; // start of source in selection in source
120 //int64_t endsource_in_selection = startsource + length; // end of source in selection
121 int64_t length_in_selection = length; // length of edit in selection
122 //printf("Edit::copy 2\n");
124 if(startproject < start)
125 { // start is after start of edit in project
126 int64_t length_difference = start - startproject;
128 startsource_in_selection += length_difference;
129 startproject_in_selection += length_difference;
130 length_in_selection -= length_difference;
132 //printf("Edit::copy 3\n");
135 { // end is before end of edit in project
136 length_in_selection = end - startproject_in_selection;
139 //printf("Edit::copy 4\n");
140 if(file) // only if not counting
142 file->tag.set_title("EDIT");
143 file->tag.set_property("STARTSOURCE", startsource_in_selection);
144 file->tag.set_property("CHANNEL", (int64_t)channel);
145 file->tag.set_property("LENGTH", length_in_selection);
146 file->tag.set_property("HARD_LEFT", hard_left);
147 file->tag.set_property("HARD_RIGHT", hard_right);
148 file->tag.set_property("COLOR", color);
149 file->tag.set_property("GROUP_ID", group_id);
150 if(user_title[0]) file->tag.set_property("USER_TITLE", user_title);
151 //printf("Edit::copy 5\n");
153 copy_properties_derived(file, length_in_selection);
156 // file->append_newline();
157 //printf("Edit::copy 6\n");
161 file->tag.set_title("NESTED_EDL");
162 file->tag.set_property("SRC", nested_edl->path);
164 file->tag.set_title("/NESTED_EDL");
166 file->append_newline();
171 //printf("Edit::copy 6 %s\n", asset->path);
172 char stored_path[BCTEXTLEN];
173 char asset_directory[BCTEXTLEN];
174 char output_directory[BCTEXTLEN];
177 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
178 fs.extract_dir(asset_directory, asset->path);
179 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
182 fs.extract_dir(output_directory, output_path);
184 output_directory[0] = 0;
185 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
187 if(output_path && !strcmp(asset_directory, output_directory))
188 fs.extract_name(stored_path, asset->path);
190 strcpy(stored_path, asset->path);
192 file->tag.set_title("FILE");
193 file->tag.set_property("SRC", stored_path);
195 file->tag.set_title("/FILE");
199 if(transition && startsource_in_selection == startsource)
201 transition->save_xml(file);
204 //printf("Edit::copy 7\n");
205 file->tag.set_title("/EDIT");
207 file->append_newline();
208 //printf("Edit::copy 8\n");
210 //printf("Edit::copy 9\n");
217 //printf("Edit::copy 10\n");
222 int64_t Edit::get_source_end(int64_t default_)
227 void Edit::insert_transition(char *title)
229 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
231 transition = new Transition(edl, this, title,
232 track->to_units(edl->session->default_transition_length, 1));
235 void Edit::detach_transition()
237 if(transition) delete transition;
243 return (track->data_type != TRACK_SUBTITLE ?
244 asset || nested_edl :
245 *((SEdit *)this)->get_text()) ? 0 : 1;
248 void Edit::set_selected(int v)
251 edl->tracks->set_group_selected(group_id, v);
253 is_selected = v >= 0 ? v : !is_selected ? 1 : 0;
256 void Edit::copy_from(Edit *edit)
258 this->orig_id = edit->orig_id;
259 this->nested_edl = edl->nested_edls.get_nested(edit->nested_edl);
260 this->asset = edl->assets->update(edit->asset);
261 this->startsource = edit->startsource;
262 this->startproject = edit->startproject;
263 this->length = edit->length;
264 this->hard_left = edit->hard_left;
265 this->hard_right = edit->hard_right;
266 this->color = edit->color;
267 this->group_id = edit->group_id;
268 strcpy (this->user_title, edit->user_title);
272 if(!transition) transition = new Transition(edl,
274 edit->transition->title,
275 edit->transition->length);
276 *this->transition = *edit->transition;
278 this->channel = edit->channel;
281 void Edit::clone_from(Edit *edit)
284 edit->orig_id = edit->id;
287 void Edit::equivalent_output(Edit *edit, int64_t *result)
289 // End of edit changed
290 if(startproject + length != edit->startproject + edit->length)
292 int64_t new_length = MIN(startproject + length,
293 edit->startproject + edit->length);
294 if(*result < 0 || new_length < *result)
295 *result = new_length;
299 // Different nested EDLs
300 (edit->nested_edl && !nested_edl) ||
301 (!edit->nested_edl && nested_edl) ||
303 (edit->asset == 0 && asset != 0) ||
304 (edit->asset != 0 && asset == 0) ||
305 // different transitions
306 (edit->transition == 0 && transition != 0) ||
307 (edit->transition != 0 && transition == 0) ||
309 (startproject != edit->startproject) ||
310 (startsource != edit->startsource) ||
311 // Transition changed
312 (transition && edit->transition &&
313 !transition->identical(edit->transition)) ||
315 (asset && edit->asset &&
316 !asset->equivalent(*edit->asset, 1, 1, edl)) ||
317 // Nested EDL changed
318 (nested_edl && edit->nested_edl &&
319 strcmp(nested_edl->path, edit->nested_edl->path))
322 // Start of edit changed
323 if(*result < 0 || startproject < *result) *result = startproject;
328 Edit& Edit::operator=(Edit& edit)
330 //printf("Edit::operator= called\n");
335 void Edit::synchronize_params(Edit *edit)
341 // Comparison for ResourcePixmap drawing
342 int Edit::identical(Edit &edit)
344 int result = (this->nested_edl == edit.nested_edl &&
345 this->asset == edit.asset &&
346 this->startsource == edit.startsource &&
347 this->startproject == edit.startproject &&
348 this->length == edit.length &&
349 this->transition == edit.transition &&
350 this->channel == edit.channel);
354 int Edit::operator==(Edit &edit)
356 return identical(edit);
359 double Edit::frames_per_picon()
361 return Units::round(picon_w()) / frame_w();
364 double Edit::frame_w()
366 return track->from_units(1) *
367 edl->session->sample_rate /
368 edl->local_session->zoom_sample;
371 double Edit::picon_w()
378 else if(nested_edl) {
379 w = nested_edl->session->output_w;
380 h = nested_edl->session->output_h;
382 return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
387 return edl->local_session->zoom_track;
391 int Edit::dump(FILE *fp)
393 fprintf(fp," EDIT %p\n", this); fflush(fp);
394 fprintf(fp," id %d, orig_id %d, nested_edl=%p %s asset=%p %s\n",
395 id, orig_id, nested_edl, nested_edl ? nested_edl->path : "",
396 asset, asset ? asset->path : "");
398 fprintf(fp," channel %d, color %08x, hard lt/rt %d/%d"
399 " group_id %d, is_selected %d\n",
400 channel, color, hard_left, hard_right, group_id, is_selected);
402 fprintf(fp," TRANSITION %p\n", transition);
403 transition->dump(fp);
405 fprintf(fp," startsource %jd startproject %jd length %jd\n",
406 startsource, startproject, length);
411 int Edit::load_properties(FileXML *file, int64_t &startproject)
413 startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
414 length = file->tag.get_property("LENGTH", (int64_t)0);
415 hard_left = file->tag.get_property("HARD_LEFT", (int64_t)0);
416 hard_right = file->tag.get_property("HARD_RIGHT", (int64_t)0);
417 color = file->tag.get_property("COLOR", 0);
418 group_id = file->tag.get_property("GROUP_ID", group_id);
420 file->tag.get_property("USER_TITLE", user_title);
421 this->startproject = startproject;
422 load_properties_derived(file);
426 void Edit::shift(int64_t difference)
428 startproject += difference;
431 void Edit::trim(int64_t difference)
433 length += difference;
434 if( startproject < 0 ) {
435 if( (startsource+=startproject) < 0 ) startsource = 0;
436 if( (length+=startproject) < 0 ) length = 0;
439 if( startsource < 0 )
441 int64_t src_len = get_source_end(INT64_MAX);
442 if( startsource + length > src_len ) {
443 length = src_len - startsource;
444 if( length < 0 ) length = 0;
450 int Edit::shift_start(int edit_mode, int64_t newposition, int64_t oldposition,
451 int edit_labels, int edit_autos, int edit_plugins, Edits *trim_edits)
453 int64_t cut_length = newposition - oldposition;
454 int rest_moved = edit_mode == MOVE_RIPPLE || edit_mode == MOVE_EDGE ? 1 : 0;
455 if( cut_length > length )
457 else if( cut_length < -length )
458 cut_length = -length;
460 int64_t start = startproject, end = start + length;
461 Edit *prev = this->previous, *next = this->next;
464 switch( edit_mode ) {
467 startsource += cut_length;
468 cut_length = -cut_length;
469 length += cut_length;
470 for( Edit *edit=next; edit; edit=edit->next )
471 edit->startproject += cut_length;
474 if( prev && prev->length + cut_length < 0 )
475 cut_length = -prev->length;
476 if( prev ) prev->trim(cut_length);
477 startproject += cut_length;
478 startsource += cut_length;
479 length -= cut_length;
483 startsource -= cut_length;
487 if( prev && prev->length + cut_length < 0 )
488 cut_length = -prev->length;
489 if( next && next->length - cut_length < 0 )
490 cut_length = next->length;
491 if( prev ) prev->trim(cut_length);
492 startproject += cut_length;
494 next->startproject += cut_length;
495 next->startsource += cut_length;
496 next->trim(-cut_length);
501 startsource -= cut_length;
502 length += cut_length;
503 for( Edit *edit=next; edit; edit=edit->next )
504 edit->startproject += cut_length;
508 return follow_edits(start, end, cut_length, edits_moved, rest_moved,
509 edit_labels, edit_autos, edit_plugins, trim_edits);
512 int Edit::shift_end(int edit_mode, int64_t newposition, int64_t oldposition,
513 int edit_labels, int edit_autos, int edit_plugins, Edits *trim_edits)
515 int64_t cut_length = newposition - oldposition;
516 int rest_moved = edit_mode == MOVE_RIPPLE || edit_mode == MOVE_EDGE ? 1 : 0;
517 if( cut_length > length ) {
518 if( !rest_moved ) cut_length = length;
520 else if( cut_length < -length )
521 cut_length = -length;
522 int64_t start = startproject, end = start + length;
523 Edit *prev = this->previous, *next = this->next;
526 switch( edit_mode ) {
529 length += cut_length;
530 for( Edit *edit=next; edit; edit=edit->next )
531 edit->startproject += cut_length;
534 if( next && next->length - cut_length < 0 )
535 cut_length = next->length;
536 length += cut_length;
538 next->startproject += cut_length;
539 next->startsource += cut_length;
540 next->trim(-cut_length);
545 startsource -= cut_length;
549 if( prev && prev->length + cut_length < 0 )
550 cut_length = -prev->length;
551 if( next && next->length - cut_length < 0 )
552 cut_length = next->length;
553 if( prev ) prev->trim(cut_length);
554 startproject += cut_length;
556 next->startproject += cut_length;
557 next->startsource += cut_length;
558 next->trim(-cut_length);
563 return follow_edits(start, end, cut_length, edits_moved, rest_moved,
564 edit_labels, edit_autos, edit_plugins, trim_edits);
567 int Edit::follow_edits(int64_t start, int64_t end, int64_t cut_length,
568 int edits_moved, int rest_moved, int edit_labels, int edit_autos,
569 int edit_plugins, Edits *trim_edits)
571 if( edits_moved && rest_moved ) {
573 double cut_len = track->from_units(cut_length);
574 double start_pos = edits->track->from_units(start);
576 edits->edl->labels->clear(start_pos + cut_len, start_pos, 1);
577 else if( cut_len > 0 )
578 edits->edl->labels->insert(start_pos, cut_len);
581 track->clear(start + cut_length, start,
582 0, 0, edit_autos, edit_plugins, trim_edits);
583 else if( cut_length > 0 ) {
585 track->shift_keyframes(start, cut_length);
587 track->shift_effects(start, cut_length, 1, trim_edits);
590 else if( edits_moved ) {
592 double cut_len = track->from_units(cut_length);
593 double start_pos = edits->track->from_units(start);
594 edits->edl->labels->insert(start_pos, cut_len);
595 double end_pos = edits->track->from_units(end);
596 edits->edl->labels->insert(end_pos + cut_len, -cut_len);
599 if( cut_length > 0 ) {
600 track->clear(end, end+cut_length, 0, 0, 0, 1, 0);
601 track->shift_keyframes(start, cut_length);
603 else if( cut_length < 0 ) {
604 track->clear(start+cut_length, start, 0, 0, 0, 1, 0);
605 track->shift_keyframes(end+cut_length, -cut_length);
609 if( cut_length > 0 ) {
610 track->clear(end, end+cut_length, 0, 0, -1, 0, 0);
611 track->shift_effects(start, cut_length, 1, 0);
613 else if( cut_length < 0 ) {
614 track->clear(start+cut_length, start, 0, 0, -1, 0, 0);
615 track->shift_effects(end+cut_length, -cut_length, 1, 0);
619 else if( rest_moved ) {
621 double cut_len = track->from_units(cut_length);
622 double end_pos = edits->track->from_units(end);
624 edits->edl->labels->clear(end_pos + cut_len, end_pos, 1);
625 else if( cut_len > 0 )
626 edits->edl->labels->insert(end_pos, cut_len);
629 track->clear(end + cut_length, end,
630 0, 0, edit_autos, edit_plugins, trim_edits);
631 else if( cut_length > 0 ) {
633 track->shift_keyframes(end, cut_length);
635 track->shift_effects(end, cut_length, 1, trim_edits);
643 int Edit::popup_transition(float view_start, float zoom_units, int cursor_x, int cursor_y)
645 int64_t left, right, left_unit, right_unit;
646 if(!transition) return 0;
647 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
649 if(cursor_x > left && cursor_x < right)
651 // transition->popup_transition(cursor_x, cursor_y);
657 int Edit::select_handle(float view_start, float zoom_units, int cursor_x, int cursor_y, int64_t &selection)
659 int64_t left, right, left_unit, right_unit;
660 get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
662 int64_t pixel1, pixel2;
664 pixel2 = pixel1 + xS(10);
667 // cursor_x is faked in acanvas
668 if(cursor_x >= pixel1 && cursor_x <= pixel2)
670 selection = left_unit;
671 return 1; // left handle
674 //int64_t endproject = startproject + length;
676 pixel1 = pixel2 - xS(10);
679 if(cursor_x >= pixel1 && cursor_x <= pixel2)
681 selection = right_unit;
682 return 2; // right handle
687 void Edit::get_title(char *title)
689 if( user_title[0] ) {
690 strcpy(title, user_title);
693 Indexable *idxbl = asset ? (Indexable*)asset : (Indexable*)nested_edl;
699 fs.extract_name(title, idxbl->path);
700 if( asset || track->data_type == TRACK_AUDIO ) {
701 char number[BCSTRLEN];
702 sprintf(number, " #%d", channel + 1);
703 strcat(title, number);