Salome HOME
Workaround for Debian 6.0 (issues #1868 and #1878)
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
index 7eb50de3409ce79f2c8a48199da60345c0a26c93..4512cf1feea0b1ade3e5fa919b5b8561ebb20cd1 100644 (file)
@@ -8,7 +8,6 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Tools.h>
-#include <Events_Error.h>
 
 #include <ModuleBase_Tools.h>
 
@@ -41,7 +40,8 @@ class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
 public:
   /// Constructor
   /// \param theParent a parent of the delegate
-  XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
+  XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
+    myTreedView(theParent) {}
 
   /// Set data for item editor (name of the item)
   /// \param editor a widget of editor
@@ -84,7 +84,7 @@ XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
 
   setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
 
-  connect(this, SIGNAL(doubleClicked(const QModelIndex&)), 
+  connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
     SLOT(onDoubleClick(const QModelIndex&)));
 }
 
@@ -106,8 +106,8 @@ void XGUI_DataTree::commitData(QWidget* theEditor)
 {
   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
+    // 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) {
@@ -127,7 +127,7 @@ void XGUI_DataTree::commitData(QWidget* theEditor)
   aEntrance = 0;
 }
 
-void XGUI_DataTree::clear() 
+void XGUI_DataTree::clear()
 {
   dataModel()->clear();
   reset();
@@ -135,12 +135,12 @@ void XGUI_DataTree::clear()
 
 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
 {
+  QTreeView::resizeEvent(theEvent);
   QSize aSize = theEvent->size();
   if (aSize.isValid()) {
-    setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
+    setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH - 6);
     setColumnWidth(1, SECOND_COL_WIDTH);
   }
-  QTreeView::resizeEvent(theEvent);
 }
 
 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
@@ -157,7 +157,7 @@ void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
   ObjectPtr aObj = aModel->object(theIndex);
 
   DocumentPtr aDoc = aMgr->activeDocument();
-  
+
   std::string aOpName = tr("History change").toStdString();
   if (aObj.get()) {
     if (aObj->document() != aDoc)
@@ -200,12 +200,13 @@ void XGUI_DataTree::drawRow(QPainter* thePainter,
 //********************************************************************
 //********************************************************************
 //********************************************************************
-void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement, 
+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);
+    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)
@@ -224,9 +225,9 @@ void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement,
 //********************************************************************
 //********************************************************************
 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
-  : QLabel(theText, theParent), 
-  myPreSelectionStyle(""), 
-  myNeutralStyle(""), 
+  : QLabel(theText, theParent),
+  myPreSelectionStyle(""),
+  myNeutralStyle(""),
   mySelectionStyle(""),
   myIsSelected(false)
 {
@@ -242,7 +243,8 @@ void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
   QColor aHighlightText = aPalet.highlightedText().color();
 
   myPreSelectionStyle = "QLabel {background-color: ";
-  myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + 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();
@@ -252,7 +254,7 @@ void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
 #if (!defined HAVE_SALOME) && (defined WIN32)
   mySelectionStyle = "QLabel {background-color: ";
   mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
-  mySelectionStyle += ", stop:1 rgb(208, 229, 255));"; 
+  mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
   mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
 #else
   mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
@@ -295,7 +297,7 @@ void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
   MYClearing = true;
   myIsSelected = true;
   setStyleSheet(mySelectionStyle);
-  // We can not block signals because on 
+  // We can not block signals because on
   // clear selection the View state will not be updated
   myTreeView->clearSelection();
   QLabel::mouseReleaseEvent(e);
@@ -333,7 +335,7 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
   aLbl->setMargin(2);
   // Do not paint background of the label (in order to show icon)
-  aLbl->setAutoFillBackground(false); 
+  aLbl->setAutoFillBackground(false);
 
   aLabelLay->addWidget(aLbl);
 
@@ -361,14 +363,8 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
   aLabelWgt->setPalette(aPalet);
 
   myDocModel = new XGUI_DataModel(this);
-  myTreeView->setModel(myDocModel);
-
-  // It has to be done after setting of model
-  myActiveDocLbl->setTreeView(myTreeView);
-
-  QItemSelectionModel* aSelMod = myTreeView->selectionModel();
-  connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
-          this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
+  connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
+  connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
 
   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
           SLOT(onContextMenuRequested(QContextMenuEvent*)));
@@ -379,6 +375,18 @@ XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
 {
 }
 
+void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
+{
+  myDocModel->setXMLReader(theReader);
+  myTreeView->setModel(myDocModel);
+
+  // It has to be done after setting of model
+  myActiveDocLbl->setTreeView(myTreeView);
+
+  QItemSelectionModel* aSelMod = myTreeView->selectionModel();
+  connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
+          this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
+}
 
 //***************************************************
 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
@@ -418,19 +426,19 @@ void XGUI_ObjectsBrowser::onEditItem()
 {
   QObjectPtrList aSelectedData = selectedObjects();
   if (aSelectedData.size() > 0) {
-    ObjectPtr aFeature = aSelectedData.first();
-    if (aFeature) {  // Selection happens in TreeView
-      QObjectPtrList aList;
-      aList.append(aFeature);
-      // check whether the object can be deleted. There should not be parts which are not loaded
-      if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
+    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()) {
         ObjectPtr aFea = dataModel()->object(aIdx);
-        if (dataModel()->object(aIdx)->isSame(aFeature)) {
+        if (dataModel()->object(aIdx)->isSame(anObject)) {
           aIndex = aIdx;
           break;
         }
@@ -449,7 +457,7 @@ QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent)
 {
   QModelIndexList aIndexes;
   QModelIndex aIndex;
-  for (int i = 0; i < myDocModel->rowCount(); i++) {
+  for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
     aIndex = myDocModel->index(i, 0, theParent);
     if (myDocModel->hasChildren(aIndex)) {
       if (myTreeView->isExpanded(aIndex)) {
@@ -466,11 +474,7 @@ QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent)
 //***************************************************
 void XGUI_ObjectsBrowser::rebuildDataTree()
 {
-  QModelIndexList aIndexList = expandedItems();
   myDocModel->rebuildDataTree();
-  foreach(QModelIndex aIndex, aIndexList) {
-    myTreeView->setExpanded(aIndex, true);
-  }
   update();
 }
 
@@ -491,9 +495,9 @@ void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
 }
 
 //***************************************************
-void XGUI_ObjectsBrowser::clearContent()  
-{ 
-  myTreeView->clear(); 
+void XGUI_ObjectsBrowser::clearContent()
+{
+  myTreeView->clear();
 }
 
 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
@@ -519,4 +523,16 @@ QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes)
     }
   }
   return aList;
-}
\ No newline at end of file
+}
+
+void XGUI_ObjectsBrowser::onBeforeReset()
+{
+  myExpandedItems = expandedItems();
+}
+
+void XGUI_ObjectsBrowser::onAfterModelReset()
+{
+  foreach(QModelIndex aIndex, myExpandedItems) {
+    myTreeView->setExpanded(aIndex, true);
+  }
+}