node_editor.h 2.2 KB

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