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