Skip to content

Commit a86bb42

Browse files
authored
Merge pull request moveit#146 from v4hn/pr-master-rework-panel
rework RViz panel
2 parents 25fea71 + 9dc43e4 commit a86bb42

File tree

9 files changed

+150
-137
lines changed

9 files changed

+150
-137
lines changed

visualization/motion_planning_tasks/src/local_task_model.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Qt::ItemFlags LocalTaskModel::flags(const QModelIndex& index) const {
134134
// dropping into containers is enabled
135135
if (c && stage_factory_)
136136
flags |= Qt::ItemIsDropEnabled;
137+
if (index.column() == 0)
138+
flags |= Qt::ItemIsEditable; // name is editable
137139
return flags;
138140
}
139141

visualization/motion_planning_tasks/src/remote_task_model.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,6 @@ QModelIndex RemoteTaskModel::parent(const QModelIndex& child) const {
232232
return this->index(p);
233233
}
234234

235-
Qt::ItemFlags RemoteTaskModel::flags(const QModelIndex& index) const {
236-
Qt::ItemFlags flags = BaseTaskModel::flags(index);
237-
if (index.column() == 0)
238-
flags |= Qt::ItemIsEditable; // name is editable
239-
return flags;
240-
}
241-
242235
QVariant RemoteTaskModel::data(const QModelIndex& index, int role) const {
243236
Node* n = node(index);
244237
if (!n)
@@ -495,7 +488,7 @@ QVariant RemoteSolutionModel::headerData(int section, Qt::Orientation orientatio
495488
return tr("comment");
496489
}
497490
case Qt::TextAlignmentRole:
498-
return section == 2 ? Qt::AlignLeft : Qt::AlignRight;
491+
return Qt::AlignLeft;
499492
}
500493
}
501494
return QAbstractItemModel::headerData(section, orientation, role);
@@ -509,9 +502,11 @@ QVariant RemoteSolutionModel::data(const QModelIndex& index, int role) const {
509502

510503
switch (role) {
511504
case Qt::UserRole:
512-
case Qt::ToolTipRole:
513505
return item.id;
514506

507+
case Qt::ToolTipRole:
508+
return item.comment;
509+
515510
case Qt::DisplayRole:
516511
switch (index.column()) {
517512
case 0:

visualization/motion_planning_tasks/src/remote_task_model.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class RemoteTaskModel : public BaseTaskModel
8181
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
8282
QModelIndex parent(const QModelIndex& index) const override;
8383

84-
Qt::ItemFlags flags(const QModelIndex& index) const override;
8584
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
8685
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
8786

visualization/motion_planning_tasks/src/task_list_model.cpp

Lines changed: 48 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Software License Agreement (BSD License)
33
*
44
* Copyright (c) 2017, Bielefeld University
5+
* Copyright (c) 2020, Hamburg University
56
* All rights reserved.
67
*
78
* Redistribution and use in source and binary forms, with or without
@@ -60,7 +61,7 @@ QVariant TaskListModel::horizontalHeader(int column, int role) {
6061
case Qt::DisplayRole:
6162
switch (column) {
6263
case 0:
63-
return tr("Name");
64+
return tr("name");
6465
case 1:
6566
return tr(u8"");
6667
case 2:
@@ -80,7 +81,7 @@ QVariant TaskListModel::horizontalHeader(int column, int role) {
8081
break;
8182

8283
case Qt::TextAlignmentRole:
83-
return column == 0 ? Qt::AlignLeft : Qt::AlignRight;
84+
return Qt::AlignLeft;
8485

8586
case Qt::ToolTipRole:
8687
switch (column) {
@@ -113,13 +114,6 @@ QVariant BaseTaskModel::data(const QModelIndex& index, int role) const {
113114
return QVariant();
114115
}
115116

116-
Qt::ItemFlags BaseTaskModel::flags(const QModelIndex& index) const {
117-
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
118-
if (index.column() == 0)
119-
flags |= Qt::ItemIsEditable; // name is editable
120-
return flags;
121-
}
122-
123117
QVariant BaseTaskModel::flowIcon(moveit::task_constructor::InterfaceFlags f) {
124118
static const QIcon CONNECT_ICON = icons::CONNECT.icon();
125119
static const QIcon FORWARD_ICON = icons::FORWARD.icon();
@@ -359,107 +353,73 @@ Qt::DropActions TaskListModel::supportedDropActions() const {
359353
return Qt::CopyAction | Qt::MoveAction;
360354
}
361355

362-
AutoAdjustingTreeView::AutoAdjustingTreeView(QWidget* parent) : QTreeView(parent) {
363-
// consider viewportSizeHint()
364-
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
365-
setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
366-
#endif
367-
}
368-
369-
void AutoAdjustingTreeView::setStretchSection(int section) {
370-
stretch_section_ = section;
371-
updateGeometry();
372-
}
356+
TaskListView::TaskListView(QWidget* parent) : QTreeView(parent) {}
373357

374-
void AutoAdjustingTreeView::setAutoHideSections(const QList<int>& sections) {
375-
auto_hide_cols_ = sections;
376-
updateGeometry();
358+
// dropping onto an item, should expand this item
359+
void TaskListView::dropEvent(QDropEvent* event) {
360+
QModelIndex index = indexAt(event->pos());
361+
QTreeView::dropEvent(event);
362+
if (event->isAccepted())
363+
expand(index);
377364
}
378365

379-
void AutoAdjustingTreeView::setModel(QAbstractItemModel* model) {
380-
size_hints_.clear();
366+
void TaskListView::setModel(QAbstractItemModel* model) {
381367
QTreeView::setModel(model);
382-
383-
updateGeometry();
368+
if (header()->count() >= 4) {
369+
header()->setSectionResizeMode(0, QHeaderView::Stretch);
370+
updateColumnWidth();
371+
}
384372
}
385373

386-
QSize AutoAdjustingTreeView::viewportSizeHint() const {
387-
bool preferred = sizePolicy().horizontalPolicy() & QSizePolicy::ShrinkFlag;
388-
auto m = model();
389-
auto* h = header();
390-
size_hints_.clear();
391-
392-
int width = 0;
393-
for (int i = 0, end = m ? m->columnCount() : 0; i < end; ++i) {
394-
size_hints_.push_back(h->sectionSizeHint(i));
395-
if (preferred || !auto_hide_cols_.contains(i))
396-
width += size_hints_.back();
374+
void TaskListView::dataChanged(const QModelIndex& /*topLeft*/, const QModelIndex& bottomRight,
375+
const QVector<int>& /*roles*/) {
376+
if (bottomRight.column() > 0) {
377+
updateColumnWidth();
397378
}
379+
}
398380

399-
QSize main_size(width, sizeHintForRow(0) * ((!preferred || !m) ? 2 : m->rowCount()));
400-
401-
// add size for header
402-
QSize header_size(0, header()->isVisible() ? header()->height() : 0);
381+
void TaskListView::updateColumnWidth() {
382+
for (int i = 3; i > 0; --i) {
383+
header()->setSectionResizeMode(i, QHeaderView::ResizeToContents);
384+
}
385+
}
403386

404-
// add size for scrollbars
405-
QSize scrollbars(verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0,
406-
horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0);
387+
SolutionListView::SolutionListView(QWidget* parent) : QTreeView(parent) {}
407388

408-
return main_size + header_size + scrollbars;
389+
void SolutionListView::setModel(QAbstractItemModel* model) {
390+
QTreeView::setModel(model);
391+
updateColumnWidth();
409392
}
410393

411-
void AutoAdjustingTreeView::resizeEvent(QResizeEvent* event) {
412-
auto* m = model();
413-
int columns = m ? m->columnCount() : 0;
414-
if ((int)size_hints_.size() != columns)
415-
viewportSizeHint();
416-
417-
// auto hide/show columns > 0, stretch last column to width
418-
int available_width = event->size().width();
394+
void SolutionListView::dataChanged(const QModelIndex& /*topLeft*/, const QModelIndex& bottomRight,
395+
const QVector<int>& /*roles*/) {
396+
if (bottomRight.column() > 0) {
397+
updateColumnWidth();
398+
}
399+
}
419400

420-
int required_width = std::accumulate(size_hints_.begin(), size_hints_.end(), 0);
421-
std::vector<int> width = size_hints_;
401+
void SolutionListView::resizeEvent(QResizeEvent* e) {
402+
QTreeView::resizeEvent(e);
403+
updateColumnWidth();
404+
}
422405

423-
// if required is larger than available width, try to hide some columns
424-
QListIterator<int> it(auto_hide_cols_);
425-
for (it.toBack(); it.hasPrevious() && required_width > available_width;) {
426-
int section = it.previous();
427-
required_width -= size_hints_[section];
428-
width[section] = 0;
406+
void SolutionListView::updateColumnWidth() {
407+
// do nothing if current model is not what we expect
408+
if (header()->count() < 3) {
409+
return;
429410
}
430411

431-
// extend width to current column width if possible
432-
for (int i = 0; i < columns; ++i) {
433-
if (i == stretch_section_)
434-
continue; // ignore auto-stretch section for now
435-
int delta = columnWidth(i) > 0 ? columnWidth(i) - width[i] : 0;
436-
if (delta < 0 || required_width + delta <= available_width) {
437-
width[i] += delta;
438-
required_width += delta;
439-
}
412+
for (int i = 0; i < 2; ++i) {
413+
header()->setSectionResizeMode(i, QHeaderView::ResizeToContents);
440414
}
441415

442-
// stretch section if there is still space available
443-
if (stretch_section_ >= 0 && stretch_section_ < (int)width.size() && width[stretch_section_] > 0 &&
444-
available_width > required_width)
445-
width[stretch_section_] += available_width - required_width;
416+
const int stretch_size = viewport()->size().width() - header()->sectionPosition(2);
446417

447-
// apply stuff
448-
for (int i = 0; i < columns; ++i)
449-
setColumnWidth(i, width[i]);
450-
}
418+
const int content_size = sizeHintForColumn(2);
451419

452-
TaskListView::TaskListView(QWidget* parent) : AutoAdjustingTreeView(parent) {
453-
setStretchSection(0);
420+
header()->resizeSection(2, std::max(stretch_size, content_size));
454421
}
455422

456-
// dropping onto an item, should expand this item
457-
void TaskListView::dropEvent(QDropEvent* event) {
458-
QModelIndex index = indexAt(event->pos());
459-
QTreeView::dropEvent(event);
460-
if (event->isAccepted())
461-
expand(index);
462-
}
463423
} // namespace moveit_rviz_plugin
464424

465425
#include "moc_task_list_model.cpp"

visualization/motion_planning_tasks/src/task_list_model.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Software License Agreement (BSD License)
33
*
44
* Copyright (c) 2017, Bielefeld University
5+
* Copyright (c) 2020, Hamburg University
56
* All rights reserved.
67
*
78
* Redistribution and use in source and binary forms, with or without
@@ -47,6 +48,7 @@
4748

4849
#include <QAbstractItemModel>
4950
#include <QTreeView>
51+
#include <QTableView>
5052
#include <memory>
5153
#include <QPointer>
5254

@@ -94,7 +96,6 @@ class BaseTaskModel : public QAbstractItemModel
9496
QVariant data(const QModelIndex& index, int role) const override;
9597

9698
virtual void setStageFactory(const StageFactoryPtr& factory) {}
97-
Qt::ItemFlags flags(const QModelIndex& index) const override;
9899
unsigned int taskFlags() const { return flags_; }
99100
static QVariant flowIcon(moveit::task_constructor::InterfaceFlags f);
100101

@@ -178,34 +179,34 @@ protected Q_SLOTS:
178179
void highlightStage(size_t id);
179180
};
180181

181-
class AutoAdjustingTreeView : public QTreeView
182+
class TaskListView : public QTreeView
182183
{
183184
Q_OBJECT
184-
Q_PROPERTY(int stretchSection READ stretchSection WRITE setStretchSection)
185-
186-
mutable std::vector<int> size_hints_; // size hints for sections
187-
QList<int> auto_hide_cols_; // auto-hiding sections
188-
int stretch_section_ = -1;
189-
190185
public:
191-
AutoAdjustingTreeView(QWidget* parent = nullptr);
192-
193-
int stretchSection() const { return stretch_section_; }
194-
void setStretchSection(int section);
186+
TaskListView(QWidget* parent = nullptr);
195187

196-
void setAutoHideSections(const QList<int>& sections);
188+
void dropEvent(QDropEvent* event) override;
197189

198190
void setModel(QAbstractItemModel* model) override;
199-
QSize viewportSizeHint() const override;
200-
void resizeEvent(QResizeEvent* event) override;
191+
void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight,
192+
const QVector<int>& roles = QVector<int>()) override;
193+
194+
protected:
195+
void updateColumnWidth();
201196
};
202197

203-
class TaskListView : public AutoAdjustingTreeView
198+
class SolutionListView : public QTreeView
204199
{
205200
Q_OBJECT
206201
public:
207-
TaskListView(QWidget* parent = nullptr);
202+
SolutionListView(QWidget* parent = nullptr);
208203

209-
void dropEvent(QDropEvent* event) override;
204+
void setModel(QAbstractItemModel* model) override;
205+
void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight,
206+
const QVector<int>& roles = QVector<int>()) override;
207+
void resizeEvent(QResizeEvent* event) override;
208+
209+
protected:
210+
void updateColumnWidth();
210211
};
211212
} // namespace moveit_rviz_plugin

0 commit comments

Comments
 (0)