node_edit_dock.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * node_edit_dock.cpp - simple text editing of selecting node
  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 <QDebug>
  19. #include <fmt/format.h>
  20. #include <core/document.h>
  21. #include <core/types.h>
  22. #include <core/serialize/node_writer.h>
  23. #include <util/strings.h>
  24. #include <generic/custom_widgets.h>
  25. #include <generic/node_editor.h>
  26. #include "node_edit_dock.h"
  27. #include "ui_node_edit_dock.h"
  28. using namespace fmt::literals;
  29. namespace rainynite::studio {
  30. NodeEditDock::NodeEditDock(shared_ptr<EditorContext> context_, QWidget* parent) :
  31. DockWidget(parent),
  32. ui(make_unique<Ui::NodeEditDock>())
  33. {
  34. ui->setupUi(this);
  35. custom_widget = ui->custom_placeholder;
  36. ui->text_edit->setReadOnly(true);
  37. connect(ui->text_edit, SIGNAL(editingFinished()), this, SLOT(write_node()));
  38. set_context(context_);
  39. }
  40. NodeEditDock::~NodeEditDock() {
  41. }
  42. void NodeEditDock::active_node_changed(shared_ptr<core::AbstractValue> node) {
  43. set_node(node);
  44. active_node = node;
  45. if (!node)
  46. return;
  47. update_generic();
  48. setup_custom_widget(node);
  49. }
  50. void NodeEditDock::time_changed(core::Time time) {
  51. ContextListener::time_changed(time);
  52. update_value();
  53. }
  54. void NodeEditDock::node_update() {
  55. // custom should be capable of updating itself..
  56. update_generic();
  57. }
  58. void NodeEditDock::update_value() {
  59. update_generic();
  60. update_custom();
  61. }
  62. void NodeEditDock::update_generic() {
  63. if (!active_node)
  64. return;
  65. bool writeable = active_node->is_const();
  66. boost::any value;
  67. try {
  68. if (writeable) {
  69. value = active_node->static_any();
  70. } else {
  71. value = active_node->get_any(get_core_context());
  72. }
  73. auto s = core::serialize::value_to_string(value);
  74. ui->text_edit->setText(util::str(s));
  75. } catch (class_init::RuntimeTypeError const& ex) {
  76. auto s = "<Type Exception: {}>"_format(ex.what());
  77. ui->text_edit->setText(util::str(s));
  78. writeable = false;
  79. } catch (std::exception const& ex) {
  80. auto s = "<Uncaught Exception: {}>"_format(ex.what());
  81. ui->text_edit->setText(util::str(s));
  82. writeable = false;
  83. }
  84. ui->text_edit->setReadOnly(!writeable);
  85. }
  86. void NodeEditDock::setup_custom_widget(shared_ptr<core::AbstractValue> node) {
  87. QWidget* widget = new_custom_widget(node->get_type());
  88. if (!widget) {
  89. widget = new QWidget();
  90. }
  91. ui->content->layout()->replaceWidget(custom_widget, widget);
  92. delete custom_widget;
  93. custom_widget = widget;
  94. update_custom();
  95. }
  96. void NodeEditDock::update_custom() {
  97. if (auto node_editor = dynamic_cast<NodeEditor*>(custom_widget)) {
  98. node_editor->set_node(active_node);
  99. }
  100. if (auto listener = dynamic_cast<ContextListener*>(custom_widget)) {
  101. listener->set_context(get_context());
  102. }
  103. }
  104. void NodeEditDock::write_node() {
  105. if (!active_node) {
  106. // this shouldn't really happen
  107. return;
  108. }
  109. if (ui->text_edit->isReadOnly())
  110. return;
  111. auto text = ui->text_edit->text().toStdString();
  112. try {
  113. active_node->set_any(core::parse_primitive_type(active_node->get_type(), text));
  114. } catch (std::exception const& ex) {
  115. qDebug() << "Exception while parsing input:" << ex.what();
  116. active_node_changed(active_node);
  117. }
  118. }
  119. } // namespace rainynite::studio