Salome HOME
bos #32216 [CEA] GUI ergonomic: fix horizontal scrollbar position.
[modules/gui.git] / src / Qtx / QtxTreeView.cxx
index 4da1f00adff15d29e71346bf315682e3c7d97fd2..17e2dfab1dc3d8e16c3031762d75caf1f0beaa81 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,7 @@
 #include <QHeaderView>
 #include <QMenu>
 #include <QMouseEvent>
+#include <QScrollBar>
 
 /*!
   \class QtxTreeView::Header
@@ -107,17 +108,10 @@ void QtxTreeView::Header::contextMenuEvent( QContextMenuEvent* e )
     QVariant appropriate = model()->headerData( i, orientation(), Qtx::AppropriateRole );
     QIcon icon;
     if ( iconData.isValid() ) {
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-      if ( iconData.canConvert( QVariant::Icon ) )
-        icon = iconData.value<QIcon>();
-      else if ( iconData.canConvert( QVariant::Pixmap ) )
-        icon = iconData.value<QPixmap>();
-#else
       if ( iconData.canConvert( QMetaType::QIcon ) )
         icon = iconData.value<QIcon>();
       else if ( iconData.canConvert( QMetaType::QPixmap ) )
         icon = iconData.value<QPixmap>();
-#endif
     }
     if( ( !lab.isEmpty() || !icon.isNull() ) && 
             appropriate.isValid() ? appropriate.toInt()==Qtx::Toggled : true )
@@ -143,11 +137,7 @@ void QtxTreeView::Header::contextMenuEvent( QContextMenuEvent* e )
     }
     else if ( a && a == sortAction ) {
       setSortIndicatorShown( a->isChecked() );
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-      setClickable( a->isChecked() );
-#else
       setSectionsClickable( a->isChecked() );
-#endif
       QtxTreeView* view = qobject_cast<QtxTreeView*>( parent() );
       if ( view ) {
         view->emitSortingEnabled( a->isChecked() );
@@ -192,11 +182,7 @@ QtxTreeView::QtxTreeView( QWidget* parent )
 : QTreeView( parent )
 {
   setHeader( new Header( false, this ) );
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-  header()->setMovable( true );
-#else
   header()->setSectionsMovable( true );
-#endif
 }
 
 /*!
@@ -208,11 +194,7 @@ QtxTreeView::QtxTreeView( const bool enableSortMenu, QWidget* parent )
 : QTreeView( parent )
 {
   setHeader( new Header( enableSortMenu, this ) );
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-  header()->setMovable( true );
-#else
   header()->setSectionsMovable( true );
-#endif
 }
 
 /*!
@@ -409,6 +391,21 @@ void QtxTreeView::setModel( QAbstractItemModel* m )
              this, SLOT( onAppropriate( Qt::Orientation, int, int ) ) );
 }
 
+// This method fixes problem with Object Browser horizontal scrollbar automatic scrolling to the right
+void QtxTreeView::scrollTo(const QModelIndex &index,
+                           QAbstractItemView::ScrollHint hint)
+{
+  QScrollBar* aScrollBar = horizontalScrollBar();
+  if (aScrollBar) {
+    int horPos = aScrollBar->value();
+    QTreeView::scrollTo(index, hint);
+    aScrollBar->setValue(horPos);
+  }
+  else {
+    QTreeView::scrollTo(index, hint);
+  }
+}
+
 void QtxTreeView::onAppropriate( Qt::Orientation orient, int first, int last )
 {
   if( orient==Qt::Horizontal )