Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[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
29 #include <ModuleBase_Tools.h>
30 #include <ModuleBase_ITreeNode.h>
31
32 #include <XGUI_Workshop.h>
33
34 #include <QLayout>
35 #include <QLineEdit>
36 #include <QPixmap>
37 #include <QEvent>
38 #include <QMouseEvent>
39 #include <QAction>
40 #include <QStyledItemDelegate>
41 #include <QMessageBox>
42 #include <QApplication>
43
44 #ifdef DEBUG_INDXES
45 #include <QToolTip>
46 #endif
47
48 /// Width of second column (minimum acceptable = 27)
49 #define FIRST_COL_WIDTH 20
50 #define SECOND_COL_WIDTH 30
51
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   XGUI_DataModel* aModel = dataModel();
235   ObjectPtr aObj = aModel->object(theIndex);
236   if (aObj.get()) {
237     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
238     XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
239     if (aResObj.get()) {
240       std::set<ObjectPtr> anObjects;
241       anObjects.insert(aResObj);
242
243       bool hasHiddenState = aModel->hasHiddenState(theIndex);
244       if (aObjBrowser && hasHiddenState && !aObjBrowser->workshop()->prepareForDisplay(anObjects))
245         return;
246       if (hasHiddenState) { // #issue 2335(hide all faces then show solid problem)
247         if (aResObj->isDisplayed())
248           aResObj->setDisplayed(false);
249         aResObj->setDisplayed(true);
250       }
251       else
252         aResObj->setDisplayed(!aResObj->isDisplayed());
253       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
254       update(theIndex);
255     }
256     // Update list of selected objects because this event happens after
257     // selection event in object browser
258     if (aObjBrowser) {
259       aObjBrowser->onSelectionChanged();
260     }
261   }
262 }
263
264 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
265 {
266   switch (theIndex.column()) {
267   case 2:
268     processHistoryChange(theIndex);
269     break;
270   }
271 }
272
273
274 //********************************************************************
275 //********************************************************************
276 //********************************************************************
277 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
278   : QLabel(theText, theParent),
279   myPreSelectionStyle(""),
280   myNeutralStyle(""),
281   mySelectionStyle(""),
282   myIsSelected(false)
283 {
284 }
285
286 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
287 {
288   myTreeView = theView;
289   setFont(myTreeView->font());
290
291   QPalette aPalet = myTreeView->palette();
292   QColor aHighlight = aPalet.highlight().color();
293   QColor aHighlightText = aPalet.highlightedText().color();
294
295   myPreSelectionStyle = "QLabel {background-color: ";
296   myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
297   //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
298   //  aHighlight.lighter(170).name() + ");";
299   //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
300
301   QString aName = aPalet.color(QPalette::Base).name();
302   myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
303
304
305 #if (!defined HAVE_SALOME) && (defined WIN32)
306   mySelectionStyle = "QLabel {background-color: ";
307   mySelectionStyle += "rgb(205, 232, 255); ";
308   //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
309   //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
310   //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
311   mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
312 #else
313   mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
314   mySelectionStyle += "; color : " + aHighlightText.name() + "}";
315 #endif
316
317   myTreeView->viewport()->installEventFilter(this);
318 }
319
320
321 #if (!defined HAVE_SALOME) && (defined WIN32)
322 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
323 {
324   switch (theEvent->type()) {
325     case QEvent::Enter:
326       if (!myIsSelected)
327         setStyleSheet(myPreSelectionStyle);
328       break;
329     case QEvent::Leave:
330       if (!myIsSelected)
331         setStyleSheet(myNeutralStyle);
332       break;
333   }
334   return QLabel::event(theEvent);
335 }
336 #endif
337
338 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
339 {
340   if (theObj == myTreeView->viewport()) {
341     if (theEvent->type() == QEvent::MouseButtonRelease)
342       unselect();
343   }
344   return QLabel::eventFilter(theObj, theEvent);
345 }
346
347 static bool MYClearing = false;
348 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
349 {
350   MYClearing = true;
351   myIsSelected = true;
352   setStyleSheet(mySelectionStyle);
353   // We can not block signals because on
354   // clear selection the View state will not be updated
355   myTreeView->clearSelection();
356   QLabel::mouseReleaseEvent(e);
357   MYClearing = false;
358 }
359
360 void XGUI_ActiveDocLbl::unselect()
361 {
362   if (!MYClearing) {
363     myIsSelected = false;
364     setStyleSheet(myNeutralStyle);
365   }
366 }
367
368
369 //********************************************************************
370 //********************************************************************
371 //********************************************************************
372 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop)
373     : QWidget(theParent), myDocModel(0), myWorkshop(theWorkshop)
374 {
375   QVBoxLayout* aLayout = new QVBoxLayout(this);
376   ModuleBase_Tools::zeroMargins(aLayout);
377   aLayout->setSpacing(0);
378
379   QWidget* aLabelWgt = new QWidget(this);
380   aLabelWgt->setAutoFillBackground(true);
381
382   aLayout->addWidget(aLabelWgt);
383   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
384   ModuleBase_Tools::zeroMargins(aLabelLay);
385   aLabelLay->setSpacing(0);
386
387   QLabel* aLbl = new QLabel(aLabelWgt);
388   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
389   aLbl->setMargin(2);
390   // Do not paint background of the label (in order to show icon)
391   aLbl->setAutoFillBackground(false);
392
393   aLabelLay->addWidget(aLbl);
394
395   SessionPtr aMgr = ModelAPI_Session::get();
396   DocumentPtr aDoc = aMgr->moduleDocument();
397
398   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
399 //  myActiveDocLbl->setReadOnly(true);
400 //  myActiveDocLbl->setFrame(false);
401   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
402
403   aLabelLay->addWidget(myActiveDocLbl);
404   aLabelLay->setStretch(1, 1);
405
406   myTreeView = new XGUI_DataTree(this);
407   myTreeView->setFrameShape(QFrame::NoFrame);
408   aLayout->addWidget(myTreeView);
409
410   QPalette aTreePalet = myTreeView->palette();
411   QColor aTreeBack = aTreePalet.color(QPalette::Base);
412
413   QPalette aPalet;
414   aPalet.setColor(QPalette::Base, aTreeBack);
415   aPalet.setColor(QPalette::Window, aTreeBack);
416   aLabelWgt->setPalette(aPalet);
417
418   myDocModel = new XGUI_DataModel(this);
419   connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
420   connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
421
422   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
423           SLOT(onContextMenuRequested(QContextMenuEvent*)));
424 }
425
426 //***************************************************
427 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
428 {
429 }
430
431 void XGUI_ObjectsBrowser::initialize(ModuleBase_ITreeNode* theRoot)
432 {
433   //myDocModel->setXMLReader(theReader);
434   myDocModel->setRoot(theRoot);
435   myTreeView->setModel(myDocModel);
436
437   // It has to be done after setting of model
438   myActiveDocLbl->setTreeView(myTreeView);
439
440   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
441   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
442           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
443 }
444
445 //***************************************************
446 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
447 {
448   QModelIndexList aIndexes;
449   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
450   bool toEnable = false;
451
452   if (aSelectedData.size() == 1) {
453     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
454     if (!aIndexes.contains(aSelected))
455       return; // menu is called on non selected item
456
457     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
458     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
459   }
460   foreach(QAction* aCmd, actions()) {
461     aCmd->setEnabled(toEnable);
462   }
463   emit contextMenuRequested(theEvent);
464 }
465
466 //***************************************************
467 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
468 {
469   myTreeView->selectionModel()->clearSelection();
470   //Empty feature pointer means that selected root document
471   foreach(QAction* aCmd, actions()) {
472     aCmd->setEnabled(true);
473   }
474   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
475   emit contextMenuRequested(&aEvent);
476 }
477
478 //***************************************************
479 void XGUI_ObjectsBrowser::onEditItem()
480 {
481   QObjectPtrList aSelectedData = selectedObjects();
482   if (aSelectedData.size() > 0) {
483     ObjectPtr anObject = aSelectedData.first();
484     if (anObject.get()) {  // Selection happens in TreeView
485       // check whether the object can be renamed. There should not be parts which are not loaded
486       std::set<FeaturePtr> aFeatures;
487       aFeatures.insert(ModelAPI_Feature::feature(anObject));
488       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
489         return;
490
491       // Find index which corresponds the feature
492       QModelIndex aIndex;
493       foreach(QModelIndex aIdx, selectedIndexes()) {
494         if (aIdx.column() == 1) {
495           ObjectPtr aFea = dataModel()->object(aIdx);
496           if (dataModel()->object(aIdx)->isSame(anObject)) {
497             aIndex = aIdx;
498             break;
499           }
500         }
501       }
502       if (aIndex.isValid()) {
503         myTreeView->setCurrentIndex(aIndex);
504         myTreeView->edit(aIndex);
505       }
506       return;
507     }
508   }
509 }
510
511 //***************************************************
512 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
513 {
514   QModelIndexList aIndexes;
515   QModelIndex aIndex;
516   for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
517     aIndex = myDocModel->index(i, 0, theParent);
518     if (myDocModel->hasChildren(aIndex)) {
519       if (myTreeView->isExpanded(aIndex)) {
520         aIndexes.append(aIndex);
521         QModelIndexList aSubIndexes = expandedItems(aIndex);
522         if (!aSubIndexes.isEmpty())
523           aIndexes.append(aSubIndexes);
524       }
525     }
526   }
527   return aIndexes;
528 }
529
530 //***************************************************
531 void XGUI_ObjectsBrowser::rebuildDataTree()
532 {
533   myDocModel->root()->update();
534   myDocModel->rebuildDataTree();
535   update();
536 }
537
538 //***************************************************
539 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
540 {
541   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
542   QModelIndexList aIndexes = aSelectModel->selectedIndexes();
543   if (theObjects.size() == 0) {
544     bool aIsBlock = aSelectModel->blockSignals(true);
545     aSelectModel->clear();
546     aSelectModel->blockSignals(aIsBlock);
547     foreach(QModelIndex aIdx, aIndexes) {
548       myTreeView->update(aIdx);
549     }
550     return;
551   }
552
553   ObjectPtr aObject;
554   QModelIndexList aUnselect;
555   QObjectPtrList aToSelect = theObjects;
556   QHash<qint64, ObjectPtr> aNotChanged;
557   foreach(QModelIndex aIdx, aIndexes) {
558     aObject = myDocModel->object(aIdx);
559     if (aObject.get()) {
560       if (aToSelect.contains(aObject)) {
561         aNotChanged.insert((qint64)aObject.get(), aObject);
562       } else {
563         aUnselect.append(aIdx);
564       }
565     }
566     else {
567       aUnselect.append(aIdx);
568     }
569   }
570
571   foreach(ObjectPtr aObj, aNotChanged)
572     aToSelect.removeAll(aObj);
573
574   bool aIsBlock = aSelectModel->blockSignals(true);
575   foreach(QModelIndex aIdx, aUnselect) {
576     aSelectModel->select(aIdx, QItemSelectionModel::Deselect);
577     myTreeView->update(aIdx);
578   }
579
580   QModelIndex aIndex0, aIndex1, aIndex2, aCurrent;
581   foreach(ObjectPtr aFeature, aToSelect) {
582     aIndex1 = myDocModel->objectIndex(aFeature, 1);
583     if (aIndex1.isValid()) {
584       if (!aCurrent.isValid())
585         aCurrent = aIndex1;
586       aIndex0 = myDocModel->objectIndex(aFeature, 0);
587       aIndex2 = myDocModel->objectIndex(aFeature, 2);
588       aSelectModel->select(aIndex1, QItemSelectionModel::Select | QItemSelectionModel::Rows);
589       myTreeView->update(aIndex0);
590       myTreeView->update(aIndex1);
591       myTreeView->update(aIndex2);
592     }
593   }
594   aSelectModel->setCurrentIndex(aCurrent, QItemSelectionModel::NoUpdate);
595   aSelectModel->blockSignals(aIsBlock);
596 }
597
598 //***************************************************
599 void XGUI_ObjectsBrowser::ensureVisible(const ObjectPtr theObject)
600 {
601   QModelIndex aIndex = myDocModel->objectIndex(theObject);
602   if (aIndex.isValid())  {
603     QModelIndex aParent = aIndex.parent();
604     while (aParent.isValid()) {
605       myTreeView->expand(aParent);
606       aParent = aParent.parent();
607     }
608     myTreeView->scrollTo(aIndex);
609   }
610 }
611
612 //***************************************************
613 void XGUI_ObjectsBrowser::clearContent()
614 {
615   myTreeView->clear();
616 }
617
618 //***************************************************
619 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
620                                        const QItemSelection& theDeselected)
621 {
622   onSelectionChanged();
623 }
624
625 //***************************************************
626 void XGUI_ObjectsBrowser::onSelectionChanged()
627 {
628   emit selectionChanged();
629 }
630
631 //***************************************************
632 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
633 {
634   QObjectPtrList aList;
635   QModelIndexList aIndexes = selectedIndexes();
636   XGUI_DataModel* aModel = dataModel();
637
638   foreach(QModelIndex aIdx, aIndexes) {
639     if (aIdx.column() == 1) {
640       ObjectPtr aObject = aModel->object(aIdx);
641       if (aObject) {
642         aList.append(aObject);
643         if (theIndexes)
644           theIndexes->append(aIdx);
645       }
646     }
647   }
648   return aList;
649 }
650
651 void XGUI_ObjectsBrowser::onBeforeReset()
652 {
653   myExpandedItems = expandedItems();
654 }
655
656 void XGUI_ObjectsBrowser::onAfterModelReset()
657 {
658   foreach(QModelIndex aIndex, myExpandedItems) {
659     if (myTreeView->dataModel()->hasIndex(aIndex))
660       myTreeView->setExpanded(aIndex, true);
661   }
662 }
663
664 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
665 {
666   std::list<bool> aStates;
667   XGUI_DataModel* aModel = dataModel();
668   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
669   int aNbChild = aModel->rowCount(aRootIdx);
670   for (int i = 0; i < aNbChild; i++) {
671     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
672     aStates.push_back(myTreeView->isExpanded(aIdx));
673   }
674   return aStates;
675 }
676
677 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
678 {
679   if (theStates.size() == 0)
680     return;
681   XGUI_DataModel* aModel = dataModel();
682   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
683   int aNbChild = aModel->rowCount(aRootIdx);
684
685   std::list<bool>::const_iterator aIt;
686   int i = 0;
687   for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
688     if (i >= aNbChild )
689       break;
690     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
691     myTreeView->setExpanded(aIdx, (*aIt));
692   }
693 }
694
695 void XGUI_ObjectsBrowser::updateAllIndexes(int theColumn, const QModelIndex& theParent)
696 {
697   int aNb = myDocModel->rowCount(theParent);
698   for (int i = 0; i < aNb; i++) {
699     QModelIndex aIdx = myDocModel->index(i, theColumn, theParent);
700     if (aIdx.isValid() && myDocModel->hasIndex(aIdx)) {
701       myTreeView->update(aIdx);
702       if (myTreeView->isExpanded(aIdx))
703         updateAllIndexes(theColumn, aIdx);
704     }
705   }
706 }
707
708 QMap<ObjectPtr, bool> XGUI_ObjectsBrowser::getFoldersState(DocumentPtr theDoc) const
709 {
710   QMap<ObjectPtr, bool> aMap;
711
712   int aNb = theDoc->size(ModelAPI_Folder::group());
713   ObjectPtr aObj;
714   for (int i = 0; i < aNb; i++) {
715     aObj = theDoc->object(ModelAPI_Folder::group(), i);
716     QModelIndex aIdx = myDocModel->objectIndex(aObj, 0);
717     aMap[aObj] = myTreeView->isExpanded(aIdx);
718   }
719   return aMap;
720 }
721
722 void XGUI_ObjectsBrowser::setFoldersState(const QMap<ObjectPtr, bool>& theStates)
723 {
724   QMap<ObjectPtr, bool>::const_iterator aIt;
725   for (aIt = theStates.constBegin(); aIt != theStates.constEnd(); aIt++) {
726     QModelIndex aIdx = myDocModel->objectIndex(aIt.key(), 0);
727     myTreeView->setExpanded(aIdx, aIt.value());
728   }
729 }