]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Issue #2210: Object Browser : new show / hide button
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "XGUI_ObjectsBrowser.h"
22 #include "XGUI_Tools.h"
23 #include "XGUI_DataModel.h"
24
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Tools.h>
29
30 #include <ModuleBase_Tools.h>
31
32 #include <QLayout>
33 #include <QLineEdit>
34 #include <QPixmap>
35 #include <QEvent>
36 #include <QMouseEvent>
37 #include <QAction>
38 #include <QStyledItemDelegate>
39 #include <QMessageBox>
40
41 #ifdef DEBUG_INDXES
42 #include <QToolTip>
43 #endif
44
45 /// Width of second column (minimum acceptable = 27)
46 #define FIRST_COL_WIDTH 30
47 #define SECOND_COL_WIDTH 30
48
49
50 /**
51 * \ingroup GUI
52 * Tree item delegate for definition of data in column items editor
53 */
54 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
55 {
56 public:
57   /// Constructor
58   /// \param theParent a parent of the delegate
59   XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
60     myTreedView(theParent) {}
61
62   /// Set data for item editor (name of the item)
63   /// \param editor a widget of editor
64   /// \param index the tree item index
65   virtual void  setEditorData ( QWidget* editor, const QModelIndex& index ) const
66   {
67     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
68     if (aEditor) {
69       XGUI_DataModel* aModel = myTreedView->dataModel();
70       ObjectPtr aObj = aModel->object(index);
71       if (aObj.get() != NULL) {
72         aEditor->setText(aObj->data()->name().c_str());
73         return;
74       }
75     }
76     QStyledItemDelegate::setEditorData(editor, index);
77   }
78
79 private:
80   XGUI_DataTree* myTreedView;
81 };
82
83
84 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
85     : QTreeView(theParent)
86 {
87   setHeaderHidden(true);
88   setTreePosition(1);
89   setEditTriggers(QAbstractItemView::NoEditTriggers);
90   setSelectionBehavior(QAbstractItemView::SelectRows);
91   setSelectionMode(QAbstractItemView::ExtendedSelection);
92
93   setItemDelegateForColumn(1, new XGUI_TreeViewItemDelegate(this));
94
95   connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
96     SLOT(onDoubleClick(const QModelIndex&)));
97 }
98
99 XGUI_DataTree::~XGUI_DataTree()
100 {
101 }
102
103 XGUI_DataModel* XGUI_DataTree::dataModel() const
104 {
105   return static_cast<XGUI_DataModel*>(model());
106 }
107
108 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
109 {
110   emit contextMenuRequested(theEvent);
111 }
112
113 void XGUI_DataTree::commitData(QWidget* theEditor)
114 {
115   static int aEntrance = 0;
116   if (aEntrance == 0) {
117     // We have to check number of enter and exit of this function because it can be called
118     // recursively by Qt in order to avoid double modifying of a data
119     aEntrance = 1;
120     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
121     if (aEditor) {
122       QString aName = aEditor->text();
123       QModelIndexList aIndexList = selectionModel()->selectedIndexes();
124       XGUI_DataModel* aModel = dataModel();
125       ObjectPtr aObj = aModel->object(aIndexList.first());
126
127       if (XGUI_Tools::canRename(aObj, aName)) {
128         SessionPtr aMgr = ModelAPI_Session::get();
129         aMgr->startOperation("Rename");
130         aObj->data()->setName(qPrintable(aName));
131         aMgr->finishOperation();
132       }
133     }
134   }
135   aEntrance = 0;
136 }
137
138 void XGUI_DataTree::clear()
139 {
140   dataModel()->clear();
141   reset();
142 }
143
144 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
145 {
146   QTreeView::resizeEvent(theEvent);
147   QSize aSize = theEvent->size();
148   if (aSize.isValid()) {
149     setColumnWidth(0, FIRST_COL_WIDTH);
150     setColumnWidth(1, aSize.width() - SECOND_COL_WIDTH - FIRST_COL_WIDTH - 12);
151     setColumnWidth(2, SECOND_COL_WIDTH);
152   }
153 }
154
155 #ifdef DEBUG_INDXES
156 void XGUI_DataTree::mousePressEvent(QMouseEvent* theEvent)
157 {
158   QTreeView::mousePressEvent(theEvent);
159   if (theEvent->button() != Qt::MidButton)
160     return;
161   QModelIndex aInd = indexAt(theEvent->pos());
162   QString aTxt = QString("r=%1 c=%2 p=%3").arg(aInd.row()).arg(aInd.column()).arg((long)aInd.internalPointer());
163
164   QModelIndex aPar = aInd.parent();
165   QString aTxt1 = QString("r=%1 c=%2 p=%3").arg(aPar.row()).arg(aPar.column()).arg((long)aPar.internalPointer());
166   QToolTip::showText(theEvent->globalPos(), aTxt + '\n' + aTxt1);
167 }
168 #endif
169
170 void XGUI_DataTree::mouseReleaseEvent(QMouseEvent* theEvent)
171 {
172   QTreeView::mouseReleaseEvent(theEvent);
173 #ifdef DEBUG_INDXES
174   if (theEvent->button() != Qt::MidButton)
175     return;
176   QToolTip::hideText();
177 #endif
178   if (theEvent->button() == Qt::LeftButton) {
179     QModelIndex aInd = indexAt(theEvent->pos());
180     if (aInd.column() == 0)
181       processEyeClick(aInd);
182   }
183 }
184
185 void XGUI_DataTree::processHistoryChange(const QModelIndex& theIndex)
186 {
187   SessionPtr aMgr = ModelAPI_Session::get();
188   // When operation is opened then we can not change history
189   if (aMgr->isOperation())
190     return;
191   XGUI_DataModel* aModel = dataModel();
192   if (aModel->flags(theIndex) == 0)
193     return;
194   ObjectPtr aObj = aModel->object(theIndex);
195
196   DocumentPtr aDoc = aMgr->activeDocument();
197
198   std::string aOpName = tr("History change").toStdString();
199   if (aObj.get()) {
200     if (aObj->document() != aDoc)
201       return;
202     aMgr->startOperation(aOpName);
203     aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
204     aMgr->finishOperation();
205   } else {
206     // Ignore clicks on folders outside current document
207     if ((theIndex.internalId() == 0) && (aDoc != aMgr->moduleDocument()))
208       // Clicked folder under root but active document is another
209       return;
210     if ((theIndex.internalId() != 0) && (aDoc.get() != theIndex.internalPointer()))
211       // Cliced not on active document folder
212       return;
213
214     aMgr->startOperation(aOpName);
215     aDoc->setCurrentFeature(FeaturePtr(), true);
216     aMgr->finishOperation();
217   }
218   QModelIndex aNewIndex = aModel->lastHistoryIndex();
219   QModelIndex aParent = theIndex.parent();
220   int aSize = aModel->rowCount(aParent);
221   for (int i = 0; i < aSize; i++) {
222     update(aModel->index(i, 0, aParent));
223     update(aModel->index(i, 1, aParent));
224     update(aModel->index(i, 2, aParent));
225   }
226 }
227
228 void XGUI_DataTree::processEyeClick(const QModelIndex& theIndex)
229 {
230   XGUI_DataModel* aModel = dataModel();
231   ObjectPtr aObj = aModel->object(theIndex);
232   if (aObj.get()) {
233     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
234     if (aResObj.get()) {
235       aResObj->setDisplayed(!aResObj->isDisplayed());
236       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
237       update(theIndex);
238     }
239     // Update list of selected objects because this event happens after selection event in object browser
240     XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
241     if (aObjBrowser) {
242       aObjBrowser->onSelectionChanged();
243     }
244   }
245 }
246
247 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
248 {
249   switch (theIndex.column()) {
250   case 2:
251     processHistoryChange(theIndex);
252     break;
253   }
254 }
255
256
257 //********************************************************************
258 //********************************************************************
259 //********************************************************************
260 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
261   : QLabel(theText, theParent),
262   myPreSelectionStyle(""),
263   myNeutralStyle(""),
264   mySelectionStyle(""),
265   myIsSelected(false)
266 {
267 }
268
269 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
270 {
271   myTreeView = theView;
272   setFont(myTreeView->font());
273
274   QPalette aPalet = myTreeView->palette();
275   QColor aHighlight = aPalet.highlight().color();
276   QColor aHighlightText = aPalet.highlightedText().color();
277
278   myPreSelectionStyle = "QLabel {background-color: ";
279   myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
280   //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
281   //  aHighlight.lighter(170).name() + ");";
282   //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
283
284   QString aName = aPalet.color(QPalette::Base).name();
285   myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
286
287
288 #if (!defined HAVE_SALOME) && (defined WIN32)
289   mySelectionStyle = "QLabel {background-color: ";
290   mySelectionStyle += "rgb(205, 232, 255); ";
291   //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
292   //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
293   //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
294   mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
295 #else
296   mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
297   mySelectionStyle += "; color : " + aHighlightText.name() + "}";
298 #endif
299
300   myTreeView->viewport()->installEventFilter(this);
301 }
302
303
304 #if (!defined HAVE_SALOME) && (defined WIN32)
305 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
306 {
307   switch (theEvent->type()) {
308     case QEvent::Enter:
309       if (!myIsSelected)
310         setStyleSheet(myPreSelectionStyle);
311       break;
312     case QEvent::Leave:
313       if (!myIsSelected)
314         setStyleSheet(myNeutralStyle);
315       break;
316   }
317   return QLabel::event(theEvent);
318 }
319 #endif
320
321 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
322 {
323   if (theObj == myTreeView->viewport()) {
324     if (theEvent->type() == QEvent::MouseButtonRelease)
325       unselect();
326   }
327   return QLabel::eventFilter(theObj, theEvent);
328 }
329
330 static bool MYClearing = false;
331 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
332 {
333   MYClearing = true;
334   myIsSelected = true;
335   setStyleSheet(mySelectionStyle);
336   // We can not block signals because on
337   // clear selection the View state will not be updated
338   myTreeView->clearSelection();
339   QLabel::mouseReleaseEvent(e);
340   MYClearing = false;
341 }
342
343 void XGUI_ActiveDocLbl::unselect()
344 {
345   if (!MYClearing) {
346     myIsSelected = false;
347     setStyleSheet(myNeutralStyle);
348   }
349 }
350
351
352 //********************************************************************
353 //********************************************************************
354 //********************************************************************
355 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop)
356     : QWidget(theParent), myDocModel(0), myWorkshop(theWorkshop)
357 {
358   QVBoxLayout* aLayout = new QVBoxLayout(this);
359   ModuleBase_Tools::zeroMargins(aLayout);
360   aLayout->setSpacing(0);
361
362   QWidget* aLabelWgt = new QWidget(this);
363   aLabelWgt->setAutoFillBackground(true);
364
365   aLayout->addWidget(aLabelWgt);
366   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
367   ModuleBase_Tools::zeroMargins(aLabelLay);
368   aLabelLay->setSpacing(0);
369
370   QLabel* aLbl = new QLabel(aLabelWgt);
371   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
372   aLbl->setMargin(2);
373   // Do not paint background of the label (in order to show icon)
374   aLbl->setAutoFillBackground(false);
375
376   aLabelLay->addWidget(aLbl);
377
378   SessionPtr aMgr = ModelAPI_Session::get();
379   DocumentPtr aDoc = aMgr->moduleDocument();
380
381   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
382 //  myActiveDocLbl->setReadOnly(true);
383 //  myActiveDocLbl->setFrame(false);
384   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
385
386   aLabelLay->addWidget(myActiveDocLbl);
387   aLabelLay->setStretch(1, 1);
388
389   myTreeView = new XGUI_DataTree(this);
390   myTreeView->setFrameShape(QFrame::NoFrame);
391   aLayout->addWidget(myTreeView);
392
393   QPalette aTreePalet = myTreeView->palette();
394   QColor aTreeBack = aTreePalet.color(QPalette::Base);
395
396   QPalette aPalet;
397   aPalet.setColor(QPalette::Base, aTreeBack);
398   aPalet.setColor(QPalette::Window, aTreeBack);
399   aLabelWgt->setPalette(aPalet);
400
401   myDocModel = new XGUI_DataModel(this);
402   connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
403   connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
404
405   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
406           SLOT(onContextMenuRequested(QContextMenuEvent*)));
407 }
408
409 //***************************************************
410 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
411 {
412 }
413
414 void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
415 {
416   myDocModel->setXMLReader(theReader);
417   myTreeView->setModel(myDocModel);
418
419   // It has to be done after setting of model
420   myActiveDocLbl->setTreeView(myTreeView);
421
422   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
423   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
424           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
425 }
426
427 //***************************************************
428 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
429 {
430   QModelIndexList aIndexes;
431   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
432   bool toEnable = false;
433
434   if (aSelectedData.size() == 1) {
435     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
436     if (!aIndexes.contains(aSelected))
437       return; // menu is called on non selected item
438
439     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
440     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
441   }
442   foreach(QAction* aCmd, actions()) {
443     aCmd->setEnabled(toEnable);
444   }
445   emit contextMenuRequested(theEvent);
446 }
447
448 //***************************************************
449 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
450 {
451   myTreeView->selectionModel()->clearSelection();
452   //Empty feature pointer means that selected root document
453   foreach(QAction* aCmd, actions()) {
454     aCmd->setEnabled(true);
455   }
456   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
457   emit contextMenuRequested(&aEvent);
458 }
459
460 //***************************************************
461 void XGUI_ObjectsBrowser::onEditItem()
462 {
463   QObjectPtrList aSelectedData = selectedObjects();
464   if (aSelectedData.size() > 0) {
465     ObjectPtr anObject = aSelectedData.first();
466     if (anObject.get()) {  // Selection happens in TreeView
467       // check whether the object can be renamed. There should not be parts which are not loaded
468       std::set<FeaturePtr> aFeatures;
469       aFeatures.insert(ModelAPI_Feature::feature(anObject));
470       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
471         return;
472
473       // Find index which corresponds the feature
474       QModelIndex aIndex;
475       foreach(QModelIndex aIdx, selectedIndexes()) {
476         ObjectPtr aFea = dataModel()->object(aIdx);
477         if (dataModel()->object(aIdx)->isSame(anObject)) {
478           aIndex = aIdx;
479           break;
480         }
481       }
482       if (aIndex.isValid()) {
483         myTreeView->setCurrentIndex(aIndex);
484         myTreeView->edit(aIndex);
485       }
486       return;
487     }
488   }
489 }
490
491 //***************************************************
492 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
493 {
494   QModelIndexList aIndexes;
495   QModelIndex aIndex;
496   for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
497     aIndex = myDocModel->index(i, 0, theParent);
498     if (myDocModel->hasChildren(aIndex)) {
499       if (myTreeView->isExpanded(aIndex)) {
500         aIndexes.append(aIndex);
501         QModelIndexList aSubIndexes = expandedItems(aIndex);
502         if (!aSubIndexes.isEmpty())
503           aIndexes.append(aSubIndexes);
504       }
505     }
506   }
507   return aIndexes;
508 }
509
510 //***************************************************
511 void XGUI_ObjectsBrowser::rebuildDataTree()
512 {
513   myDocModel->rebuildDataTree();
514   update();
515 }
516
517 //***************************************************
518 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
519 {
520   QList<QModelIndex> theIndexes;
521   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
522   aSelectModel->clear();
523
524   foreach(ObjectPtr aFeature, theObjects)
525   {
526     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
527     if (aIndex.isValid()) {
528       aSelectModel->select(aIndex, QItemSelectionModel::Select);
529     }
530   }
531 }
532
533 //***************************************************
534 void XGUI_ObjectsBrowser::clearContent()
535 {
536   myTreeView->clear();
537 }
538
539 //***************************************************
540 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
541                                        const QItemSelection& theDeselected)
542 {
543   onSelectionChanged();
544 }
545
546 //***************************************************
547 void XGUI_ObjectsBrowser::onSelectionChanged()
548 {
549   emit selectionChanged();
550 }
551
552 //***************************************************
553 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
554 {
555   QObjectPtrList aList;
556   QModelIndexList aIndexes = selectedIndexes();
557   XGUI_DataModel* aModel = dataModel();
558   QModelIndexList::const_iterator aIt;
559   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
560     if ((*aIt).column() == 1) {
561       ObjectPtr aObject = aModel->object(*aIt);
562       if (aObject) {
563         aList.append(aObject);
564         if (theIndexes)
565           theIndexes->append(*aIt);
566       }
567     }
568   }
569   return aList;
570 }
571
572 void XGUI_ObjectsBrowser::onBeforeReset()
573 {
574   myExpandedItems = expandedItems();
575 }
576
577 void XGUI_ObjectsBrowser::onAfterModelReset()
578 {
579   foreach(QModelIndex aIndex, myExpandedItems) {
580     myTreeView->setExpanded(aIndex, true);
581   }
582 }
583
584 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
585 {
586   std::list<bool> aStates;
587   XGUI_DataModel* aModel = dataModel();
588   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
589   int aNbChild = aModel->rowCount(aRootIdx);
590   for (int i = 0; i < aNbChild; i++) {
591     QModelIndex aIdx = aModel->index(i, 1, aRootIdx);
592     aStates.push_back(myTreeView->isExpanded(aIdx));
593   }
594   return aStates;
595 }
596
597 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
598 {
599   if (theStates.size() == 0)
600     return;
601   XGUI_DataModel* aModel = dataModel();
602   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
603   int aNbChild = aModel->rowCount(aRootIdx);
604
605   std::list<bool>::const_iterator aIt;
606   int i = 0;
607   for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
608     if (i >= aNbChild )
609       break;
610     QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
611     myTreeView->setExpanded(aIdx, (*aIt));
612   }
613 }