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