]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ObjectsBrowser.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SolveSpace
[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     aFeature->document()->startOperation();
87     if (aFeature->data())
88       aFeature->data()->setName(qPrintable(aRes));
89     else
90       boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->setName(qPrintable(aRes));
91     aFeature->document()->finishOperation();
92   }
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   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
125   DocumentPtr aDoc = aMgr->rootDocument();
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, SIGNAL(selectionChanged()));
148   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(onActivePartChanged(FeaturePtr)));
149   connect(myTreeView, SIGNAL(activePartChanged(FeaturePtr)), this, SIGNAL(activePartChanged(FeaturePtr)));
150
151   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), 
152           this, SLOT(onLabelContextMenuRequested(const QPoint&)));
153   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
154           this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
155   
156   onActivePartChanged(FeaturePtr());
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(FeaturePtr 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(FeaturePtr());
193         emit activePartChanged(FeaturePtr());
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: // Accept current input
207           closeDocNameEditing(true);
208           break;
209         case Qt::Key_Escape: // Cancel the input
210           closeDocNameEditing(false);
211           break;
212         } 
213       }
214     }
215   }
216   return QWidget::eventFilter(obj, theEvent);
217 }
218
219 //***************************************************
220 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
221 {
222   myActiveDocLbl->deselect();
223   myActiveDocLbl->clearFocus();
224   myActiveDocLbl->releaseMouse();
225   myActiveDocLbl->setReadOnly(true);
226   if (toSave) {
227     // TODO: Save the name of root document
228     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
229     DocumentPtr aDoc = aMgr->rootDocument();
230   } else {
231     myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
232   }
233 }
234
235 //***************************************************
236 void XGUI_ObjectsBrowser::activatePart(const FeaturePtr& thePart)
237 {
238   if (thePart) {
239     QModelIndex aIndex = myDocModel->partIndex(thePart);
240
241     if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
242       myTreeView->setExpanded(myDocModel->activePartIndex(), false);
243     }
244     bool isChanged = myDocModel->activatedIndex(aIndex);
245     if (isChanged) {
246       if (myDocModel->activePartIndex().isValid()) {
247         myTreeView->setExpanded(aIndex.parent(), true);
248         myTreeView->setExpanded(aIndex, true);
249         onActivePartChanged(myDocModel->feature(aIndex));
250       } else {
251         onActivePartChanged(FeaturePtr());
252       }
253     }
254   } else {
255     QModelIndex aIndex = myDocModel->activePartIndex();
256     if (aIndex.isValid()) {
257       myDocModel->activatedIndex(aIndex);
258       myTreeView->setExpanded(aIndex, false);
259       onActivePartChanged(FeaturePtr());
260     }
261   }
262 }
263
264 //***************************************************
265 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) 
266 {
267   myFeaturesList = myTreeView->selectedFeatures();
268   bool toEnable = myFeaturesList.size() > 0;
269   foreach(QAction* aCmd, actions()) {
270     aCmd->setEnabled(toEnable);
271   }
272   emit contextMenuRequested(theEvent);
273 }
274
275 //***************************************************
276 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
277 {
278   myFeaturesList.clear();
279   //Empty feature pointer means that selected root document
280   myFeaturesList.append(FeaturePtr()); 
281
282   foreach(QAction* aCmd, actions()) {
283     aCmd->setEnabled(true);
284   }
285   QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
286   emit contextMenuRequested(&aEvent);
287 }
288
289 //***************************************************
290 void XGUI_ObjectsBrowser::onEditItem()
291 {
292   if (myFeaturesList.size() > 0) {
293     FeaturePtr aFeature = myFeaturesList.first();
294     if (aFeature) { // Selection happens in TreeView
295       // Find index which corresponds the feature
296       QModelIndex aIndex;
297       foreach(QModelIndex aIdx, selectedIndexes()) {
298         FeaturePtr aFea = dataModel()->feature(aIdx);
299         if (dataModel()->feature(aIdx)->isSame(aFeature)) {
300           aIndex = aIdx;
301           break;
302         }
303       }
304       if (aIndex.isValid()) {
305         myTreeView->setCurrentIndex(aIndex);
306         myTreeView->edit(aIndex);
307       }
308     } else { //Selection happens in Upper label
309       myActiveDocLbl->setReadOnly(false);
310       myActiveDocLbl->setFocus();
311       myActiveDocLbl->selectAll();
312       myActiveDocLbl->grabMouse();
313       myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
314     }
315   }
316 }