playback_dock.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * playback_dock.cpp - playback controls dock
  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 "playback_dock.h"
  19. #include "ui_playback_dock.h"
  20. namespace studio {
  21. PlaybackDock::PlaybackDock(std::shared_ptr<EditorContext> context_, QWidget* parent) :
  22. QDockWidget(parent),
  23. ContextListener(context_),
  24. ui(std::make_unique<Ui::PlaybackDock>()),
  25. timer(new QTimer(this))
  26. {
  27. ui->setupUi(this);
  28. connect(ui->move_start_button, SIGNAL(clicked()), this, SLOT(move_start()));
  29. connect(ui->move_end_button, SIGNAL(clicked()), this, SLOT(move_end()));
  30. connect(ui->play_button, SIGNAL(toggled(bool)), this, SLOT(toggle_playback(bool)));
  31. connect(ui->stop_button, SIGNAL(clicked()), this, SLOT(stop()));
  32. connect(ui->timeline_zoom, SIGNAL(valueChanged(int)), ui->timeline, SLOT(set_zoom_level(int)));
  33. // TODO: move actual playing out of dock
  34. connect(timer, SIGNAL(timeout()), this, SLOT(next_frame()));
  35. set_context(get_context());
  36. }
  37. PlaybackDock::~PlaybackDock() {
  38. }
  39. void PlaybackDock::set_context(std::shared_ptr<EditorContext> context_) {
  40. ContextListener::set_context(context_);
  41. ui->timeline->set_context(context_);
  42. }
  43. void PlaybackDock::closeEvent(QCloseEvent* event) {
  44. QDockWidget::closeEvent(event);
  45. deleteLater();
  46. }
  47. void PlaybackDock::toggle_playback(bool play) {
  48. if (auto context = get_core_context()) {
  49. if (play)
  50. timer->start(1000/context->get_fps());
  51. else
  52. timer->stop();
  53. }
  54. }
  55. void PlaybackDock::stop() {
  56. ui->play_button->setChecked(false);
  57. move_start();
  58. }
  59. void PlaybackDock::next_frame() {
  60. if (auto context = get_core_context()) {
  61. auto time = context->get_time();
  62. if (time == context->get_period().get_last())
  63. ui->play_button->setChecked(false);
  64. else
  65. context->set_time(time + core::Time(0, context->get_fps(), 1));
  66. }
  67. }
  68. void PlaybackDock::move_start() {
  69. if (auto context = get_core_context()) {
  70. context->to_start();
  71. }
  72. }
  73. void PlaybackDock::move_end() {
  74. if (auto context = get_core_context()) {
  75. context->to_end();
  76. }
  77. }
  78. }