node_tree_dock.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * node_tree_dock.cpp - Dock with node tree
  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 <QMenu>
  19. #include <QContextMenuEvent>
  20. #include <core/document.h>
  21. #include <core/node_info.h>
  22. #include <models/node_model.h>
  23. #include "node_tree_dock.h"
  24. #include "ui_node_tree_dock.h"
  25. namespace studio {
  26. NodeTreeDock::NodeTreeDock(std::shared_ptr<core::Context> context_, QWidget* parent) :
  27. QDockWidget(parent),
  28. ContextListener(context_),
  29. ui(std::make_unique<Ui::NodeTreeDock>())
  30. {
  31. ui->setupUi(this);
  32. ui->tree_view->setSelectionMode(QAbstractItemView::SelectionMode::ExtendedSelection);
  33. connect(ui->tree_view, SIGNAL(activated(QModelIndex)), this, SLOT(activate(QModelIndex)));
  34. set_context(get_context());
  35. }
  36. NodeTreeDock::~NodeTreeDock() {
  37. }
  38. void NodeTreeDock::set_context(std::shared_ptr<core::Context> context_) {
  39. ContextListener::set_context(context_);
  40. if (auto document = context_->get_document()) {
  41. model = std::make_unique<NodeModel>(document);
  42. } else {
  43. model = std::make_unique<NodeModel>(nullptr);
  44. }
  45. ui->tree_view->setModel(model.get());
  46. }
  47. void NodeTreeDock::contextMenuEvent(QContextMenuEvent* event) {
  48. auto coord = ui->tree_view->mapFromGlobal(event->globalPos());
  49. auto index = ui->tree_view->indexAt(coord);
  50. auto parent_index = index.parent();
  51. if (auto parent_node = std::dynamic_pointer_cast<core::AbstractListLinked>(model->get_node(parent_index))) {
  52. QMenu menu(this);
  53. size_t node_index = model->get_node_index(index);
  54. auto type = parent_node->get_link_type(node_index);
  55. auto node_infos = type ? core::node_types()[*type] : core::all_node_infos();
  56. auto selection = ui->tree_view->selectionModel()->selectedIndexes();
  57. if (selection.size() > 1) {
  58. menu.addAction(
  59. QIcon::fromTheme("insert-link"),
  60. "Connect",
  61. [this, index, selection]() {
  62. model->connect_nodes(selection, index);
  63. }
  64. );
  65. }
  66. if (model->node_is_connected(index)) {
  67. menu.addAction(
  68. QIcon::fromTheme("remove-link"),
  69. "Disconnect",
  70. [this, index]() {
  71. model->disconnect_node(index);
  72. }
  73. );
  74. }
  75. if (model->can_add_element(index)) {
  76. menu.addAction(
  77. QIcon::fromTheme("list-add"),
  78. "Add element",
  79. [this, index]() {
  80. model->add_empty_element(index);
  81. }
  82. );
  83. menu.addSeparator();
  84. }
  85. if (parent_node->is_editable_list()) {
  86. menu.addAction(
  87. QIcon::fromTheme("list-remove"),
  88. "Remove",
  89. [this, node_index, parent_index]() {
  90. model->removeRow(node_index, parent_index);
  91. }
  92. );
  93. menu.addSeparator();
  94. }
  95. if (node_infos.size() == 0)
  96. menu.addAction("No node types available!");
  97. else for (auto node_info : node_infos) {
  98. auto name = QString::fromStdString(node_info->name());
  99. menu.addAction(name, [this, index, node_info]() {
  100. model->convert_node(index, node_info, get_time());
  101. });
  102. }
  103. menu.exec(event->globalPos());
  104. }
  105. }
  106. void NodeTreeDock::activate(QModelIndex const& index) {
  107. auto node = model->get_node(index);
  108. Q_EMIT activated(node);
  109. }
  110. } // namespace studio