Salome HOME
Porting on Qt5
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
index 348eb2a31c8d30d6fffe169f8a0439eb702d0bd9..5fb0e655b623e63ed9512a24b8f503e18616b252 100644 (file)
 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
 
 #include "XGUI_ObjectsBrowser.h"
-#include "XGUI_DocumentDataModel.h"
 #include "XGUI_Tools.h"
+#include "XGUI_DataModel.h"
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
-#include <ModelAPI_Object.h>
+#include <ModelAPI_Tools.h>
+
+#include <ModuleBase_Tools.h>
 
 #include <QLayout>
-#include <QLabel>
 #include <QLineEdit>
 #include <QPixmap>
 #include <QEvent>
 #include <QMouseEvent>
 #include <QAction>
+#include <QStyledItemDelegate>
+#include <QMessageBox>
+
+#ifdef WIN32
+#ifdef HAVE_SALOME
+// PORTING_TO_SALOME_8
+//#include <QWindowsStyle>
+#include <QCommonStyle>
+#endif
+#endif
+
+
+/// Width of second column (minimum acceptable = 27)
+#define SECOND_COL_WIDTH 30
+
+
+/**
+* \ingroup GUI
+* Tree item delegate for definition of data in column items editor
+*/
+class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
+{
+public:
+  /// Constructor
+  /// \param theParent a parent of the delegate
+  XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
+    myTreedView(theParent) {}
+
+  /// Set data for item editor (name of the item)
+  /// \param editor a widget of editor
+  /// \param index the tree item index
+  virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
+  {
+    QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
+    if (aEditor) {
+      XGUI_DataModel* aModel = myTreedView->dataModel();
+      ObjectPtr aObj = aModel->object(index);
+      if (aObj.get() != NULL) {
+        aEditor->setText(aObj->data()->name().c_str());
+        return;
+      }
+    }
+    QStyledItemDelegate::setEditorData(editor, index);
+  }
+
+private:
+  XGUI_DataTree* myTreedView;
+};
+
 
 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
     : QTreeView(theParent)
 {
+//#ifdef WIN32
+//#ifdef HAVE_SALOME
+//  setStyle(new QCommonStyle());
+//#else
+//  myStyle = new XGUI_TreeViewStyle();
+//  setStyle(myStyle);
+//#endif
+//#endif
+
   setHeaderHidden(true);
-  setModel(new XGUI_DocumentDataModel(this));
   setEditTriggers(QAbstractItemView::NoEditTriggers);
   setSelectionBehavior(QAbstractItemView::SelectRows);
   setSelectionMode(QAbstractItemView::ExtendedSelection);
 
-  connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
-          this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
+  setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
+
+  connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
+    SLOT(onDoubleClick(const QModelIndex&)));
 }
 
 XGUI_DataTree::~XGUI_DataTree()
 {
 }
 
-XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
+XGUI_DataModel* XGUI_DataTree::dataModel() const
 {
-  return static_cast<XGUI_DocumentDataModel*>(model());
+  return static_cast<XGUI_DataModel*>(model());
 }
 
-void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
-                                       const QItemSelection& theDeselected)
+void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
 {
-  mySelectedData.clear();
-  QModelIndexList aIndexes = selectionModel()->selectedIndexes();
-  XGUI_DocumentDataModel* aModel = dataModel();
-  QModelIndexList::const_iterator aIt;
-  for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
-    ObjectPtr aObject = aModel->object(*aIt);
-    if (aObject)
-      mySelectedData.append(aObject);
-  }
-  emit selectionChanged();
+  emit contextMenuRequested(theEvent);
 }
 
-void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
+void XGUI_DataTree::commitData(QWidget* theEditor)
 {
-  if (theEvent->button() == Qt::LeftButton) {
-    QModelIndex aIndex = currentIndex();
-    XGUI_DocumentDataModel* aModel = dataModel();
-    ObjectPtr aObject = aModel->object(aIndex);
-    ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
-    if (aPart) {
-      aPart->activate();
+  static int aEntrance = 0;
+  if (aEntrance == 0) {
+    // We have to check number of enter and exit of this function because it can be called
+    // recursively by Qt in order to avoid double modifying of a data
+    aEntrance = 1;
+    QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
+    if (aEditor) {
+      QString aName = aEditor->text();
+      QModelIndexList aIndexList = selectionModel()->selectedIndexes();
+      XGUI_DataModel* aModel = dataModel();
+      ObjectPtr aObj = aModel->object(aIndexList.first());
+
+      if (XGUI_Tools::canRename(aObj, aName)) {
+        SessionPtr aMgr = ModelAPI_Session::get();
+        aMgr->startOperation("Rename");
+        aObj->data()->setName(qPrintable(aName));
+        aMgr->finishOperation();
+      }
     }
-  } else
-    QTreeView::mouseDoubleClickEvent(theEvent);
+  }
+  aEntrance = 0;
 }
 
-void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
+void XGUI_DataTree::clear()
 {
-  emit contextMenuRequested(theEvent);
+  dataModel()->clear();
+  reset();
 }
 
-void XGUI_DataTree::commitData(QWidget* theEditor)
+void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
+{
+  QTreeView::resizeEvent(theEvent);
+  QSize aSize = theEvent->size();
+  if (aSize.isValid()) {
+    setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH - 6);
+    setColumnWidth(1, SECOND_COL_WIDTH);
+  }
+}
+
+void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
 {
-  QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
-  if (aEditor) {
-    QString aRes = aEditor->text();
-    ObjectPtr aFeature = mySelectedData.first();
-    SessionPtr aMgr = ModelAPI_Session::get();
-    aMgr->startOperation("Rename");
-    aFeature->data()->setName(qPrintable(aRes));
+  if (theIndex.column() != 1)
+    return;
+  SessionPtr aMgr = ModelAPI_Session::get();
+  // When operation is opened then we can not change history
+  if (aMgr->isOperation())
+    return;
+  XGUI_DataModel* aModel = dataModel();
+  if (aModel->flags(theIndex) == 0)
+    return;
+  ObjectPtr aObj = aModel->object(theIndex);
+
+  DocumentPtr aDoc = aMgr->activeDocument();
+
+  std::string aOpName = tr("History change").toStdString();
+  if (aObj.get()) {
+    if (aObj->document() != aDoc)
+      return;
+    aMgr->startOperation(aOpName);
+    aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
     aMgr->finishOperation();
+  } else {
+    // Ignore clicks on folders outside current document
+    if ((theIndex.internalId() == 0) && (aDoc != aMgr->moduleDocument()))
+      // Clicked folder under root but active document is another
+      return;
+    if ((theIndex.internalId() != 0) && (aDoc.get() != theIndex.internalPointer()))
+      // Cliced not on active document folder
+      return;
+
+    aMgr->startOperation(aOpName);
+    aDoc->setCurrentFeature(FeaturePtr(), true);
+    aMgr->finishOperation();
+  }
+  QModelIndex aNewIndex = aModel->lastHistoryIndex();
+  QModelIndex aParent = theIndex.parent();
+  int aSize = aModel->rowCount(aParent);
+  for (int i = 0; i < aSize; i++) {
+    update(aModel->index(i, 0, aParent));
+    update(aModel->index(i, 1, aParent));
   }
 }
 
-void XGUI_DataTree::clear() 
+//#if (!defined HAVE_SALOME) && (defined WIN32)
+//void XGUI_DataTree::drawRow(QPainter* thePainter,
+//                            const QStyleOptionViewItem& theOptions,
+//                            const QModelIndex& theIndex) const
+//{
+//  QStyleOptionViewItemV4 aOptions = theOptions;
+//  myStyle->setIndex(theIndex);
+//  QTreeView::drawRow(thePainter, aOptions, theIndex);
+//}
+//
+////********************************************************************
+////********************************************************************
+////********************************************************************
+//void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement,
+//                                       const QStyleOption* theOption,
+//                                       QPainter* thePainter, const QWidget* theWidget) const
+//{
+//  if ((theElement == QStyle::PE_PanelItemViewRow) || (theElement == QStyle::PE_PanelItemViewItem)) {
+//    const QStyleOptionViewItemV4* aOptions =
+//      qstyleoption_cast<const QStyleOptionViewItemV4 *>(theOption);
+//    if (myIndex.isValid() && ((myIndex.flags() & Qt::ItemIsSelectable) == 0)) {
+//      QStyle::State aState = aOptions->state;
+//      if ((aState & QStyle::State_MouseOver) != 0)
+//        aState &= ~QStyle::State_MouseOver;
+//      QStyleOptionViewItemV4* aOpt = (QStyleOptionViewItemV4*) aOptions;
+//      aOpt->state = aState;
+//      QCommonStyle::drawPrimitive(theElement, aOpt, thePainter, theWidget);
+//    }
+//  }
+//  QCommonStyle::drawPrimitive(theElement, theOption, thePainter, theWidget);
+//}
+//#endif
+
+
+//********************************************************************
+//********************************************************************
+//********************************************************************
+XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
+  : QLabel(theText, theParent),
+  myPreSelectionStyle(""),
+  myNeutralStyle(""),
+  mySelectionStyle(""),
+  myIsSelected(false)
 {
-  mySelectedData.clear();
-  XGUI_DocumentDataModel* aModel = dataModel();
-  aModel->clear();
-  reset();
 }
 
+void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
+{
+  myTreeView = theView;
+  setFont(myTreeView->font());
+
+  QPalette aPalet = myTreeView->palette();
+  QColor aHighlight = aPalet.highlight().color();
+  QColor aHighlightText = aPalet.highlightedText().color();
+
+  myPreSelectionStyle = "QLabel {background-color: ";
+  myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
+  //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
+  //  aHighlight.lighter(170).name() + ");";
+  //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
+
+  QString aName = aPalet.color(QPalette::Base).name();
+  myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
+
+
+#if (!defined HAVE_SALOME) && (defined WIN32)
+  mySelectionStyle = "QLabel {background-color: ";
+  mySelectionStyle += "rgb(205, 232, 255); ";
+  //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
+  //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
+  //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
+  mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
+#else
+  mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
+  mySelectionStyle += "; color : " + aHighlightText.name() + "}";
+#endif
+
+  myTreeView->viewport()->installEventFilter(this);
+}
+
+
+#if (!defined HAVE_SALOME) && (defined WIN32)
+bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
+{
+  switch (theEvent->type()) {
+    case QEvent::Enter:
+      if (!myIsSelected)
+        setStyleSheet(myPreSelectionStyle);
+      break;
+    case QEvent::Leave:
+      if (!myIsSelected)
+        setStyleSheet(myNeutralStyle);
+      break;
+  }
+  return QLabel::event(theEvent);
+}
+#endif
+
+bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
+{
+  if (theObj == myTreeView->viewport()) {
+    if (theEvent->type() == QEvent::MouseButtonRelease)
+      unselect();
+  }
+  return QLabel::eventFilter(theObj, theEvent);
+}
+
+static bool MYClearing = false;
+void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
+{
+  MYClearing = true;
+  myIsSelected = true;
+  setStyleSheet(mySelectionStyle);
+  // We can not block signals because on
+  // clear selection the View state will not be updated
+  myTreeView->clearSelection();
+  QLabel::mouseReleaseEvent(e);
+  MYClearing = false;
+}
+
+void XGUI_ActiveDocLbl::unselect()
+{
+  if (!MYClearing) {
+    myIsSelected = false;
+    setStyleSheet(myNeutralStyle);
+  }
+}
+
+
 //********************************************************************
 //********************************************************************
 //********************************************************************
 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
-    : QWidget(theParent)
+    : QWidget(theParent), myDocModel(0)
 {
   QVBoxLayout* aLayout = new QVBoxLayout(this);
-  aLayout->setContentsMargins(0, 0, 0, 0);
+  ModuleBase_Tools::zeroMargins(aLayout);
   aLayout->setSpacing(0);
 
-  QFrame* aLabelWgt = new QFrame(this);
+  QWidget* aLabelWgt = new QWidget(this);
   aLabelWgt->setAutoFillBackground(true);
-  QPalette aPalet = aLabelWgt->palette();
-  aPalet.setColor(QPalette::Window, Qt::white);
-  aLabelWgt->setPalette(aPalet);
 
   aLayout->addWidget(aLabelWgt);
   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
-  aLabelLay->setContentsMargins(0, 0, 0, 0);
+  ModuleBase_Tools::zeroMargins(aLabelLay);
   aLabelLay->setSpacing(0);
 
   QLabel* aLbl = new QLabel(aLabelWgt);
   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
   aLbl->setMargin(2);
-
-  aLbl->setAutoFillBackground(true);
+  // Do not paint background of the label (in order to show icon)
+  aLbl->setAutoFillBackground(false);
 
   aLabelLay->addWidget(aLbl);
 
   SessionPtr aMgr = ModelAPI_Session::get();
   DocumentPtr aDoc = aMgr->moduleDocument();
-  // TODO: Find a name of the root document
 
-  myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
-  myActiveDocLbl->setReadOnly(true);
-  myActiveDocLbl->setFrame(false);
-  //myActiveDocLbl->setMargin(2);
+  myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
+//  myActiveDocLbl->setReadOnly(true);
+//  myActiveDocLbl->setFrame(false);
   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
 
-  myActiveDocLbl->installEventFilter(this);
-
   aLabelLay->addWidget(myActiveDocLbl);
   aLabelLay->setStretch(1, 1);
 
   myTreeView = new XGUI_DataTree(this);
+  myTreeView->setFrameShape(QFrame::NoFrame);
   aLayout->addWidget(myTreeView);
 
-  myDocModel = myTreeView->dataModel();
+  QPalette aTreePalet = myTreeView->palette();
+  QColor aTreeBack = aTreePalet.color(QPalette::Base);
 
-  aLabelWgt->setFrameShape(myTreeView->frameShape());
-  aLabelWgt->setFrameShadow(myTreeView->frameShadow());
+  QPalette aPalet;
+  aPalet.setColor(QPalette::Base, aTreeBack);
+  aPalet.setColor(QPalette::Window, aTreeBack);
+  aLabelWgt->setPalette(aPalet);
 
-  connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-  connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
-          SLOT(onActivePartChanged(ObjectPtr)));
-  connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
-          SIGNAL(activePartChanged(ObjectPtr)));
+  myDocModel = new XGUI_DataModel(this);
+  connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
+  connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
 
-  connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
-          SLOT(onLabelContextMenuRequested(const QPoint&)));
   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
           SLOT(onContextMenuRequested(QContextMenuEvent*)));
-
-  onActivePartChanged(ObjectPtr());
-
-  // Create internal actions
-  QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
-  aAction->setData("RENAME_CMD");
-  connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
-  addAction(aAction);
 }
 
 //***************************************************
@@ -171,108 +380,35 @@ XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
 {
 }
 
-//***************************************************
-void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
+void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
 {
-  QPalette aPalet = myActiveDocLbl->palette();
-  if (thePart) {
-    aPalet.setColor(QPalette::Text, Qt::black);
-  } else {
-    aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
-  }
-  myActiveDocLbl->setPalette(aPalet);
-}
+  myDocModel->setXMLReader(theReader);
+  myTreeView->setModel(myDocModel);
 
-//***************************************************
-bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
-{
-  if (obj == myActiveDocLbl) {
-    if (myActiveDocLbl->isReadOnly()) {
-      if (theEvent->type() == QEvent::MouseButtonDblClick) {
-        if (myDocModel->activePartIndex().isValid()) {
-          myTreeView->setExpanded(myDocModel->activePartIndex(), false);
-        }
-        myDocModel->deactivatePart();
-        onActivePartChanged(ObjectPtr());
-        emit activePartChanged(ObjectPtr());
-      }
-    } else {
-      // End of editing by mouse click
-      if (theEvent->type() == QEvent::MouseButtonRelease) {
-        QMouseEvent* aEvent = (QMouseEvent*) theEvent;
-        QPoint aPnt = mapFromGlobal(aEvent->globalPos());
-        if (childAt(aPnt) != myActiveDocLbl) {
-          closeDocNameEditing(true);
-        }
-      } else if (theEvent->type() == QEvent::KeyRelease) {
-        QKeyEvent* aEvent = (QKeyEvent*) theEvent;
-        switch (aEvent->key()) {
-          case Qt::Key_Return:
-          case Qt::Key_Enter:  // Accept current input
-            closeDocNameEditing(true);
-            break;
-          case Qt::Key_Escape:  // Cancel the input
-            closeDocNameEditing(false);
-            break;
-        }
-      }
-    }
-  }
-  return QWidget::eventFilter(obj, theEvent);
-}
+  // It has to be done after setting of model
+  myActiveDocLbl->setTreeView(myTreeView);
 
-//***************************************************
-void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
-{
-  myActiveDocLbl->deselect();
-  myActiveDocLbl->clearFocus();
-  myActiveDocLbl->releaseMouse();
-  myActiveDocLbl->setReadOnly(true);
-  if (toSave) {
-    // TODO: Save the name of root document
-    SessionPtr aMgr = ModelAPI_Session::get();
-    DocumentPtr aDoc = aMgr->moduleDocument();
-  } else {
-    myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
-  }
+  QItemSelectionModel* aSelMod = myTreeView->selectionModel();
+  connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
+          this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
 }
 
 //***************************************************
-void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
+void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
 {
-  if (thePart) {
-    QModelIndex aIndex = myDocModel->partIndex(thePart);
+  QModelIndexList aIndexes;
+  QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
+  bool toEnable = false;
 
-    if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
-      myTreeView->setExpanded(myDocModel->activePartIndex(), false);
-    }
-    bool isChanged = myDocModel->activatedIndex(aIndex);
-    if (isChanged) {
-      if (myDocModel->activePartIndex().isValid()) {
-        myTreeView->setExpanded(aIndex.parent(), true);
-        myTreeView->setExpanded(aIndex, true);
-        onActivePartChanged(myDocModel->object(aIndex));
-      } else {
-        onActivePartChanged(ObjectPtr());
-      }
-    }
-  } else {
-    QModelIndex aIndex = myDocModel->activePartIndex();
-    if (aIndex.isValid()) {
-      myDocModel->activatedIndex(aIndex);
-      myTreeView->setExpanded(aIndex, false);
-      onActivePartChanged(ObjectPtr());
-    }
-  }
-}
+  if (aSelectedData.size() == 1) {
+    QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
+    if (!aIndexes.contains(aSelected))
+      return; // menu is called on non selected item
 
-//***************************************************
-void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
-{
-  myObjectsList = myTreeView->selectedObjects();
-  bool toEnable = myObjectsList.size() == 1;
-  foreach(QAction* aCmd, actions())
-  {
+    Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
+    toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
+  }
+  foreach(QAction* aCmd, actions()) {
     aCmd->setEnabled(toEnable);
   }
   emit contextMenuRequested(theEvent);
@@ -281,12 +417,9 @@ void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
 //***************************************************
 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
 {
-  myObjectsList.clear();
+  myTreeView->selectionModel()->clearSelection();
   //Empty feature pointer means that selected root document
-  myObjectsList.append(ObjectPtr());
-
-  foreach(QAction* aCmd, actions())
-  {
+  foreach(QAction* aCmd, actions()) {
     aCmd->setEnabled(true);
   }
   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
@@ -296,15 +429,21 @@ void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
 //***************************************************
 void XGUI_ObjectsBrowser::onEditItem()
 {
-  if (myObjectsList.size() > 0) {
-    ObjectPtr aFeature = myObjectsList.first();
-    if (aFeature) {  // Selection happens in TreeView
+  QObjectPtrList aSelectedData = selectedObjects();
+  if (aSelectedData.size() > 0) {
+    ObjectPtr anObject = aSelectedData.first();
+    if (anObject.get()) {  // Selection happens in TreeView
+      // check whether the object can be renamed. There should not be parts which are not loaded
+      std::set<FeaturePtr> aFeatures;
+      aFeatures.insert(ModelAPI_Feature::feature(anObject));
+      if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
+        return;
+
       // Find index which corresponds the feature
       QModelIndex aIndex;
-      foreach(QModelIndex aIdx, selectedIndexes())
-      {
+      foreach(QModelIndex aIdx, selectedIndexes()) {
         ObjectPtr aFea = dataModel()->object(aIdx);
-        if (dataModel()->object(aIdx)->isSame(aFeature)) {
+        if (dataModel()->object(aIdx)->isSame(anObject)) {
           aIndex = aIdx;
           break;
         }
@@ -313,21 +452,28 @@ void XGUI_ObjectsBrowser::onEditItem()
         myTreeView->setCurrentIndex(aIndex);
         myTreeView->edit(aIndex);
       }
-    } else {  //Selection happens in Upper label
-      myActiveDocLbl->setReadOnly(false);
-      myActiveDocLbl->setFocus();
-      myActiveDocLbl->selectAll();
-      myActiveDocLbl->grabMouse();
-      myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
+      return;
     }
   }
 }
 
 //***************************************************
-void XGUI_ObjectsBrowser::onSelectionChanged()
+QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
 {
-  myObjectsList = myTreeView->selectedObjects();
-  emit selectionChanged();
+  QModelIndexList aIndexes;
+  QModelIndex aIndex;
+  for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
+    aIndex = myDocModel->index(i, 0, theParent);
+    if (myDocModel->hasChildren(aIndex)) {
+      if (myTreeView->isExpanded(aIndex)) {
+        aIndexes.append(aIndex);
+        QModelIndexList aSubIndexes = expandedItems(aIndex);
+        if (!aSubIndexes.isEmpty())
+          aIndexes.append(aSubIndexes);
+      }
+    }
+  }
+  return aIndexes;
 }
 
 //***************************************************
@@ -354,15 +500,75 @@ void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
 }
 
 //***************************************************
-void XGUI_ObjectsBrowser::processEvent(const std::shared_ptr<Events_Message>& theMessage)
-{ 
-  myDocModel->processEvent(theMessage); 
+void XGUI_ObjectsBrowser::clearContent()
+{
+  myTreeView->clear();
 }
 
+void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
+                                       const QItemSelection& theDeselected)
+{
+  emit selectionChanged();
+}
 
-//***************************************************
-void XGUI_ObjectsBrowser::clearContent()  
-{ 
-  myObjectsList.clear();
-  myTreeView->clear(); 
+QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
+{
+  QObjectPtrList aList;
+  QModelIndexList aIndexes = selectedIndexes();
+  XGUI_DataModel* aModel = dataModel();
+  QModelIndexList::const_iterator aIt;
+  for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
+    if ((*aIt).column() == 0) {
+      ObjectPtr aObject = aModel->object(*aIt);
+      if (aObject) {
+        aList.append(aObject);
+        if (theIndexes)
+          theIndexes->append(*aIt);
+      }
+    }
+  }
+  return aList;
+}
+
+void XGUI_ObjectsBrowser::onBeforeReset()
+{
+  myExpandedItems = expandedItems();
+}
+
+void XGUI_ObjectsBrowser::onAfterModelReset()
+{
+  foreach(QModelIndex aIndex, myExpandedItems) {
+    myTreeView->setExpanded(aIndex, true);
+  }
+}
+
+std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
+{
+  std::list<bool> aStates;
+  XGUI_DataModel* aModel = dataModel();
+  QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
+  int aNbChild = aModel->rowCount(aRootIdx);
+  for (int i = 0; i < aNbChild; i++) {
+    QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
+    aStates.push_back(myTreeView->isExpanded(aIdx));
+  }
+  return aStates;
+}
+
+void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
+{
+  if (theStates.size() == 0)
+    return;
+  XGUI_DataModel* aModel = dataModel();
+  QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
+  int aNbChild = aModel->rowCount(aRootIdx);
+
+  std::list<bool>::const_iterator aIt;
+  int i = 0;
+  for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
+    if (i >= aNbChild )
+      break;
+    QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
+    myTreeView->setExpanded(aIdx, (*aIt));
+  }
 }