node_tree_dock.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <QInputDialog>
  21. #include <core/document.h>
  22. #include <core/node_info.h>
  23. #include <models/node_model.h>
  24. #include "node_tree_dock.h"
  25. #include "ui_node_tree_dock.h"
  26. namespace studio {
  27. NodeTreeDock::NodeTreeDock(std::shared_ptr<EditorContext> context_, QWidget* parent) :
  28. QDockWidget(parent),
  29. ContextListener(context_),
  30. ui(std::make_unique<Ui::NodeTreeDock>())
  31. {
  32. ui->setupUi(this);
  33. ui->tree_view->setSelectionMode(QAbstractItemView::SelectionMode::ExtendedSelection);
  34. connect(ui->tree_view, SIGNAL(activated(QModelIndex)), this, SLOT(activate(QModelIndex)));
  35. set_context(get_context());
  36. }
  37. NodeTreeDock::~NodeTreeDock() {
  38. }
  39. void NodeTreeDock::set_context(std::shared_ptr<EditorContext> context_) {
  40. ContextListener::set_context(context_);
  41. if (auto document = get_core_context()->get_document()) {
  42. model = std::make_unique<NodeModel>(document, document->get_action_stack());
  43. } else {
  44. model = std::make_unique<NodeModel>(nullptr, nullptr);
  45. }
  46. ui->tree_view->setModel(model.get());
  47. }
  48. void NodeTreeDock::contextMenuEvent(QContextMenuEvent* event) {
  49. auto coord = ui->tree_view->mapFromGlobal(event->globalPos());
  50. auto index = ui->tree_view->indexAt(coord);
  51. auto parent_index = index.parent();
  52. if (auto parent_node = std::dynamic_pointer_cast<core::AbstractListLinked>(model->get_node(parent_index))) {
  53. QMenu menu(this);
  54. size_t node_index = model->get_node_index(index);
  55. auto type = parent_node->get_link_type(node_index);
  56. auto node_infos = type ? core::node_types()[*type] : core::all_node_infos();
  57. auto selection = ui->tree_view->selectionModel()->selectedIndexes();
  58. if (selection.size() > 1) {
  59. menu.addAction(
  60. QIcon::fromTheme("insert-link"),
  61. "Connect",
  62. [this, index, selection]() {
  63. model->connect_nodes(selection, index);
  64. }
  65. );
  66. }
  67. if (selection.size() == 2) {
  68. auto a = selection[0];
  69. auto b = selection[1];
  70. menu.addAction(
  71. QIcon::fromTheme("exchange-positions"),
  72. "Swap",
  73. [this, a, b]() {
  74. model->swap_nodes(a, b);
  75. }
  76. );
  77. }
  78. if (model->node_is_connected(index)) {
  79. menu.addAction(
  80. QIcon::fromTheme("remove-link"),
  81. "Disconnect",
  82. [this, index]() {
  83. model->disconnect_node(index);
  84. }
  85. );
  86. }
  87. if (model->can_remove_node(index)) {
  88. menu.addAction(
  89. QIcon::fromTheme("list-remove"), // TODO
  90. "Remove",
  91. [this, index]() {
  92. model->remove_node(index);
  93. }
  94. );
  95. }
  96. if (model->can_add_element(index)) {
  97. menu.addAction(
  98. QIcon::fromTheme("list-add"),
  99. "Add element",
  100. [this, index]() {
  101. model->add_empty_element(index);
  102. }
  103. );
  104. menu.addSeparator();
  105. }
  106. if (model->can_add_custom_property(index)) {
  107. menu.addAction(
  108. QIcon::fromTheme("list-add"), // TODO
  109. "Add custom property",
  110. [this, index]() {
  111. auto text = QInputDialog::getText(
  112. this,
  113. "Add new custom property",
  114. "Property name:"
  115. );
  116. if (!text.isEmpty())
  117. model->add_empty_custom_property(index, text.toStdString());
  118. }
  119. );
  120. menu.addSeparator();
  121. }
  122. if (parent_node->is_editable_list()) {
  123. menu.addAction(
  124. QIcon::fromTheme("list-remove"),
  125. "Remove list item",
  126. [this, node_index, parent_index]() {
  127. model->removeRow(node_index, parent_index);
  128. }
  129. );
  130. auto up_action = menu.addAction(
  131. QIcon::fromTheme("go-up"),
  132. "Move up",
  133. [this, node_index, parent_index]() {
  134. model->move_up(node_index, parent_index);
  135. }
  136. );
  137. auto down_action = menu.addAction(
  138. QIcon::fromTheme("go-down"),
  139. "Move down",
  140. [this, node_index, parent_index]() {
  141. model->move_down(node_index, parent_index);
  142. }
  143. );
  144. up_action->setEnabled(model->can_move_up(node_index, parent_index));
  145. down_action->setEnabled(model->can_move_down(node_index, parent_index));
  146. menu.addSeparator();
  147. }
  148. if (node_infos.size() == 0)
  149. menu.addAction("No node types available!");
  150. else for (auto node_info : node_infos) {
  151. auto name = QString::fromStdString(node_info->name());
  152. menu.addAction(name, [this, index, node_info]() {
  153. model->convert_node(index, node_info, get_time());
  154. });
  155. }
  156. menu.exec(event->globalPos());
  157. }
  158. }
  159. void NodeTreeDock::activate(QModelIndex const& index) {
  160. auto node = model->get_node(index);
  161. Q_EMIT activated(node);
  162. }
  163. } // namespace studio