]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Merge branch 'vsr/make_test' into Dev_1.5.0
[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 <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       XGUI_DataModel* 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   connect(this, SIGNAL(doubleClicked(const QModelIndex&)), 
73     SLOT(onDoubleClick(const QModelIndex&)));
74 }
75
76 XGUI_DataTree::~XGUI_DataTree()
77 {
78 }
79
80 XGUI_DataModel* XGUI_DataTree::dataModel() const
81 {
82   return static_cast<XGUI_DataModel*>(model());
83 }
84
85 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
86 {
87   emit contextMenuRequested(theEvent);
88 }
89
90 void XGUI_DataTree::commitData(QWidget* theEditor)
91 {
92   static int aEntrance = 0;
93   if (aEntrance == 0) {
94     // We have to check number of enter and exit of this function because it can be called recursively by Qt
95     // in order to avoid double modifying of a data
96     aEntrance = 1;
97     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
98     if (aEditor) {
99       QString aName = aEditor->text();
100       QModelIndexList aIndexList = selectionModel()->selectedIndexes();
101       XGUI_DataModel* aModel = dataModel();
102       ObjectPtr aObj = aModel->object(aIndexList.first());
103
104       if (XGUI_Tools::canRename(aObj, aName))
105         aObj->data()->setName(qPrintable(aName));
106     }
107   }
108   aEntrance = 0;
109 }
110
111 void XGUI_DataTree::clear() 
112 {
113   dataModel()->clear();
114   reset();
115 }
116
117 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
118 {
119   QSize aSize = theEvent->size();
120   if (aSize.isValid()) {
121     setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
122     setColumnWidth(1, SECOND_COL_WIDTH);
123   }
124 }
125
126 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
127 {
128   if (theIndex.column() != 1)
129     return;
130   SessionPtr aMgr = ModelAPI_Session::get();
131   // When operation is opened then we can not change history
132   if (aMgr->isOperation())
133     return;
134   XGUI_DataModel* aModel = dataModel();
135   if (aModel->flags(theIndex) == 0)
136     return;
137   ObjectPtr aObj = aModel->object(theIndex);
138
139   DocumentPtr aDoc = aMgr->activeDocument();
140   
141   std::string aOpName = tr("History change").toStdString();
142   if (aObj.get()) {
143     if (aObj->document() != aDoc)
144       return;
145     aMgr->startOperation(aOpName);
146     aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
147     aMgr->finishOperation();
148   } else {
149     // Ignore clicks on folders outside current document
150     if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
151       // Clicked folder under root but active document is another
152       return;
153     if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
154       // Cliced not on active document folder
155       return;
156
157     aMgr->startOperation(aOpName);
158     aDoc->setCurrentFeature(FeaturePtr(), true);
159     aMgr->finishOperation();
160   }
161   QModelIndex aNewIndex = aModel->lastHistoryIndex();
162   QModelIndex aParent = theIndex.parent();
163   int aSize = aModel->rowCount(aParent);
164   for (int i = 0; i < aSize; i++) {
165     update(aModel->index(i, 0, aParent));
166   }
167 }
168
169 //********************************************************************
170 //********************************************************************
171 //********************************************************************
172 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
173     : QWidget(theParent), myDocModel(0)
174 {
175   QVBoxLayout* aLayout = new QVBoxLayout(this);
176   ModuleBase_Tools::zeroMargins(aLayout);
177   aLayout->setSpacing(0);
178
179   QFrame* aLabelWgt = new QFrame(this);
180   //QWidget* aLabelWgt = new QWidget(this);
181   aLabelWgt->setAutoFillBackground(true);
182   QPalette aPalet = aLabelWgt->palette();
183   aPalet.setColor(QPalette::Window, Qt::white);
184   aLabelWgt->setPalette(aPalet);
185
186   aLayout->addWidget(aLabelWgt);
187   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
188   ModuleBase_Tools::zeroMargins(aLabelLay);
189   aLabelLay->setSpacing(0);
190
191   QLabel* aLbl = new QLabel(aLabelWgt);
192   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
193   aLbl->setMargin(2);
194
195   aLbl->setAutoFillBackground(true);
196
197   aLabelLay->addWidget(aLbl);
198
199   SessionPtr aMgr = ModelAPI_Session::get();
200   DocumentPtr aDoc = aMgr->moduleDocument();
201   // TODO: Find a name of the root document
202
203   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
204   myActiveDocLbl->setReadOnly(true);
205   myActiveDocLbl->setFrame(false);
206   //myActiveDocLbl->setMargin(2);
207   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
208
209   myActiveDocLbl->installEventFilter(this);
210
211   aLabelLay->addWidget(myActiveDocLbl);
212   aLabelLay->setStretch(1, 1);
213
214   myTreeView = new XGUI_DataTree(this);
215   //myTreeView->setFrameShape(QFrame::NoFrame);
216   aLayout->addWidget(myTreeView);
217
218   aLabelWgt->setFrameShape(myTreeView->frameShape());
219   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
220
221   myDocModel = new XGUI_DataModel(this);
222   myTreeView->setModel(myDocModel);
223   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
224   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
225           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
226
227   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
228           SLOT(onLabelContextMenuRequested(const QPoint&)));
229   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
230           SLOT(onContextMenuRequested(QContextMenuEvent*)));
231 }
232
233 //***************************************************
234 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
235 {
236 }
237
238 //***************************************************
239 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
240 {
241   if (obj == myActiveDocLbl) {
242     if (!myActiveDocLbl->isReadOnly()) {
243       // End of editing by mouse click
244       if (theEvent->type() == QEvent::MouseButtonRelease) {
245         QMouseEvent* aEvent = (QMouseEvent*) theEvent;
246         QPoint aPnt = mapFromGlobal(aEvent->globalPos());
247         if (childAt(aPnt) != myActiveDocLbl) {
248           closeDocNameEditing(true);
249         }
250       } else if (theEvent->type() == QEvent::KeyRelease) {
251         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
252         switch (aEvent->key()) {
253           case Qt::Key_Return:
254           case Qt::Key_Enter:  // Accept current input
255             closeDocNameEditing(true);
256             break;
257           case Qt::Key_Escape:  // Cancel the input
258             closeDocNameEditing(false);
259             break;
260         }
261       }
262     } else {
263       if (theEvent->type() == QEvent::MouseButtonDblClick) {
264         emit headerMouseDblClicked(QModelIndex());
265         return true;
266       }  
267     }
268   }
269   return QWidget::eventFilter(obj, theEvent);
270 }
271
272 //***************************************************
273 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
274 {
275   myActiveDocLbl->deselect();
276   myActiveDocLbl->clearFocus();
277   myActiveDocLbl->releaseMouse();
278   myActiveDocLbl->setReadOnly(true);
279   if (toSave) {
280     // TODO: Save the name of root document
281     SessionPtr aMgr = ModelAPI_Session::get();
282     DocumentPtr aDoc = aMgr->moduleDocument();
283   } else {
284     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
285   }
286 }
287
288 //***************************************************
289 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
290 {
291   QModelIndexList aIndexes;
292   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
293   bool toEnable = false;
294
295   if (aSelectedData.size() == 1) {
296     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
297     if (!aIndexes.contains(aSelected))
298       return; // menu is called on non selected item
299
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 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
388                                        const QItemSelection& theDeselected)
389 {
390   emit selectionChanged();
391 }
392
393 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
394 {
395   QObjectPtrList aList;
396   QModelIndexList aIndexes = selectedIndexes();
397   XGUI_DataModel* aModel = dataModel();
398   QModelIndexList::const_iterator aIt;
399   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
400     if ((*aIt).column() == 0) {
401       ObjectPtr aObject = aModel->object(*aIt);
402       if (aObject) {
403         aList.append(aObject);
404         if (theIndexes)
405           theIndexes->append(*aIt);
406       }
407     }
408   }
409   return aList;
410 }