Salome HOME
Issue #905 Update of invalid feature representation in property panel
[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 #include <ModuleBase_IDocumentDataModel.h>
14
15 #include <QLayout>
16 #include <QLabel>
17 #include <QLineEdit>
18 #include <QPixmap>
19 #include <QEvent>
20 #include <QMouseEvent>
21 #include <QAction>
22 #include <QStyledItemDelegate>
23 #include <QMessageBox>
24
25 /// Width of second column (minimum acceptable = 27)
26 #define SECOND_COL_WIDTH 30
27
28
29 /**
30 * \ingroup GUI
31 * Tree item delegate for definition of data in column items editor
32 */
33 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
34 {
35 public:
36   /// Constructor
37   /// \param theParent a parent of the delegate
38   XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
39
40   /// Set data for item editor (name of the item)
41   /// \param editor a widget of editor
42   /// \param index the tree item index
43   virtual void  setEditorData ( QWidget* editor, const QModelIndex& index ) const
44   {
45     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
46     if (aEditor) {
47       ModuleBase_IDocumentDataModel* aModel = myTreedView->dataModel();
48       ObjectPtr aObj = aModel->object(index);
49       if (aObj.get() != NULL) {
50         aEditor->setText(aObj->data()->name().c_str());
51         return;
52       }
53     }
54     QStyledItemDelegate::setEditorData(editor, index);
55   }
56
57 private:
58   XGUI_DataTree* myTreedView;
59 };
60
61
62 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
63     : QTreeView(theParent)
64 {
65   setHeaderHidden(true);
66   setEditTriggers(QAbstractItemView::NoEditTriggers);
67   setSelectionBehavior(QAbstractItemView::SelectRows);
68   setSelectionMode(QAbstractItemView::ExtendedSelection);
69
70   setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
71
72 #ifndef ModuleDataModel
73   connect(this, SIGNAL(doubleClicked(const QModelIndex&)), 
74     SLOT(onDoubleClick(const QModelIndex&)));
75 #endif
76 }
77
78 XGUI_DataTree::~XGUI_DataTree()
79 {
80 }
81
82 ModuleBase_IDocumentDataModel* XGUI_DataTree::dataModel() const
83 {
84   return static_cast<ModuleBase_IDocumentDataModel*>(model());
85 }
86
87 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
88 {
89   emit contextMenuRequested(theEvent);
90 }
91
92 void XGUI_DataTree::commitData(QWidget* theEditor)
93 {
94   QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
95   if (aEditor) {
96     QString aName = aEditor->text();
97     QModelIndexList aIndexList = selectionModel()->selectedIndexes();
98     ModuleBase_IDocumentDataModel* aModel = dataModel();
99     ObjectPtr aObj = aModel->object(aIndexList.first());
100     SessionPtr aMgr = ModelAPI_Session::get();
101     aMgr->startOperation("Rename");
102
103     if (!XGUI_Tools::canRename(this, aObj, aName)) {
104       aMgr->abortOperation();
105       return;
106     }
107
108     aObj->data()->setName(qPrintable(aName));
109     aMgr->finishOperation();
110   }
111 }
112
113 void XGUI_DataTree::clear() 
114 {
115   ModuleBase_IDocumentDataModel* aModel = dataModel();
116   aModel->clear();
117   reset();
118 }
119
120 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
121 {
122   QSize aSize = theEvent->size();
123   if (aSize.isValid()) {
124     setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
125     setColumnWidth(1, SECOND_COL_WIDTH);
126   }
127 }
128
129 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
130 {
131   if (theIndex.column() != 1)
132     return;
133   SessionPtr aMgr = ModelAPI_Session::get();
134   // When operation is opened then we can not change history
135   if (aMgr->isOperation())
136     return;
137   ModuleBase_IDocumentDataModel* aModel = dataModel();
138   if (aModel->flags(theIndex) == 0)
139     return;
140   ObjectPtr aObj = aModel->object(theIndex);
141
142   DocumentPtr aDoc = aMgr->activeDocument();
143   
144   std::string aOpName = tr("History change").toStdString();
145   if (aObj.get()) {
146     if (aObj->document() != aDoc)
147       return;
148     aMgr->startOperation(aOpName);
149     aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
150     aMgr->finishOperation();
151   } else {
152     // Ignore clicks on folders outside current document
153     if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
154       // Clicked folder under root but active document is another
155       return;
156     if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
157       // Cliced not on active document folder
158       return;
159
160     aMgr->startOperation(aOpName);
161     aDoc->setCurrentFeature(FeaturePtr(), true);
162     aMgr->finishOperation();
163   }
164   QModelIndex aNewIndex = aModel->lastHistoryIndex();
165   QModelIndex aParent = theIndex.parent();
166   int aSize = aModel->rowCount(aParent);
167   for (int i = 0; i < aSize; i++) {
168     update(aModel->index(i, 0, aParent));
169   }
170 }
171
172 //********************************************************************
173 //********************************************************************
174 //********************************************************************
175 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
176     : QWidget(theParent), myDocModel(0)
177 {
178   QVBoxLayout* aLayout = new QVBoxLayout(this);
179   ModuleBase_Tools::zeroMargins(aLayout);
180   aLayout->setSpacing(0);
181
182   QFrame* aLabelWgt = new QFrame(this);
183   aLabelWgt->setAutoFillBackground(true);
184   QPalette aPalet = aLabelWgt->palette();
185   aPalet.setColor(QPalette::Window, Qt::white);
186   aLabelWgt->setPalette(aPalet);
187
188   aLayout->addWidget(aLabelWgt);
189   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
190   ModuleBase_Tools::zeroMargins(aLabelLay);
191   aLabelLay->setSpacing(0);
192
193   QLabel* aLbl = new QLabel(aLabelWgt);
194   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
195   aLbl->setMargin(2);
196
197   aLbl->setAutoFillBackground(true);
198
199   aLabelLay->addWidget(aLbl);
200
201   SessionPtr aMgr = ModelAPI_Session::get();
202   DocumentPtr aDoc = aMgr->moduleDocument();
203   // TODO: Find a name of the root document
204
205   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
206   myActiveDocLbl->setReadOnly(true);
207   myActiveDocLbl->setFrame(false);
208   //myActiveDocLbl->setMargin(2);
209   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
210
211   myActiveDocLbl->installEventFilter(this);
212
213   aLabelLay->addWidget(myActiveDocLbl);
214   aLabelLay->setStretch(1, 1);
215
216   myTreeView = new XGUI_DataTree(this);
217   aLayout->addWidget(myTreeView);
218
219   aLabelWgt->setFrameShape(myTreeView->frameShape());
220   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
221
222 #ifndef ModuleDataModel
223   myDocModel = new XGUI_DataModel(this);
224   myTreeView->setModel(myDocModel);
225   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
226   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
227           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
228 #endif
229
230   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
231           SLOT(onLabelContextMenuRequested(const QPoint&)));
232   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
233           SLOT(onContextMenuRequested(QContextMenuEvent*)));
234 }
235
236 //***************************************************
237 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
238 {
239 }
240
241 //***************************************************
242 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
243 {
244   if (obj == myActiveDocLbl) {
245     if (!myActiveDocLbl->isReadOnly()) {
246       // End of editing by mouse click
247       if (theEvent->type() == QEvent::MouseButtonRelease) {
248         QMouseEvent* aEvent = (QMouseEvent*) theEvent;
249         QPoint aPnt = mapFromGlobal(aEvent->globalPos());
250         if (childAt(aPnt) != myActiveDocLbl) {
251           closeDocNameEditing(true);
252         }
253       } else if (theEvent->type() == QEvent::KeyRelease) {
254         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
255         switch (aEvent->key()) {
256           case Qt::Key_Return:
257           case Qt::Key_Enter:  // Accept current input
258             closeDocNameEditing(true);
259             break;
260           case Qt::Key_Escape:  // Cancel the input
261             closeDocNameEditing(false);
262             break;
263         }
264       }
265     } else {
266       if (theEvent->type() == QEvent::MouseButtonDblClick) {
267         emit headerMouseDblClicked(QModelIndex());
268         return true;
269       }  
270     }
271   }
272   return QWidget::eventFilter(obj, theEvent);
273 }
274
275 //***************************************************
276 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
277 {
278   myActiveDocLbl->deselect();
279   myActiveDocLbl->clearFocus();
280   myActiveDocLbl->releaseMouse();
281   myActiveDocLbl->setReadOnly(true);
282   if (toSave) {
283     // TODO: Save the name of root document
284     SessionPtr aMgr = ModelAPI_Session::get();
285     DocumentPtr aDoc = aMgr->moduleDocument();
286   } else {
287     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
288   }
289 }
290
291 //***************************************************
292 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
293 {
294   QModelIndexList aIndexes;
295   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
296   bool toEnable = false;
297   if (aSelectedData.size() == 1) {
298     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
299     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
300   }
301   foreach(QAction* aCmd, actions()) {
302     aCmd->setEnabled(toEnable);
303   }
304   emit contextMenuRequested(theEvent);
305 }
306
307 //***************************************************
308 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
309 {
310   myTreeView->selectionModel()->clearSelection();
311   //Empty feature pointer means that selected root document
312   foreach(QAction* aCmd, actions()) {
313     aCmd->setEnabled(true);
314   }
315   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
316   emit contextMenuRequested(&aEvent);
317 }
318
319 //***************************************************
320 void XGUI_ObjectsBrowser::onEditItem()
321 {
322   QObjectPtrList aSelectedData = selectedObjects();
323   if (aSelectedData.size() > 0) {
324     ObjectPtr aFeature = aSelectedData.first();
325     if (aFeature) {  // Selection happens in TreeView
326       QObjectPtrList aList;
327       aList.append(aFeature);
328       // check whether the object can be deleted. There should not be parts which are not loaded
329       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
330         return;
331
332       // Find index which corresponds the feature
333       QModelIndex aIndex;
334       foreach(QModelIndex aIdx, selectedIndexes()) {
335         ObjectPtr aFea = dataModel()->object(aIdx);
336         if (dataModel()->object(aIdx)->isSame(aFeature)) {
337           aIndex = aIdx;
338           break;
339         }
340       }
341       if (aIndex.isValid()) {
342         myTreeView->setCurrentIndex(aIndex);
343         myTreeView->edit(aIndex);
344       }
345       return;
346     }
347   }
348   //Selection happens in Upper label
349   myActiveDocLbl->setReadOnly(false);
350   myActiveDocLbl->setFocus();
351   myActiveDocLbl->selectAll();
352   myActiveDocLbl->grabMouse();
353   myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
354 }
355
356 //***************************************************
357 void XGUI_ObjectsBrowser::rebuildDataTree()
358 {
359   myDocModel->rebuildDataTree();
360   update();
361 }
362
363 //***************************************************
364 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
365 {
366   QList<QModelIndex> theIndexes;
367   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
368   aSelectModel->clear();
369
370   foreach(ObjectPtr aFeature, theObjects)
371   {
372     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
373     if (aIndex.isValid()) {
374       aSelectModel->select(aIndex, QItemSelectionModel::Select);
375     }
376   }
377 }
378
379 //***************************************************
380 void XGUI_ObjectsBrowser::clearContent()  
381
382   myTreeView->clear(); 
383 }
384
385 #ifdef ModuleDataModel
386 void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
387 {
388   myDocModel = theModel;
389   //myDocModel = new XGUI_DataModel(this);
390   myTreeView->setModel(myDocModel);
391   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
392   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
393           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
394 }
395 #endif
396
397 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
398                                        const QItemSelection& theDeselected)
399 {
400   emit selectionChanged();
401 }
402
403 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
404 {
405   QObjectPtrList aList;
406   QModelIndexList aIndexes = selectedIndexes();
407 #ifdef ModuleDataModel
408   ModuleBase_IDocumentDataModel* aModel = dataModel();
409 #else
410   XGUI_DataModel* aModel = dataModel();
411 #endif
412   QModelIndexList::const_iterator aIt;
413   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
414     if ((*aIt).column() == 0) {
415       ObjectPtr aObject = aModel->object(*aIt);
416       if (aObject) {
417         aList.append(aObject);
418         if (theIndexes)
419           theIndexes->append(*aIt);
420       }
421     }
422   }
423   return aList;
424 }