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