]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
4846e2237b2d4d5b6aa31f011880f9dc28bcda5a
[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 #ifndef ModuleDataModel
225   myDocModel = new XGUI_DataModel(this);
226   myTreeView->setModel(myDocModel);
227   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
228   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
229           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
230 #endif
231
232   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
233           SLOT(onLabelContextMenuRequested(const QPoint&)));
234   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
235           SLOT(onContextMenuRequested(QContextMenuEvent*)));
236 }
237
238 //***************************************************
239 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
240 {
241 }
242
243 //***************************************************
244 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
245 {
246   if (obj == myActiveDocLbl) {
247     if (!myActiveDocLbl->isReadOnly()) {
248       // End of editing by mouse click
249       if (theEvent->type() == QEvent::MouseButtonRelease) {
250         QMouseEvent* aEvent = (QMouseEvent*) theEvent;
251         QPoint aPnt = mapFromGlobal(aEvent->globalPos());
252         if (childAt(aPnt) != myActiveDocLbl) {
253           closeDocNameEditing(true);
254         }
255       } else if (theEvent->type() == QEvent::KeyRelease) {
256         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
257         switch (aEvent->key()) {
258           case Qt::Key_Return:
259           case Qt::Key_Enter:  // Accept current input
260             closeDocNameEditing(true);
261             break;
262           case Qt::Key_Escape:  // Cancel the input
263             closeDocNameEditing(false);
264             break;
265         }
266       }
267     } else {
268       if (theEvent->type() == QEvent::MouseButtonDblClick) {
269         emit headerMouseDblClicked(QModelIndex());
270         return true;
271       }  
272     }
273   }
274   return QWidget::eventFilter(obj, theEvent);
275 }
276
277 //***************************************************
278 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
279 {
280   myActiveDocLbl->deselect();
281   myActiveDocLbl->clearFocus();
282   myActiveDocLbl->releaseMouse();
283   myActiveDocLbl->setReadOnly(true);
284   if (toSave) {
285     // TODO: Save the name of root document
286     SessionPtr aMgr = ModelAPI_Session::get();
287     DocumentPtr aDoc = aMgr->moduleDocument();
288   } else {
289     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
290   }
291 }
292
293 //***************************************************
294 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
295 {
296   QModelIndexList aIndexes;
297   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
298   bool toEnable = false;
299   if (aSelectedData.size() == 1) {
300     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
301     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
302   }
303   foreach(QAction* aCmd, actions()) {
304     aCmd->setEnabled(toEnable);
305   }
306   emit contextMenuRequested(theEvent);
307 }
308
309 //***************************************************
310 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
311 {
312   myTreeView->selectionModel()->clearSelection();
313   //Empty feature pointer means that selected root document
314   foreach(QAction* aCmd, actions()) {
315     aCmd->setEnabled(true);
316   }
317   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
318   emit contextMenuRequested(&aEvent);
319 }
320
321 //***************************************************
322 void XGUI_ObjectsBrowser::onEditItem()
323 {
324   QObjectPtrList aSelectedData = selectedObjects();
325   if (aSelectedData.size() > 0) {
326     ObjectPtr aFeature = aSelectedData.first();
327     if (aFeature) {  // Selection happens in TreeView
328       QObjectPtrList aList;
329       aList.append(aFeature);
330       // check whether the object can be deleted. There should not be parts which are not loaded
331       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
332         return;
333
334       // Find index which corresponds the feature
335       QModelIndex aIndex;
336       foreach(QModelIndex aIdx, selectedIndexes()) {
337         ObjectPtr aFea = dataModel()->object(aIdx);
338         if (dataModel()->object(aIdx)->isSame(aFeature)) {
339           aIndex = aIdx;
340           break;
341         }
342       }
343       if (aIndex.isValid()) {
344         myTreeView->setCurrentIndex(aIndex);
345         myTreeView->edit(aIndex);
346       }
347       return;
348     }
349   }
350   //Selection happens in Upper label
351   myActiveDocLbl->setReadOnly(false);
352   myActiveDocLbl->setFocus();
353   myActiveDocLbl->selectAll();
354   myActiveDocLbl->grabMouse();
355   myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
356 }
357
358 //***************************************************
359 void XGUI_ObjectsBrowser::rebuildDataTree()
360 {
361   myDocModel->rebuildDataTree();
362   update();
363 }
364
365 //***************************************************
366 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
367 {
368   QList<QModelIndex> theIndexes;
369   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
370   aSelectModel->clear();
371
372   foreach(ObjectPtr aFeature, theObjects)
373   {
374     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
375     if (aIndex.isValid()) {
376       aSelectModel->select(aIndex, QItemSelectionModel::Select);
377     }
378   }
379 }
380
381 //***************************************************
382 void XGUI_ObjectsBrowser::clearContent()  
383
384   myTreeView->clear(); 
385 }
386
387 #ifdef ModuleDataModel
388 void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
389 {
390   myDocModel = theModel;
391   //myDocModel = new XGUI_DataModel(this);
392   myTreeView->setModel(myDocModel);
393   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
394   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
395           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
396 }
397 #endif
398
399 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
400                                        const QItemSelection& theDeselected)
401 {
402   emit selectionChanged();
403 }
404
405 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
406 {
407   QObjectPtrList aList;
408   QModelIndexList aIndexes = selectedIndexes();
409 #ifdef ModuleDataModel
410   ModuleBase_IDocumentDataModel* aModel = dataModel();
411 #else
412   XGUI_DataModel* aModel = dataModel();
413 #endif
414   QModelIndexList::const_iterator aIt;
415   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
416     if ((*aIt).column() == 0) {
417       ObjectPtr aObject = aModel->object(*aIt);
418       if (aObject) {
419         aList.append(aObject);
420         if (theIndexes)
421           theIndexes->append(*aIt);
422       }
423     }
424   }
425   return aList;
426 }