timeline_view.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * timeline_view.cpp - simple timeline
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <QDebug>
  19. #include <QPainter>
  20. #include <QMouseEvent>
  21. #include "timeline_view.h"
  22. namespace studio {
  23. TimelineView::TimelineView(QWidget* parent) :
  24. QWidget(parent),
  25. time_cursor_pen {{{0xff, 0xff, 0xff}}, 3.0},
  26. destroy_detector(std::make_shared<Null>())
  27. {
  28. }
  29. QSize TimelineView::sizeHint() const {
  30. return {20, 20};
  31. }
  32. void TimelineView::set_zoom_level(int level) {
  33. zoom_level = level;
  34. update();
  35. }
  36. void TimelineView::time_changed(core::Time) {
  37. update();
  38. }
  39. void TimelineView::paintEvent(QPaintEvent* /*event*/) {
  40. if (auto context = get_context()) {
  41. QPainter painter(this);
  42. painter.setPen(time_cursor_pen);
  43. int x = frames_to_x(context->get_time().get_frames());
  44. painter.drawLine(x, 0, x, height());
  45. }
  46. }
  47. void TimelineView::mousePressEvent(QMouseEvent* event) {
  48. if (event->button() == Qt::LeftButton)
  49. start_moving(event->x());
  50. }
  51. void TimelineView::mouseReleaseEvent(QMouseEvent* event) {
  52. if (is_moving && event->button() == Qt::LeftButton)
  53. stop_moving(event->x());
  54. }
  55. void TimelineView::mouseMoveEvent(QMouseEvent* event) {
  56. if (is_moving)
  57. move(event->x());
  58. }
  59. void TimelineView::start_moving(double x) {
  60. is_moving = true;
  61. move(x);
  62. }
  63. void TimelineView::stop_moving(double x) {
  64. is_moving = false;
  65. move(x);
  66. }
  67. void TimelineView::move(double x) {
  68. if (auto context = get_context()) {
  69. int frames = x_to_frames(x);
  70. context->set_frames(frames);
  71. }
  72. }
  73. double TimelineView::frames_to_x(double frames) {
  74. return frames * zoom_factor();
  75. }
  76. double TimelineView::x_to_frames(double x) {
  77. return x / zoom_factor();
  78. }
  79. }