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