Salome HOME
Issue #943: Process double clock on root label in ObjectBrowser
[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 <QLineEdit>
17 #include <QPixmap>
18 #include <QEvent>
19 #include <QMouseEvent>
20 #include <QAction>
21 #include <QStyledItemDelegate>
22 #include <QMessageBox>
23
24 #ifdef WIN32
25 #ifdef HAVE_SALOME
26 #include <QWindowsStyle>
27 #endif
28 #endif
29
30
31 /// Width of second column (minimum acceptable = 27)
32 #define SECOND_COL_WIDTH 30
33
34
35 /**
36 * \ingroup GUI
37 * Tree item delegate for definition of data in column items editor
38 */
39 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
40 {
41 public:
42   /// Constructor
43   /// \param theParent a parent of the delegate
44   XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
45
46   /// Set data for item editor (name of the item)
47   /// \param editor a widget of editor
48   /// \param index the tree item index
49   virtual void  setEditorData ( QWidget* editor, const QModelIndex& index ) const
50   {
51     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
52     if (aEditor) {
53       XGUI_DataModel* aModel = myTreedView->dataModel();
54       ObjectPtr aObj = aModel->object(index);
55       if (aObj.get() != NULL) {
56         aEditor->setText(aObj->data()->name().c_str());
57         return;
58       }
59     }
60     QStyledItemDelegate::setEditorData(editor, index);
61   }
62
63 private:
64   XGUI_DataTree* myTreedView;
65 };
66
67
68 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
69     : QTreeView(theParent)
70 {
71 #ifdef WIN32
72 #ifdef HAVE_SALOME
73   setStyle(new QWindowsStyle());
74 #else
75   myStyle = new XGUI_TreeViewStyle();
76   setStyle(myStyle);
77 #endif
78 #endif
79
80   setHeaderHidden(true);
81   setEditTriggers(QAbstractItemView::NoEditTriggers);
82   setSelectionBehavior(QAbstractItemView::SelectRows);
83   setSelectionMode(QAbstractItemView::ExtendedSelection);
84
85   setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
86
87   connect(this, SIGNAL(doubleClicked(const QModelIndex&)), 
88     SLOT(onDoubleClick(const QModelIndex&)));
89 }
90
91 XGUI_DataTree::~XGUI_DataTree()
92 {
93 }
94
95 XGUI_DataModel* XGUI_DataTree::dataModel() const
96 {
97   return static_cast<XGUI_DataModel*>(model());
98 }
99
100 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
101 {
102   emit contextMenuRequested(theEvent);
103 }
104
105 void XGUI_DataTree::commitData(QWidget* theEditor)
106 {
107   static int aEntrance = 0;
108   if (aEntrance == 0) {
109     // We have to check number of enter and exit of this function because it can be called recursively by Qt
110     // in order to avoid double modifying of a data
111     aEntrance = 1;
112     QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
113     if (aEditor) {
114       QString aName = aEditor->text();
115       QModelIndexList aIndexList = selectionModel()->selectedIndexes();
116       XGUI_DataModel* aModel = dataModel();
117       ObjectPtr aObj = aModel->object(aIndexList.first());
118
119       if (XGUI_Tools::canRename(aObj, aName)) {
120         SessionPtr aMgr = ModelAPI_Session::get();
121         aMgr->startOperation("Rename");
122         aObj->data()->setName(qPrintable(aName));
123         aMgr->finishOperation();
124       }
125     }
126   }
127   aEntrance = 0;
128 }
129
130 void XGUI_DataTree::clear() 
131 {
132   dataModel()->clear();
133   reset();
134 }
135
136 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
137 {
138   QSize aSize = theEvent->size();
139   if (aSize.isValid()) {
140     setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
141     setColumnWidth(1, SECOND_COL_WIDTH);
142   }
143 }
144
145 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
146 {
147   if (theIndex.column() != 1)
148     return;
149   SessionPtr aMgr = ModelAPI_Session::get();
150   // When operation is opened then we can not change history
151   if (aMgr->isOperation())
152     return;
153   XGUI_DataModel* aModel = dataModel();
154   if (aModel->flags(theIndex) == 0)
155     return;
156   ObjectPtr aObj = aModel->object(theIndex);
157
158   DocumentPtr aDoc = aMgr->activeDocument();
159   
160   std::string aOpName = tr("History change").toStdString();
161   if (aObj.get()) {
162     if (aObj->document() != aDoc)
163       return;
164     aMgr->startOperation(aOpName);
165     aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
166     aMgr->finishOperation();
167   } else {
168     // Ignore clicks on folders outside current document
169     if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
170       // Clicked folder under root but active document is another
171       return;
172     if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
173       // Cliced not on active document folder
174       return;
175
176     aMgr->startOperation(aOpName);
177     aDoc->setCurrentFeature(FeaturePtr(), true);
178     aMgr->finishOperation();
179   }
180   QModelIndex aNewIndex = aModel->lastHistoryIndex();
181   QModelIndex aParent = theIndex.parent();
182   int aSize = aModel->rowCount(aParent);
183   for (int i = 0; i < aSize; i++) {
184     update(aModel->index(i, 0, aParent));
185     update(aModel->index(i, 1, aParent));
186   }
187 }
188
189 #if (!defined HAVE_SALOME) && (defined WIN32)
190 void XGUI_DataTree::drawRow(QPainter* thePainter,
191                             const QStyleOptionViewItem& theOptions,
192                             const QModelIndex& theIndex) const
193 {
194   QStyleOptionViewItemV4 aOptions = theOptions;
195   myStyle->setIndex(theIndex);
196   QTreeView::drawRow(thePainter, aOptions, theIndex);
197 }
198
199 //********************************************************************
200 //********************************************************************
201 //********************************************************************
202 void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement, 
203                                        const QStyleOption* theOption,
204                                        QPainter* thePainter, const QWidget* theWidget) const
205 {
206   if ((theElement == QStyle::PE_PanelItemViewRow) || (theElement == QStyle::PE_PanelItemViewItem)) {
207     const QStyleOptionViewItemV4* aOptions = qstyleoption_cast<const QStyleOptionViewItemV4 *>(theOption);
208     if (myIndex.isValid() && ((myIndex.flags() & Qt::ItemIsSelectable) == 0)) {
209       QStyle::State aState = aOptions->state;
210       if ((aState & QStyle::State_MouseOver) != 0)
211         aState &= ~QStyle::State_MouseOver;
212       QStyleOptionViewItemV4* aOpt = (QStyleOptionViewItemV4*) aOptions;
213       aOpt->state = aState;
214       QWindowsVistaStyle::drawPrimitive(theElement, aOpt, thePainter, theWidget);
215     }
216   }
217   QWindowsVistaStyle::drawPrimitive(theElement, theOption, thePainter, theWidget);
218 }
219 #endif
220
221
222 //********************************************************************
223 //********************************************************************
224 //********************************************************************
225 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
226   : QLabel(theText, theParent), 
227   myPreSelectionStyle(""), 
228   myNeutralStyle(""), 
229   mySelectionStyle(""),
230   myIsSelected(false)
231 {
232 }
233
234 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
235 {
236   myTreeView = theView;
237   QPalette aPalet = myTreeView->palette();
238   QColor aHighlight = aPalet.highlight().color();
239   QColor aHighlightText = aPalet.highlightedText().color();
240
241   myPreSelectionStyle = "QLabel {background-color: ";
242   myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");"; 
243   myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
244
245   QString aName = aPalet.color(QPalette::Base).name();
246   myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
247
248
249 #if (!defined HAVE_SALOME) && (defined WIN32)
250   mySelectionStyle = "QLabel {background-color: ";
251   mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
252   mySelectionStyle += ", stop:1 rgb(208, 229, 255));"; 
253   mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
254 #else
255   mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
256   mySelectionStyle += "; color : " + aHighlightText.name() + "}";
257 #endif
258
259   myTreeView->viewport()->installEventFilter(this);
260 }
261
262
263 #if (!defined HAVE_SALOME) && (defined WIN32)
264 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
265 {
266   switch (theEvent->type()) {
267     case QEvent::Enter:
268       if (!myIsSelected)
269         setStyleSheet(myPreSelectionStyle);
270       break;
271     case QEvent::Leave:
272       if (!myIsSelected)
273         setStyleSheet(myNeutralStyle);
274       break;
275   }
276   return QLabel::event(theEvent);
277 }
278 #endif
279
280 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
281 {
282   if (theObj == myTreeView->viewport()) {
283     if (theEvent->type() == QEvent::MouseButtonRelease)
284       unselect();
285   }
286   return QLabel::eventFilter(theObj, theEvent);
287 }
288
289 static bool MYClearing = false;
290 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
291 {
292   MYClearing = true;
293   myIsSelected = true;
294   setStyleSheet(mySelectionStyle);
295   // We can not block signals because on 
296   // clear selection the View state will not be updated
297   myTreeView->clearSelection();
298   QLabel::mouseReleaseEvent(e);
299   MYClearing = false;
300 }
301
302 void XGUI_ActiveDocLbl::unselect()
303 {
304   if (!MYClearing) {
305     myIsSelected = false;
306     setStyleSheet(myNeutralStyle);
307   }
308 }
309
310
311 //********************************************************************
312 //********************************************************************
313 //********************************************************************
314 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
315     : QWidget(theParent), myDocModel(0)
316 {
317   QVBoxLayout* aLayout = new QVBoxLayout(this);
318   ModuleBase_Tools::zeroMargins(aLayout);
319   aLayout->setSpacing(0);
320
321   QWidget* aLabelWgt = new QWidget(this);
322   aLabelWgt->setAutoFillBackground(true);
323
324   aLayout->addWidget(aLabelWgt);
325   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
326   ModuleBase_Tools::zeroMargins(aLabelLay);
327   aLabelLay->setSpacing(0);
328
329   QLabel* aLbl = new QLabel(aLabelWgt);
330   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
331   aLbl->setMargin(2);
332   // Do not paint background of the label (in order to show icon)
333   aLbl->setAutoFillBackground(false); 
334
335   aLabelLay->addWidget(aLbl);
336
337   SessionPtr aMgr = ModelAPI_Session::get();
338   DocumentPtr aDoc = aMgr->moduleDocument();
339
340   myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
341 //  myActiveDocLbl->setReadOnly(true);
342 //  myActiveDocLbl->setFrame(false);
343   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
344
345   aLabelLay->addWidget(myActiveDocLbl);
346   aLabelLay->setStretch(1, 1);
347
348   myTreeView = new XGUI_DataTree(this);
349   myTreeView->setFrameShape(QFrame::NoFrame);
350   aLayout->addWidget(myTreeView);
351
352   QPalette aTreePalet = myTreeView->palette();
353   QColor aTreeBack = aTreePalet.color(QPalette::Base);
354
355   QPalette aPalet;
356   aPalet.setColor(QPalette::Base, aTreeBack);
357   aPalet.setColor(QPalette::Window, aTreeBack);
358   aLabelWgt->setPalette(aPalet);
359
360   myDocModel = new XGUI_DataModel(this);
361   myTreeView->setModel(myDocModel);
362
363   // It has to be done after setting of model
364   myActiveDocLbl->setTreeView(myTreeView);
365
366   QItemSelectionModel* aSelMod = myTreeView->selectionModel();
367   connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
368           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
369
370   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
371           SLOT(onContextMenuRequested(QContextMenuEvent*)));
372 }
373
374 //***************************************************
375 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
376 {
377 }
378
379
380 //***************************************************
381 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
382 {
383   QModelIndexList aIndexes;
384   QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
385   bool toEnable = false;
386
387   if (aSelectedData.size() == 1) {
388     QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
389     if (!aIndexes.contains(aSelected))
390       return; // menu is called on non selected item
391
392     Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
393     toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
394   }
395   foreach(QAction* aCmd, actions()) {
396     aCmd->setEnabled(toEnable);
397   }
398   emit contextMenuRequested(theEvent);
399 }
400
401 //***************************************************
402 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
403 {
404   myTreeView->selectionModel()->clearSelection();
405   //Empty feature pointer means that selected root document
406   foreach(QAction* aCmd, actions()) {
407     aCmd->setEnabled(true);
408   }
409   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
410   emit contextMenuRequested(&aEvent);
411 }
412
413 //***************************************************
414 void XGUI_ObjectsBrowser::onEditItem()
415 {
416   QObjectPtrList aSelectedData = selectedObjects();
417   if (aSelectedData.size() > 0) {
418     ObjectPtr aFeature = aSelectedData.first();
419     if (aFeature) {  // Selection happens in TreeView
420       QObjectPtrList aList;
421       aList.append(aFeature);
422       // check whether the object can be deleted. There should not be parts which are not loaded
423       if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
424         return;
425
426       // Find index which corresponds the feature
427       QModelIndex aIndex;
428       foreach(QModelIndex aIdx, selectedIndexes()) {
429         ObjectPtr aFea = dataModel()->object(aIdx);
430         if (dataModel()->object(aIdx)->isSame(aFeature)) {
431           aIndex = aIdx;
432           break;
433         }
434       }
435       if (aIndex.isValid()) {
436         myTreeView->setCurrentIndex(aIndex);
437         myTreeView->edit(aIndex);
438       }
439       return;
440     }
441   }
442 }
443
444 //***************************************************
445 void XGUI_ObjectsBrowser::rebuildDataTree()
446 {
447   myDocModel->rebuildDataTree();
448   update();
449 }
450
451 //***************************************************
452 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
453 {
454   QList<QModelIndex> theIndexes;
455   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
456   aSelectModel->clear();
457
458   foreach(ObjectPtr aFeature, theObjects)
459   {
460     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
461     if (aIndex.isValid()) {
462       aSelectModel->select(aIndex, QItemSelectionModel::Select);
463     }
464   }
465 }
466
467 //***************************************************
468 void XGUI_ObjectsBrowser::clearContent()  
469
470   myTreeView->clear(); 
471 }
472
473 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
474                                        const QItemSelection& theDeselected)
475 {
476   emit selectionChanged();
477 }
478
479 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
480 {
481   QObjectPtrList aList;
482   QModelIndexList aIndexes = selectedIndexes();
483   XGUI_DataModel* aModel = dataModel();
484   QModelIndexList::const_iterator aIt;
485   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
486     if ((*aIt).column() == 0) {
487       ObjectPtr aObject = aModel->object(*aIt);
488       if (aObject) {
489         aList.append(aObject);
490         if (theIndexes)
491           theIndexes->append(*aIt);
492       }
493     }
494   }
495   return aList;
496 }