]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Merge branch 'Dev_1.5.0' into BR_REENTRANCE_OPERATION
[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 <QLabel>
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   }
186 }
187
188 #if (!defined HAVE_SALOME) && (defined WIN32)
189 void XGUI_DataTree::drawRow(QPainter* thePainter,
190                             const QStyleOptionViewItem& theOptions,
191                             const QModelIndex& theIndex) const
192 {
193   QStyleOptionViewItemV4 aOptions = theOptions;
194   myStyle->setIndex(theIndex);
195   QTreeView::drawRow(thePainter, aOptions, theIndex);
196 }
197
198 //********************************************************************
199 //********************************************************************
200 //********************************************************************
201 void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement, 
202                                        const QStyleOption* theOption,
203                                        QPainter* thePainter, const QWidget* theWidget) const
204 {
205   if ((theElement == QStyle::PE_PanelItemViewRow) || (theElement == QStyle::PE_PanelItemViewItem)) {
206     const QStyleOptionViewItemV4* aOptions = qstyleoption_cast<const QStyleOptionViewItemV4 *>(theOption);
207     if (myIndex.isValid() && ((myIndex.flags() & Qt::ItemIsSelectable) == 0)) {
208       QStyle::State aState = aOptions->state;
209       if ((aState & QStyle::State_MouseOver) != 0)
210         aState &= ~QStyle::State_MouseOver;
211       QStyleOptionViewItemV4* aOpt = (QStyleOptionViewItemV4*) aOptions;
212       aOpt->state = aState;
213       QWindowsVistaStyle::drawPrimitive(theElement, aOpt, thePainter, theWidget);
214     }
215   }
216   QWindowsVistaStyle::drawPrimitive(theElement, theOption, thePainter, theWidget);
217 }
218 #endif
219
220
221 //********************************************************************
222 //********************************************************************
223 //********************************************************************
224 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
225   : QLineEdit(theText, theParent), 
226   myPreSelectionStyle(""), 
227   myNeutralStyle(""), 
228   mySelectionStyle(""),
229   myIsSelected(false)
230 {
231 }
232
233 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
234 {
235   myTreeView = theView;
236   QPalette aPalet = myTreeView->palette();
237   QColor aHighlight = aPalet.highlight().color();
238   QColor aHighlightText = aPalet.highlightedText().color();
239
240   myPreSelectionStyle = "QLineEdit {background-color: ";
241   myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");"; 
242   myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
243
244   QString aName = aPalet.color(QPalette::Base).name();
245   myNeutralStyle = "QLineEdit { border: 1px solid " + aName + " }";
246
247
248 #if (!defined HAVE_SALOME) && (defined WIN32)
249   mySelectionStyle = "QLineEdit {background-color: ";
250   mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
251   mySelectionStyle += ", stop:1 rgb(208, 229, 255));"; 
252   mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
253 #else
254   mySelectionStyle = "QLineEdit {background-color: " + aHighlight.name();
255   mySelectionStyle += "; color : " + aHighlightText.name() + "}";
256 #endif
257
258   myTreeView->viewport()->installEventFilter(this);
259 }
260
261
262 #if (!defined HAVE_SALOME) && (defined WIN32)
263 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
264 {
265   switch (theEvent->type()) {
266     case QEvent::Enter:
267       if (!myIsSelected)
268         setStyleSheet(myPreSelectionStyle);
269       break;
270     case QEvent::Leave:
271       if (!myIsSelected)
272         setStyleSheet(myNeutralStyle);
273       break;
274   }
275   return QLineEdit::event(theEvent);
276 }
277 #endif
278
279 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
280 {
281   if (theObj == myTreeView->viewport()) {
282     if (theEvent->type() == QEvent::MouseButtonRelease)
283       unselect();
284   }
285   return QLineEdit::eventFilter(theObj, theEvent);
286 }
287
288 static bool MYClearing = false;
289 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
290 {
291   MYClearing = true;
292   myIsSelected = true;
293   setStyleSheet(mySelectionStyle);
294   // We can not block signals because on 
295   // clear selection the View state will not be updated
296   myTreeView->clearSelection();
297   QLineEdit::mouseReleaseEvent(e);
298   MYClearing = false;
299 }
300
301 void XGUI_ActiveDocLbl::unselect()
302 {
303   if (!MYClearing) {
304     myIsSelected = false;
305     setStyleSheet(myNeutralStyle);
306   }
307 }
308
309
310 //********************************************************************
311 //********************************************************************
312 //********************************************************************
313 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
314     : QWidget(theParent), myDocModel(0)
315 {
316   QVBoxLayout* aLayout = new QVBoxLayout(this);
317   ModuleBase_Tools::zeroMargins(aLayout);
318   aLayout->setSpacing(0);
319
320   QWidget* aLabelWgt = new QWidget(this);
321   aLabelWgt->setAutoFillBackground(true);
322
323   aLayout->addWidget(aLabelWgt);
324   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
325   ModuleBase_Tools::zeroMargins(aLabelLay);
326   aLabelLay->setSpacing(0);
327
328   QLabel* aLbl = new QLabel(aLabelWgt);
329   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
330   aLbl->setMargin(2);
331   // Do not paint background of the label (in order to show icon)
332   aLbl->setAutoFillBackground(false); 
333
334   aLabelLay->addWidget(aLbl);
335
336   SessionPtr aMgr = ModelAPI_Session::get();
337   DocumentPtr aDoc = aMgr->moduleDocument();
338
339   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
340   myActiveDocLbl->setReadOnly(true);
341   myActiveDocLbl->setFrame(false);
342   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
343
344   aLabelLay->addWidget(myActiveDocLbl);
345   aLabelLay->setStretch(1, 1);
346
347   myTreeView = new XGUI_DataTree(this);
348   myTreeView->setFrameShape(QFrame::NoFrame);
349   aLayout->addWidget(myTreeView);
350
351   QPalette aTreePalet = myTreeView->palette();
352   QColor aTreeBack = aTreePalet.color(QPalette::Base);
353
354   QPalette aPalet;
355   aPalet.setColor(QPalette::Base, aTreeBack);
356   aPalet.setColor(QPalette::Window, aTreeBack);
357   aLabelWgt->setPalette(aPalet);
358
359   myDocModel = new XGUI_DataModel(this);
360   myTreeView->setModel(myDocModel);
361
362   // It has to be done after setting of model
363   myActiveDocLbl->setTreeView(myTreeView);
364
365   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
366   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
367           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
368
369   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
370           SLOT(onContextMenuRequested(QContextMenuEvent*)));
371 }
372
373 //***************************************************
374 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
375 {
376 }
377
378
379 //***************************************************
380 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
381 {
382   QModelIndexList aIndexes;
383   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
384   bool toEnable = false;
385
386   if (aSelectedData.size() == 1) {
387     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
388     if (!aIndexes.contains(aSelected))
389       return; // menu is called on non selected item
390
391     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
392     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
393   }
394   foreach(QAction* aCmd, actions()) {
395     aCmd->setEnabled(toEnable);
396   }
397   emit contextMenuRequested(theEvent);
398 }
399
400 //***************************************************
401 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
402 {
403   myTreeView->selectionModel()->clearSelection();
404   //Empty feature pointer means that selected root document
405   foreach(QAction* aCmd, actions()) {
406     aCmd->setEnabled(true);
407   }
408   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
409   emit contextMenuRequested(&aEvent);
410 }
411
412 //***************************************************
413 void XGUI_ObjectsBrowser::onEditItem()
414 {
415   QObjectPtrList aSelectedData = selectedObjects();
416   if (aSelectedData.size() > 0) {
417     ObjectPtr aFeature = aSelectedData.first();
418     if (aFeature) {  // Selection happens in TreeView
419       QObjectPtrList aList;
420       aList.append(aFeature);
421       // check whether the object can be deleted. There should not be parts which are not loaded
422       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
423         return;
424
425       // Find index which corresponds the feature
426       QModelIndex aIndex;
427       foreach(QModelIndex aIdx, selectedIndexes()) {
428         ObjectPtr aFea = dataModel()->object(aIdx);
429         if (dataModel()->object(aIdx)->isSame(aFeature)) {
430           aIndex = aIdx;
431           break;
432         }
433       }
434       if (aIndex.isValid()) {
435         myTreeView->setCurrentIndex(aIndex);
436         myTreeView->edit(aIndex);
437       }
438       return;
439     }
440   }
441 }
442
443 //***************************************************
444 void XGUI_ObjectsBrowser::rebuildDataTree()
445 {
446   myDocModel->rebuildDataTree();
447   update();
448 }
449
450 //***************************************************
451 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
452 {
453   QList<QModelIndex> theIndexes;
454   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
455   aSelectModel->clear();
456
457   foreach(ObjectPtr aFeature, theObjects)
458   {
459     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
460     if (aIndex.isValid()) {
461       aSelectModel->select(aIndex, QItemSelectionModel::Select);
462     }
463   }
464 }
465
466 //***************************************************
467 void XGUI_ObjectsBrowser::clearContent()  
468
469   myTreeView->clear(); 
470 }
471
472 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
473                                        const QItemSelection& theDeselected)
474 {
475   emit selectionChanged();
476 }
477
478 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
479 {
480   QObjectPtrList aList;
481   QModelIndexList aIndexes = selectedIndexes();
482   XGUI_DataModel* aModel = dataModel();
483   QModelIndexList::const_iterator aIt;
484   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
485     if ((*aIt).column() == 0) {
486       ObjectPtr aObject = aModel->object(*aIt);
487       if (aObject) {
488         aList.append(aObject);
489         if (theIndexes)
490           theIndexes->append(*aIt);
491       }
492     }
493   }
494   return aList;
495 }