node_editor.h 2.5 KB

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