]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Make label on ObjectBrowser top similar to tree node
[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 /// Width of second column (minimum acceptable = 27)
25 #define SECOND_COL_WIDTH 30
26
27
28 /**
29 * \ingroup GUI
30 * Tree item delegate for definition of data in column items editor
31 */
32 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
33 {
34 public:
35   /// Constructor
36   /// \param theParent a parent of the delegate
37   XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
38
39   /// Set data for item editor (name of the item)
40   /// \param editor a widget of editor
41   /// \param index the tree item index
42   virtual void  setEditorData ( QWidget* editor, const QModelIndex& index ) const
43   {
44     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
45     if (aEditor) {
46       XGUI_DataModel* aModel = myTreedView->dataModel();
47       ObjectPtr aObj = aModel->object(index);
48       if (aObj.get() != NULL) {
49         aEditor->setText(aObj->data()->name().c_str());
50         return;
51       }
52     }
53     QStyledItemDelegate::setEditorData(editor, index);
54   }
55
56 private:
57   XGUI_DataTree* myTreedView;
58 };
59
60
61 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
62     : QTreeView(theParent)
63 {
64   setHeaderHidden(true);
65   setEditTriggers(QAbstractItemView::NoEditTriggers);
66   setSelectionBehavior(QAbstractItemView::SelectRows);
67   setSelectionMode(QAbstractItemView::ExtendedSelection);
68
69   setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
70
71   connect(this, SIGNAL(doubleClicked(const QModelIndex&)), 
72     SLOT(onDoubleClick(const QModelIndex&)));
73 }
74
75 XGUI_DataTree::~XGUI_DataTree()
76 {
77 }
78
79 XGUI_DataModel* XGUI_DataTree::dataModel() const
80 {
81   return static_cast<XGUI_DataModel*>(model());
82 }
83
84 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
85 {
86   emit contextMenuRequested(theEvent);
87 }
88
89 void XGUI_DataTree::commitData(QWidget* theEditor)
90 {
91   static int aEntrance = 0;
92   if (aEntrance == 0) {
93     // We have to check number of enter and exit of this function because it can be called recursively by Qt
94     // in order to avoid double modifying of a data
95     aEntrance = 1;
96     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
97     if (aEditor) {
98       QString aName = aEditor->text();
99       QModelIndexList aIndexList = selectionModel()->selectedIndexes();
100       XGUI_DataModel* aModel = dataModel();
101       ObjectPtr aObj = aModel->object(aIndexList.first());
102
103       if (XGUI_Tools::canRename(aObj, aName)) {
104         SessionPtr aMgr = ModelAPI_Session::get();
105         aMgr->startOperation("Rename");
106         aObj->data()->setName(qPrintable(aName));
107         aMgr->finishOperation();
108       }
109     }
110   }
111   aEntrance = 0;
112 }
113
114 void XGUI_DataTree::clear() 
115 {
116   dataModel()->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   XGUI_DataModel* 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_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
176   : QLineEdit(theText, theParent), 
177   myPreSelectionStyle(""), 
178   myNeutralStyle(""), 
179   mySelectionStyle(""),
180   myIsSelected(false)
181 {
182 }
183
184 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
185 {
186   myTreeView = theView;
187   QPalette aPalet = myTreeView->palette();
188   QColor aHighlight = aPalet.highlight().color();
189
190   myPreSelectionStyle = "QLineEdit {background-color: ";
191   myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");"; 
192   myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
193
194   QString aName = aPalet.color(QPalette::Base).name();
195   myNeutralStyle = "QLineEdit { border: 1px solid " + aName + " }";
196
197   mySelectionStyle = "QLineEdit {background-color: ";
198   mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 " + aHighlight.lighter(170).name();
199   mySelectionStyle += ", stop:1 " + aHighlight.lighter().name() + ");"; 
200   mySelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
201
202   connect(myTreeView->selectionModel(), 
203     SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
204     SLOT(unselect()));
205 }
206
207
208 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
209 {
210   switch (theEvent->type()) {
211     case QEvent::Enter:
212       if (!myIsSelected)
213         setStyleSheet(myPreSelectionStyle);
214       break;
215     case QEvent::Leave:
216       if (!myIsSelected)
217         setStyleSheet(myNeutralStyle);
218       break;
219   }
220   return QLineEdit::event(theEvent);
221 }
222
223 static bool MYClearing = false;
224 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
225 {
226   MYClearing = true;
227   myIsSelected = true;
228   setStyleSheet(mySelectionStyle);
229   // We can not block signals because on 
230   // clear selection the View state will not be updated
231   myTreeView->clearSelection();
232   QLineEdit::mouseReleaseEvent(e);
233   MYClearing = false;
234 }
235
236 void XGUI_ActiveDocLbl::unselect()
237 {
238   if (!MYClearing) {
239     myIsSelected = false;
240     setStyleSheet(myNeutralStyle);
241   }
242 }
243
244
245 //********************************************************************
246 //********************************************************************
247 //********************************************************************
248 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
249     : QWidget(theParent), myDocModel(0)
250 {
251   QVBoxLayout* aLayout = new QVBoxLayout(this);
252   ModuleBase_Tools::zeroMargins(aLayout);
253   aLayout->setSpacing(0);
254
255   QWidget* aLabelWgt = new QWidget(this);
256   aLabelWgt->setAutoFillBackground(true);
257
258   aLayout->addWidget(aLabelWgt);
259   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
260   ModuleBase_Tools::zeroMargins(aLabelLay);
261   aLabelLay->setSpacing(0);
262
263   QLabel* aLbl = new QLabel(aLabelWgt);
264   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
265   aLbl->setMargin(2);
266   // Do not paint background of the label (in order to show icon)
267   aLbl->setAutoFillBackground(false); 
268
269   aLabelLay->addWidget(aLbl);
270
271   SessionPtr aMgr = ModelAPI_Session::get();
272   DocumentPtr aDoc = aMgr->moduleDocument();
273
274   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
275   myActiveDocLbl->setReadOnly(true);
276   myActiveDocLbl->setFrame(false);
277   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
278
279   aLabelLay->addWidget(myActiveDocLbl);
280   aLabelLay->setStretch(1, 1);
281
282   myTreeView = new XGUI_DataTree(this);
283   myTreeView->setFrameShape(QFrame::NoFrame);
284   aLayout->addWidget(myTreeView);
285
286   QPalette aTreePalet = myTreeView->palette();
287   QColor aTreeBack = aTreePalet.color(QPalette::Base);
288
289   QPalette aPalet;
290   aPalet.setColor(QPalette::Base, aTreeBack);
291   aPalet.setColor(QPalette::Window, aTreeBack);
292   aLabelWgt->setPalette(aPalet);
293
294   myDocModel = new XGUI_DataModel(this);
295   myTreeView->setModel(myDocModel);
296
297   // It has to be done after setting of model
298   myActiveDocLbl->setTreeView(myTreeView);
299
300   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
301   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
302           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
303
304   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
305           SLOT(onLabelContextMenuRequested(const QPoint&)));
306   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
307           SLOT(onContextMenuRequested(QContextMenuEvent*)));
308 }
309
310 //***************************************************
311 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
312 {
313 }
314
315
316 //***************************************************
317 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
318 {
319   QModelIndexList aIndexes;
320   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
321   bool toEnable = false;
322
323   if (aSelectedData.size() == 1) {
324     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
325     if (!aIndexes.contains(aSelected))
326       return; // menu is called on non selected item
327
328     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
329     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
330   }
331   foreach(QAction* aCmd, actions()) {
332     aCmd->setEnabled(toEnable);
333   }
334   emit contextMenuRequested(theEvent);
335 }
336
337 //***************************************************
338 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
339 {
340   myTreeView->selectionModel()->clearSelection();
341   //Empty feature pointer means that selected root document
342   foreach(QAction* aCmd, actions()) {
343     aCmd->setEnabled(true);
344   }
345   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
346   emit contextMenuRequested(&aEvent);
347 }
348
349 //***************************************************
350 void XGUI_ObjectsBrowser::onEditItem()
351 {
352   QObjectPtrList aSelectedData = selectedObjects();
353   if (aSelectedData.size() > 0) {
354     ObjectPtr aFeature = aSelectedData.first();
355     if (aFeature) {  // Selection happens in TreeView
356       QObjectPtrList aList;
357       aList.append(aFeature);
358       // check whether the object can be deleted. There should not be parts which are not loaded
359       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
360         return;
361
362       // Find index which corresponds the feature
363       QModelIndex aIndex;
364       foreach(QModelIndex aIdx, selectedIndexes()) {
365         ObjectPtr aFea = dataModel()->object(aIdx);
366         if (dataModel()->object(aIdx)->isSame(aFeature)) {
367           aIndex = aIdx;
368           break;
369         }
370       }
371       if (aIndex.isValid()) {
372         myTreeView->setCurrentIndex(aIndex);
373         myTreeView->edit(aIndex);
374       }
375       return;
376     }
377   }
378 }
379
380 //***************************************************
381 void XGUI_ObjectsBrowser::rebuildDataTree()
382 {
383   myDocModel->rebuildDataTree();
384   update();
385 }
386
387 //***************************************************
388 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
389 {
390   QList<QModelIndex> theIndexes;
391   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
392   aSelectModel->clear();
393
394   foreach(ObjectPtr aFeature, theObjects)
395   {
396     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
397     if (aIndex.isValid()) {
398       aSelectModel->select(aIndex, QItemSelectionModel::Select);
399     }
400   }
401 }
402
403 //***************************************************
404 void XGUI_ObjectsBrowser::clearContent()  
405
406   myTreeView->clear(); 
407 }
408
409 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
410                                        const QItemSelection& theDeselected)
411 {
412   emit selectionChanged();
413 }
414
415 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
416 {
417   QObjectPtrList aList;
418   QModelIndexList aIndexes = selectedIndexes();
419   XGUI_DataModel* aModel = dataModel();
420   QModelIndexList::const_iterator aIt;
421   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
422     if ((*aIt).column() == 0) {
423       ObjectPtr aObject = aModel->object(*aIt);
424       if (aObject) {
425         aList.append(aObject);
426         if (theIndexes)
427           theIndexes->append(*aIt);
428       }
429     }
430   }
431   return aList;
432 }