node_list.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * node_list.cpp - node list model
  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 <core/node_info.h>
  19. #include <util/strings.h>
  20. #include "node_list.h"
  21. namespace rainynite::studio {
  22. NodeListModel::NodeListModel(QObject* parent) :
  23. QAbstractItemModel(parent)
  24. {
  25. }
  26. NodeListModel::~NodeListModel() {
  27. }
  28. QVariant NodeListModel::data(QModelIndex const& index, int role) const {
  29. if (!index.isValid() || index.column() != 0 || index.parent().isValid())
  30. return {};
  31. switch (role) {
  32. case Qt::DisplayRole: {
  33. return util::str(core::node_name(*get_node(index)));
  34. } break;
  35. default:
  36. return {};
  37. }
  38. }
  39. Qt::ItemFlags NodeListModel::flags(QModelIndex const& /*index*/) const {
  40. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  41. }
  42. QVariant NodeListModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role*/) const {
  43. return {};
  44. }
  45. QModelIndex NodeListModel::index(int row, int column, QModelIndex const& parent) const {
  46. if (parent.isValid() || column != 0)
  47. return {};
  48. return createIndex(row, column, nullptr);
  49. }
  50. QModelIndex NodeListModel::parent(QModelIndex const& /*index*/) const {
  51. return {};
  52. }
  53. int NodeListModel::rowCount(QModelIndex const& parent) const {
  54. if (parent.isValid())
  55. return 0;
  56. return nodes.size();
  57. }
  58. int NodeListModel::columnCount(QModelIndex const& /*parent*/) const {
  59. return 1;
  60. }
  61. bool NodeListModel::removeRows(int row, int count, QModelIndex const& parent) {
  62. if (parent.isValid())
  63. return false;
  64. beginRemoveRows(parent, row, row+count-1);
  65. nodes.erase(nodes.begin()+row, nodes.begin()+row+count);
  66. endRemoveRows();
  67. return true;
  68. }
  69. void NodeListModel::insert_node(shared_ptr<core::AbstractValue> node, int position) {
  70. if (position < 0)
  71. position = nodes.size();
  72. beginInsertRows({}, position, position+1);
  73. nodes.insert(nodes.begin()+position, node);
  74. endInsertRows();
  75. }
  76. bool NodeListModel::insert_unique_node(shared_ptr<core::AbstractValue> node, int position) {
  77. if (std::find(nodes.begin(), nodes.end(), node) == nodes.end()) {
  78. insert_node(node, position);
  79. return true;
  80. }
  81. return false;
  82. }
  83. shared_ptr<core::AbstractValue> NodeListModel::get_node(QModelIndex const& index) const {
  84. return nodes[index.row()];
  85. }
  86. } // namespace rainynite::studio