node_model.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * node_model.h - node tree model wrapper
  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. #ifndef __STUDIO__NODE_MODEL_H_E924C400
  19. #define __STUDIO__NODE_MODEL_H_E924C400
  20. #include <QAbstractItemModel>
  21. #include <core/node_info.h>
  22. namespace studio {
  23. class NodeModel : public QAbstractItemModel
  24. {
  25. Q_OBJECT
  26. public:
  27. explicit NodeModel(core::AbstractReference root, QObject* parent = 0);
  28. virtual ~NodeModel();
  29. QVariant data(QModelIndex const& index, int role) const override;
  30. Qt::ItemFlags flags(QModelIndex const& index) const override;
  31. QVariant headerData(int section, Qt::Orientation orientation,
  32. int role = Qt::DisplayRole) const override;
  33. QModelIndex index(int row, int column,
  34. QModelIndex const& parent = QModelIndex()) const override;
  35. QModelIndex parent(QModelIndex const& index) const override;
  36. int rowCount(QModelIndex const& parent = QModelIndex()) const override;
  37. int columnCount(QModelIndex const& parent = QModelIndex()) const override;
  38. public:
  39. virtual bool removeRows(int row, int count, QModelIndex const& parent = QModelIndex()) override;
  40. public:
  41. bool can_add_element(QModelIndex const& parent) const;
  42. void add_empty_element(QModelIndex const& parent);
  43. void convert_node(QModelIndex const& index, core::NodeInfo const* node_info, core::Time time);
  44. bool node_is_connected(QModelIndex const& index) const;
  45. void disconnect_node(QModelIndex const& index);
  46. void connect_nodes(QList<QModelIndex> const& selection, QModelIndex const& source);
  47. void replace_node(QModelIndex const& index, core::AbstractReference node);
  48. public:
  49. template <typename T, typename F>
  50. T find_nodes(core::AbstractReference node_to_find, F f) const {
  51. // TODO: perfect forward f
  52. return core::traverse_once<T>(
  53. root,
  54. [this, &f, &node_to_find](auto const& node) -> boost::optional<T> {
  55. if (node == node_to_find)
  56. return f();
  57. return boost::none;
  58. },
  59. core::TraverseDepth::Deeper
  60. );
  61. }
  62. public:
  63. std::shared_ptr<core::AbstractValue> get_node(QModelIndex const& index) const;
  64. template <class T>
  65. std::shared_ptr<T> get_node_as(QModelIndex const& index) const {
  66. return std::dynamic_pointer_cast<T>(get_node(index));
  67. }
  68. inline std::shared_ptr<core::AbstractListLinked> get_list_node(QModelIndex const& index) const {
  69. return get_node_as<core::AbstractListLinked>(index);
  70. }
  71. size_t get_node_index(QModelIndex const& index) const;
  72. private:
  73. quintptr get_id(QModelIndex const& parent, size_t i) const;
  74. private:
  75. core::AbstractReference root;
  76. private:
  77. mutable std::map<std::pair<QModelIndex, size_t>, quintptr> indexes;
  78. mutable std::map<quintptr, QModelIndex> parents;
  79. mutable quintptr index_count = 0;
  80. };
  81. } // namespace studio
  82. #endif