node_edit_dock.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <generic/custom_widgets.h>
  24. #include <generic/node_editor.h>
  25. #include "node_edit_dock.h"
  26. #include "ui_node_edit_dock.h"
  27. using namespace fmt::literals;
  28. namespace studio {
  29. NodeEditDock::NodeEditDock(std::shared_ptr<EditorContext> context_, QWidget* parent) :
  30. QDockWidget(parent),
  31. ContextListener(context_),
  32. ui(std::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. }
  39. NodeEditDock::~NodeEditDock() {
  40. }
  41. void NodeEditDock::active_node_changed(std::shared_ptr<core::AbstractValue> node) {
  42. active_node = node;
  43. if (!node)
  44. return;
  45. update_generic();
  46. setup_custom_widget(node);
  47. }
  48. void NodeEditDock::time_changed(core::Time time) {
  49. ContextListener::time_changed(time);
  50. update_value();
  51. }
  52. void NodeEditDock::update_value() {
  53. update_generic();
  54. update_custom();
  55. }
  56. void NodeEditDock::update_generic() {
  57. if (!active_node)
  58. return;
  59. bool writeable = active_node->is_const();
  60. boost::any value;
  61. try {
  62. if (writeable) {
  63. value = active_node->any();
  64. } else {
  65. value = active_node->get_any(get_core_context()->get_time());
  66. }
  67. auto s = core::serialize::value_to_string(value);
  68. ui->text_edit->setText(QString::fromStdString(s));
  69. } catch (class_init::RuntimeTypeError const& ex) {
  70. auto s = "<Type Exception: {}>"_format(ex.what());
  71. ui->text_edit->setText(QString::fromStdString(s));
  72. writeable = false;
  73. } catch (std::exception const& ex) {
  74. auto s = "<Uncaught Exception: {}>"_format(ex.what());
  75. ui->text_edit->setText(QString::fromStdString(s));
  76. writeable = false;
  77. }
  78. ui->text_edit->setReadOnly(!writeable);
  79. }
  80. void NodeEditDock::setup_custom_widget(std::shared_ptr<core::AbstractValue> node) {
  81. QWidget* widget = new_custom_widget(node->get_type());
  82. if (!widget) {
  83. widget = new QWidget();
  84. }
  85. ui->content->layout()->replaceWidget(custom_widget, widget);
  86. delete custom_widget;
  87. custom_widget = widget;
  88. update_custom();
  89. }
  90. void NodeEditDock::update_custom() {
  91. if (auto node_editor = dynamic_cast<NodeEditor*>(custom_widget)) {
  92. node_editor->set_node(active_node);
  93. }
  94. if (auto listener = dynamic_cast<ContextListener*>(custom_widget)) {
  95. listener->set_context(get_context());
  96. }
  97. }
  98. void NodeEditDock::closeEvent(QCloseEvent* event) {
  99. QDockWidget::closeEvent(event);
  100. deleteLater();
  101. }
  102. void NodeEditDock::write_node() {
  103. if (!active_node) {
  104. // this shouldn't really happen
  105. return;
  106. }
  107. if (ui->text_edit->isReadOnly())
  108. return;
  109. auto text = ui->text_edit->text().toStdString();
  110. try {
  111. active_node->set_any(core::parse_primitive_type(active_node->get_type(), text));
  112. } catch (std::exception const& ex) {
  113. qDebug() << "Exception while parsing input:" << QString::fromStdString(ex.what());
  114. active_node_changed(active_node);
  115. }
  116. }
  117. } // namespace studio