Salome HOME
Issue #83: renamed PluginManager to Session
[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_Session.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 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
19     : QTreeView(theParent)
20 {
21   setHeaderHidden(true);
22   setModel(new XGUI_DocumentDataModel(this));
23   setEditTriggers(QAbstractItemView::NoEditTriggers);
24   setSelectionBehavior(QAbstractItemView::SelectRows);
25   setSelectionMode(QAbstractItemView::ExtendedSelection);
26
27   connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
28           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
29 }
30
31 XGUI_DataTree::~XGUI_DataTree()
32 {
33 }
34
35 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
36 {
37   return static_cast<XGUI_DocumentDataModel*>(model());
38 }
39
40 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
41                                        const QItemSelection& theDeselected)
42 {
43   mySelectedData.clear();
44   QModelIndexList aIndexes = selectionModel()->selectedIndexes();
45   XGUI_DocumentDataModel* aModel = dataModel();
46   QModelIndexList::const_iterator aIt;
47   for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
48     ObjectPtr aObject = aModel->object(*aIt);
49     if (aObject)
50       mySelectedData.append(aObject);
51   }
52   emit selectionChanged();
53 }
54
55 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
56 {
57   if (theEvent->button() == Qt::LeftButton) {
58     QModelIndex aIndex = currentIndex();
59     XGUI_DocumentDataModel* aModel = dataModel();
60
61     if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
62       setExpanded(aModel->activePartIndex(), false);
63     }
64     bool isChanged = aModel->activatedIndex(aIndex);
65     QTreeView::mouseDoubleClickEvent(theEvent);
66     if (isChanged) {
67       if (aModel->activePartIndex().isValid())
68         setExpanded(aIndex, true);
69       emit activePartChanged(aModel->activePart());
70     }
71   } else
72     QTreeView::mouseDoubleClickEvent(theEvent);
73 }
74
75 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
76 {
77   emit contextMenuRequested(theEvent);
78 }
79
80 void XGUI_DataTree::commitData(QWidget* theEditor)
81 {
82   QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
83   if (aEditor) {
84     QString aRes = aEditor->text();
85     ObjectPtr aFeature = mySelectedData.first();
86     SessionPtr aMgr = ModelAPI_Session::get();
87     aMgr->rootDocument()->startOperation();
88     aFeature->data()->setName(qPrintable(aRes));
89     aMgr->rootDocument()->finishOperation();
90   }
91 }
92
93 //********************************************************************
94 //********************************************************************
95 //********************************************************************
96 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
97     : QWidget(theParent)
98 {
99   QVBoxLayout* aLayout = new QVBoxLayout(this);
100   aLayout->setContentsMargins(0, 0, 0, 0);
101   aLayout->setSpacing(0);
102
103   QFrame* aLabelWgt = new QFrame(this);
104   aLabelWgt->setAutoFillBackground(true);
105   QPalette aPalet = aLabelWgt->palette();
106   aPalet.setColor(QPalette::Window, Qt::white);
107   aLabelWgt->setPalette(aPalet);
108
109   aLayout->addWidget(aLabelWgt);
110   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
111   aLabelLay->setContentsMargins(0, 0, 0, 0);
112   aLabelLay->setSpacing(0);
113
114   QLabel* aLbl = new QLabel(aLabelWgt);
115   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
116   aLbl->setMargin(2);
117
118   aLbl->setAutoFillBackground(true);
119
120   aLabelLay->addWidget(aLbl);
121
122   SessionPtr aMgr = ModelAPI_Session::get();
123   DocumentPtr aDoc = aMgr->rootDocument();
124   // TODO: Find a name of the root document
125
126   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
127   myActiveDocLbl->setReadOnly(true);
128   myActiveDocLbl->setFrame(false);
129   //myActiveDocLbl->setMargin(2);
130   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
131
132   myActiveDocLbl->installEventFilter(this);
133
134   aLabelLay->addWidget(myActiveDocLbl);
135   aLabelLay->setStretch(1, 1);
136
137   myTreeView = new XGUI_DataTree(this);
138   aLayout->addWidget(myTreeView);
139
140   myDocModel = myTreeView->dataModel();
141
142   aLabelWgt->setFrameShape(myTreeView->frameShape());
143   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
144
145   connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
146   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
147           SLOT(onActivePartChanged(ObjectPtr)));
148   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
149           SIGNAL(activePartChanged(ObjectPtr)));
150
151   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
152           SLOT(onLabelContextMenuRequested(const QPoint&)));
153   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
154           SLOT(onContextMenuRequested(QContextMenuEvent*)));
155
156   onActivePartChanged(ObjectPtr());
157
158   // Create internal actions
159   QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
160   aAction->setData("RENAME_CMD");
161   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
162   addAction(aAction);
163 }
164
165 //***************************************************
166 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
167 {
168 }
169
170 //***************************************************
171 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
172 {
173   QPalette aPalet = myActiveDocLbl->palette();
174   if (thePart) {
175     aPalet.setColor(QPalette::Text, Qt::black);
176   } else {
177     aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
178   }
179   myActiveDocLbl->setPalette(aPalet);
180 }
181
182 //***************************************************
183 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
184 {
185   if (obj == myActiveDocLbl) {
186     if (myActiveDocLbl->isReadOnly()) {
187       if (theEvent->type() == QEvent::MouseButtonDblClick) {
188         if (myDocModel->activePartIndex().isValid()) {
189           myTreeView->setExpanded(myDocModel->activePartIndex(), false);
190         }
191         myDocModel->deactivatePart();
192         onActivePartChanged(ObjectPtr());
193         emit activePartChanged(ObjectPtr());
194       }
195     } else {
196       // End of editing by mouse click
197       if (theEvent->type() == QEvent::MouseButtonRelease) {
198         QMouseEvent* aEvent = (QMouseEvent*) theEvent;
199         QPoint aPnt = mapFromGlobal(aEvent->globalPos());
200         if (childAt(aPnt) != myActiveDocLbl) {
201           closeDocNameEditing(true);
202         }
203       } else if (theEvent->type() == QEvent::KeyRelease) {
204         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
205         switch (aEvent->key()) {
206           case Qt::Key_Return:
207           case Qt::Key_Enter:  // 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     SessionPtr aMgr = ModelAPI_Session::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   {
272     aCmd->setEnabled(toEnable);
273   }
274   emit contextMenuRequested(theEvent);
275 }
276
277 //***************************************************
278 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
279 {
280   myObjectsList.clear();
281   //Empty feature pointer means that selected root document
282   myObjectsList.append(ObjectPtr());
283
284   foreach(QAction* aCmd, actions())
285   {
286     aCmd->setEnabled(true);
287   }
288   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
289   emit contextMenuRequested(&aEvent);
290 }
291
292 //***************************************************
293 void XGUI_ObjectsBrowser::onEditItem()
294 {
295   if (myObjectsList.size() > 0) {
296     ObjectPtr aFeature = myObjectsList.first();
297     if (aFeature) {  // Selection happens in TreeView
298       // Find index which corresponds the feature
299       QModelIndex aIndex;
300       foreach(QModelIndex aIdx, selectedIndexes())
301       {
302         ObjectPtr aFea = dataModel()->object(aIdx);
303         if (dataModel()->object(aIdx)->isSame(aFeature)) {
304           aIndex = aIdx;
305           break;
306         }
307       }
308       if (aIndex.isValid()) {
309         myTreeView->setCurrentIndex(aIndex);
310         myTreeView->edit(aIndex);
311       }
312     } else {  //Selection happens in Upper label
313       myActiveDocLbl->setReadOnly(false);
314       myActiveDocLbl->setFocus();
315       myActiveDocLbl->selectAll();
316       myActiveDocLbl->grabMouse();
317       myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
318     }
319   }
320 }
321
322 //***************************************************
323 void XGUI_ObjectsBrowser::onSelectionChanged()
324 {
325   myObjectsList = myTreeView->selectedObjects();
326   emit selectionChanged();
327 }
328
329 //***************************************************
330 void XGUI_ObjectsBrowser::rebuildDataTree()
331 {
332   myDocModel->rebuildDataTree();
333   update();
334 }
335
336 //***************************************************
337 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
338 {
339   QList<QModelIndex> theIndexes;
340   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
341   aSelectModel->clear();
342
343   foreach(ObjectPtr aFeature, theObjects)
344   {
345     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
346     if (aIndex.isValid()) {
347       aSelectModel->select(aIndex, QItemSelectionModel::Select);
348     }
349   }
350 }