diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index 8ec0fa0c73..0dada0e958 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -97,6 +97,10 @@ static bool isVisibilityIconEnabled() { return true; } +static bool isSelectionCheckBoxesEnabled() { + return TreeParams::getCheckBoxesSelection(); +} + void TreeParams::onItemBackgroundChanged() { if (getItemBackground()) { @@ -1557,6 +1561,50 @@ void TreeWidget::keyPressEvent(QKeyEvent* event) QTreeWidget::keyPressEvent(event); } +void TreeWidget::mousePressEvent(QMouseEvent* event) +{ + QTreeWidget::mousePressEvent(event); + + // Handle the visibility icon after the normal event processing to not interfer with + // the selection logic. + if (isVisibilityIconEnabled()) { + QTreeWidgetItem* item = itemAt(event->pos()); + if (item && item->type() == TreeWidget::ObjectType && event->button() == Qt::LeftButton) { + auto objitem = static_cast(item); + + // Mouse position relative to viewport + auto mousePos = event->pos(); + + // Rect occupied by the item relative to viewport + auto iconRect = visualItemRect(objitem); + + // If the checkboxes are visible, these are displayed before the icon + // and we have to compensate for its width. + if (isSelectionCheckBoxesEnabled()) { + auto style = this->style(); + int checkboxWidth = style->pixelMetric(QStyle::PM_IndicatorWidth) + + style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing); + iconRect.adjust(checkboxWidth, 0, 0, 0); + } + + // We are interested in the first icon (visibility icon) + iconRect.setWidth(iconSize()); + + // If the visibility icon was clicked, toggle the DocumentObject visibility + if (iconRect.contains(mousePos)) { + auto vp = objitem->object(); + if (vp->isShow()) { + vp->hide(); + } else { + vp->show(); + } + event->setAccepted(true); + return; + } + } + } +} + void TreeWidget::mouseDoubleClickEvent(QMouseEvent* event) { QTreeWidgetItem* item = itemAt(event->pos()); @@ -3134,10 +3182,6 @@ void TreeWidget::onItemSelectionChanged() this->blockSelection(lock); } -static bool isSelectionCheckBoxesEnabled() { - return TreeParams::getCheckBoxesSelection(); -} - void TreeWidget::synchronizeSelectionCheckBoxes() { const bool useCheckBoxes = isSelectionCheckBoxesEnabled(); for (auto tree : TreeWidget::Instances) { diff --git a/src/Gui/Tree.h b/src/Gui/Tree.h index 6d35edfec2..02597a19ea 100644 --- a/src/Gui/Tree.h +++ b/src/Gui/Tree.h @@ -143,6 +143,7 @@ protected: //@} bool event(QEvent *e) override; void keyPressEvent(QKeyEvent *event) override; + void mousePressEvent(QMouseEvent * event) override; void mouseDoubleClickEvent(QMouseEvent * event) override; protected: