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