timeline_cursor.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* timeline_cursor.cpp - timeline cursor widget
  2. * Copyright (C) 2017-2018 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 <QGraphicsRectItem>
  18. #include <generic/context_listener.h>
  19. #include <generic/timeline_editor.h>
  20. #include <widgets/timeline_area.h>
  21. #include "time_item.h"
  22. namespace rainynite::studio {
  23. class TimelineCursor;
  24. class CursorItem : public TimeItem {
  25. public:
  26. CursorItem(TimelineCursor* parent) :
  27. p(parent)
  28. {
  29. set_readonly(false);
  30. setFlag(QGraphicsItem::ItemIsSelectable, false);
  31. }
  32. void mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) override {
  33. if (auto canvas = scene()) {
  34. canvas->clearSelection();
  35. }
  36. }
  37. protected:
  38. void position_changed(core::Time time) override;
  39. private:
  40. observer_ptr<TimelineCursor> p;
  41. };
  42. /**
  43. * Cursor for timeline area.
  44. *
  45. * Uses TimeItem (through CursorItem) for real work.
  46. * TODO: merge generic parts with TimeEditor
  47. */
  48. class TimelineCursor :
  49. public TimelineEditor,
  50. public ContextListener
  51. {
  52. public:
  53. void setup_canvas() override {
  54. time_item = make_unique<CursorItem>(this);
  55. get_scene()->addItem(time_item.get());
  56. time_item->set_pos_height(0, 1024); // "infinitely" big
  57. }
  58. void time_changed(core::Time time) override {
  59. ContextListener::time_changed(time);
  60. if (time_item && !ignore_time_change)
  61. time_item->move_to(time);
  62. }
  63. void fps_changed(core::Time::fps_type fps) override {
  64. ContextListener::fps_changed(fps);
  65. if (time_item)
  66. time_item->set_fps(fps);
  67. }
  68. void cursor_moved(core::Time time) {
  69. ignore_time_change = true;
  70. get_core_context()->set_time(time);
  71. ignore_time_change = false;
  72. }
  73. private:
  74. unique_ptr<CursorItem> time_item;
  75. bool ignore_time_change = false;
  76. };
  77. void CursorItem::position_changed(core::Time time) {
  78. p->cursor_moved(time);
  79. }
  80. REGISTER_CANVAS_EDITOR_NAME(TimelineArea, TimelineCursor, TimelineCursor);
  81. } // rainynite::studio