node_tree_dock.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <algorithm>
  19. #include <QMenu>
  20. #include <QContextMenuEvent>
  21. #include <QInputDialog>
  22. #include <core/document.h>
  23. #include <core/node_info.h>
  24. #include <util/strings.h>
  25. #include <models/node_model.h>
  26. #include <generic/node_menu.h>
  27. #include "node_tree_dock.h"
  28. #include "ui_node_tree_dock.h"
  29. namespace rainynite::studio {
  30. NodeTreeDock::NodeTreeDock(shared_ptr<EditorContext> context_, QWidget* parent) :
  31. DockWidget(parent),
  32. ContextListener(context_),
  33. ui(make_unique<Ui::NodeTreeDock>())
  34. {
  35. ui->setupUi(this);
  36. ui->tree_view->setSelectionMode(QAbstractItemView::SelectionMode::ExtendedSelection);
  37. connect(ui->tree_view, SIGNAL(activated(QModelIndex)), this, SLOT(activate(QModelIndex)));
  38. set_context(get_context());
  39. }
  40. NodeTreeDock::~NodeTreeDock() {
  41. }
  42. void NodeTreeDock::set_context(shared_ptr<EditorContext> context_) {
  43. ContextListener::set_context(context_);
  44. if (auto document = get_core_context()->get_document()) {
  45. model = make_unique<NodeModel>(document, document->get_action_stack());
  46. } else {
  47. model = make_unique<NodeModel>(nullptr, nullptr);
  48. }
  49. model->set_context(context_);
  50. ui->tree_view->setModel(model.get());
  51. }
  52. void NodeTreeDock::contextMenuEvent(QContextMenuEvent* event) {
  53. auto selection_model = ui->tree_view->selectionModel();
  54. if (menu = node_context_menu(model.get(), selection_model, get_time())) {
  55. menu->exec(event->globalPos());
  56. }
  57. }
  58. void NodeTreeDock::activate(QModelIndex const& index) {
  59. auto node = model->get_node(index);
  60. Q_EMIT activated(node);
  61. }
  62. } // namespace rainynite::studio