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