Salome HOME
Simple fix for 40160 [CEA] No name in SHAPER
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_ObjectsBrowser.h"
21 #include "XGUI_Tools.h"
22 #include "XGUI_DataModel.h"
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Document.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_ResultField.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_ResultGroup.h>
33 #include <ModelAPI_ResultField.h>
34
35 #include <ModuleBase_Tools.h>
36 #include <ModuleBase_ITreeNode.h>
37
38 #include <XGUI_Workshop.h>
39 #include <XGUI_Displayer.h>
40
41 #include <QLayout>
42 #include <QLineEdit>
43 #include <QPixmap>
44 #include <QEvent>
45 #include <QMouseEvent>
46 #include <QAction>
47 #include <QStyledItemDelegate>
48 #include <QMessageBox>
49 #include <QApplication>
50
51 #ifdef DEBUG_INDXES
52 #include <QToolTip>
53 #endif
54
55 /// Width of second column (minimum acceptable = 27)
56 #define FIRST_COL_WIDTH 20
57 #define SECOND_COL_WIDTH 30
58
59 /**
60 * \ingroup GUI
61 * Tree item delegate for definition of data in column items editor
62 */
63 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
64 {
65 public:
66   /// Constructor
67   /// \param theParent a parent of the delegate
68   XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
69     myTreedView(theParent) {}
70
71   /// Set data for item editor (name of the item)
72   /// \param editor a widget of editor
73   /// \param index the tree item index
74   virtual void  setEditorData ( QWidget* editor, const QModelIndex& index ) const
75   {
76     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
77     if (aEditor) {
78       XGUI_DataModel* aModel = myTreedView->dataModel();
79       ObjectPtr aObj = aModel->object(index);
80       if (aObj.get() != NULL) {
81         aEditor->setText(QString::fromStdWString(aObj->data()->name()));
82         return;
83       }
84     }
85     QStyledItemDelegate::setEditorData(editor, index);
86   }
87
88 private:
89   XGUI_DataTree* myTreedView;
90 };
91
92
93 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
94     : QTreeView(theParent)
95 {
96   setHeaderHidden(true);
97   setTreePosition(1);
98   setEditTriggers(QAbstractItemView::NoEditTriggers);
99   setSelectionBehavior(QAbstractItemView::SelectRows);
100   setSelectionMode(QAbstractItemView::ExtendedSelection);
101
102   // drag and drop
103   setDragEnabled(true);
104   setAcceptDrops(true);
105   viewport()->setAcceptDrops(true);
106   setDropIndicatorShown(true);
107   setDragDropMode(QAbstractItemView::InternalMove);
108
109   setItemDelegateForColumn(1, new XGUI_TreeViewItemDelegate(this));
110
111   connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
112     SLOT(onDoubleClick(const QModelIndex&)));
113 }
114
115 XGUI_DataTree::~XGUI_DataTree()
116 {
117 }
118
119 XGUI_DataModel* XGUI_DataTree::dataModel() const
120 {
121   return static_cast<XGUI_DataModel*>(model());
122 }
123
124 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
125 {
126   emit contextMenuRequested(theEvent);
127 }
128
129 void XGUI_DataTree::commitData(QWidget* theEditor)
130 {
131   static int aEntrance = 0;
132   if (aEntrance == 0) {
133     // We have to check number of enter and exit of this function because it can be called
134     // recursively by Qt in order to avoid double modifying of a data
135     aEntrance = 1;
136     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
137     if (aEditor) {
138       QString aName = aEditor->text();
139       QModelIndexList aIndexList = selectionModel()->selectedIndexes();
140       XGUI_DataModel* aModel = dataModel();
141       ObjectPtr aObj = aModel->object(aIndexList.first());
142
143       if (XGUI_Tools::canRename(aObj, aName)) {
144         SessionPtr aMgr = ModelAPI_Session::get();
145         aMgr->startOperation("Rename");
146         aObj->data()->setName(aName.toStdWString());
147         aMgr->finishOperation();
148       }
149     }
150   }
151   aEntrance = 0;
152 }
153
154 void XGUI_DataTree::clear()
155 {
156   dataModel()->clear();
157   reset();
158 }
159
160 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
161 {
162   QTreeView::resizeEvent(theEvent);
163   QSize aSize = theEvent->size();
164   if (aSize.isValid()) {
165     setColumnWidth(0, FIRST_COL_WIDTH);
166     setColumnWidth(1, aSize.width() - SECOND_COL_WIDTH - FIRST_COL_WIDTH - 10);
167     setColumnWidth(2, SECOND_COL_WIDTH);
168   }
169 }
170
171 #ifdef DEBUG_INDXES
172 void XGUI_DataTree::mousePressEvent(QMouseEvent* theEvent)
173 {
174   QTreeView::mousePressEvent(theEvent);
175   if (theEvent->button() != Qt::MidButton)
176     return;
177   QModelIndex aInd = indexAt(theEvent->pos());
178   QString aTxt =
179     QString("r=%1 c=%2 p=%3").arg(aInd.row()).arg(aInd.column()).arg((long)aInd.internalPointer());
180
181   QModelIndex aPar = aInd.parent();
182   QString aTxt1 =
183     QString("r=%1 c=%2 p=%3").arg(aPar.row()).arg(aPar.column()).arg((long)aPar.internalPointer());
184   QToolTip::showText(theEvent->globalPos(), aTxt + '\n' + aTxt1);
185 }
186 #endif
187
188 void XGUI_DataTree::mouseReleaseEvent(QMouseEvent* theEvent)
189 {
190   QTreeView::mouseReleaseEvent(theEvent);
191 #ifdef DEBUG_INDXES
192   if (theEvent->button() != Qt::MidButton)
193     return;
194   QToolTip::hideText();
195 #endif
196   if (theEvent->button() == Qt::LeftButton) {
197     QModelIndex aInd = indexAt(theEvent->pos());
198     if (aInd.column() == 0)
199       processEyeClick(aInd);
200   }
201 }
202
203 void XGUI_DataTree::highlightDependencies(const QModelIndex& theIndex)
204 {
205   XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
206   XGUI_Workshop* aWorkshop = aObjBrowser->workshop();
207
208   XGUI_DataModel* aModel = dataModel();
209   ObjectPtr aObj = aModel->object(theIndex);
210   if(!aObj)
211     return;
212
213   QObjectPtrList anObjs;
214   anObjs.append(aObj);
215   std::string aName = aObj->groupName();
216   if(aName == ModelAPI_ResultBody::group() ||
217      aName == ModelAPI_ResultConstruction::group() ||
218      aName == ModelAPI_ResultGroup::group() ||
219      aName == ModelAPI_ResultField::group())
220   {
221     aWorkshop->highlightFeature(anObjs);
222   }
223   else if(aName == ModelAPI_Feature::group())
224   {
225     aWorkshop->highlightResults(anObjs);
226   }
227 }
228
229 void XGUI_DataTree::processHistoryChange(const QModelIndex& theIndex)
230 {
231   SessionPtr aMgr = ModelAPI_Session::get();
232   // When operation is opened then we can not change history
233   if (aMgr->isOperation())
234     return;
235   XGUI_DataModel* aModel = dataModel();
236   if (aModel->flags(theIndex) == 0)
237     return;
238   ObjectPtr aObj = aModel->object(theIndex);
239
240   DocumentPtr aDoc = aMgr->activeDocument();
241
242   std::string aOpName = tr("History change").toStdString();
243   if (aObj.get()) {
244     if (aObj->document() != aDoc)
245       return;
246     aMgr->startOperation(aOpName);
247     aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
248     aMgr->finishOperation();
249   } else {
250     // Ignore clicks on folders outside current document
251     if ((theIndex.internalId() == 0) && (aDoc != aMgr->moduleDocument()))
252       // Clicked folder under root but active document is another
253       return;
254     if ((theIndex.internalId() != 0) && (aDoc != aModel->document(theIndex)))
255       // Cliced not on active document folder
256       return;
257
258     aMgr->startOperation(aOpName);
259     aDoc->setCurrentFeature(FeaturePtr(), true);
260     aMgr->finishOperation();
261   }
262   QModelIndex aParent = theIndex.parent();
263   int aSize = aModel->rowCount(aParent);
264   for (int i = 0; i < aSize; i++) {
265     update(aModel->index(i, 0, aParent));
266     update(aModel->index(i, 1, aParent));
267     update(aModel->index(i, 2, aParent));
268   }
269   XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
270   aObjBrowser->workshop()->displayer()->updateViewer();
271 }
272
273 void XGUI_DataTree::processEyeClick(const QModelIndex& theIndex)
274 {
275   XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
276   XGUI_DataModel* aModel = dataModel();
277   ObjectPtr aObj = aModel->object(theIndex);
278   if (aObj.get()) {
279     std::set<ObjectPtr> anObjects;
280     anObjects.insert(aObj);
281
282     bool hasHiddenState = aModel->hasHiddenState(theIndex);
283     if (aObjBrowser && hasHiddenState && !aObjBrowser->workshop()->prepareForDisplay(anObjects))
284       return;
285     if (hasHiddenState) { // #issue 2335(hide all faces then show solid problem)
286       if (aObj->isDisplayed())
287         aObj->setDisplayed(false);
288       aObj->setDisplayed(true);
289     }
290     else
291       aObj->setDisplayed(!aObj->isDisplayed());
292
293     // Update list of selected objects because this event happens after
294     // selection event in object browser
295     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
296     update(theIndex);
297     if (aObjBrowser) {
298       aObjBrowser->onSelectionChanged();
299     }
300   }
301 }
302
303 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
304 {
305   switch (theIndex.column()) {
306   case 1:
307     highlightDependencies(theIndex);
308     break;
309   case 2:
310     processHistoryChange(theIndex);
311     break;
312   }
313 }
314
315
316 //********************************************************************
317 //********************************************************************
318 //********************************************************************
319 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
320   : QLabel(theText, theParent),
321   myPreSelectionStyle(""),
322   myNeutralStyle(""),
323   mySelectionStyle(""),
324   myIsSelected(false)
325 {
326 }
327
328 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
329 {
330   myTreeView = theView;
331   setFont(myTreeView->font());
332
333   QPalette aPalet = myTreeView->palette();
334   QColor aHighlight = aPalet.highlight().color();
335   QColor aHighlightText = aPalet.highlightedText().color();
336
337   myPreSelectionStyle = "QLabel {background-color: ";
338   myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
339   //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
340   //  aHighlight.lighter(170).name() + ");";
341   //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
342
343   QString aName = aPalet.color(QPalette::Base).name();
344   myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
345
346
347 #if (!defined HAVE_SALOME) && (defined WIN32)
348   mySelectionStyle = "QLabel {background-color: ";
349   mySelectionStyle += "rgb(205, 232, 255); ";
350   //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
351   //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
352   //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
353   mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
354 #else
355   mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
356   mySelectionStyle += "; color : " + aHighlightText.name() + "}";
357 #endif
358
359   myTreeView->viewport()->installEventFilter(this);
360 }
361
362
363 #if (!defined HAVE_SALOME) && (defined WIN32)
364 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
365 {
366   switch (theEvent->type()) {
367     case QEvent::Enter:
368       if (!myIsSelected)
369         setStyleSheet(myPreSelectionStyle);
370       break;
371     case QEvent::Leave:
372       if (!myIsSelected)
373         setStyleSheet(myNeutralStyle);
374       break;
375   }
376   return QLabel::event(theEvent);
377 }
378 #endif
379
380 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
381 {
382   if (theObj == myTreeView->viewport()) {
383     if (theEvent->type() == QEvent::MouseButtonRelease)
384       unselect();
385   }
386   return QLabel::eventFilter(theObj, theEvent);
387 }
388
389 static bool MYClearing = false;
390 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
391 {
392   MYClearing = true;
393   myIsSelected = true;
394   setStyleSheet(mySelectionStyle);
395   // We can not block signals because on
396   // clear selection the View state will not be updated
397   myTreeView->clearSelection();
398   QLabel::mouseReleaseEvent(e);
399   MYClearing = false;
400 }
401
402 void XGUI_ActiveDocLbl::unselect()
403 {
404   if (!MYClearing) {
405     myIsSelected = false;
406     setStyleSheet(myNeutralStyle);
407   }
408 }
409
410
411 //********************************************************************
412 //********************************************************************
413 //********************************************************************
414 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop)
415     : QWidget(theParent), myDocModel(0), myWorkshop(theWorkshop)
416 {
417   QVBoxLayout* aLayout = new QVBoxLayout(this);
418   ModuleBase_Tools::zeroMargins(aLayout);
419   aLayout->setSpacing(0);
420
421   QWidget* aLabelWgt = new QWidget(this);
422   aLabelWgt->setAutoFillBackground(true);
423
424   aLayout->addWidget(aLabelWgt);
425   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
426   ModuleBase_Tools::zeroMargins(aLabelLay);
427   aLabelLay->setSpacing(0);
428
429   QLabel* aLbl = new QLabel(aLabelWgt);
430   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
431   aLbl->setMargin(2);
432   // Do not paint background of the label (in order to show icon)
433   aLbl->setAutoFillBackground(false);
434
435   aLabelLay->addWidget(aLbl);
436
437   SessionPtr aMgr = ModelAPI_Session::get();
438   DocumentPtr aDoc = aMgr->moduleDocument();
439
440   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
441 //  myActiveDocLbl->setReadOnly(true);
442 //  myActiveDocLbl->setFrame(false);
443   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
444
445   aLabelLay->addWidget(myActiveDocLbl);
446   aLabelLay->setStretch(1, 1);
447
448   myTreeView = new XGUI_DataTree(this);
449   myTreeView->setFrameShape(QFrame::NoFrame);
450   aLayout->addWidget(myTreeView);
451
452   QPalette aTreePalet = myTreeView->palette();
453   QColor aTreeBack = aTreePalet.color(QPalette::Base);
454
455   QPalette aPalet;
456   aPalet.setColor(QPalette::Base, aTreeBack);
457   aPalet.setColor(QPalette::Window, aTreeBack);
458   aLabelWgt->setPalette(aPalet);
459
460   myDocModel = new XGUI_DataModel(this);
461   connect(myDocModel, SIGNAL(beforeTreeRebuild()), SLOT(onBeforeReset()));
462   connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
463
464   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
465           SLOT(onContextMenuRequested(QContextMenuEvent*)));
466 }
467
468 //***************************************************
469 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
470 {
471 }
472
473 void XGUI_ObjectsBrowser::initialize(ModuleBase_ITreeNode* theRoot)
474 {
475   //myDocModel->setXMLReader(theReader);
476   myDocModel->setRoot(theRoot);
477   myTreeView->setModel(myDocModel);
478
479   // It has to be done after setting of model
480   myActiveDocLbl->setTreeView(myTreeView);
481
482   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
483   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
484           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
485 }
486
487 //***************************************************
488 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
489 {
490   QModelIndexList aIndexes;
491   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
492   bool toEnable = false;
493
494   if (aSelectedData.size() == 1) {
495     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
496     if (!aIndexes.contains(aSelected))
497       return; // menu is called on non selected item
498
499     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
500     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
501   }
502   foreach(QAction* aCmd, actions()) {
503     aCmd->setEnabled(toEnable);
504   }
505   emit contextMenuRequested(theEvent);
506 }
507
508 //***************************************************
509 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
510 {
511   myTreeView->selectionModel()->clearSelection();
512   //Empty feature pointer means that selected root document
513   foreach(QAction* aCmd, actions()) {
514     aCmd->setEnabled(true);
515   }
516   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
517   emit contextMenuRequested(&aEvent);
518 }
519
520 //***************************************************
521 void XGUI_ObjectsBrowser::onEditItem()
522 {
523   QObjectPtrList aSelectedData = selectedObjects();
524   if (aSelectedData.size() > 0) {
525     ObjectPtr anObject = aSelectedData.first();
526     if (anObject.get()) {  // Selection happens in TreeView
527       // check whether the object can be renamed. There should not be parts which are not loaded
528       std::set<FeaturePtr> aFeatures;
529       aFeatures.insert(ModelAPI_Feature::feature(anObject));
530       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
531         return;
532
533       // Find index which corresponds the feature
534       QModelIndex aIndex;
535       foreach(QModelIndex aIdx, selectedIndexes()) {
536         if (aIdx.column() == 1) {
537           ObjectPtr aFea = dataModel()->object(aIdx);
538           if (dataModel()->object(aIdx)->isSame(anObject)) {
539             aIndex = aIdx;
540             break;
541           }
542         }
543       }
544       if (aIndex.isValid()) {
545         myTreeView->setCurrentIndex(aIndex);
546         myTreeView->edit(aIndex);
547       }
548       return;
549     }
550   }
551 }
552
553 //***************************************************
554 QList<ModuleBase_ITreeNode*> XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
555 {
556   QList<ModuleBase_ITreeNode*> aIndexes;
557   QModelIndex aIndex;
558   int aCount = myDocModel->rowCount(theParent);
559   for (int i = 0; i < aCount; i++) {
560     aIndex = myDocModel->index(i, 0, theParent);
561     if (myDocModel->hasChildren(aIndex)) {
562       if (myTreeView->isExpanded(aIndex)) {
563         aIndexes.append((ModuleBase_ITreeNode*)aIndex.internalPointer());
564         QList<ModuleBase_ITreeNode*> aSubIndexes = expandedItems(aIndex);
565         if (!aSubIndexes.isEmpty())
566           aIndexes.append(aSubIndexes);
567       }
568     }
569   }
570   return aIndexes;
571 }
572
573 //***************************************************
574 void XGUI_ObjectsBrowser::rebuildDataTree()
575 {
576   myDocModel->root()->update();
577   myDocModel->rebuildDataTree();
578   update();
579 }
580
581 //***************************************************
582 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
583 {
584   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
585   QModelIndexList aIndexes = aSelectModel->selectedIndexes();
586   if (theObjects.size() == 0) {
587     bool aIsBlock = aSelectModel->blockSignals(true);
588     aSelectModel->clear();
589     aSelectModel->blockSignals(aIsBlock);
590     foreach(QModelIndex aIdx, aIndexes) {
591       myTreeView->update(aIdx);
592     }
593     return;
594   }
595
596   ObjectPtr aObject;
597   QModelIndexList aUnselect;
598   QObjectPtrList aToSelect = theObjects;
599   QHash<qint64, ObjectPtr> aNotChanged;
600   foreach(QModelIndex aIdx, aIndexes) {
601     aObject = myDocModel->object(aIdx);
602     if (aObject.get()) {
603       if (aToSelect.contains(aObject)) {
604         aNotChanged.insert((qint64)aObject.get(), aObject);
605       } else {
606         aUnselect.append(aIdx);
607       }
608     }
609     else {
610       aUnselect.append(aIdx);
611     }
612   }
613
614   foreach(ObjectPtr aObj, aNotChanged)
615     aToSelect.removeAll(aObj);
616
617   bool aIsBlock = aSelectModel->blockSignals(true);
618   foreach(QModelIndex aIdx, aUnselect) {
619     aSelectModel->select(aIdx, QItemSelectionModel::Deselect);
620     myTreeView->update(aIdx);
621   }
622
623   QModelIndex aIndex0, aIndex1, aIndex2, aCurrent;
624   foreach(ObjectPtr aFeature, aToSelect) {
625     aIndex1 = myDocModel->objectIndex(aFeature, 1);
626     if (aIndex1.isValid()) {
627       if (!aCurrent.isValid())
628         aCurrent = aIndex1;
629       aIndex0 = myDocModel->objectIndex(aFeature, 0);
630       aIndex2 = myDocModel->objectIndex(aFeature, 2);
631       aSelectModel->select(aIndex1, QItemSelectionModel::Select | QItemSelectionModel::Rows);
632       myTreeView->update(aIndex0);
633       myTreeView->update(aIndex1);
634       myTreeView->update(aIndex2);
635     }
636   }
637   aSelectModel->setCurrentIndex(aCurrent, QItemSelectionModel::NoUpdate);
638   aSelectModel->blockSignals(aIsBlock);
639 }
640
641 //***************************************************
642 void XGUI_ObjectsBrowser::ensureVisible(const ObjectPtr theObject)
643 {
644   QModelIndex aIndex = myDocModel->objectIndex(theObject);
645   if (aIndex.isValid())  {
646     QModelIndex aParent = aIndex.parent();
647     while (aParent.isValid()) {
648       myTreeView->expand(aParent);
649       aParent = aParent.parent();
650     }
651     myTreeView->scrollTo(aIndex);
652   }
653 }
654
655 //***************************************************
656 void XGUI_ObjectsBrowser::clearContent()
657 {
658   myTreeView->clear();
659 }
660
661 //***************************************************
662 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& /*theSelected*/,
663                                              const QItemSelection& /*theDeselected*/)
664 {
665   onSelectionChanged();
666 }
667
668 //***************************************************
669 void XGUI_ObjectsBrowser::onSelectionChanged()
670 {
671   emit selectionChanged();
672 }
673
674 //***************************************************
675 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
676 {
677   QObjectPtrList aList;
678   QModelIndexList aIndexes = selectedIndexes();
679   XGUI_DataModel* aModel = dataModel();
680
681   foreach(QModelIndex aIdx, aIndexes) {
682     if (aIdx.column() == 1) {
683       ObjectPtr aObject = aModel->object(aIdx);
684       if (aObject) {
685         aList.append(aObject);
686         if (theIndexes)
687           theIndexes->append(aIdx);
688       }
689     }
690   }
691   return aList;
692 }
693
694 void XGUI_ObjectsBrowser::onBeforeReset()
695 {
696   myExpandedItems = expandedItems();
697 }
698
699 void XGUI_ObjectsBrowser::onAfterModelReset()
700 {
701   XGUI_DataModel* aModel = myTreeView->dataModel();
702   QModelIndex aIndex;
703   foreach(ModuleBase_ITreeNode* aNode, myExpandedItems) {
704     if (aModel->hasNode(aNode)) {
705       aIndex = aModel->getIndex(aNode, 0);
706       if (aIndex.isValid() && (myTreeView->dataModel()->hasIndex(aIndex)))
707         myTreeView->setExpanded(aIndex, true);
708     }
709   }
710   myExpandedItems.clear();
711 }
712
713 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
714 {
715   std::list<bool> aStates;
716   XGUI_DataModel* aModel = dataModel();
717   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
718   int aNbChild = aModel->rowCount(aRootIdx);
719   for (int i = 0; i < aNbChild; i++) {
720     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
721     aStates.push_back(myTreeView->isExpanded(aIdx));
722   }
723   return aStates;
724 }
725
726 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
727 {
728   if (theStates.size() == 0)
729     return;
730   XGUI_DataModel* aModel = dataModel();
731   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
732   int aNbChild = aModel->rowCount(aRootIdx);
733
734   std::list<bool>::const_iterator aIt;
735   int i = 0;
736   for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
737     if (i >= aNbChild )
738       break;
739     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
740     myTreeView->setExpanded(aIdx, (*aIt));
741   }
742 }
743
744 void XGUI_ObjectsBrowser::updateAllIndexes(int theColumn, const QModelIndex& theParent)
745 {
746   int aNb = myDocModel->rowCount(theParent);
747   for (int i = 0; i < aNb; i++) {
748     QModelIndex aIdx = myDocModel->index(i, theColumn, theParent);
749     if (aIdx.isValid() && myDocModel->hasIndex(aIdx)) {
750       myTreeView->update(aIdx);
751       if (myTreeView->isExpanded(aIdx))
752         updateAllIndexes(theColumn, aIdx);
753     }
754   }
755 }
756
757 QMap<ObjectPtr, bool> XGUI_ObjectsBrowser::getFoldersState(DocumentPtr theDoc) const
758 {
759   QMap<ObjectPtr, bool> aMap;
760
761   int aNb = theDoc->size(ModelAPI_Folder::group());
762   ObjectPtr aObj;
763   for (int i = 0; i < aNb; i++) {
764     aObj = theDoc->object(ModelAPI_Folder::group(), i);
765     QModelIndex aIdx = myDocModel->objectIndex(aObj, 0);
766     aMap[aObj] = myTreeView->isExpanded(aIdx);
767   }
768   return aMap;
769 }
770
771 void XGUI_ObjectsBrowser::setFoldersState(const QMap<ObjectPtr, bool>& theStates)
772 {
773   QMap<ObjectPtr, bool>::const_iterator aIt;
774   for (aIt = theStates.constBegin(); aIt != theStates.constEnd(); aIt++) {
775     QModelIndex aIdx = myDocModel->objectIndex(aIt.key(), 0);
776     myTreeView->setExpanded(aIdx, aIt.value());
777   }
778 }
779
780
781 void XGUI_ObjectsBrowser::resizeEvent(QResizeEvent* theEvent)
782 {
783   QWidget::resizeEvent(theEvent);
784   emit sizeChanged();
785 }