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-
123117QVariant 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"
0 commit comments