node_edit_dock.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <boost/uuid/uuid_io.hpp>
  21. #include <core/document.h>
  22. #include <core/types.h>
  23. #include <core/serialize/node_writer.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 studio {
  30. NodeEditDock::NodeEditDock(std::shared_ptr<core::Context> context_, QWidget* parent) :
  31. QDockWidget(parent),
  32. ContextListener(context_),
  33. ui(std::make_unique<Ui::NodeEditDock>())
  34. {
  35. ui->setupUi(this);
  36. custom_widget = ui->custom_placeholder;
  37. ui->text_edit->setReadOnly(true);
  38. connect(ui->text_edit, SIGNAL(editingFinished()), this, SLOT(write_node()));
  39. set_context(get_context());
  40. }
  41. void NodeEditDock::active_node_changed(std::shared_ptr<core::AbstractValue> node) {
  42. active_node = node;
  43. if (!node) {
  44. ui->uid_label->setText("<no node>");
  45. return;
  46. }
  47. ui->uid_label->setText(QString::fromStdString(to_string(node->get_id())));
  48. bool writeable = node->is_const();
  49. boost::any value;
  50. try {
  51. if (writeable) {
  52. value = node->any();
  53. } else {
  54. value = node->get_any(get_context()->get_time());
  55. }
  56. auto s = core::serialize::value_to_string(value);
  57. ui->text_edit->setText(QString::fromStdString(s));
  58. } catch (class_init::RuntimeTypeError const& ex) {
  59. auto s = "<Type Exception: {}>"_format(ex.what());
  60. ui->text_edit->setText(QString::fromStdString(s));
  61. writeable = false;
  62. } catch (std::exception const& ex) {
  63. auto s = "<Uncaught Exception: {}>"_format(ex.what());
  64. ui->text_edit->setText(QString::fromStdString(s));
  65. writeable = false;
  66. }
  67. ui->text_edit->setReadOnly(!writeable);
  68. setup_custom_widget(node);
  69. }
  70. void NodeEditDock::setup_custom_widget(std::shared_ptr<core::AbstractValue> node) {
  71. QWidget* widget = new_custom_widget(node->get_type());
  72. if (!widget) {
  73. widget = new QWidget();
  74. }
  75. ui->content->layout()->replaceWidget(custom_widget, widget);
  76. delete custom_widget;
  77. custom_widget = widget;
  78. if (auto node_editor = dynamic_cast<NodeEditor*>(widget)) {
  79. node_editor->set_node(node);
  80. }
  81. }
  82. NodeEditDock::~NodeEditDock() {
  83. }
  84. void NodeEditDock::closeEvent(QCloseEvent* event) {
  85. QDockWidget::closeEvent(event);
  86. deleteLater();
  87. }
  88. void NodeEditDock::write_node() {
  89. if (!active_node) {
  90. // this shouldn't really happen
  91. return;
  92. }
  93. if (ui->text_edit->isReadOnly())
  94. return;
  95. qDebug() << ui->text_edit->text();
  96. auto text = ui->text_edit->text().toStdString();
  97. active_node->set_any(core::parse_primitive_type(active_node->get_type(), text));
  98. }
  99. } // namespace studio