1 #include "XGUI_ObjectsBrowser.h"
2 #include "XGUI_DocumentDataModel.h"
3 #include "XGUI_Tools.h"
5 #include <ModelAPI_Data.h>
6 #include <ModelAPI_PluginManager.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Object.h>
15 #include <QMouseEvent>
20 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
21 : QTreeView(theParent)
23 setHeaderHidden(true);
24 setModel(new XGUI_DocumentDataModel(this));
25 setEditTriggers(QAbstractItemView::NoEditTriggers);
27 connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
28 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
31 XGUI_DataTree::~XGUI_DataTree()
35 XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const
37 return static_cast<XGUI_DocumentDataModel*>(model());
41 void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected,
42 const QItemSelection& theDeselected)
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 ObjectPtr aFeature = aModel->feature(*aIt);
51 mySelectedData.append(aFeature);
53 emit selectionChanged();
56 void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent)
58 if (theEvent->button() == Qt::LeftButton) {
59 QModelIndex aIndex = currentIndex();
60 XGUI_DocumentDataModel* aModel = dataModel();
62 if ((aModel->activePartIndex() != aIndex) && aModel->activePartIndex().isValid()) {
63 setExpanded(aModel->activePartIndex(), false);
65 bool isChanged = aModel->activatedIndex(aIndex);
66 QTreeView::mouseDoubleClickEvent(theEvent);
68 if (aModel->activePartIndex().isValid())
69 setExpanded(aIndex, true);
70 emit activePartChanged(aModel->activePart());
73 QTreeView::mouseDoubleClickEvent(theEvent);
76 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
78 emit contextMenuRequested(theEvent);
81 void XGUI_DataTree::commitData(QWidget* theEditor)
83 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
85 QString aRes = aEditor->text();
86 ObjectPtr 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));
92 boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->setName(qPrintable(aRes));
93 aMgr->rootDocument()->finishOperation();
97 //********************************************************************
98 //********************************************************************
99 //********************************************************************
100 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
103 QVBoxLayout* aLayout = new QVBoxLayout(this);
104 aLayout->setContentsMargins(0, 0, 0, 0);
105 aLayout->setSpacing(0);
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);
113 aLayout->addWidget(aLabelWgt);
114 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
115 aLabelLay->setContentsMargins(0, 0, 0, 0);
116 aLabelLay->setSpacing(0);
118 QLabel* aLbl = new QLabel(aLabelWgt);
119 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
122 aLbl->setAutoFillBackground(true);
124 aLabelLay->addWidget(aLbl);
126 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
127 DocumentPtr aDoc = aMgr->rootDocument();
128 // TODO: Find a name of the root document
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);
136 myActiveDocLbl->installEventFilter(this);
138 aLabelLay->addWidget(myActiveDocLbl);
139 aLabelLay->setStretch(1,1);
141 myTreeView = new XGUI_DataTree(this);
142 aLayout->addWidget(myTreeView);
144 myDocModel = myTreeView->dataModel();
146 aLabelWgt->setFrameShape(myTreeView->frameShape());
147 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
149 connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
150 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SLOT(onActivePartChanged(ObjectPtr)));
151 connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, SIGNAL(activePartChanged(ObjectPtr)));
153 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)),
154 this, SLOT(onLabelContextMenuRequested(const QPoint&)));
155 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)),
156 this, SLOT(onContextMenuRequested(QContextMenuEvent*)));
158 onActivePartChanged(ObjectPtr());
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()));
167 //***************************************************
168 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
172 //***************************************************
173 void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart)
175 QPalette aPalet = myActiveDocLbl->palette();
177 aPalet.setColor(QPalette::Text, Qt::black);
179 aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
181 myActiveDocLbl->setPalette(aPalet);
184 //***************************************************
185 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
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);
193 myDocModel->deactivatePart();
194 onActivePartChanged(ObjectPtr());
195 emit activePartChanged(ObjectPtr());
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);
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);
211 case Qt::Key_Escape: // Cancel the input
212 closeDocNameEditing(false);
218 return QWidget::eventFilter(obj, theEvent);
221 //***************************************************
222 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
224 myActiveDocLbl->deselect();
225 myActiveDocLbl->clearFocus();
226 myActiveDocLbl->releaseMouse();
227 myActiveDocLbl->setReadOnly(true);
229 // TODO: Save the name of root document
230 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
231 DocumentPtr aDoc = aMgr->rootDocument();
233 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
237 //***************************************************
238 void XGUI_ObjectsBrowser::activatePart(const ObjectPtr& thePart)
241 QModelIndex aIndex = myDocModel->partIndex(thePart);
243 if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) {
244 myTreeView->setExpanded(myDocModel->activePartIndex(), false);
246 bool isChanged = myDocModel->activatedIndex(aIndex);
248 if (myDocModel->activePartIndex().isValid()) {
249 myTreeView->setExpanded(aIndex.parent(), true);
250 myTreeView->setExpanded(aIndex, true);
251 onActivePartChanged(myDocModel->feature(aIndex));
253 onActivePartChanged(ObjectPtr());
257 QModelIndex aIndex = myDocModel->activePartIndex();
258 if (aIndex.isValid()) {
259 myDocModel->activatedIndex(aIndex);
260 myTreeView->setExpanded(aIndex, false);
261 onActivePartChanged(ObjectPtr());
266 //***************************************************
267 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
269 myObjectsList = myTreeView->selectedFeatures();
270 bool toEnable = myObjectsList.size() > 0;
271 foreach(QAction* aCmd, actions()) {
272 aCmd->setEnabled(toEnable);
274 emit contextMenuRequested(theEvent);
277 //***************************************************
278 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
280 myObjectsList.clear();
281 //Empty feature pointer means that selected root document
282 myObjectsList.append(ObjectPtr());
284 foreach(QAction* aCmd, actions()) {
285 aCmd->setEnabled(true);
287 QContextMenuEvent aEvent( QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt) );
288 emit contextMenuRequested(&aEvent);
291 //***************************************************
292 void XGUI_ObjectsBrowser::onEditItem()
294 if (myObjectsList.size() > 0) {
295 ObjectPtr aFeature = myObjectsList.first();
296 if (aFeature) { // Selection happens in TreeView
297 // Find index which corresponds the feature
299 foreach(QModelIndex aIdx, selectedIndexes()) {
300 ObjectPtr aFea = dataModel()->object(aIdx);
301 if (dataModel()->object(aIdx)->isSame(aFeature)) {
306 if (aIndex.isValid()) {
307 myTreeView->setCurrentIndex(aIndex);
308 myTreeView->edit(aIndex);
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());
320 //***************************************************
321 void XGUI_ObjectsBrowser::onSelectionChanged()
323 myObjectsList = myTreeView->selectedObjects();
324 emit selectionChanged();
327 //***************************************************
328 void XGUI_ObjectsBrowser::rebuildDataTree()
330 myDocModel->rebuildDataTree();
334 //***************************************************
335 void XGUI_ObjectsBrowser::setObjectsSelected(const QList<ObjectPtr>& theObjects)
337 QList<QModelIndex> theIndexes;
338 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
339 aSelectModel->clear();
341 foreach(ObjectPtr aFeature, theObjects) {
342 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
343 if (aIndex.isValid()) {
344 aSelectModel->select(aIndex, QItemSelectionModel::Select);