Salome HOME
#refs 76 - reported by Hervé Legrand: Edit Sketch as Constructions child - crash
[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     FeaturePtr aFeature = aModel->feature(*aIt);
50     if (aFeature)
51       mySelectedData.append(aFeature);
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     FeaturePtr aFeature = mySelectedData.first();
87     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
88     aMgr->rootDocument()->startOperation();
89     if (!XGUI_Tools::isModelObject(aFeature))
90       aFeature->data()->setName(qPrintable(aRes));
91     else
92       boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->setName(qPrintable(aRes));
93     aMgr->rootDocument()->finishOperation();
94   }
95 }
96
97 //********************************************************************
98 //********************************************************************
99 //********************************************************************
100 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
101   : QWidget(theParent)
102 {
103   QVBoxLayout* aLayout = new QVBoxLayout(this);
104   aLayout->setContentsMargins(0, 0, 0, 0);
105   aLayout->setSpacing(0);
106
107   QFrame* aLabelWgt = new QFrame(this);
108   aLabelWgt->setAutoFillBackground(true);
109   QPalette aPalet = aLabelWgt->palette();
110   aPalet.setColor(QPalette::Window, Qt::white);
111   aLabelWgt->setPalette(aPalet);
112
113   aLayout->addWidget(aLabelWgt);
114   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
115   aLabelLay->setContentsMargins(0, 0, 0, 0);
116   aLabelLay->setSpacing(0);
117
118   QLabel* aLbl = new QLabel(aLabelWgt);
119   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
120   aLbl->setMargin(2);
121
122   aLbl->setAutoFillBackground(true);
123
124   aLabelLay->addWidget(aLbl);
125
126   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
127   DocumentPtr aDoc = aMgr->rootDocument();
128   // TODO: Find a name of the root document
129
130   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
131   myActiveDocLbl->setReadOnly(true);
132   myActiveDocLbl->setFrame(false);
133   //myActiveDocLbl->setMargin(2);
134   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
135
136   myActiveDocLbl->installEventFilter(this);
137
138   aLabelLay->addWidget(myActiveDocLbl);
139   aLabelLay->setStretch(1,1);
140
141   myTreeView = new XGUI_DataTree(this);
142   aLayout->addWidget(myTreeView);
143
144   myDocModel = myTreeView->dataModel();
145
146   aLabelWgt->setFrameShape(myTreeView->frameShape());
147   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
148
149   connect(myTreeView, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
150   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
151   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
152
153   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), 
154           this, SLOT(onLabelContextMenuRequested(const QPoint&)));
155   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
156           this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
157   
158   onActivePartChanged(FeaturePtr());
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(FeaturePtr 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(FeaturePtr());
195         emit activePartChanged(FeaturePtr());
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: // Accept current input
209           closeDocNameEditing(true);
210           break;
211         case Qt::Key_Escape: // Cancel the input
212           closeDocNameEditing(false);
213           break;
214         } 
215       }
216     }
217   }
218   return QWidget::eventFilter(obj, theEvent);
219 }
220
221 //***************************************************
222 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
223 {
224   myActiveDocLbl->deselect();
225   myActiveDocLbl->clearFocus();
226   myActiveDocLbl->releaseMouse();
227   myActiveDocLbl->setReadOnly(true);
228   if (toSave) {
229     // TODO: Save the name of root document
230     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
231     DocumentPtr aDoc = aMgr->rootDocument();
232   } else {
233     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
234   }
235 }
236
237 //***************************************************
238 void XGUI_ObjectsBrowser::activatePart(const FeaturePtr& thePart)
239 {
240   if (thePart) {
241     QModelIndex aIndex = myDocModel->partIndex(thePart);
242
243     if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
244       myTreeView->setExpanded(myDocModel->activePartIndex(), false);
245     }
246     bool isChanged = myDocModel->activatedIndex(aIndex);
247     if (isChanged) {
248       if (myDocModel->activePartIndex().isValid()) {
249         myTreeView->setExpanded(aIndex.parent(), true);
250         myTreeView->setExpanded(aIndex, true);
251         onActivePartChanged(myDocModel->feature(aIndex));
252       } else {
253         onActivePartChanged(FeaturePtr());
254       }
255     }
256   } else {
257     QModelIndex aIndex = myDocModel->activePartIndex();
258     if (aIndex.isValid()) {
259       myDocModel->activatedIndex(aIndex);
260       myTreeView->setExpanded(aIndex, false);
261       onActivePartChanged(FeaturePtr());
262     }
263   }
264 }
265
266 //***************************************************
267 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) 
268 {
269   myFeaturesList = myTreeView->selectedFeatures();
270   bool toEnable = myFeaturesList.size() > 0;
271   foreach(QAction* aCmd, actions()) {
272     aCmd->setEnabled(toEnable);
273   }
274   emit contextMenuRequested(theEvent);
275 }
276
277 //***************************************************
278 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
279 {
280   myFeaturesList.clear();
281   //Empty feature pointer means that selected root document
282   myFeaturesList.append(FeaturePtr()); 
283
284   foreach(QAction* aCmd, actions()) {
285     aCmd->setEnabled(true);
286   }
287   QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
288   emit contextMenuRequested(&aEvent);
289 }
290
291 //***************************************************
292 void XGUI_ObjectsBrowser::onEditItem()
293 {
294   if (myFeaturesList.size() > 0) {
295     FeaturePtr aFeature = myFeaturesList.first();
296     if (aFeature) { // Selection happens in TreeView
297       // Find index which corresponds the feature
298       QModelIndex aIndex;
299       foreach(QModelIndex aIdx, selectedIndexes()) {
300         FeaturePtr aFea = dataModel()->feature(aIdx);
301         if (dataModel()->feature(aIdx)->isSame(aFeature)) {
302           aIndex = aIdx;
303           break;
304         }
305       }
306       if (aIndex.isValid()) {
307         myTreeView->setCurrentIndex(aIndex);
308         myTreeView->edit(aIndex);
309       }
310     } else { //Selection happens in Upper label
311       myActiveDocLbl->setReadOnly(false);
312       myActiveDocLbl->setFocus();
313       myActiveDocLbl->selectAll();
314       myActiveDocLbl->grabMouse();
315       myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
316     }
317   }
318 }