]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[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     ObjectPtr aObject = aModel->object(aIndex);
61     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
62     if (aPart) {
63       aPart->activate();
64     }
65   } else
66     QTreeView::mouseDoubleClickEvent(theEvent);
67 }
68
69 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
70 {
71   emit contextMenuRequested(theEvent);
72 }
73
74 void XGUI_DataTree::commitData(QWidget* theEditor)
75 {
76   QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
77   if (aEditor) {
78     QString aRes = aEditor->text();
79     ObjectPtr aFeature = mySelectedData.first();
80     SessionPtr aMgr = ModelAPI_Session::get();
81     aMgr->startOperation();
82     aFeature->data()->setName(qPrintable(aRes));
83     aMgr->finishOperation();
84   }
85 }
86
87 void XGUI_DataTree::clear() 
88 {
89   mySelectedData.clear();
90   XGUI_DocumentDataModel* aModel = dataModel();
91   aModel->clear();
92   reset();
93 }
94
95 //********************************************************************
96 //********************************************************************
97 //********************************************************************
98 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
99     : QWidget(theParent)
100 {
101   QVBoxLayout* aLayout = new QVBoxLayout(this);
102   aLayout->setContentsMargins(0, 0, 0, 0);
103   aLayout->setSpacing(0);
104
105   QFrame* aLabelWgt = new QFrame(this);
106   aLabelWgt->setAutoFillBackground(true);
107   QPalette aPalet = aLabelWgt->palette();
108   aPalet.setColor(QPalette::Window, Qt::white);
109   aLabelWgt->setPalette(aPalet);
110
111   aLayout->addWidget(aLabelWgt);
112   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
113   aLabelLay->setContentsMargins(0, 0, 0, 0);
114   aLabelLay->setSpacing(0);
115
116   QLabel* aLbl = new QLabel(aLabelWgt);
117   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
118   aLbl->setMargin(2);
119
120   aLbl->setAutoFillBackground(true);
121
122   aLabelLay->addWidget(aLbl);
123
124   SessionPtr aMgr = ModelAPI_Session::get();
125   DocumentPtr aDoc = aMgr->moduleDocument();
126   // TODO: Find a name of the root document
127
128   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
129   myActiveDocLbl->setReadOnly(true);
130   myActiveDocLbl->setFrame(false);
131   //myActiveDocLbl->setMargin(2);
132   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
133
134   myActiveDocLbl->installEventFilter(this);
135
136   aLabelLay->addWidget(myActiveDocLbl);
137   aLabelLay->setStretch(1, 1);
138
139   myTreeView = new XGUI_DataTree(this);
140   aLayout->addWidget(myTreeView);
141
142   myDocModel = myTreeView->dataModel();
143
144   aLabelWgt->setFrameShape(myTreeView->frameShape());
145   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
146
147   connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
148   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
149           SLOT(onActivePartChanged(ObjectPtr)));
150   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
151           SIGNAL(activePartChanged(ObjectPtr)));
152
153   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
154           SLOT(onLabelContextMenuRequested(const QPoint&)));
155   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
156           SLOT(onContextMenuRequested(QContextMenuEvent*)));
157
158   onActivePartChanged(ObjectPtr());
159
160   // Create internal actions
161   QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
162   aAction->setData("RENAME_CMD");
163   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
164   addAction(aAction);
165 }
166
167 //***************************************************
168 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
169 {
170 }
171
172 //***************************************************
173 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
174 {
175   QPalette aPalet = myActiveDocLbl->palette();
176   if (thePart) {
177     aPalet.setColor(QPalette::Text, Qt::black);
178   } else {
179     aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
180   }
181   myActiveDocLbl->setPalette(aPalet);
182 }
183
184 //***************************************************
185 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
186 {
187   if (obj == myActiveDocLbl) {
188     if (myActiveDocLbl->isReadOnly()) {
189       if (theEvent->type() == QEvent::MouseButtonDblClick) {
190         if (myDocModel->activePartIndex().isValid()) {
191           myTreeView->setExpanded(myDocModel->activePartIndex(), false);
192         }
193         myDocModel->deactivatePart();
194         onActivePartChanged(ObjectPtr());
195         emit activePartChanged(ObjectPtr());
196       }
197     } else {
198       // End of editing by mouse click
199       if (theEvent->type() == QEvent::MouseButtonRelease) {
200         QMouseEvent* aEvent = (QMouseEvent*) theEvent;
201         QPoint aPnt = mapFromGlobal(aEvent->globalPos());
202         if (childAt(aPnt) != myActiveDocLbl) {
203           closeDocNameEditing(true);
204         }
205       } else if (theEvent->type() == QEvent::KeyRelease) {
206         QKeyEvent* aEvent = (QKeyEvent*) theEvent;
207         switch (aEvent->key()) {
208           case Qt::Key_Return:
209           case Qt::Key_Enter:  // Accept current input
210             closeDocNameEditing(true);
211             break;
212           case Qt::Key_Escape:  // Cancel the input
213             closeDocNameEditing(false);
214             break;
215         }
216       }
217     }
218   }
219   return QWidget::eventFilter(obj, theEvent);
220 }
221
222 //***************************************************
223 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
224 {
225   myActiveDocLbl->deselect();
226   myActiveDocLbl->clearFocus();
227   myActiveDocLbl->releaseMouse();
228   myActiveDocLbl->setReadOnly(true);
229   if (toSave) {
230     // TODO: Save the name of root document
231     SessionPtr aMgr = ModelAPI_Session::get();
232     DocumentPtr aDoc = aMgr->moduleDocument();
233   } else {
234     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
235   }
236 }
237
238 //***************************************************
239 void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart)
240 {
241   if (thePart) {
242     QModelIndex aIndex = myDocModel->partIndex(thePart);
243
244     if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
245       myTreeView->setExpanded(myDocModel->activePartIndex(), false);
246     }
247     bool isChanged = myDocModel->activatedIndex(aIndex);
248     if (isChanged) {
249       if (myDocModel->activePartIndex().isValid()) {
250         myTreeView->setExpanded(aIndex.parent(), true);
251         myTreeView->setExpanded(aIndex, true);
252         onActivePartChanged(myDocModel->object(aIndex));
253       } else {
254         onActivePartChanged(ObjectPtr());
255       }
256     }
257   } else {
258     QModelIndex aIndex = myDocModel->activePartIndex();
259     if (aIndex.isValid()) {
260       myDocModel->activatedIndex(aIndex);
261       myTreeView->setExpanded(aIndex, false);
262       onActivePartChanged(ObjectPtr());
263     }
264   }
265 }
266
267 //***************************************************
268 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
269 {
270   myObjectsList = myTreeView->selectedObjects();
271   bool toEnable = myObjectsList.size() == 1;
272   foreach(QAction* aCmd, actions())
273   {
274     aCmd->setEnabled(toEnable);
275   }
276   emit contextMenuRequested(theEvent);
277 }
278
279 //***************************************************
280 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
281 {
282   myObjectsList.clear();
283   //Empty feature pointer means that selected root document
284   myObjectsList.append(ObjectPtr());
285
286   foreach(QAction* aCmd, actions())
287   {
288     aCmd->setEnabled(true);
289   }
290   QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
291   emit contextMenuRequested(&aEvent);
292 }
293
294 //***************************************************
295 void XGUI_ObjectsBrowser::onEditItem()
296 {
297   if (myObjectsList.size() > 0) {
298     ObjectPtr aFeature = myObjectsList.first();
299     if (aFeature) {  // Selection happens in TreeView
300       // Find index which corresponds the feature
301       QModelIndex aIndex;
302       foreach(QModelIndex aIdx, selectedIndexes())
303       {
304         ObjectPtr aFea = dataModel()->object(aIdx);
305         if (dataModel()->object(aIdx)->isSame(aFeature)) {
306           aIndex = aIdx;
307           break;
308         }
309       }
310       if (aIndex.isValid()) {
311         myTreeView->setCurrentIndex(aIndex);
312         myTreeView->edit(aIndex);
313       }
314     } else {  //Selection happens in Upper label
315       myActiveDocLbl->setReadOnly(false);
316       myActiveDocLbl->setFocus();
317       myActiveDocLbl->selectAll();
318       myActiveDocLbl->grabMouse();
319       myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
320     }
321   }
322 }
323
324 //***************************************************
325 void XGUI_ObjectsBrowser::onSelectionChanged()
326 {
327   myObjectsList = myTreeView->selectedObjects();
328   emit selectionChanged();
329 }
330
331 //***************************************************
332 void XGUI_ObjectsBrowser::rebuildDataTree()
333 {
334   myDocModel->rebuildDataTree();
335   update();
336 }
337
338 //***************************************************
339 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
340 {
341   QList<QModelIndex> theIndexes;
342   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
343   aSelectModel->clear();
344
345   foreach(ObjectPtr aFeature, theObjects)
346   {
347     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
348     if (aIndex.isValid()) {
349       aSelectModel->select(aIndex, QItemSelectionModel::Select);
350     }
351   }
352 }
353
354 //***************************************************
355 void XGUI_ObjectsBrowser::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
356
357   myDocModel->processEvent(theMessage); 
358 }
359
360
361 //***************************************************
362 void XGUI_ObjectsBrowser::clearContent()  
363
364   myObjectsList.clear();
365   myTreeView->clear(); 
366 }