Salome HOME
Sources formated according to the codeing standards
[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 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
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     ObjectPtr aFeature = mySelectedData.first();
86     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
87     aMgr->rootDocument()->startOperation();
88     aFeature->data()->setName(qPrintable(aRes));
89     aMgr->rootDocument()->finishOperation();
90   }
91 }
92
93 //********************************************************************
94 //********************************************************************
95 //********************************************************************
96 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
97     : QWidget(theParent)
98 {
99   QVBoxLayout* aLayout = new QVBoxLayout(this);
100   aLayout->setContentsMargins(0, 0, 0, 0);
101   aLayout->setSpacing(0);
102
103   QFrame* aLabelWgt = new QFrame(this);
104   aLabelWgt->setAutoFillBackground(true);
105   QPalette aPalet = aLabelWgt->palette();
106   aPalet.setColor(QPalette::Window, Qt::white);
107   aLabelWgt->setPalette(aPalet);
108
109   aLayout->addWidget(aLabelWgt);
110   QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
111   aLabelLay->setContentsMargins(0, 0, 0, 0);
112   aLabelLay->setSpacing(0);
113
114   QLabel* aLbl = new QLabel(aLabelWgt);
115   aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
116   aLbl->setMargin(2);
117
118   aLbl->setAutoFillBackground(true);
119
120   aLabelLay->addWidget(aLbl);
121
122   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
123   DocumentPtr aDoc = aMgr->rootDocument();
124   // TODO: Find a name of the root document
125
126   myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
127   myActiveDocLbl->setReadOnly(true);
128   myActiveDocLbl->setFrame(false);
129   //myActiveDocLbl->setMargin(2);
130   myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
131
132   myActiveDocLbl->installEventFilter(this);
133
134   aLabelLay->addWidget(myActiveDocLbl);
135   aLabelLay->setStretch(1, 1);
136
137   myTreeView = new XGUI_DataTree(this);
138   aLayout->addWidget(myTreeView);
139
140   myDocModel = myTreeView->dataModel();
141
142   aLabelWgt->setFrameShape(myTreeView->frameShape());
143   aLabelWgt->setFrameShadow(myTreeView->frameShadow());
144
145   connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
146   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
147           SLOT(onActivePartChanged(ObjectPtr)));
148   connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this,
149           SIGNAL(activePartChanged(ObjectPtr)));
150
151   connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
152           SLOT(onLabelContextMenuRequested(const QPoint&)));
153   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
154           SLOT(onContextMenuRequested(QContextMenuEvent*)));
155
156   onActivePartChanged(ObjectPtr());
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(ObjectPtr 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(ObjectPtr());
193         emit activePartChanged(ObjectPtr());
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 ResultPartPtr& 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->object(aIndex));
250       } else {
251         onActivePartChanged(ObjectPtr());
252       }
253     }
254   } else {
255     QModelIndex aIndex = myDocModel->activePartIndex();
256     if (aIndex.isValid()) {
257       myDocModel->activatedIndex(aIndex);
258       myTreeView->setExpanded(aIndex, false);
259       onActivePartChanged(ObjectPtr());
260     }
261   }
262 }
263
264 //***************************************************
265 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
266 {
267   myObjectsList = myTreeView->selectedObjects();
268   bool toEnable = myObjectsList.size() > 0;
269   foreach(QAction* aCmd, actions())
270   {
271     aCmd->setEnabled(toEnable);
272   }
273   emit contextMenuRequested(theEvent);
274 }
275
276 //***************************************************
277 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
278 {
279   myObjectsList.clear();
280   //Empty feature pointer means that selected root document
281   myObjectsList.append(ObjectPtr());
282
283   foreach(QAction* aCmd, actions())
284   {
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 (myObjectsList.size() > 0) {
295     ObjectPtr aFeature = myObjectsList.first();
296     if (aFeature) {  // Selection happens in TreeView
297       // Find index which corresponds the feature
298       QModelIndex aIndex;
299       foreach(QModelIndex aIdx, selectedIndexes())
300       {
301         ObjectPtr aFea = dataModel()->object(aIdx);
302         if (dataModel()->object(aIdx)->isSame(aFeature)) {
303           aIndex = aIdx;
304           break;
305         }
306       }
307       if (aIndex.isValid()) {
308         myTreeView->setCurrentIndex(aIndex);
309         myTreeView->edit(aIndex);
310       }
311     } else {  //Selection happens in Upper label
312       myActiveDocLbl->setReadOnly(false);
313       myActiveDocLbl->setFocus();
314       myActiveDocLbl->selectAll();
315       myActiveDocLbl->grabMouse();
316       myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
317     }
318   }
319 }
320
321 //***************************************************
322 void XGUI_ObjectsBrowser::onSelectionChanged()
323 {
324   myObjectsList = myTreeView->selectedObjects();
325   emit selectionChanged();
326 }
327
328 //***************************************************
329 void XGUI_ObjectsBrowser::rebuildDataTree()
330 {
331   myDocModel->rebuildDataTree();
332   update();
333 }
334
335 //***************************************************
336 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
337 {
338   QList<QModelIndex> theIndexes;
339   QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
340   aSelectModel->clear();
341
342   foreach(ObjectPtr aFeature, theObjects)
343   {
344     QModelIndex aIndex = myDocModel->objectIndex(aFeature);
345     if (aIndex.isValid()) {
346       aSelectModel->select(aIndex, QItemSelectionModel::Select);
347     }
348   }
349 }