local_task_model.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Bielefeld University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Bielefeld University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Robert Haschke */
36 
37 #include "local_task_model.h"
38 #include "factory_model.h"
41 
42 #include <ros/console.h>
43 
44 #include <QMimeData>
45 
46 using namespace moveit::task_constructor;
47 
48 namespace moveit_rviz_plugin {
49 
50 // return Node* corresponding to index
51 LocalTaskModel::Node* LocalTaskModel::node(const QModelIndex& index) const {
52  if (!index.isValid())
53  return root_;
54 
55  Q_ASSERT(index.model() == this);
56  // internal pointer refers to index' pimpl()
57  return static_cast<Node*>(index.internalPointer());
58 }
59 
60 // return QModelIndex corresponding to Node*
61 QModelIndex LocalTaskModel::index(Node* n) const {
62  if (n == root_)
63  return QModelIndex();
64 
65  const ContainerBase* parent = n->parent();
66 
67  // the internal pointer refers to n
68  size_t row = 0;
69  auto find_row = [n, &row](const Stage& child, int /* depth */) -> bool {
70  if (&child == n)
71  return false; // found, don't continue traversal
72  ++row;
73  return true;
74  };
75  parent->traverseChildren(find_row);
76  Q_ASSERT(row < parent->numChildren());
77  return createIndex(row, 0, n);
78 }
79 
80 LocalTaskModel::LocalTaskModel(ContainerBase::pointer&& container, const planning_scene::PlanningSceneConstPtr& scene,
81  rviz::DisplayContext* display_context, QObject* parent)
82  : BaseTaskModel(scene, display_context, parent), Task("", true, std::move(container)) {
83  root_ = this;
85 }
86 
87 int LocalTaskModel::rowCount(const QModelIndex& parent) const {
88  if (parent.column() > 0)
89  return 0;
90 
91  ContainerBase* c = dynamic_cast<ContainerBase*>(node(parent));
92  if (!c)
93  return 0;
94 
95  return c->numChildren();
96 }
97 
98 QModelIndex LocalTaskModel::index(int row, int column, const QModelIndex& parent) const {
99  if (column < 0 || column >= columnCount())
100  return QModelIndex();
101 
102  Q_ASSERT(dynamic_cast<ContainerBase*>(node(parent)));
103  ContainerBase* p = static_cast<ContainerBase*>(node(parent));
104  if (!p || row < 0 || static_cast<size_t>(row) >= p->numChildren())
105  return QModelIndex();
106 
107  Node* child = nullptr;
108  int idx = 0;
109  p->traverseChildren([&idx, row, &child](const Stage& ch, int /* depth */) -> bool {
110  if (idx == row) {
111  child = const_cast<Node*>(&ch);
112  return false;
113  } else {
114  ++idx;
115  return true;
116  }
117  });
118  return createIndex(row, column, child);
119 }
120 
121 QModelIndex LocalTaskModel::parent(const QModelIndex& index) const {
122  if (index.model() != this)
123  return QModelIndex();
124 
125  ContainerBase* parent = const_cast<ContainerBase*>(node(index)->parent());
126  Q_ASSERT(parent);
127 
128  return this->index(parent);
129 }
130 
131 Qt::ItemFlags LocalTaskModel::flags(const QModelIndex& index) const {
132  Qt::ItemFlags flags = BaseTaskModel::flags(index);
133  ContainerBase* c = dynamic_cast<ContainerBase*>(node(index));
134  // dropping into containers is enabled
135  if (c && stage_factory_)
136  flags |= Qt::ItemIsDropEnabled;
137  if (index.column() == 0)
138  flags |= Qt::ItemIsEditable; // name is editable
139  return flags;
140 }
141 
142 QVariant LocalTaskModel::data(const QModelIndex& index, int role) const {
143  Node* n = node(index);
144  if (!n)
145  return QVariant();
146 
147  switch (role) {
148  case Qt::EditRole:
149  case Qt::DisplayRole:
150  switch (index.column()) {
151  case 0:
152  return QString::fromStdString(n->name());
153  case 1:
154  return static_cast<uint>(n->solutions().size());
155  case 2:
156  return 0;
157  }
158  break;
159  }
160  return BaseTaskModel::data(index, role);
161 }
162 
163 bool LocalTaskModel::setData(const QModelIndex& index, const QVariant& value, int role) {
164  Node* n = node(index);
165  if (!n || index.column() != 0 || role != Qt::EditRole)
166  return false;
167 
168  // change name
169  const QString& name = value.toString();
170  if (name == n->name().c_str())
171  return false;
172  n->setName(name.toStdString());
173  dataChanged(index, index);
174  return true;
175 }
176 
177 bool LocalTaskModel::removeRows(int row, int count, const QModelIndex& parent) {
178  if (!parent.isValid())
179  return false; // cannot remove top-level container
180  if (flags_ & IS_RUNNING)
181  return false; // cannot modify running task
182 
183  Q_ASSERT(dynamic_cast<ContainerBase*>(node(parent)));
184  ContainerBase* c = static_cast<ContainerBase*>(node(parent));
185  if (row < 0 || static_cast<size_t>(row + count) > c->numChildren())
186  return false;
187 
188  beginRemoveRows(parent, row, row + count - 1);
189  for (; count > 0; --count)
190  c->remove(row);
191  endRemoveRows();
192  return true;
193 }
194 
196  stage_factory_ = factory;
197 }
198 
199 bool LocalTaskModel::dropMimeData(const QMimeData* mime, Qt::DropAction /*action*/, int row, int column,
200  const QModelIndex& parent) {
201  Q_UNUSED(column);
202 
203  if (!stage_factory_ || (flags_ & IS_RUNNING))
204  return false;
205  const QString mime_type = stage_factory_->mimeType();
206  if (!mime->hasFormat(mime_type))
207  return false;
208 
209  ContainerBase* c = dynamic_cast<ContainerBase*>(node(parent));
210  Q_ASSERT(c);
211 
212  QString error;
213  moveit::task_constructor::Stage* stage = stage_factory_->makeRaw(mime->data(mime_type), &error);
214  if (!stage)
215  return false;
216 
217  beginInsertRows(parent, row, row);
218  c->insert(moveit::task_constructor::Stage::pointer(stage), row);
219  endInsertRows();
220  return true;
221 }
222 
223 QModelIndex LocalTaskModel::indexFromStageId(size_t /*id*/) const {
224  // TODO implement
225  return QModelIndex();
226 }
227 
228 QAbstractItemModel* LocalTaskModel::getSolutionModel(const QModelIndex& /*index*/) {
229  // TODO implement
230  return nullptr;
231 }
232 
233 DisplaySolutionPtr LocalTaskModel::getSolution(const QModelIndex& /*index*/) {
234  // TODO implement
235  return DisplaySolutionPtr();
236 }
237 
239  Node* n = node(index);
240  if (!n)
241  return nullptr;
242  auto it_inserted = properties_.insert(std::make_pair(n, nullptr));
243  if (it_inserted.second) { // newly inserted, create new model
244  it_inserted.first->second =
246  it_inserted.first->second->setParent(this);
247  }
248  return it_inserted.first->second;
249 }
250 } // namespace moveit_rviz_plugin
moveit_rviz_plugin::LocalTaskModel::getSolutionModel
QAbstractItemModel * getSolutionModel(const QModelIndex &index) override
get solution model for given stage index
Definition: local_task_model.cpp:228
moveit_rviz_plugin::LocalTaskModel::stage_factory_
StageFactoryPtr stage_factory_
Definition: local_task_model.h:113
moveit::task_constructor::Stage::parent
const ContainerBase * parent() const
moveit::task_constructor::ContainerBase::traverseChildren
bool traverseChildren(const StageCallback &processor) const
rviz::PropertyTreeModel
moveit_rviz_plugin::PropertyFactory::instance
static PropertyFactory & instance()
Definition: property_factory.cpp:114
moveit_rviz_plugin::BaseTaskModel::LOCAL_MODEL
@ LOCAL_MODEL
Definition: task_list_model.h:82
moveit_rviz_plugin::LocalTaskModel::root_
Node * root_
Definition: local_task_model.h:112
moveit_rviz_plugin::LocalTaskModel::getPropertyModel
rviz::PropertyTreeModel * getPropertyModel(const QModelIndex &index) override
get property model for given stage index
Definition: local_task_model.cpp:238
moveit_rviz_plugin::LocalTaskModel::setStageFactory
void setStageFactory(const StageFactoryPtr &factory) override
providing a StageFactory makes the model accepting drops
Definition: local_task_model.cpp:195
moveit_rviz_plugin::BaseTaskModel
Definition: task_list_model.h:71
property_factory.h
factory_model.h
moveit_rviz_plugin::LocalTaskModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: local_task_model.cpp:142
moveit::task_constructor::Stage
moveit::task_constructor::Task
moveit_rviz_plugin::PropertyFactory::createPropertyTreeModel
rviz::PropertyTreeModel * createPropertyTreeModel(moveit::task_constructor::Stage &stage, const planning_scene::PlanningScene *scene, rviz::DisplayContext *display_context)
create PropertyTreeModel for given Stage
Definition: property_factory.cpp:138
console.h
property_tree_model.h
c
c
moveit_rviz_plugin::StageFactoryPtr
std::shared_ptr< StageFactory > StageFactoryPtr
Definition: task_list_model.h:66
moveit::task_constructor
moveit_rviz_plugin::LocalTaskModel::getSolution
DisplaySolutionPtr getSolution(const QModelIndex &index) override
get solution for given solution index
Definition: local_task_model.cpp:233
moveit::task_constructor::Stage::pointer
std::unique_ptr< Stage > pointer
value
float value
moveit::task_constructor::Task::name
const std::string & name() const
moveit_rviz_plugin::LocalTaskModel::properties_
std::map< Node *, rviz::PropertyTreeModel * > properties_
Definition: local_task_model.h:114
moveit_rviz_plugin::LocalTaskModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: local_task_model.cpp:199
rviz::DisplayContext
moveit_rviz_plugin::BaseTaskModel::scene_
planning_scene::PlanningSceneConstPtr scene_
Definition: task_list_model.h:76
moveit::task_constructor::ContainerBase::numChildren
size_t numChildren() const
moveit::task_constructor::ContainerBase::pointer
std::unique_ptr< ContainerBase > pointer
moveit_rviz_plugin::LocalTaskModel::indexFromStageId
QModelIndex indexFromStageId(size_t id) const override
retrieve model index associated with given stage id
Definition: local_task_model.cpp:223
moveit_rviz_plugin::LocalTaskModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent) override
Definition: local_task_model.cpp:177
moveit_rviz_plugin
moveit_rviz_plugin::LocalTaskModel::node
Node * node(const QModelIndex &index) const
Definition: local_task_model.cpp:51
moveit::task_constructor::Stage::setName
void setName(const std::string &name)
local_task_model.h
moveit::task_constructor::Stage::solutions
const ordered< SolutionBaseConstPtr > & solutions() const
moveit::task_constructor::Stage::name
const std::string & name() const
std
moveit_rviz_plugin::BaseTaskModel::display_context_
rviz::DisplayContext * display_context_
Definition: task_list_model.h:77
moveit_rviz_plugin::LocalTaskModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: local_task_model.cpp:131
moveit_rviz_plugin::BaseTaskModel::flags_
unsigned int flags_
Definition: task_list_model.h:75
index
unsigned int index
moveit_rviz_plugin::LocalTaskModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: local_task_model.cpp:87
moveit_rviz_plugin::LocalTaskModel::index
QModelIndex index(Node *n) const
Definition: local_task_model.cpp:61
moveit::task_constructor::ContainerBase
moveit_rviz_plugin::BaseTaskModel::columnCount
int columnCount(const QModelIndex &=QModelIndex()) const override
Definition: task_list_model.h:92
moveit_rviz_plugin::LocalTaskModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: local_task_model.cpp:163
moveit_rviz_plugin::BaseTaskModel::IS_RUNNING
@ IS_RUNNING
Definition: task_list_model.h:85
moveit_rviz_plugin::BaseTaskModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: task_list_model.cpp:109


visualization
Author(s): Robert Haschke
autogenerated on Thu Feb 27 2025 03:39:51