Salome HOME
The selection problem with an object deactivate:
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
index 45d287465f3e87b761670c99239021ce63ea29ae..75f6c32a9d86d2f0c27e7bc1fd6793ef5bde89ea 100644 (file)
@@ -1,12 +1,16 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
+
 #include "XGUI_ObjectsBrowser.h"
 #include "XGUI_DocumentDataModel.h"
 #include "XGUI_Tools.h"
 
 #include <ModelAPI_Data.h>
-#include <ModelAPI_PluginManager.h>
+#include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Object.h>
 
+#include <ModuleBase_Tools.h>
+
 #include <QLayout>
 #include <QLabel>
 #include <QLineEdit>
 #include <QEvent>
 #include <QMouseEvent>
 #include <QAction>
+#include <QStyledItemDelegate>
+
+/**
+* \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_DocumentDataModel* 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)
+    : QTreeView(theParent)
 {
   setHeaderHidden(true);
   setModel(new XGUI_DocumentDataModel(this));
@@ -26,7 +62,9 @@ XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
   setSelectionBehavior(QAbstractItemView::SelectRows);
   setSelectionMode(QAbstractItemView::ExtendedSelection);
 
-  connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), 
+  setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
+
+  connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
 }
 
@@ -34,14 +72,13 @@ XGUI_DataTree::~XGUI_DataTree()
 {
 }
 
-XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const 
-{ 
-  return static_cast<XGUI_DocumentDataModel*>(model()); 
+XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
+{
+  return static_cast<XGUI_DocumentDataModel*>(model());
 }
 
-
-void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected, 
-                                             const QItemSelection& theDeselected)
+void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
+                                       const QItemSelection& theDeselected)
 {
   mySelectedData.clear();
   QModelIndexList aIndexes = selectionModel()->selectedIndexes();
@@ -60,18 +97,12 @@ void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
   if (theEvent->button() == Qt::LeftButton) {
     QModelIndex aIndex = currentIndex();
     XGUI_DocumentDataModel* aModel = dataModel();
-
-    if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
-      setExpanded(aModel->activePartIndex(), false);
-    }
-    bool isChanged = aModel->activatedIndex(aIndex);
-    QTreeView::mouseDoubleClickEvent(theEvent);
-    if (isChanged) {
-      if (aModel->activePartIndex().isValid())
-        setExpanded(aIndex, true);
-      emit activePartChanged(aModel->activePart());
+    ObjectPtr aObject = aModel->object(aIndex);
+    ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
+    if (aPart) {
+      aPart->activate();
     }
-  } else 
+  } else
     QTreeView::mouseDoubleClickEvent(theEvent);
 }
 
@@ -86,21 +117,29 @@ void XGUI_DataTree::commitData(QWidget* theEditor)
   if (aEditor) {
     QString aRes = aEditor->text();
     ObjectPtr aFeature = mySelectedData.first();
-    PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
-    aMgr->rootDocument()->startOperation();
+    SessionPtr aMgr = ModelAPI_Session::get();
+    aMgr->startOperation("Rename");
     aFeature->data()->setName(qPrintable(aRes));
-    aMgr->rootDocument()->finishOperation();
+    aMgr->finishOperation();
   }
 }
 
+void XGUI_DataTree::clear() 
+{
+  mySelectedData.clear();
+  XGUI_DocumentDataModel* aModel = dataModel();
+  aModel->clear();
+  reset();
+}
+
 //********************************************************************
 //********************************************************************
 //********************************************************************
 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
-  : QWidget(theParent)
+    : QWidget(theParent)
 {
   QVBoxLayout* aLayout = new QVBoxLayout(this);
-  aLayout->setContentsMargins(0, 0, 0, 0);
+  ModuleBase_Tools::zeroMargins(aLayout);
   aLayout->setSpacing(0);
 
   QFrame* aLabelWgt = new QFrame(this);
@@ -111,7 +150,7 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
 
   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);
@@ -122,8 +161,8 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
 
   aLabelLay->addWidget(aLbl);
 
-  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
-  DocumentPtr aDoc = aMgr->rootDocument();
+  SessionPtr aMgr = ModelAPI_Session::get();
+  DocumentPtr aDoc = aMgr->moduleDocument();
   // TODO: Find a name of the root document
 
   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
@@ -135,7 +174,7 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
   myActiveDocLbl->installEventFilter(this);
 
   aLabelLay->addWidget(myActiveDocLbl);
-  aLabelLay->setStretch(1,1);
+  aLabelLay->setStretch(1, 1);
 
   myTreeView = new XGUI_DataTree(this);
   aLayout->addWidget(myTreeView);
@@ -146,14 +185,16 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
 
   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)));
-
-  connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), 
-          this, SLOT(onLabelContextMenuRequested(const QPoint&)));
-  connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
-          this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
-  
+  connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
+          SLOT(onActivePartChanged(ObjectPtr)));
+  connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
+          SIGNAL(activePartChanged(ObjectPtr)));
+
+  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
@@ -174,7 +215,7 @@ void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
   QPalette aPalet = myActiveDocLbl->palette();
   if (thePart) {
     aPalet.setColor(QPalette::Text, Qt::black);
-  }  else {
+  } else {
     aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
   }
   myActiveDocLbl->setPalette(aPalet);
@@ -204,13 +245,14 @@ bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
       } else if (theEvent->type() == QEvent::KeyRelease) {
         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
         switch (aEvent->key()) {
-        case Qt::Key_Return: // Accept current input
-          closeDocNameEditing(true);
-          break;
-        case Qt::Key_Escape: // Cancel the input
-          closeDocNameEditing(false);
-          break;
-        } 
+          case Qt::Key_Return:
+          case Qt::Key_Enter:  // Accept current input
+            closeDocNameEditing(true);
+            break;
+          case Qt::Key_Escape:  // Cancel the input
+            closeDocNameEditing(false);
+            break;
+        }
       }
     }
   }
@@ -226,8 +268,8 @@ void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
   myActiveDocLbl->setReadOnly(true);
   if (toSave) {
     // TODO: Save the name of root document
-    PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
-    DocumentPtr aDoc = aMgr->rootDocument();
+    SessionPtr aMgr = ModelAPI_Session::get();
+    DocumentPtr aDoc = aMgr->moduleDocument();
   } else {
     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
   }
@@ -263,11 +305,12 @@ void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
 }
 
 //***************************************************
-void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) 
+void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
 {
   myObjectsList = myTreeView->selectedObjects();
-  bool toEnable = myObjectsList.size() > 0;
-  foreach(QAction* aCmd, actions()) {
+  bool toEnable = myObjectsList.size() == 1;
+  foreach(QAction* aCmd, actions())
+  {
     aCmd->setEnabled(toEnable);
   }
   emit contextMenuRequested(theEvent);
@@ -278,12 +321,13 @@ void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
 {
   myObjectsList.clear();
   //Empty feature pointer means that selected root document
-  myObjectsList.append(ObjectPtr()); 
+  myObjectsList.append(ObjectPtr());
 
-  foreach(QAction* aCmd, actions()) {
+  foreach(QAction* aCmd, actions())
+  {
     aCmd->setEnabled(true);
   }
-  QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
+  QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
   emit contextMenuRequested(&aEvent);
 }
 
@@ -292,10 +336,11 @@ void XGUI_ObjectsBrowser::onEditItem()
 {
   if (myObjectsList.size() > 0) {
     ObjectPtr aFeature = myObjectsList.first();
-    if (aFeature) { // Selection happens in TreeView
+    if (aFeature) {  // Selection happens in TreeView
       // 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)) {
           aIndex = aIdx;
@@ -306,7 +351,7 @@ void XGUI_ObjectsBrowser::onEditItem()
         myTreeView->setCurrentIndex(aIndex);
         myTreeView->edit(aIndex);
       }
-    } else { //Selection happens in Upper label
+    } else {  //Selection happens in Upper label
       myActiveDocLbl->setReadOnly(false);
       myActiveDocLbl->setFocus();
       myActiveDocLbl->selectAll();
@@ -331,16 +376,31 @@ void XGUI_ObjectsBrowser::rebuildDataTree()
 }
 
 //***************************************************
-void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
+void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
 {
   QList<QModelIndex> theIndexes;
   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
   aSelectModel->clear();
 
-  foreach(ObjectPtr aFeature, theObjects) {
+  foreach(ObjectPtr aFeature, theObjects)
+  {
     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
     if (aIndex.isValid()) {
       aSelectModel->select(aIndex, QItemSelectionModel::Select);
     }
   }
 }
+
+//***************************************************
+void XGUI_ObjectsBrowser::processEvent(const std::shared_ptr<Events_Message>& theMessage)
+{ 
+  myDocModel->processEvent(theMessage); 
+}
+
+
+//***************************************************
+void XGUI_ObjectsBrowser::clearContent()  
+{ 
+  myObjectsList.clear();
+  myTreeView->clear(); 
+}