Salome HOME
1. Mirror/Rotation/Translation constraint: using RefList instead of SelectionList
[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   ModuleBase_IDocumentDataModel* aModel = dataModel();
134   if (aModel->flags(theIndex) == 0)
135     return;
136   ObjectPtr aObj = aModel->object(theIndex);
137
138   SessionPtr aMgr = ModelAPI_Session::get();
139   DocumentPtr aDoc = aMgr->activeDocument();
140   
141   QModelIndex aOldIndex = aModel->lastHistoryIndex();
142
143   std::string aOpName = tr("History change").toStdString();
144   if (aObj.get()) {
145     if (aObj->document() != aDoc)
146       return;
147     aMgr->startOperation(aOpName);
148     aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
149     aMgr->finishOperation();
150   } else {
151     // Ignore clicks on folders outside current document
152     if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
153       // Clicked folder under root but active document is another
154       return;
155     if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
156       // Cliced not on active document folder
157       return;
158
159     aMgr->startOperation(aOpName);
160     aDoc->setCurrentFeature(FeaturePtr(), true);
161     aMgr->finishOperation();
162   }
163   QModelIndex aNewIndex = aModel->lastHistoryIndex();
164   QModelIndex aParent = theIndex.parent();
165   int aStartRow = std::min(aOldIndex.row(), aNewIndex.row());
166   int aEndRow = std::max(aOldIndex.row(), aNewIndex.row());
167   for (int i = aStartRow; i <= aEndRow; i++) {
168     update(aModel->index(i, 0, aParent));
169   }
170   update(aOldIndex);
171   update(aNewIndex);
172 }
173
174 //********************************************************************
175 //********************************************************************
176 //********************************************************************
177 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
178     : QWidget(theParent), myDocModel(0)
179 {
180   QVBoxLayout* aLayout = new QVBoxLayout(this);
181   ModuleBase_Tools::zeroMargins(aLayout);
182   aLayout->setSpacing(0);
183
184   QFrame* aLabelWgt = new QFrame(this);
185   aLabelWgt->setAutoFillBackground(true);
186   QPalette aPalet = aLabelWgt->palette();
187   aPalet.setColor(QPalette::Window, Qt::white);
188   aLabelWgt->setPalette(aPalet);
189
190   aLayout->addWidget(aLabelWgt);
191   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
192   ModuleBase_Tools::zeroMargins(aLabelLay);
193   aLabelLay->setSpacing(0);
194
195   QLabel* aLbl = new QLabel(aLabelWgt);
196   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
197   aLbl->setMargin(2);
198
199   aLbl->setAutoFillBackground(true);
200
201   aLabelLay->addWidget(aLbl);
202
203   SessionPtr aMgr = ModelAPI_Session::get();
204   DocumentPtr aDoc = aMgr->moduleDocument();
205   // TODO: Find a name of the root document
206
207   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
208   myActiveDocLbl->setReadOnly(true);
209   myActiveDocLbl->setFrame(false);
210   //myActiveDocLbl->setMargin(2);
211   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
212
213   myActiveDocLbl->installEventFilter(this);
214
215   aLabelLay->addWidget(myActiveDocLbl);
216   aLabelLay->setStretch(1, 1);
217
218   myTreeView = new XGUI_DataTree(this);
219   aLayout->addWidget(myTreeView);
220
221   aLabelWgt->setFrameShape(myTreeView->frameShape());
222   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
223
224   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
225           SLOT(onLabelContextMenuRequested(const QPoint&)));
226   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
227           SLOT(onContextMenuRequested(QContextMenuEvent*)));
228 }
229
230 //***************************************************
231 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
232 {
233 }
234
235 //***************************************************
236 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
237 {
238   if (obj == myActiveDocLbl) {
239     if (!myActiveDocLbl->isReadOnly()) {
240       // End of editing by mouse click
241       if (theEvent->type() == QEvent::MouseButtonRelease) {
242         QMouseEvent* aEvent = (QMouseEvent*) theEvent;
243         QPoint aPnt = mapFromGlobal(aEvent->globalPos());
244         if (childAt(aPnt) != myActiveDocLbl) {
245           closeDocNameEditing(true);
246         }
247       } else if (theEvent->type() == QEvent::KeyRelease) {
248         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
249         switch (aEvent->key()) {
250           case Qt::Key_Return:
251           case Qt::Key_Enter:  // Accept current input
252             closeDocNameEditing(true);
253             break;
254           case Qt::Key_Escape:  // Cancel the input
255             closeDocNameEditing(false);
256             break;
257         }
258       }
259     } else {
260       if (theEvent->type() == QEvent::MouseButtonDblClick) {
261         emit headerMouseDblClicked(QModelIndex());
262         return true;
263       }  
264     }
265   }
266   return QWidget::eventFilter(obj, theEvent);
267 }
268
269 //***************************************************
270 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
271 {
272   myActiveDocLbl->deselect();
273   myActiveDocLbl->clearFocus();
274   myActiveDocLbl->releaseMouse();
275   myActiveDocLbl->setReadOnly(true);
276   if (toSave) {
277     // TODO: Save the name of root document
278     SessionPtr aMgr = ModelAPI_Session::get();
279     DocumentPtr aDoc = aMgr->moduleDocument();
280   } else {
281     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
282   }
283 }
284
285 //***************************************************
286 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
287 {
288   QModelIndexList aIndexes;
289   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
290   bool toEnable = false;
291   if (aSelectedData.size() == 1) {
292     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
293     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
294   }
295   foreach(QAction* aCmd, actions()) {
296     aCmd->setEnabled(toEnable);
297   }
298   emit contextMenuRequested(theEvent);
299 }
300
301 //***************************************************
302 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
303 {
304   myTreeView->selectionModel()->clearSelection();
305   //Empty feature pointer means that selected root document
306   foreach(QAction* aCmd, actions()) {
307     aCmd->setEnabled(true);
308   }
309   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
310   emit contextMenuRequested(&aEvent);
311 }
312
313 //***************************************************
314 void XGUI_ObjectsBrowser::onEditItem()
315 {
316   QObjectPtrList aSelectedData = selectedObjects();
317   if (aSelectedData.size() > 0) {
318     ObjectPtr aFeature = aSelectedData.first();
319     if (aFeature) {  // Selection happens in TreeView
320       QObjectPtrList aList;
321       aList.append(aFeature);
322       // check whether the object can be deleted. There should not be parts which are not loaded
323       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
324         return;
325
326       // Find index which corresponds the feature
327       QModelIndex aIndex;
328       foreach(QModelIndex aIdx, selectedIndexes()) {
329         ObjectPtr aFea = dataModel()->object(aIdx);
330         if (dataModel()->object(aIdx)->isSame(aFeature)) {
331           aIndex = aIdx;
332           break;
333         }
334       }
335       if (aIndex.isValid()) {
336         myTreeView->setCurrentIndex(aIndex);
337         myTreeView->edit(aIndex);
338       }
339       return;
340     }
341   }
342   //Selection happens in Upper label
343   myActiveDocLbl->setReadOnly(false);
344   myActiveDocLbl->setFocus();
345   myActiveDocLbl->selectAll();
346   myActiveDocLbl->grabMouse();
347   myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
348 }
349
350 //***************************************************
351 void XGUI_ObjectsBrowser::rebuildDataTree()
352 {
353   myDocModel->rebuildDataTree();
354   update();
355 }
356
357 //***************************************************
358 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
359 {
360   QList<QModelIndex> theIndexes;
361   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
362   aSelectModel->clear();
363
364   foreach(ObjectPtr aFeature, theObjects)
365   {
366     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
367     if (aIndex.isValid()) {
368       aSelectModel->select(aIndex, QItemSelectionModel::Select);
369     }
370   }
371 }
372
373 //***************************************************
374 void XGUI_ObjectsBrowser::clearContent()  
375
376   myTreeView->clear(); 
377 }
378
379 void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
380 {
381 #ifdef ModuleDataModel
382   myDocModel = theModel;
383 #else
384   myDocModel = new XGUI_DataModel(this);
385 #endif
386   myTreeView->setModel(myDocModel);
387   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
388   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
389           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
390 }
391
392 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
393                                        const QItemSelection& theDeselected)
394 {
395   emit selectionChanged();
396 }
397
398 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
399 {
400   QObjectPtrList aList;
401   QModelIndexList aIndexes = selectedIndexes();
402 #ifdef ModuleDataModel
403   ModuleBase_IDocumentDataModel* aModel = dataModel();
404 #else
405   XGUI_DataModel* aModel = dataModel();
406 #endif
407   QModelIndexList::const_iterator aIt;
408   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
409     if ((*aIt).column() == 0) {
410       ObjectPtr aObject = aModel->object(*aIt);
411       if (aObject) {
412         aList.append(aObject);
413         if (theIndexes)
414           theIndexes->append(*aIt);
415       }
416     }
417   }
418   return aList;
419 }