Salome HOME
Merge branch 'master' of salome:modules/shaper
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
index 147d0c3beb9ccf86155b07d74647d85169a6e3d3..7d6410a49eed6162e1e89f6cb7a3aaa89ab905e3 100644 (file)
@@ -13,7 +13,7 @@
 #include <ModuleBase_Tools.h>
 
 #include <QLayout>
-#include <QLabel>
+#include <QLineEdit>
 #include <QPixmap>
 #include <QEvent>
 #include <QMouseEvent>
 #include <QStyledItemDelegate>
 #include <QMessageBox>
 
+#ifdef WIN32
+#ifdef HAVE_SALOME
+#include <QWindowsStyle>
+#endif
+#endif
+
+
 /// Width of second column (minimum acceptable = 27)
 #define SECOND_COL_WIDTH 30
 
@@ -61,6 +68,15 @@ private:
 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
     : QTreeView(theParent)
 {
+#ifdef WIN32
+#ifdef HAVE_SALOME
+  setStyle(new QWindowsStyle());
+#else
+  myStyle = new XGUI_TreeViewStyle();
+  setStyle(myStyle);
+#endif
+#endif
+
   setHeaderHidden(true);
   setEditTriggers(QAbstractItemView::NoEditTriggers);
   setSelectionBehavior(QAbstractItemView::SelectRows);
@@ -124,6 +140,7 @@ void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
     setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
     setColumnWidth(1, SECOND_COL_WIDTH);
   }
+  QTreeView::resizeEvent(theEvent);
 }
 
 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
@@ -166,14 +183,48 @@ void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
   int aSize = aModel->rowCount(aParent);
   for (int i = 0; i < aSize; i++) {
     update(aModel->index(i, 0, aParent));
+    update(aModel->index(i, 1, aParent));
+  }
+}
+
+#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;
+      QWindowsVistaStyle::drawPrimitive(theElement, aOpt, thePainter, theWidget);
+    }
   }
+  QWindowsVistaStyle::drawPrimitive(theElement, theOption, thePainter, theWidget);
 }
+#endif
+
 
 //********************************************************************
 //********************************************************************
 //********************************************************************
 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
-  : QLineEdit(theText, theParent), 
+  : QLabel(theText, theParent), 
   myPreSelectionStyle(""), 
   myNeutralStyle(""), 
   mySelectionStyle(""),
@@ -184,25 +235,35 @@ XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent
 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 = "QLineEdit {background-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 += "border: 1px solid lightblue; border-radius: 2px }";
 
   QString aName = aPalet.color(QPalette::Base).name();
-  myNeutralStyle = "QLineEdit { border: 1px solid " + aName + " }";
+  myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
+
 
-  mySelectionStyle = "QLineEdit {background-color: ";
-  mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 " + aHighlight.lighter(170).name();
-  mySelectionStyle += ", stop:1 " + aHighlight.lighter().name() + ");"; 
-  mySelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
+#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 += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
+#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()) {
@@ -215,8 +276,9 @@ bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
         setStyleSheet(myNeutralStyle);
       break;
   }
-  return QLineEdit::event(theEvent);
+  return QLabel::event(theEvent);
 }
+#endif
 
 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
 {
@@ -224,7 +286,7 @@ bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
     if (theEvent->type() == QEvent::MouseButtonRelease)
       unselect();
   }
-  return QLineEdit::eventFilter(theObj, theEvent);
+  return QLabel::eventFilter(theObj, theEvent);
 }
 
 static bool MYClearing = false;
@@ -236,7 +298,7 @@ void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
   // We can not block signals because on 
   // clear selection the View state will not be updated
   myTreeView->clearSelection();
-  QLineEdit::mouseReleaseEvent(e);
+  QLabel::mouseReleaseEvent(e);
   MYClearing = false;
 }
 
@@ -279,8 +341,8 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
   DocumentPtr aDoc = aMgr->moduleDocument();
 
   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
-  myActiveDocLbl->setReadOnly(true);
-  myActiveDocLbl->setFrame(false);
+//  myActiveDocLbl->setReadOnly(true);
+//  myActiveDocLbl->setFrame(false);
   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
 
   aLabelLay->addWidget(myActiveDocLbl);
@@ -299,14 +361,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*)));
@@ -317,6 +373,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)
@@ -382,6 +450,25 @@ void XGUI_ObjectsBrowser::onEditItem()
   }
 }
 
+//***************************************************
+QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
+{
+  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;
+}
+
 //***************************************************
 void XGUI_ObjectsBrowser::rebuildDataTree()
 {
@@ -434,4 +521,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);
+  }
+}