node_editor.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * node_editor.h - abstract node editor widget class
  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. #ifndef __STUDIO__NODE_EDITOR_H__8BFEEC92
  19. #define __STUDIO__NODE_EDITOR_H__8BFEEC92
  20. #include <memory>
  21. #include <QWidget>
  22. #include <core/actions/change_value.h>
  23. #include <core/node/value.h>
  24. #include "context_listener.h"
  25. namespace core {
  26. class AbstractValue;
  27. }
  28. namespace studio {
  29. class NodeEditor {
  30. public:
  31. virtual ~NodeEditor() = default;
  32. public:
  33. virtual void set_node(std::shared_ptr<core::AbstractValue> node_);
  34. inline std::shared_ptr<core::AbstractValue> get_node() {
  35. return node;
  36. }
  37. private:
  38. std::shared_ptr<core::AbstractValue> node = nullptr;
  39. };
  40. template <class W, typename T>
  41. class NodeEditorWidget : public NodeEditor, public ContextListener, public W {
  42. public:
  43. using Widget = W;
  44. using ValueType = T;
  45. public:
  46. NodeEditorWidget(QWidget* parent = nullptr) :
  47. NodeEditor(),
  48. ContextListener(),
  49. W(parent)
  50. {
  51. QObject::connect(
  52. this,
  53. &W::editingFinished,
  54. [this]() {
  55. if (auto vnode = dynamic_cast<core::Value<ValueType>*>(value_node)) {
  56. if (vnode->mod() == W::value())
  57. return;
  58. get_context()->action_stack()->template emplace<core::actions::ChangeValue>(get_node(), W::value());
  59. }
  60. }
  61. );
  62. }
  63. public:
  64. virtual void set_node(std::shared_ptr<core::AbstractValue> node_) override {
  65. if (value_node = dynamic_cast<core::BaseValue<ValueType>*>(node_.get())) {
  66. NodeEditor::set_node(node_);
  67. this->update_value(value_node->get(get_time()));
  68. this->setReadOnly(!dynamic_cast<core::Value<ValueType>*>(value_node));
  69. }
  70. }
  71. virtual void time_changed(core::Time time_) {
  72. ContextListener::time_changed(time_);
  73. if (value_node = dynamic_cast<core::BaseValue<ValueType>*>(this->get_node().get()))
  74. this->update_value(value_node->get(get_time()));
  75. }
  76. protected:
  77. core::BaseValue<ValueType>* value_node = nullptr;
  78. };
  79. } // namespace studio
  80. #endif