collision_linear_model.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016, CITEC, 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 Willow Garage 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 "collision_linear_model.h"
38 #include "collision_matrix_model.h"
39 
40 #include <QItemSelection>
41 #include <QPainter>
42 
43 using namespace moveit_setup_assistant;
44 
45 CollisionLinearModel::CollisionLinearModel(CollisionMatrixModel* src, QObject* parent) : QAbstractProxyModel(parent)
46 {
47  setSourceModel(src);
48 }
50 {
51  delete sourceModel();
52 }
53 
54 QModelIndex CollisionLinearModel::mapFromSource(const QModelIndex& sourceIndex) const
55 {
56  // map (row,column) index to linear index k
57  // http://stackoverflow.com/questions/27086195/linear-index-upper-triangular-matrix
58  int r = sourceIndex.row(), c = sourceIndex.column();
59  int n = this->sourceModel()->columnCount();
60  if (r == c)
61  return QModelIndex(); // main diagonal elements are invalid
62  if (r > c) // only consider upper triagonal matrix
63  std::swap(r, c); // swap r,c if below diagonal
64 
65  int k = (n * (n - 1) / 2) - (n - r) * ((n - r) - 1) / 2 + c - r - 1;
66  return index(k, 2);
67 }
68 
69 QModelIndex CollisionLinearModel::mapToSource(const QModelIndex& proxyIndex) const
70 {
71  // map linear index k to (row, column)
72  // http://stackoverflow.com/questions/27086195/linear-index-upper-triangular-matrix
73  int n = sourceModel()->columnCount();
74  int k = proxyIndex.row(); // linear (row) index
75  int r = n - 2 - (int)(sqrt(-8 * k + 4 * n * (n - 1) - 7) / 2.0 - 0.5);
76  int c = k + r + 1 - n * (n - 1) / 2 + (n - r) * ((n - r) - 1) / 2;
77  return sourceModel()->index(r, c);
78 }
79 
80 int CollisionLinearModel::rowCount(const QModelIndex& parent) const
81 {
82  int n = this->sourceModel()->rowCount();
83  return (n * (n - 1) / 2);
84 }
85 
86 int CollisionLinearModel::columnCount(const QModelIndex& parent) const
87 {
88  return 4;
89 }
90 
91 QModelIndex CollisionLinearModel::index(int row, int column, const QModelIndex& parent) const
92 {
93  return createIndex(row, column);
94 }
95 
96 QModelIndex CollisionLinearModel::parent(const QModelIndex& child) const
97 {
98  return QModelIndex();
99 }
100 
101 QVariant CollisionLinearModel::data(const QModelIndex& index, int role) const
102 {
103  QModelIndex srcIndex = this->mapToSource(index);
104  switch (index.column())
105  {
106  case 0: // link name 1
107  if (role != Qt::DisplayRole)
108  return QVariant();
109  else
110  return this->sourceModel()->headerData(srcIndex.row(), Qt::Horizontal, Qt::DisplayRole);
111  case 1: // link name 2
112  if (role != Qt::DisplayRole)
113  return QVariant();
114  return this->sourceModel()->headerData(srcIndex.column(), Qt::Vertical, Qt::DisplayRole);
115  case 2: // checkbox
116  if (role != Qt::CheckStateRole)
117  return QVariant();
118  else
119  return this->sourceModel()->data(srcIndex, Qt::CheckStateRole);
120  case 3: // reason
121  if (role != Qt::DisplayRole)
122  return QVariant();
123  else
124  return this->sourceModel()->data(srcIndex, Qt::ToolTipRole);
125  }
126  return QVariant();
127 }
128 
130 {
131  QModelIndex srcIndex = this->mapToSource(index(row, 0));
132  return qobject_cast<CollisionMatrixModel*>(sourceModel())->reason(srcIndex);
133 }
134 
135 bool CollisionLinearModel::setData(const QModelIndex& index, const QVariant& value, int role)
136 {
137  QModelIndex srcIndex = this->mapToSource(index);
138 
139  if (role == Qt::CheckStateRole)
140  {
141  sourceModel()->setData(srcIndex, value, role);
142  int r = index.row();
143  Q_EMIT dataChanged(this->index(r, 2), this->index(r, 3)); // reason changed too
144  return true;
145  }
146  return false; // reject all other changes
147 }
148 
149 void CollisionLinearModel::setEnabled(const QItemSelection& selection, bool value)
150 {
151  for (const auto idx : selection.indexes())
152  {
153  if (idx.column() != 2) // only consider checkbox indexes
154  continue;
155  setData(idx, value ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
156  }
157 }
158 
159 Qt::ItemFlags CollisionLinearModel::flags(const QModelIndex& index) const
160 {
161  if (index.column() == 2)
162  return Qt::ItemIsUserCheckable | QAbstractItemModel::flags(index);
163  else
164  return QAbstractItemModel::flags(index);
165 }
166 
167 QVariant CollisionLinearModel::headerData(int section, Qt::Orientation orientation, int role) const
168 {
169  if (role != Qt::DisplayRole)
170  return QVariant();
171 
172  if (orientation == Qt::Horizontal)
173  {
174  switch (section)
175  {
176  case 0:
177  return "Link A";
178  case 1:
179  return "Link B";
180  case 2:
181  return "Disabled";
182  case 3:
183  return "Reason to Disable";
184  }
185  }
186  else if (orientation == Qt::Vertical)
187  {
188  return section + 1;
189  }
190  return QVariant();
191 }
192 
193 SortFilterProxyModel::SortFilterProxyModel(QObject* parent) : QSortFilterProxyModel(parent), show_all_(false)
194 {
195 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
196  connect(this, SIGNAL(sourceModelChanged()), this, SLOT(initSorting()));
197 #endif
198 
199  // by default: sort by link A (col 0), then link B (col 1)
200  sort_columns_ << 0 << 1;
201  sort_orders_ << Qt::AscendingOrder << Qt::AscendingOrder;
202 }
203 
204 QVariant SortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
205 {
206  if (role == Qt::DisplayRole && orientation == Qt::Vertical)
207  return section + 1; // simply enumerate rows
208  else
209  return QSortFilterProxyModel::headerData(section, orientation, role);
210 }
211 
212 void SortFilterProxyModel::setEnabled(const QItemSelection& selection, bool value)
213 {
214  static_cast<CollisionLinearModel*>(sourceModel())->setEnabled(mapSelectionToSource(selection), value);
215 }
216 
218 {
219  int cols = sourceModel()->columnCount();
220  int prev_size = sort_columns_.size();
221  sort_columns_.resize(cols);
222  sort_orders_.resize(cols);
223 
224  // initialize new entries to -1
225  for (int i = prev_size, end = sort_columns_.size(); i < end; ++i)
226  sort_columns_[i] = -1;
227 }
228 
230 {
231  if (show_all_ == show_all)
232  return;
233 
234  show_all_ = show_all;
235  invalidateFilter();
236 }
237 
238 bool SortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
239 {
240  CollisionLinearModel* m = qobject_cast<CollisionLinearModel*>(sourceModel());
241  if (!(show_all_ || m->reason(source_row) <= moveit_setup_assistant::ALWAYS ||
242  m->data(m->index(source_row, 2), Qt::CheckStateRole) == Qt::Checked))
243  return false; // not accepted due to check state
244 
245  const QRegExp regexp = this->filterRegExp();
246  if (regexp.isEmpty())
247  return true;
248 
249  return m->data(m->index(source_row, 0, source_parent), Qt::DisplayRole).toString().contains(regexp) ||
250  m->data(m->index(source_row, 1, source_parent), Qt::DisplayRole).toString().contains(regexp);
251 }
252 
253 // define a fallback comparison operator for QVariants
254 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
255 namespace
256 {
257 bool operator<(const QVariant& left, const QVariant& right)
258 {
259  if (left.userType() == QVariant::Type::Int)
260  return left.toInt() < right.toInt();
261  else
262  return left.toString() < right.toString();
263 }
264 }
265 #endif
266 
267 bool SortFilterProxyModel::lessThan(const QModelIndex& src_left, const QModelIndex& src_right) const
268 {
269  int row_left = src_left.row();
270  int row_right = src_right.row();
271  QAbstractItemModel* m = sourceModel();
272 
273  for (int i = 0, end = sort_columns_.size(); i < end && sort_columns_[i] >= 0; ++i)
274  {
275  int sc = sort_columns_[i];
276  int role = sc == 2 ? Qt::CheckStateRole : Qt::DisplayRole;
277  QVariant value_left = m->data(m->index(row_left, sc), role);
278  QVariant value_right = m->data(m->index(row_right, sc), role);
279 
280  if (value_left == value_right)
281  continue;
282 
283  bool smaller = (value_left < value_right);
284  if (sort_orders_[i] == Qt::DescendingOrder)
285  smaller = !smaller;
286  return smaller;
287  }
288  return false;
289 }
290 
291 void SortFilterProxyModel::sort(int column, Qt::SortOrder order)
292 {
293  beginResetModel();
294  if (column < 0)
295  initSorting();
296  else
297  {
298  // remember sorting history
299  int prev_idx = sort_columns_.indexOf(column);
300  if (prev_idx < 0)
301  prev_idx = sort_columns_.size() - 1;
302  // remove old entries
303  sort_columns_.remove(prev_idx);
304  sort_orders_.remove(prev_idx);
305  // add new entries at front
306  sort_columns_.insert(0, column);
307  sort_orders_.insert(0, order);
308  }
309  QSortFilterProxyModel::sort(column, Qt::AscendingOrder);
310  endResetModel();
311 }
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
CollisionLinearModel(CollisionMatrixModel *src, QObject *parent=NULL)
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
SortFilterProxyModel(QObject *parent=0)
QVariant headerData(int section, Qt::Orientation orientation, int role) const
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
moveit_setup_assistant::DisabledReason reason(int row) const
void setEnabled(const QItemSelection &selection, bool value)
QModelIndex parent(const QModelIndex &child) const
int columnCount(const QModelIndex &parent) const
QVariant data(const QModelIndex &index, int role) const
r
std::string toString(double d)
void sort(int column, Qt::SortOrder order)
DisabledReason
Reasons for disabling link pairs. Append "in collision" for understanding. NOT_DISABLED means the lin...
void setShowAll(bool show_all)
int rowCount(const QModelIndex &parent) const
int cols
bool lessThan(const QModelIndex &src_left, const QModelIndex &src_right) const
void setEnabled(const QItemSelection &selection, bool value)
bool setData(const QModelIndex &index, const QVariant &value, int role)
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Qt::ItemFlags flags(const QModelIndex &index) const


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Sun Oct 18 2020 13:19:28