]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Provide presentation for a step
[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   static Events_Loop* aLoop = Events_Loop::loop();
235   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
236   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
237
238   XGUI_DataModel* aModel = dataModel();
239   ObjectPtr aObj = aModel->object(theIndex);
240   if (aObj.get()) {
241     XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
242     ResultFieldPtr aField = std::dynamic_pointer_cast<ModelAPI_ResultField>(aObj);
243     if (aField.get()) {
244       bool toDisplay = !aField->isDisplayed();
245       aField->setDisplayed(toDisplay);
246       if (toDisplay) {
247         for (int i = 0; i < aField->stepsSize(); i++) {
248           aField->step(i)->setDisplayed(false);
249           // TODO: Sending events has to be removed when step will be defined as Object
250           aECreator->sendUpdated(aField->step(i), EVENT_DISP);
251         }
252       }
253       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
254     }
255     else {
256       FieldStepPtr aStep =
257         std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(aObj);
258       if (aStep.get()) {
259         // Only one step from a field can be visible at once
260         bool toDisplay = !aStep->isDisplayed();
261         if (toDisplay) {
262           int aId = aStep->id();
263           ModelAPI_ResultField* aField = aStep->field();
264           aField->setDisplayed(false);
265           for (int i = 0; i < aField->stepsSize(); i++) {
266             aField->step(i)->setDisplayed(i == aId);
267             // TODO: Sending events has to be removed when step will be defined as Object
268             aECreator->sendUpdated(aField->step(i), EVENT_DISP);
269           }
270         }
271         else {
272           aStep->setDisplayed(false);
273           aECreator->sendUpdated(aStep, EVENT_DISP);
274         }
275         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
276       }
277       else {
278         ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
279         if (aResObj.get()) {
280           std::set<ObjectPtr> anObjects;
281           anObjects.insert(aResObj);
282
283           bool hasHiddenState = aModel->hasHiddenState(theIndex);
284           if (aObjBrowser && hasHiddenState && !aObjBrowser->workshop()->prepareForDisplay(anObjects))
285             return;
286           if (hasHiddenState) { // #issue 2335(hide all faces then show solid problem)
287             if (aResObj->isDisplayed())
288               aResObj->setDisplayed(false);
289             aResObj->setDisplayed(true);
290           }
291           else
292             aResObj->setDisplayed(!aResObj->isDisplayed());
293           Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
294         }
295       }
296     }
297     // Update list of selected objects because this event happens after
298     // selection event in object browser
299     if (aObjBrowser) {
300       aObjBrowser->onSelectionChanged();
301     }
302     update(theIndex);
303   }
304 }
305
306 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
307 {
308   switch (theIndex.column()) {
309   case 2:
310     processHistoryChange(theIndex);
311     break;
312   }
313 }
314
315
316 //********************************************************************
317 //********************************************************************
318 //********************************************************************
319 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
320   : QLabel(theText, theParent),
321   myPreSelectionStyle(""),
322   myNeutralStyle(""),
323   mySelectionStyle(""),
324   myIsSelected(false)
325 {
326 }
327
328 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
329 {
330   myTreeView = theView;
331   setFont(myTreeView->font());
332
333   QPalette aPalet = myTreeView->palette();
334   QColor aHighlight = aPalet.highlight().color();
335   QColor aHighlightText = aPalet.highlightedText().color();
336
337   myPreSelectionStyle = "QLabel {background-color: ";
338   myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
339   //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
340   //  aHighlight.lighter(170).name() + ");";
341   //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
342
343   QString aName = aPalet.color(QPalette::Base).name();
344   myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
345
346
347 #if (!defined HAVE_SALOME) && (defined WIN32)
348   mySelectionStyle = "QLabel {background-color: ";
349   mySelectionStyle += "rgb(205, 232, 255); ";
350   //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
351   //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
352   //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
353   mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
354 #else
355   mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
356   mySelectionStyle += "; color : " + aHighlightText.name() + "}";
357 #endif
358
359   myTreeView->viewport()->installEventFilter(this);
360 }
361
362
363 #if (!defined HAVE_SALOME) && (defined WIN32)
364 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
365 {
366   switch (theEvent->type()) {
367     case QEvent::Enter:
368       if (!myIsSelected)
369         setStyleSheet(myPreSelectionStyle);
370       break;
371     case QEvent::Leave:
372       if (!myIsSelected)
373         setStyleSheet(myNeutralStyle);
374       break;
375   }
376   return QLabel::event(theEvent);
377 }
378 #endif
379
380 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
381 {
382   if (theObj == myTreeView->viewport()) {
383     if (theEvent->type() == QEvent::MouseButtonRelease)
384       unselect();
385   }
386   return QLabel::eventFilter(theObj, theEvent);
387 }
388
389 static bool MYClearing = false;
390 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
391 {
392   MYClearing = true;
393   myIsSelected = true;
394   setStyleSheet(mySelectionStyle);
395   // We can not block signals because on
396   // clear selection the View state will not be updated
397   myTreeView->clearSelection();
398   QLabel::mouseReleaseEvent(e);
399   MYClearing = false;
400 }
401
402 void XGUI_ActiveDocLbl::unselect()
403 {
404   if (!MYClearing) {
405     myIsSelected = false;
406     setStyleSheet(myNeutralStyle);
407   }
408 }
409
410
411 //********************************************************************
412 //********************************************************************
413 //********************************************************************
414 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop)
415     : QWidget(theParent), myDocModel(0), myWorkshop(theWorkshop)
416 {
417   QVBoxLayout* aLayout = new QVBoxLayout(this);
418   ModuleBase_Tools::zeroMargins(aLayout);
419   aLayout->setSpacing(0);
420
421   QWidget* aLabelWgt = new QWidget(this);
422   aLabelWgt->setAutoFillBackground(true);
423
424   aLayout->addWidget(aLabelWgt);
425   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
426   ModuleBase_Tools::zeroMargins(aLabelLay);
427   aLabelLay->setSpacing(0);
428
429   QLabel* aLbl = new QLabel(aLabelWgt);
430   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
431   aLbl->setMargin(2);
432   // Do not paint background of the label (in order to show icon)
433   aLbl->setAutoFillBackground(false);
434
435   aLabelLay->addWidget(aLbl);
436
437   SessionPtr aMgr = ModelAPI_Session::get();
438   DocumentPtr aDoc = aMgr->moduleDocument();
439
440   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
441 //  myActiveDocLbl->setReadOnly(true);
442 //  myActiveDocLbl->setFrame(false);
443   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
444
445   aLabelLay->addWidget(myActiveDocLbl);
446   aLabelLay->setStretch(1, 1);
447
448   myTreeView = new XGUI_DataTree(this);
449   myTreeView->setFrameShape(QFrame::NoFrame);
450   aLayout->addWidget(myTreeView);
451
452   QPalette aTreePalet = myTreeView->palette();
453   QColor aTreeBack = aTreePalet.color(QPalette::Base);
454
455   QPalette aPalet;
456   aPalet.setColor(QPalette::Base, aTreeBack);
457   aPalet.setColor(QPalette::Window, aTreeBack);
458   aLabelWgt->setPalette(aPalet);
459
460   myDocModel = new XGUI_DataModel(this);
461   connect(myDocModel, SIGNAL(beforeTreeRebuild()), SLOT(onBeforeReset()));
462   connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
463
464   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
465           SLOT(onContextMenuRequested(QContextMenuEvent*)));
466 }
467
468 //***************************************************
469 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
470 {
471 }
472
473 void XGUI_ObjectsBrowser::initialize(ModuleBase_ITreeNode* theRoot)
474 {
475   //myDocModel->setXMLReader(theReader);
476   myDocModel->setRoot(theRoot);
477   myTreeView->setModel(myDocModel);
478
479   // It has to be done after setting of model
480   myActiveDocLbl->setTreeView(myTreeView);
481
482   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
483   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
484           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
485 }
486
487 //***************************************************
488 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
489 {
490   QModelIndexList aIndexes;
491   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
492   bool toEnable = false;
493
494   if (aSelectedData.size() == 1) {
495     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
496     if (!aIndexes.contains(aSelected))
497       return; // menu is called on non selected item
498
499     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
500     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
501   }
502   foreach(QAction* aCmd, actions()) {
503     aCmd->setEnabled(toEnable);
504   }
505   emit contextMenuRequested(theEvent);
506 }
507
508 //***************************************************
509 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
510 {
511   myTreeView->selectionModel()->clearSelection();
512   //Empty feature pointer means that selected root document
513   foreach(QAction* aCmd, actions()) {
514     aCmd->setEnabled(true);
515   }
516   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
517   emit contextMenuRequested(&aEvent);
518 }
519
520 //***************************************************
521 void XGUI_ObjectsBrowser::onEditItem()
522 {
523   QObjectPtrList aSelectedData = selectedObjects();
524   if (aSelectedData.size() > 0) {
525     ObjectPtr anObject = aSelectedData.first();
526     if (anObject.get()) {  // Selection happens in TreeView
527       // check whether the object can be renamed. There should not be parts which are not loaded
528       std::set<FeaturePtr> aFeatures;
529       aFeatures.insert(ModelAPI_Feature::feature(anObject));
530       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
531         return;
532
533       // Find index which corresponds the feature
534       QModelIndex aIndex;
535       foreach(QModelIndex aIdx, selectedIndexes()) {
536         if (aIdx.column() == 1) {
537           ObjectPtr aFea = dataModel()->object(aIdx);
538           if (dataModel()->object(aIdx)->isSame(anObject)) {
539             aIndex = aIdx;
540             break;
541           }
542         }
543       }
544       if (aIndex.isValid()) {
545         myTreeView->setCurrentIndex(aIndex);
546         myTreeView->edit(aIndex);
547       }
548       return;
549     }
550   }
551 }
552
553 //***************************************************
554 QList<ModuleBase_ITreeNode*> XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
555 {
556   QList<ModuleBase_ITreeNode*> aIndexes;
557   QModelIndex aIndex;
558   int aCount = myDocModel->rowCount(theParent);
559   for (int i = 0; i < aCount; i++) {
560     aIndex = myDocModel->index(i, 0, theParent);
561     if (myDocModel->hasChildren(aIndex)) {
562       if (myTreeView->isExpanded(aIndex)) {
563         aIndexes.append((ModuleBase_ITreeNode*)aIndex.internalPointer());
564         QList<ModuleBase_ITreeNode*> aSubIndexes = expandedItems(aIndex);
565         if (!aSubIndexes.isEmpty())
566           aIndexes.append(aSubIndexes);
567       }
568     }
569   }
570   return aIndexes;
571 }
572
573 //***************************************************
574 void XGUI_ObjectsBrowser::rebuildDataTree()
575 {
576   myDocModel->root()->update();
577   myDocModel->rebuildDataTree();
578   update();
579 }
580
581 //***************************************************
582 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
583 {
584   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
585   QModelIndexList aIndexes = aSelectModel->selectedIndexes();
586   if (theObjects.size() == 0) {
587     bool aIsBlock = aSelectModel->blockSignals(true);
588     aSelectModel->clear();
589     aSelectModel->blockSignals(aIsBlock);
590     foreach(QModelIndex aIdx, aIndexes) {
591       myTreeView->update(aIdx);
592     }
593     return;
594   }
595
596   ObjectPtr aObject;
597   QModelIndexList aUnselect;
598   QObjectPtrList aToSelect = theObjects;
599   QHash<qint64, ObjectPtr> aNotChanged;
600   foreach(QModelIndex aIdx, aIndexes) {
601     aObject = myDocModel->object(aIdx);
602     if (aObject.get()) {
603       if (aToSelect.contains(aObject)) {
604         aNotChanged.insert((qint64)aObject.get(), aObject);
605       } else {
606         aUnselect.append(aIdx);
607       }
608     }
609     else {
610       aUnselect.append(aIdx);
611     }
612   }
613
614   foreach(ObjectPtr aObj, aNotChanged)
615     aToSelect.removeAll(aObj);
616
617   bool aIsBlock = aSelectModel->blockSignals(true);
618   foreach(QModelIndex aIdx, aUnselect) {
619     aSelectModel->select(aIdx, QItemSelectionModel::Deselect);
620     myTreeView->update(aIdx);
621   }
622
623   QModelIndex aIndex0, aIndex1, aIndex2, aCurrent;
624   foreach(ObjectPtr aFeature, aToSelect) {
625     aIndex1 = myDocModel->objectIndex(aFeature, 1);
626     if (aIndex1.isValid()) {
627       if (!aCurrent.isValid())
628         aCurrent = aIndex1;
629       aIndex0 = myDocModel->objectIndex(aFeature, 0);
630       aIndex2 = myDocModel->objectIndex(aFeature, 2);
631       aSelectModel->select(aIndex1, QItemSelectionModel::Select | QItemSelectionModel::Rows);
632       myTreeView->update(aIndex0);
633       myTreeView->update(aIndex1);
634       myTreeView->update(aIndex2);
635     }
636   }
637   aSelectModel->setCurrentIndex(aCurrent, QItemSelectionModel::NoUpdate);
638   aSelectModel->blockSignals(aIsBlock);
639 }
640
641 //***************************************************
642 void XGUI_ObjectsBrowser::ensureVisible(const ObjectPtr theObject)
643 {
644   QModelIndex aIndex = myDocModel->objectIndex(theObject);
645   if (aIndex.isValid())  {
646     QModelIndex aParent = aIndex.parent();
647     while (aParent.isValid()) {
648       myTreeView->expand(aParent);
649       aParent = aParent.parent();
650     }
651     myTreeView->scrollTo(aIndex);
652   }
653 }
654
655 //***************************************************
656 void XGUI_ObjectsBrowser::clearContent()
657 {
658   myTreeView->clear();
659 }
660
661 //***************************************************
662 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
663                                        const QItemSelection& theDeselected)
664 {
665   onSelectionChanged();
666 }
667
668 //***************************************************
669 void XGUI_ObjectsBrowser::onSelectionChanged()
670 {
671   emit selectionChanged();
672 }
673
674 //***************************************************
675 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
676 {
677   QObjectPtrList aList;
678   QModelIndexList aIndexes = selectedIndexes();
679   XGUI_DataModel* aModel = dataModel();
680
681   foreach(QModelIndex aIdx, aIndexes) {
682     if (aIdx.column() == 1) {
683       ObjectPtr aObject = aModel->object(aIdx);
684       if (aObject) {
685         aList.append(aObject);
686         if (theIndexes)
687           theIndexes->append(aIdx);
688       }
689     }
690   }
691   return aList;
692 }
693
694 void XGUI_ObjectsBrowser::onBeforeReset()
695 {
696   myExpandedItems = expandedItems();
697 }
698
699 void XGUI_ObjectsBrowser::onAfterModelReset()
700 {
701   XGUI_DataModel* aModel = myTreeView->dataModel();
702   QModelIndex aIndex;
703   foreach(ModuleBase_ITreeNode* aNode, myExpandedItems) {
704     if (aModel->hasNode(aNode)) {
705       aIndex = aModel->getIndex(aNode, 0);
706       if (aIndex.isValid() && (myTreeView->dataModel()->hasIndex(aIndex)))
707         myTreeView->setExpanded(aIndex, true);
708     }
709   }
710   myExpandedItems.clear();
711 }
712
713 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
714 {
715   std::list<bool> aStates;
716   XGUI_DataModel* aModel = dataModel();
717   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
718   int aNbChild = aModel->rowCount(aRootIdx);
719   for (int i = 0; i < aNbChild; i++) {
720     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
721     aStates.push_back(myTreeView->isExpanded(aIdx));
722   }
723   return aStates;
724 }
725
726 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
727 {
728   if (theStates.size() == 0)
729     return;
730   XGUI_DataModel* aModel = dataModel();
731   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
732   int aNbChild = aModel->rowCount(aRootIdx);
733
734   std::list<bool>::const_iterator aIt;
735   int i = 0;
736   for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
737     if (i >= aNbChild )
738       break;
739     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
740     myTreeView->setExpanded(aIdx, (*aIt));
741   }
742 }
743
744 void XGUI_ObjectsBrowser::updateAllIndexes(int theColumn, const QModelIndex& theParent)
745 {
746   int aNb = myDocModel->rowCount(theParent);
747   for (int i = 0; i < aNb; i++) {
748     QModelIndex aIdx = myDocModel->index(i, theColumn, theParent);
749     if (aIdx.isValid() && myDocModel->hasIndex(aIdx)) {
750       myTreeView->update(aIdx);
751       if (myTreeView->isExpanded(aIdx))
752         updateAllIndexes(theColumn, aIdx);
753     }
754   }
755 }
756
757 QMap<ObjectPtr, bool> XGUI_ObjectsBrowser::getFoldersState(DocumentPtr theDoc) const
758 {
759   QMap<ObjectPtr, bool> aMap;
760
761   int aNb = theDoc->size(ModelAPI_Folder::group());
762   ObjectPtr aObj;
763   for (int i = 0; i < aNb; i++) {
764     aObj = theDoc->object(ModelAPI_Folder::group(), i);
765     QModelIndex aIdx = myDocModel->objectIndex(aObj, 0);
766     aMap[aObj] = myTreeView->isExpanded(aIdx);
767   }
768   return aMap;
769 }
770
771 void XGUI_ObjectsBrowser::setFoldersState(const QMap<ObjectPtr, bool>& theStates)
772 {
773   QMap<ObjectPtr, bool>::const_iterator aIt;
774   for (aIt = theStates.constBegin(); aIt != theStates.constEnd(); aIt++) {
775     QModelIndex aIdx = myDocModel->objectIndex(aIt.key(), 0);
776     myTreeView->setExpanded(aIdx, aIt.value());
777   }
778 }
779
780
781 void XGUI_ObjectsBrowser::resizeEvent(QResizeEvent* theEvent)
782 {
783   QWidget::resizeEvent(theEvent);
784   emit sizeChanged();
785 }