Salome HOME
Copyright update 2021
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
1 // Copyright (C) 2014-2021  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   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(aName.toStdWString());
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   XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
232   aObjBrowser->workshop()->displayer()->updateViewer();
233 }
234
235 void XGUI_DataTree::processEyeClick(const QModelIndex& theIndex)
236 {
237   XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
238   XGUI_DataModel* aModel = dataModel();
239   ObjectPtr aObj = aModel->object(theIndex);
240   if (aObj.get()) {
241     std::set<ObjectPtr> anObjects;
242     anObjects.insert(aObj);
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 (aObj->isDisplayed())
249         aObj->setDisplayed(false);
250       aObj->setDisplayed(true);
251     }
252     else
253       aObj->setDisplayed(!aObj->isDisplayed());
254
255     // Update list of selected objects because this event happens after
256     // selection event in object browser
257     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
258     update(theIndex);
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(beforeTreeRebuild()), 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 QList<ModuleBase_ITreeNode*> XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
514 {
515   QList<ModuleBase_ITreeNode*> aIndexes;
516   QModelIndex aIndex;
517   int aCount = myDocModel->rowCount(theParent);
518   for (int i = 0; i < aCount; i++) {
519     aIndex = myDocModel->index(i, 0, theParent);
520     if (myDocModel->hasChildren(aIndex)) {
521       if (myTreeView->isExpanded(aIndex)) {
522         aIndexes.append((ModuleBase_ITreeNode*)aIndex.internalPointer());
523         QList<ModuleBase_ITreeNode*> aSubIndexes = expandedItems(aIndex);
524         if (!aSubIndexes.isEmpty())
525           aIndexes.append(aSubIndexes);
526       }
527     }
528   }
529   return aIndexes;
530 }
531
532 //***************************************************
533 void XGUI_ObjectsBrowser::rebuildDataTree()
534 {
535   myDocModel->root()->update();
536   myDocModel->rebuildDataTree();
537   update();
538 }
539
540 //***************************************************
541 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
542 {
543   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
544   QModelIndexList aIndexes = aSelectModel->selectedIndexes();
545   if (theObjects.size() == 0) {
546     bool aIsBlock = aSelectModel->blockSignals(true);
547     aSelectModel->clear();
548     aSelectModel->blockSignals(aIsBlock);
549     foreach(QModelIndex aIdx, aIndexes) {
550       myTreeView->update(aIdx);
551     }
552     return;
553   }
554
555   ObjectPtr aObject;
556   QModelIndexList aUnselect;
557   QObjectPtrList aToSelect = theObjects;
558   QHash<qint64, ObjectPtr> aNotChanged;
559   foreach(QModelIndex aIdx, aIndexes) {
560     aObject = myDocModel->object(aIdx);
561     if (aObject.get()) {
562       if (aToSelect.contains(aObject)) {
563         aNotChanged.insert((qint64)aObject.get(), aObject);
564       } else {
565         aUnselect.append(aIdx);
566       }
567     }
568     else {
569       aUnselect.append(aIdx);
570     }
571   }
572
573   foreach(ObjectPtr aObj, aNotChanged)
574     aToSelect.removeAll(aObj);
575
576   bool aIsBlock = aSelectModel->blockSignals(true);
577   foreach(QModelIndex aIdx, aUnselect) {
578     aSelectModel->select(aIdx, QItemSelectionModel::Deselect);
579     myTreeView->update(aIdx);
580   }
581
582   QModelIndex aIndex0, aIndex1, aIndex2, aCurrent;
583   foreach(ObjectPtr aFeature, aToSelect) {
584     aIndex1 = myDocModel->objectIndex(aFeature, 1);
585     if (aIndex1.isValid()) {
586       if (!aCurrent.isValid())
587         aCurrent = aIndex1;
588       aIndex0 = myDocModel->objectIndex(aFeature, 0);
589       aIndex2 = myDocModel->objectIndex(aFeature, 2);
590       aSelectModel->select(aIndex1, QItemSelectionModel::Select | QItemSelectionModel::Rows);
591       myTreeView->update(aIndex0);
592       myTreeView->update(aIndex1);
593       myTreeView->update(aIndex2);
594     }
595   }
596   aSelectModel->setCurrentIndex(aCurrent, QItemSelectionModel::NoUpdate);
597   aSelectModel->blockSignals(aIsBlock);
598 }
599
600 //***************************************************
601 void XGUI_ObjectsBrowser::ensureVisible(const ObjectPtr theObject)
602 {
603   QModelIndex aIndex = myDocModel->objectIndex(theObject);
604   if (aIndex.isValid())  {
605     QModelIndex aParent = aIndex.parent();
606     while (aParent.isValid()) {
607       myTreeView->expand(aParent);
608       aParent = aParent.parent();
609     }
610     myTreeView->scrollTo(aIndex);
611   }
612 }
613
614 //***************************************************
615 void XGUI_ObjectsBrowser::clearContent()
616 {
617   myTreeView->clear();
618 }
619
620 //***************************************************
621 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& /*theSelected*/,
622                                              const QItemSelection& /*theDeselected*/)
623 {
624   onSelectionChanged();
625 }
626
627 //***************************************************
628 void XGUI_ObjectsBrowser::onSelectionChanged()
629 {
630   emit selectionChanged();
631 }
632
633 //***************************************************
634 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
635 {
636   QObjectPtrList aList;
637   QModelIndexList aIndexes = selectedIndexes();
638   XGUI_DataModel* aModel = dataModel();
639
640   foreach(QModelIndex aIdx, aIndexes) {
641     if (aIdx.column() == 1) {
642       ObjectPtr aObject = aModel->object(aIdx);
643       if (aObject) {
644         aList.append(aObject);
645         if (theIndexes)
646           theIndexes->append(aIdx);
647       }
648     }
649   }
650   return aList;
651 }
652
653 void XGUI_ObjectsBrowser::onBeforeReset()
654 {
655   myExpandedItems = expandedItems();
656 }
657
658 void XGUI_ObjectsBrowser::onAfterModelReset()
659 {
660   XGUI_DataModel* aModel = myTreeView->dataModel();
661   QModelIndex aIndex;
662   foreach(ModuleBase_ITreeNode* aNode, myExpandedItems) {
663     if (aModel->hasNode(aNode)) {
664       aIndex = aModel->getIndex(aNode, 0);
665       if (aIndex.isValid() && (myTreeView->dataModel()->hasIndex(aIndex)))
666         myTreeView->setExpanded(aIndex, true);
667     }
668   }
669   myExpandedItems.clear();
670 }
671
672 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
673 {
674   std::list<bool> aStates;
675   XGUI_DataModel* aModel = dataModel();
676   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
677   int aNbChild = aModel->rowCount(aRootIdx);
678   for (int i = 0; i < aNbChild; i++) {
679     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
680     aStates.push_back(myTreeView->isExpanded(aIdx));
681   }
682   return aStates;
683 }
684
685 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
686 {
687   if (theStates.size() == 0)
688     return;
689   XGUI_DataModel* aModel = dataModel();
690   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
691   int aNbChild = aModel->rowCount(aRootIdx);
692
693   std::list<bool>::const_iterator aIt;
694   int i = 0;
695   for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
696     if (i >= aNbChild )
697       break;
698     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
699     myTreeView->setExpanded(aIdx, (*aIt));
700   }
701 }
702
703 void XGUI_ObjectsBrowser::updateAllIndexes(int theColumn, const QModelIndex& theParent)
704 {
705   int aNb = myDocModel->rowCount(theParent);
706   for (int i = 0; i < aNb; i++) {
707     QModelIndex aIdx = myDocModel->index(i, theColumn, theParent);
708     if (aIdx.isValid() && myDocModel->hasIndex(aIdx)) {
709       myTreeView->update(aIdx);
710       if (myTreeView->isExpanded(aIdx))
711         updateAllIndexes(theColumn, aIdx);
712     }
713   }
714 }
715
716 QMap<ObjectPtr, bool> XGUI_ObjectsBrowser::getFoldersState(DocumentPtr theDoc) const
717 {
718   QMap<ObjectPtr, bool> aMap;
719
720   int aNb = theDoc->size(ModelAPI_Folder::group());
721   ObjectPtr aObj;
722   for (int i = 0; i < aNb; i++) {
723     aObj = theDoc->object(ModelAPI_Folder::group(), i);
724     QModelIndex aIdx = myDocModel->objectIndex(aObj, 0);
725     aMap[aObj] = myTreeView->isExpanded(aIdx);
726   }
727   return aMap;
728 }
729
730 void XGUI_ObjectsBrowser::setFoldersState(const QMap<ObjectPtr, bool>& theStates)
731 {
732   QMap<ObjectPtr, bool>::const_iterator aIt;
733   for (aIt = theStates.constBegin(); aIt != theStates.constEnd(); aIt++) {
734     QModelIndex aIdx = myDocModel->objectIndex(aIt.key(), 0);
735     myTreeView->setExpanded(aIdx, aIt.value());
736   }
737 }
738
739
740 void XGUI_ObjectsBrowser::resizeEvent(QResizeEvent* theEvent)
741 {
742   QWidget::resizeEvent(theEvent);
743   emit sizeChanged();
744 }