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