timeline_ruler.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* timeline_ruler.cpp - timeline ruler
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <cmath>
  18. #include <fmt/format.h>
  19. #include <QDebug>
  20. #include <QPainter>
  21. #include <QMouseEvent>
  22. #include <QGuiApplication>
  23. #include <util/strings.h>
  24. #include "timeline_ruler.h"
  25. namespace rainynite::studio {
  26. using util::str;
  27. using namespace fmt::literals;
  28. TimelineRuler::TimelineRuler(QWidget* parent) :
  29. QWidget(parent),
  30. brush(QGuiApplication::palette().brush(QPalette::Disabled, QPalette::Text)),
  31. pen {brush, 1, Qt::SolidLine},
  32. bold_pen {brush, 2, Qt::SolidLine}
  33. {
  34. }
  35. QSize TimelineRuler::sizeHint() const {
  36. return {16, 16};
  37. }
  38. void TimelineRuler::paintEvent(QPaintEvent* /*event*/) {
  39. QPainter painter(this);
  40. if (step < 2)
  41. return;
  42. int sec = -(zero_pos / step);
  43. auto start_pos = zero_pos + sec*step;
  44. painter.setFont({"sans", 8});
  45. for (double x = start_pos; x < width(); x += step) {
  46. painter.setPen(pen);
  47. if (sec % 5 == 0) {
  48. painter.drawText(x + 2, 12, str("{}"_format(std::abs(sec))));
  49. painter.setPen(bold_pen);
  50. }
  51. painter.drawLine(x, 0, x, height());
  52. ++sec;
  53. }
  54. }
  55. void TimelineRuler::mousePressEvent(QMouseEvent* event) {
  56. if (step <= 0)
  57. return;
  58. auto s = (event->pos().x()-zero_pos)/step;
  59. get_core_context()->set_seconds(s);
  60. }
  61. void TimelineRuler::mouseMoveEvent(QMouseEvent* event) {
  62. mousePressEvent(event);
  63. }
  64. void TimelineRuler::set_scroll(int zero_pos_) {
  65. zero_pos = zero_pos_;
  66. update();
  67. }
  68. void TimelineRuler::set_zoom(double step_) {
  69. step = step_;
  70. update();
  71. }
  72. } // namespace rainynite::studio