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