Salome HOME
c169228c2c5be89691e4827da64039aaf8237071
[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 #include <Events_Error.h>
12
13 #include <ModuleBase_Tools.h>
14
15 #include <QLayout>
16 #include <QLineEdit>
17 #include <QPixmap>
18 #include <QEvent>
19 #include <QMouseEvent>
20 #include <QAction>
21 #include <QStyledItemDelegate>
22 #include <QMessageBox>
23
24 #ifdef WIN32
25 #ifdef HAVE_SALOME
26 #include <QWindowsStyle>
27 #endif
28 #endif
29
30
31 /// Width of second column (minimum acceptable = 27)
32 #define SECOND_COL_WIDTH 30
33
34
35 /**
36 * \ingroup GUI
37 * Tree item delegate for definition of data in column items editor
38 */
39 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
40 {
41 public:
42   /// Constructor
43   /// \param theParent a parent of the delegate
44   XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
45
46   /// Set data for item editor (name of the item)
47   /// \param editor a widget of editor
48   /// \param index the tree item index
49   virtual void  setEditorData ( QWidget* editor, const QModelIndex& index ) const
50   {
51     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
52     if (aEditor) {
53       XGUI_DataModel* aModel = myTreedView->dataModel();
54       ObjectPtr aObj = aModel->object(index);
55       if (aObj.get() != NULL) {
56         aEditor->setText(aObj->data()->name().c_str());
57         return;
58       }
59     }
60     QStyledItemDelegate::setEditorData(editor, index);
61   }
62
63 private:
64   XGUI_DataTree* myTreedView;
65 };
66
67
68 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
69     : QTreeView(theParent)
70 {
71 #ifdef WIN32
72 #ifdef HAVE_SALOME
73   setStyle(new QWindowsStyle());
74 #else
75   myStyle = new XGUI_TreeViewStyle();
76   setStyle(myStyle);
77 #endif
78 #endif
79
80   setHeaderHidden(true);
81   setEditTriggers(QAbstractItemView::NoEditTriggers);
82   setSelectionBehavior(QAbstractItemView::SelectRows);
83   setSelectionMode(QAbstractItemView::ExtendedSelection);
84
85   setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
86
87   connect(this, SIGNAL(doubleClicked(const QModelIndex&)), 
88     SLOT(onDoubleClick(const QModelIndex&)));
89 }
90
91 XGUI_DataTree::~XGUI_DataTree()
92 {
93 }
94
95 XGUI_DataModel* XGUI_DataTree::dataModel() const
96 {
97   return static_cast<XGUI_DataModel*>(model());
98 }
99
100 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
101 {
102   emit contextMenuRequested(theEvent);
103 }
104
105 void XGUI_DataTree::commitData(QWidget* theEditor)
106 {
107   static int aEntrance = 0;
108   if (aEntrance == 0) {
109     // We have to check number of enter and exit of this function because it can be called recursively by Qt
110     // in order to avoid double modifying of a data
111     aEntrance = 1;
112     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
113     if (aEditor) {
114       QString aName = aEditor->text();
115       QModelIndexList aIndexList = selectionModel()->selectedIndexes();
116       XGUI_DataModel* aModel = dataModel();
117       ObjectPtr aObj = aModel->object(aIndexList.first());
118
119       if (XGUI_Tools::canRename(aObj, aName)) {
120         SessionPtr aMgr = ModelAPI_Session::get();
121         aMgr->startOperation("Rename");
122         aObj->data()->setName(qPrintable(aName));
123         aMgr->finishOperation();
124       }
125     }
126   }
127   aEntrance = 0;
128 }
129
130 void XGUI_DataTree::clear() 
131 {
132   dataModel()->clear();
133   reset();
134 }
135
136 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
137 {
138   QSize aSize = theEvent->size();
139   if (aSize.isValid()) {
140     setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
141     setColumnWidth(1, SECOND_COL_WIDTH);
142   }
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   myTreeView->setModel(myDocModel);
364
365   // It has to be done after setting of model
366   myActiveDocLbl->setTreeView(myTreeView);
367
368   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
369   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
370           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
371
372   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
373           SLOT(onContextMenuRequested(QContextMenuEvent*)));
374 }
375
376 //***************************************************
377 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
378 {
379 }
380
381
382 //***************************************************
383 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
384 {
385   QModelIndexList aIndexes;
386   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
387   bool toEnable = false;
388
389   if (aSelectedData.size() == 1) {
390     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
391     if (!aIndexes.contains(aSelected))
392       return; // menu is called on non selected item
393
394     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
395     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
396   }
397   foreach(QAction* aCmd, actions()) {
398     aCmd->setEnabled(toEnable);
399   }
400   emit contextMenuRequested(theEvent);
401 }
402
403 //***************************************************
404 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
405 {
406   myTreeView->selectionModel()->clearSelection();
407   //Empty feature pointer means that selected root document
408   foreach(QAction* aCmd, actions()) {
409     aCmd->setEnabled(true);
410   }
411   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
412   emit contextMenuRequested(&aEvent);
413 }
414
415 //***************************************************
416 void XGUI_ObjectsBrowser::onEditItem()
417 {
418   QObjectPtrList aSelectedData = selectedObjects();
419   if (aSelectedData.size() > 0) {
420     ObjectPtr aFeature = aSelectedData.first();
421     if (aFeature) {  // Selection happens in TreeView
422       QObjectPtrList aList;
423       aList.append(aFeature);
424       // check whether the object can be deleted. There should not be parts which are not loaded
425       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
426         return;
427
428       // Find index which corresponds the feature
429       QModelIndex aIndex;
430       foreach(QModelIndex aIdx, selectedIndexes()) {
431         ObjectPtr aFea = dataModel()->object(aIdx);
432         if (dataModel()->object(aIdx)->isSame(aFeature)) {
433           aIndex = aIdx;
434           break;
435         }
436       }
437       if (aIndex.isValid()) {
438         myTreeView->setCurrentIndex(aIndex);
439         myTreeView->edit(aIndex);
440       }
441       return;
442     }
443   }
444 }
445
446 //***************************************************
447 void XGUI_ObjectsBrowser::rebuildDataTree()
448 {
449   myDocModel->rebuildDataTree();
450   update();
451 }
452
453 //***************************************************
454 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
455 {
456   QList<QModelIndex> theIndexes;
457   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
458   aSelectModel->clear();
459
460   foreach(ObjectPtr aFeature, theObjects)
461   {
462     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
463     if (aIndex.isValid()) {
464       aSelectModel->select(aIndex, QItemSelectionModel::Select);
465     }
466   }
467 }
468
469 //***************************************************
470 void XGUI_ObjectsBrowser::clearContent()  
471
472   myTreeView->clear(); 
473 }
474
475 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
476                                        const QItemSelection& theDeselected)
477 {
478   emit selectionChanged();
479 }
480
481 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
482 {
483   QObjectPtrList aList;
484   QModelIndexList aIndexes = selectedIndexes();
485   XGUI_DataModel* aModel = dataModel();
486   QModelIndexList::const_iterator aIt;
487   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
488     if ((*aIt).column() == 0) {
489       ObjectPtr aObject = aModel->object(*aIt);
490       if (aObject) {
491         aList.append(aObject);
492         if (theIndexes)
493           theIndexes->append(*aIt);
494       }
495     }
496   }
497   return aList;
498 }