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