]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
258a59f8021fe2b6663f57dbac02b351f4151dad
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
1 #include "XGUI_ObjectsBrowser.h"
2 #include "XGUI_DocumentDataModel.h"
3
4 #include <ModelAPI_Data.h>
5 #include <ModelAPI_PluginManager.h>
6 #include <ModelAPI_Document.h>
7 #include <ModelAPI_Object.h>
8
9 #include <QLayout>
10 #include <QLabel>
11 #include <QLineEdit>
12 #include <QPixmap>
13 #include <QEvent>
14 #include <QMouseEvent>
15 #include <QAction>
16
17
18
19 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
20   : QTreeView(theParent)
21 {
22   setHeaderHidden(true);
23   setModel(new XGUI_DocumentDataModel(this));
24   setEditTriggers(QAbstractItemView::NoEditTriggers);
25
26   connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), 
27           this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
28 }
29
30 XGUI_DataTree::~XGUI_DataTree()
31 {
32 }
33
34 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const 
35
36   return static_cast<XGUI_DocumentDataModel*>(model()); 
37 }
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     FeaturePtr aFeature = aModel->feature(*aIt);
49     if (aFeature)
50       mySelectedData.append(aFeature);
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     FeaturePtr aFeature = mySelectedData.first();
86     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
87     aMgr->rootDocument()->startOperation();
88     if (aFeature->data())
89       aFeature->data()->setName(qPrintable(aRes));
90     else
91       boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->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, SIGNAL(selectionChanged()));
149   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
150   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
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(FeaturePtr());
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(FeaturePtr 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(FeaturePtr());
194         emit activePartChanged(FeaturePtr());
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 FeaturePtr& 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->feature(aIndex));
251       } else {
252         onActivePartChanged(FeaturePtr());
253       }
254     }
255   } else {
256     QModelIndex aIndex = myDocModel->activePartIndex();
257     if (aIndex.isValid()) {
258       myDocModel->activatedIndex(aIndex);
259       myTreeView->setExpanded(aIndex, false);
260       onActivePartChanged(FeaturePtr());
261     }
262   }
263 }
264
265 //***************************************************
266 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) 
267 {
268   myFeaturesList = myTreeView->selectedFeatures();
269   bool toEnable = myFeaturesList.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   myFeaturesList.clear();
280   //Empty feature pointer means that selected root document
281   myFeaturesList.append(FeaturePtr()); 
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 (myFeaturesList.size() > 0) {
294     FeaturePtr aFeature = myFeaturesList.first();
295     if (aFeature) { // Selection happens in TreeView
296       // Find index which corresponds the feature
297       QModelIndex aIndex;
298       foreach(QModelIndex aIdx, selectedIndexes()) {
299         FeaturePtr aFea = dataModel()->feature(aIdx);
300         if (dataModel()->feature(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 }