Salome HOME
Copyright update 2022
[modules/gui.git] / src / ObjBrowser / OB_Browser.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 3b1e172..acf7ecc
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -433,7 +433,8 @@ void OB_Browser::select( const QModelIndexList& indexes, const bool on, const bo
       for (int i = 1; i < indexes.size(); ++i) 
       {
         idx=indexes.at(i);
-        if(idx.parent().internalId()==last.parent().internalId() && idx.row()==last.row()+1 && idx.column()==last.column())
+        if(idx.parent().row()==last.parent().row() && idx.parent().column()==last.parent().column() &&
+           idx.row()==last.row()+1 && idx.column()==last.column())
         {
           // index is contiguous to last: extend the range
           last=idx;
@@ -1194,3 +1195,63 @@ void OB_Browser::onDoubleClicked( QListViewItem* item )
   \fn void OB_Browser::selectionChanged();
   \brief Emitted when selection is changed in the Object Browser.
 */
+
+QByteArray OB_Browser::getOpenStates( int theColumn ) const
+{
+  QByteArray aData;
+  QDataStream aStream( &aData, QIODevice::WriteOnly );
+  MapOfOpenStates aMap;
+  const_cast<OB_Browser*>( this )->openStates( true, aMap, QModelIndex(), theColumn );
+  MapOfOpenStates::const_iterator anIt = aMap.begin(), aLast = aMap.end();
+  for( ; anIt!=aLast; anIt++ )
+  {
+    QString anEntry = anIt.key();
+    qint32 anOpenAttr = anIt.value() ? 1 : 0;
+    aStream << anEntry << anOpenAttr;
+  }
+  return aData;
+}
+
+void OB_Browser::setOpenStates( const QByteArray& theData, int theColumn )
+{
+  QByteArray* aData = const_cast<QByteArray*>( &theData );
+  QDataStream aStream( aData, QIODevice::ReadOnly );
+  MapOfOpenStates aMap;
+  while( !aStream.atEnd() )
+  {
+    QString anEntry;
+    qint32 anOpenAttr;
+    aStream >> anEntry >> anOpenAttr;
+    bool isOpen = anOpenAttr!=0;
+    aMap[anEntry] = isOpen;
+  }
+  openStates( false, aMap, QModelIndex(), theColumn );
+}
+
+void OB_Browser::openStates( bool isGet, MapOfOpenStates& theMap, const QModelIndex& theIndex, int theColumn )
+{
+  if( theIndex.isValid() )
+  {
+    QString anEntry = theIndex.sibling( theIndex.row(), theColumn ).data().toString();
+    bool isOpen;
+    if( isGet )
+    {
+      isOpen = treeView()->isExpanded( theIndex );
+      theMap[anEntry] = isOpen;
+    }
+    else
+    {
+      isOpen = theMap.contains( anEntry ) ? theMap[anEntry] : false;
+      treeView()->setExpanded( theIndex, isOpen );
+    }
+  }
+
+  const QAbstractItemModel* aModel = model();
+
+  int n = aModel->rowCount( theIndex );
+  for( int i=0; i<n; i++ )
+  {
+    QModelIndex aChild = aModel->index( i, 0, theIndex );
+    openStates( isGet, theMap, aChild, theColumn );
+  }
+}