node_edit.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* docks/node_edit.cpp - simple text editing of selecting node
  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 <fmt/format.h>
  18. #include <core/document.h>
  19. #include <core/util/type_info.h>
  20. #include <core/serialize/node_writer.h>
  21. #include <util/strings.h>
  22. #include <generic/custom_widgets.h>
  23. #include <generic/node_editor.h>
  24. #include "node_edit.h"
  25. #include "ui_node_edit.h"
  26. using namespace fmt::literals;
  27. namespace rainynite::studio {
  28. NodeEditDock::NodeEditDock(shared_ptr<EditorContext> context_, QWidget* parent) :
  29. DockWidget(parent),
  30. ui(make_unique<Ui::NodeEditDock>())
  31. {
  32. ui->setupUi(this);
  33. custom_widget.reset(ui->custom_placeholder);
  34. ui->text_edit->setReadOnly(true);
  35. set_context(context_);
  36. connect(
  37. ui->enable_autoupdate,
  38. &QAbstractButton::toggled,
  39. [this](bool value) {
  40. ui->text_edit->set_update_enabled(value);
  41. if (auto node_editor = dynamic_cast<NodeEditor*>(custom_widget.get())) {
  42. node_editor->set_update_enabled(value);
  43. }
  44. }
  45. );
  46. }
  47. NodeEditDock::~NodeEditDock() {
  48. }
  49. void NodeEditDock::active_node_index_changed(core::NodeTreeIndex index) {
  50. set_node(index);
  51. active_node = index;
  52. if (!index)
  53. return;
  54. update_generic();
  55. setup_custom_widget(get_node());
  56. }
  57. void NodeEditDock::time_changed(core::Time time) {
  58. ContextListener::time_changed(time);
  59. if (ui->enable_autoupdate->isChecked())
  60. update_value();
  61. }
  62. void NodeEditDock::update_value() {
  63. update_generic();
  64. update_custom();
  65. }
  66. void NodeEditDock::setup_custom_widget(shared_ptr<core::AbstractValue> node) {
  67. if (!node)
  68. return;
  69. auto widget = new_custom_widget(node->get_type());
  70. if (!widget) {
  71. widget = make_unique<QWidget>();
  72. }
  73. ui->content->layout()->replaceWidget(custom_widget.get(), widget.get());
  74. custom_widget = std::move(widget);
  75. update_custom();
  76. }
  77. void NodeEditDock::update_generic() {
  78. ui->text_edit->set_node(active_node);
  79. ui->text_edit->set_context(get_context());
  80. }
  81. void NodeEditDock::update_custom() {
  82. if (auto listener = dynamic_cast<ContextListener*>(custom_widget.get())) {
  83. listener->set_context(get_context());
  84. }
  85. if (auto node_editor = dynamic_cast<NodeEditor*>(custom_widget.get())) {
  86. node_editor->set_node(active_node);
  87. }
  88. }
  89. } // namespace rainynite::studio