Salome HOME
Process multi-selection in pop-up menu
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
1 #include "XGUI_ObjectsBrowser.h"
2 #include "XGUI_DocumentDataModel.h"
3 #include "XGUI_Tools.h"
4
5 #include <ModelAPI_Data.h>
6 #include <ModelAPI_PluginManager.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Object.h>
9
10 #include <QLayout>
11 #include <QLabel>
12 #include <QLineEdit>
13 #include <QPixmap>
14 #include <QEvent>
15 #include <QMouseEvent>
16 #include <QAction>
17
18
19
20 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
21   : QTreeView(theParent)
22 {
23   setHeaderHidden(true);
24   setModel(new XGUI_DocumentDataModel(this));
25   setEditTriggers(QAbstractItemView::NoEditTriggers);
26   setSelectionBehavior(QAbstractItemView::SelectRows);
27   setSelectionMode(QAbstractItemView::ExtendedSelection);
28
29   connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), 
30           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
31 }
32
33 XGUI_DataTree::~XGUI_DataTree()
34 {
35 }
36
37 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const 
38
39   return static_cast<XGUI_DocumentDataModel*>(model()); 
40 }
41
42
43 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected, 
44                                              const QItemSelection& theDeselected)
45 {
46   mySelectedData.clear();
47   QModelIndexList aIndexes = selectionModel()->selectedIndexes();
48   XGUI_DocumentDataModel* aModel = dataModel();
49   QModelIndexList::const_iterator aIt;
50   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
51     ObjectPtr aObject = aModel->object(*aIt);
52     if (aObject)
53       mySelectedData.append(aObject);
54   }
55   emit selectionChanged();
56 }
57
58 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
59 {
60   if (theEvent->button() == Qt::LeftButton) {
61     QModelIndex aIndex = currentIndex();
62     XGUI_DocumentDataModel* aModel = dataModel();
63
64     if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
65       setExpanded(aModel->activePartIndex(), false);
66     }
67     bool isChanged = aModel->activatedIndex(aIndex);
68     QTreeView::mouseDoubleClickEvent(theEvent);
69     if (isChanged) {
70       if (aModel->activePartIndex().isValid())
71         setExpanded(aIndex, true);
72       emit activePartChanged(aModel->activePart());
73     }
74   } else 
75     QTreeView::mouseDoubleClickEvent(theEvent);
76 }
77
78 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
79 {
80   emit contextMenuRequested(theEvent);
81 }
82
83 void XGUI_DataTree::commitData(QWidget* theEditor)
84 {
85   QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
86   if (aEditor) {
87     QString aRes = aEditor->text();
88     ObjectPtr aFeature = mySelectedData.first();
89     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
90     aMgr->rootDocument()->startOperation();
91     aFeature->data()->setName(qPrintable(aRes));
92     aMgr->rootDocument()->finishOperation();
93   }
94 }
95
96 //********************************************************************
97 //********************************************************************
98 //********************************************************************
99 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
100   : QWidget(theParent)
101 {
102   QVBoxLayout* aLayout = new QVBoxLayout(this);
103   aLayout->setContentsMargins(0, 0, 0, 0);
104   aLayout->setSpacing(0);
105
106   QFrame* aLabelWgt = new QFrame(this);
107   aLabelWgt->setAutoFillBackground(true);
108   QPalette aPalet = aLabelWgt->palette();
109   aPalet.setColor(QPalette::Window, Qt::white);
110   aLabelWgt->setPalette(aPalet);
111
112   aLayout->addWidget(aLabelWgt);
113   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
114   aLabelLay->setContentsMargins(0, 0, 0, 0);
115   aLabelLay->setSpacing(0);
116
117   QLabel* aLbl = new QLabel(aLabelWgt);
118   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
119   aLbl->setMargin(2);
120
121   aLbl->setAutoFillBackground(true);
122
123   aLabelLay->addWidget(aLbl);
124
125   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
126   DocumentPtr aDoc = aMgr->rootDocument();
127   // TODO: Find a name of the root document
128
129   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
130   myActiveDocLbl->setReadOnly(true);
131   myActiveDocLbl->setFrame(false);
132   //myActiveDocLbl->setMargin(2);
133   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
134
135   myActiveDocLbl->installEventFilter(this);
136
137   aLabelLay->addWidget(myActiveDocLbl);
138   aLabelLay->setStretch(1,1);
139
140   myTreeView = new XGUI_DataTree(this);
141   aLayout->addWidget(myTreeView);
142
143   myDocModel = myTreeView->dataModel();
144
145   aLabelWgt->setFrameShape(myTreeView->frameShape());
146   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
147
148   connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
149   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SLOT(onActivePartChanged(ObjectPtr)));
150   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SIGNAL(activePartChanged(ObjectPtr)));
151
152   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), 
153           this, SLOT(onLabelContextMenuRequested(const QPoint&)));
154   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
155           this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
156   
157   onActivePartChanged(ObjectPtr());
158
159   // Create internal actions
160   QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
161   aAction->setData("RENAME_CMD");
162   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
163   addAction(aAction);
164 }
165
166 //***************************************************
167 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
168 {
169 }
170
171 //***************************************************
172 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
173 {
174   QPalette aPalet = myActiveDocLbl->palette();
175   if (thePart) {
176     aPalet.setColor(QPalette::Text, Qt::black);
177   }  else {
178     aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
179   }
180   myActiveDocLbl->setPalette(aPalet);
181 }
182
183 //***************************************************
184 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
185 {
186   if (obj == myActiveDocLbl) {
187     if (myActiveDocLbl->isReadOnly()) {
188       if (theEvent->type() == QEvent::MouseButtonDblClick) {
189         if (myDocModel->activePartIndex().isValid()) {
190           myTreeView->setExpanded(myDocModel->activePartIndex(), false);
191         }
192         myDocModel->deactivatePart();
193         onActivePartChanged(ObjectPtr());
194         emit activePartChanged(ObjectPtr());
195       }
196     } else {
197       // End of editing by mouse click
198       if (theEvent->type() == QEvent::MouseButtonRelease) {
199         QMouseEvent* aEvent = (QMouseEvent*) theEvent;
200         QPoint aPnt = mapFromGlobal(aEvent->globalPos());
201         if (childAt(aPnt) != myActiveDocLbl) {
202           closeDocNameEditing(true);
203         }
204       } else if (theEvent->type() == QEvent::KeyRelease) {
205         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
206         switch (aEvent->key()) {
207         case Qt::Key_Return: // Accept current input
208           closeDocNameEditing(true);
209           break;
210         case Qt::Key_Escape: // Cancel the input
211           closeDocNameEditing(false);
212           break;
213         } 
214       }
215     }
216   }
217   return QWidget::eventFilter(obj, theEvent);
218 }
219
220 //***************************************************
221 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
222 {
223   myActiveDocLbl->deselect();
224   myActiveDocLbl->clearFocus();
225   myActiveDocLbl->releaseMouse();
226   myActiveDocLbl->setReadOnly(true);
227   if (toSave) {
228     // TODO: Save the name of root document
229     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
230     DocumentPtr aDoc = aMgr->rootDocument();
231   } else {
232     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
233   }
234 }
235
236 //***************************************************
237 void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
238 {
239   if (thePart) {
240     QModelIndex aIndex = myDocModel->partIndex(thePart);
241
242     if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
243       myTreeView->setExpanded(myDocModel->activePartIndex(), false);
244     }
245     bool isChanged = myDocModel->activatedIndex(aIndex);
246     if (isChanged) {
247       if (myDocModel->activePartIndex().isValid()) {
248         myTreeView->setExpanded(aIndex.parent(), true);
249         myTreeView->setExpanded(aIndex, true);
250         onActivePartChanged(myDocModel->object(aIndex));
251       } else {
252         onActivePartChanged(ObjectPtr());
253       }
254     }
255   } else {
256     QModelIndex aIndex = myDocModel->activePartIndex();
257     if (aIndex.isValid()) {
258       myDocModel->activatedIndex(aIndex);
259       myTreeView->setExpanded(aIndex, false);
260       onActivePartChanged(ObjectPtr());
261     }
262   }
263 }
264
265 //***************************************************
266 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) 
267 {
268   myObjectsList = myTreeView->selectedObjects();
269   bool toEnable = myObjectsList.size() > 0;
270   foreach(QAction* aCmd, actions()) {
271     aCmd->setEnabled(toEnable);
272   }
273   emit contextMenuRequested(theEvent);
274 }
275
276 //***************************************************
277 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
278 {
279   myObjectsList.clear();
280   //Empty feature pointer means that selected root document
281   myObjectsList.append(ObjectPtr()); 
282
283   foreach(QAction* aCmd, actions()) {
284     aCmd->setEnabled(true);
285   }
286   QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
287   emit contextMenuRequested(&aEvent);
288 }
289
290 //***************************************************
291 void XGUI_ObjectsBrowser::onEditItem()
292 {
293   if (myObjectsList.size() > 0) {
294     ObjectPtr aFeature = myObjectsList.first();
295     if (aFeature) { // Selection happens in TreeView
296       // Find index which corresponds the feature
297       QModelIndex aIndex;
298       foreach(QModelIndex aIdx, selectedIndexes()) {
299         ObjectPtr aFea = dataModel()->object(aIdx);
300         if (dataModel()->object(aIdx)->isSame(aFeature)) {
301           aIndex = aIdx;
302           break;
303         }
304       }
305       if (aIndex.isValid()) {
306         myTreeView->setCurrentIndex(aIndex);
307         myTreeView->edit(aIndex);
308       }
309     } else { //Selection happens in Upper label
310       myActiveDocLbl->setReadOnly(false);
311       myActiveDocLbl->setFocus();
312       myActiveDocLbl->selectAll();
313       myActiveDocLbl->grabMouse();
314       myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
315     }
316   }
317 }
318
319 //***************************************************
320 void XGUI_ObjectsBrowser::onSelectionChanged()
321 {
322   myObjectsList = myTreeView->selectedObjects();
323   emit selectionChanged();
324 }
325
326 //***************************************************
327 void XGUI_ObjectsBrowser::rebuildDataTree()
328 {
329   myDocModel->rebuildDataTree();
330   update();
331 }
332
333 //***************************************************
334 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
335 {
336   QList<QModelIndex> theIndexes;
337   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
338   aSelectModel->clear();
339
340   foreach(ObjectPtr aFeature, theObjects) {
341     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
342     if (aIndex.isValid()) {
343       aSelectModel->select(aIndex, QItemSelectionModel::Select);
344     }
345   }
346 }