From 045015e0960ee41647e8361877bb0ac57adfc3c9 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 22 Jun 2018 16:05:30 -0700 Subject: [PATCH] added resizable tracks #75 --- mainwindow.cpp | 10 +++ mainwindow.h | 4 ++ mainwindow.ui | 18 ++++++ panels/timeline.cpp | 25 ++++++-- panels/timeline.h | 2 + ui/timelinewidget.cpp | 144 +++++++++++++++++++++++++++++++----------- ui/timelinewidget.h | 10 ++- 7 files changed, 170 insertions(+), 43 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 19b648740..4d5b685c7 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -433,3 +433,13 @@ void MainWindow::on_actionPreferences_triggered() PreferencesDialog pd(this); pd.exec(); } + +void MainWindow::on_actionIncrease_Track_Height_triggered() +{ + panel_timeline->increase_track_height(); +} + +void MainWindow::on_actionDecrease_Track_Height_triggered() +{ + panel_timeline->decrease_track_height(); +} diff --git a/mainwindow.h b/mainwindow.h index c27db14ca..432620005 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -111,6 +111,10 @@ private slots: void on_actionPreferences_triggered(); + void on_actionIncrease_Track_Height_triggered(); + + void on_actionDecrease_Track_Height_triggered(); + private: Ui::MainWindow *ui; void setup_layout(); diff --git a/mainwindow.ui b/mainwindow.ui index 74472a021..f1cd0cb57 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -92,6 +92,8 @@ + + @@ -474,6 +476,22 @@ Edit Tool Also Seeks + + + Increase Track Height + + + Ctrl+= + + + + + Decrease Track Height + + + Ctrl+- + + diff --git a/panels/timeline.cpp b/panels/timeline.cpp index 3a8114cc9..f14e2767f 100644 --- a/panels/timeline.cpp +++ b/panels/timeline.cpp @@ -224,15 +224,17 @@ void Timeline::repaint_timeline() { } void Timeline::redraw_all_clips() { - project_changed = true; + if (sequence != NULL) { + project_changed = true; - ui->video_area->redraw_clips(); - ui->audio_area->redraw_clips(); - ui->headers->update(); + ui->video_area->redraw_clips(); + ui->audio_area->redraw_clips(); + ui->headers->update(); - panel_viewer->ui->endTimecode->setText(frame_to_timecode(sequence->getEndFrame())); + panel_viewer->ui->endTimecode->setText(frame_to_timecode(sequence->getEndFrame())); - reset_all_audio(); + reset_all_audio(); + } } void Timeline::select_all() { @@ -241,6 +243,7 @@ void Timeline::select_all() { Clip* c = sequence->get_clip(i); selections.append({c->timeline_in, c->timeline_out, c->track}); } + repaint_timeline(); } void Timeline::delete_selection(bool ripple_delete) { @@ -667,6 +670,16 @@ void Timeline::snap_to_clip(long* l, bool playhead_inclusive) { } } +void Timeline::increase_track_height() { + ui->video_area->increase_track_height(); + ui->audio_area->increase_track_height(); +} + +void Timeline::decrease_track_height() { + ui->video_area->decrease_track_height(); + ui->audio_area->decrease_track_height(); +} + void Timeline::deselect() { selections.clear(); repaint_timeline(); diff --git a/panels/timeline.h b/panels/timeline.h index 844bca096..7bb436ba4 100644 --- a/panels/timeline.h +++ b/panels/timeline.h @@ -70,6 +70,8 @@ public: void split_clip_and_relink(Clip* clip, long frame, bool relink); void delete_areas_and_relink(QVector areas); void update_sequence(); + void increase_track_height(); + void decrease_track_height(); int get_snap_range(); int getScreenPointFromFrame(long frame); diff --git a/ui/timelinewidget.cpp b/ui/timelinewidget.cpp index 13fa80a44..35f563711 100644 --- a/ui/timelinewidget.cpp +++ b/ui/timelinewidget.cpp @@ -22,11 +22,15 @@ #include #include +#define TRACK_MIN_HEIGHT 30 +#define TRACK_DEFAULT_HEIGHT 60 +#define TRACK_HEIGHT_INCREMENT 10 + TimelineWidget::TimelineWidget(QWidget *parent) : QWidget(parent) { bottom_align = false; - setMouseTracking(true); - track_height = 80; + track_resizing = false; + setMouseTracking(true); setAcceptDrops(true); @@ -44,6 +48,15 @@ bool same_sign(int a, int b) { return (a < 0) == (b < 0); } +int TimelineWidget::calculate_track_height(int track) { + int index = (track < 0) ? qAbs(track + 1) : track; + while (track_heights.size() < index+1) { + track_heights.append(TRACK_DEFAULT_HEIGHT); + } + qDebug() << track_heights.at(index); + return track_heights.at(index); +} + void TimelineWidget::resizeEvent(QResizeEvent*) { if (sequence != NULL) redraw_clips(); } @@ -184,7 +197,10 @@ void TimelineWidget::mousePressEvent(QMouseEvent *event) { case TIMELINE_TOOL_RIPPLE: case TIMELINE_TOOL_SLIP: { - if (clip_index >= 0) { + if (track_resizing) { + track_resize_mouse_cache = event->pos().y(); + panel_timeline->moving_init = true; + } else if (clip_index >= 0) { if (panel_timeline->is_clip_selected(sequence->get_clip(clip_index))) { // TODO if shift is down, deselect it } else { @@ -620,7 +636,20 @@ void TimelineWidget::mouseMoveEvent(QMouseEvent *event) { panel_timeline->repaint_timeline(); } } else if (panel_timeline->moving_init) { - if (panel_timeline->moving_proc) { + if (track_resizing) { + int diff = track_resize_mouse_cache - event->pos().y(); + int new_height = track_resize_old_value; + if (bottom_align) { + new_height += diff; + } else { + new_height -= diff; + } + if (new_height < TRACK_MIN_HEIGHT) new_height = TRACK_MIN_HEIGHT; + if (new_height != track_heights.at(track_target)) { + track_heights[track_target] = new_height; + redraw_clips(); + } + } else if (panel_timeline->moving_proc) { QPoint pos = event->pos(); update_ghosts(pos); } else { @@ -687,10 +716,7 @@ void TimelineWidget::mouseMoveEvent(QMouseEvent *event) { } } } - } - - // debug code - print the information we got -// qDebug() << "found" << pre_clips.size() << "preceding clips and" << post_clips.size() << "following"; + } } init_ghosts(); @@ -712,6 +738,8 @@ void TimelineWidget::mouseMoveEvent(QMouseEvent *event) { // redraw clips since we changed them if (repaint) panel_timeline->redraw_all_clips(); } else if (panel_timeline->tool == TIMELINE_TOOL_POINTER || panel_timeline->tool == TIMELINE_TOOL_RIPPLE) { + track_resizing = false; + QPoint pos = event->pos(); int lim = 5; @@ -746,8 +774,29 @@ void TimelineWidget::mouseMoveEvent(QMouseEvent *event) { if (found) { setCursor(Qt::SizeHorCursor); } else { - unsetCursor(); - panel_timeline->trim_target = -1; + panel_timeline->trim_target = -1; + + // look for track heights + found = false; + int track_y = 0; + for (int i=0;i y_test_value-test_range && pos.y() < y_test_value+test_range) { + found = true; + track_resizing = true; + track_target = i; + track_resize_old_value = track_height; + } + } + + if (found) { + setCursor(Qt::SizeVerCursor); + } else { + unsetCursor(); + } } } else if (panel_timeline->tool == TIMELINE_TOOL_EDIT || panel_timeline->tool == TIMELINE_TOOL_RAZOR) { // redraw because we have a cursor @@ -787,7 +836,7 @@ void TimelineWidget::redraw_clips() { audio_track_limit = clip->track; } - QRect clip_rect(panel_timeline->getScreenPointFromFrame(clip->timeline_in), getScreenPointFromTrack(clip->track), clip->getLength() * panel_timeline->zoom, track_height); + QRect clip_rect(panel_timeline->getScreenPointFromFrame(clip->timeline_in), getScreenPointFromTrack(clip->track), clip->getLength() * panel_timeline->zoom, calculate_track_height(clip->track)); clip_painter.fillRect(clip_rect, QColor(clip->color_r, clip->color_g, clip->color_b)); // draw thumbnail/waveform @@ -860,7 +909,7 @@ void TimelineWidget::redraw_clips() { } else { // only draw lines for audio tracks for (int i=0;igetScreenPointFromFrame(s.in); - p.fillRect(selection_x, selection_y, panel_timeline->getScreenPointFromFrame(s.out) - selection_x, track_height, QColor(0, 0, 0, 64)); + p.fillRect(selection_x, selection_y, panel_timeline->getScreenPointFromFrame(s.out) - selection_x, calculate_track_height(s.track), QColor(0, 0, 0, 64)); } } @@ -892,7 +941,7 @@ void TimelineWidget::paintEvent(QPaintEvent*) { int ghost_x = panel_timeline->getScreenPointFromFrame(g.in); int ghost_y = getScreenPointFromTrack(g.track); int ghost_width = panel_timeline->getScreenPointFromFrame(g.out - g.in) - 1; - int ghost_height = track_height - 1; + int ghost_height = calculate_track_height(g.track) - 1; p.setPen(QColor(255, 255, 0)); for (int j=0;jcursor_track); p.setPen(Qt::gray); - p.drawLine(cursor_x, cursor_y, cursor_x, cursor_y + track_height); + p.drawLine(cursor_x, cursor_y, cursor_x, cursor_y + calculate_track_height(panel_timeline->cursor_track)); } } } } +void TimelineWidget::increase_track_height() { + for (int i=0;i= 0)); } @@ -937,30 +1001,38 @@ bool TimelineWidget::is_track_visible(int track) { // screen point <-> frame/track functions // ************************************** -int TimelineWidget::getTrackFromScreenPoint(int y) { - if (bottom_align) { - y -= rect().bottom(); - if (show_track_lines) y -= 1; - } else { - y -= 1; - } - int temp_track_height = track_height; - if (show_track_lines) temp_track_height--; - return (int)qFloor((float) (y)/ (float) temp_track_height); +int TimelineWidget::getTrackFromScreenPoint(int y) { + if (bottom_align) { + y = qAbs(y - rect().height()); + } + y--; + int height_measure = 0; + int counter = (bottom_align) ? -1 : 0; + int track_height = calculate_track_height(counter); + while (y > height_measure+track_height) { + if (show_track_lines && counter != -1) y--; + height_measure += track_height; + if (bottom_align) { + counter--; + } else { + counter++; + } + track_height = calculate_track_height(counter); + } + return counter; } int TimelineWidget::getScreenPointFromTrack(int track) { - int temp_track_height = track_height; - if (show_track_lines) temp_track_height++; - int y = track * temp_track_height; - - if (bottom_align) { // video track - y += rect().bottom(); - if (show_track_lines) y += 1; - } else { // audio track - y += 1; - } - return y; + int y = 0; + int counter = 0; + while (counter != track) { + if (bottom_align) counter--; + y += calculate_track_height(counter); + if (!bottom_align) counter++; + if (show_track_lines && counter != -1) y++; + } + y++; + return (bottom_align) ? rect().height() - y : y; } int TimelineWidget::getClipIndexFromCoords(long frame, int track) { diff --git a/ui/timelinewidget.h b/ui/timelinewidget.h index 7201bbf84..fc51f1cf9 100644 --- a/ui/timelinewidget.h +++ b/ui/timelinewidget.h @@ -20,6 +20,8 @@ public: bool bottom_align; void redraw_clips(); + void increase_track_height(); + void decrease_track_height(); protected: void paintEvent(QPaintEvent*); void resizeEvent(QResizeEvent*); @@ -40,7 +42,13 @@ private: int getTrackFromScreenPoint(int y); int getScreenPointFromTrack(int track); int getClipIndexFromCoords(long frame, int track); - int track_height; + + QVector track_heights; + int track_resize_mouse_cache; + int track_resize_old_value; + bool track_resizing; + int track_target; + int calculate_track_height(int track); QList pre_clips; QList post_clips;