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