1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_ObjectsBrowser.h"
4 #include "XGUI_Tools.h"
5 #include "XGUI_DataModel.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Tools.h>
12 #include <ModuleBase_Tools.h>
19 #include <QMouseEvent>
21 #include <QStyledItemDelegate>
22 #include <QMessageBox>
24 /// Width of second column (minimum acceptable = 27)
25 #define SECOND_COL_WIDTH 30
30 * Tree item delegate for definition of data in column items editor
32 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
36 /// \param theParent a parent of the delegate
37 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
39 /// Set data for item editor (name of the item)
40 /// \param editor a widget of editor
41 /// \param index the tree item index
42 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
44 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
46 XGUI_DataModel* aModel = myTreedView->dataModel();
47 ObjectPtr aObj = aModel->object(index);
48 if (aObj.get() != NULL) {
49 aEditor->setText(aObj->data()->name().c_str());
53 QStyledItemDelegate::setEditorData(editor, index);
57 XGUI_DataTree* myTreedView;
61 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
62 : QTreeView(theParent)
64 setHeaderHidden(true);
65 setEditTriggers(QAbstractItemView::NoEditTriggers);
66 setSelectionBehavior(QAbstractItemView::SelectRows);
67 setSelectionMode(QAbstractItemView::ExtendedSelection);
69 setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
71 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
72 SLOT(onDoubleClick(const QModelIndex&)));
75 XGUI_DataTree::~XGUI_DataTree()
79 XGUI_DataModel* XGUI_DataTree::dataModel() const
81 return static_cast<XGUI_DataModel*>(model());
84 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
86 emit contextMenuRequested(theEvent);
89 void XGUI_DataTree::commitData(QWidget* theEditor)
91 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
93 QString aName = aEditor->text();
94 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
95 XGUI_DataModel* aModel = dataModel();
96 ObjectPtr aObj = aModel->object(aIndexList.first());
97 SessionPtr aMgr = ModelAPI_Session::get();
98 aMgr->startOperation("Rename");
100 if (!XGUI_Tools::canRename(this, aObj, aName)) {
101 aMgr->abortOperation();
105 aObj->data()->setName(qPrintable(aName));
106 aMgr->finishOperation();
110 void XGUI_DataTree::clear()
112 dataModel()->clear();
116 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
118 QSize aSize = theEvent->size();
119 if (aSize.isValid()) {
120 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
121 setColumnWidth(1, SECOND_COL_WIDTH);
125 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
127 if (theIndex.column() != 1)
129 SessionPtr aMgr = ModelAPI_Session::get();
130 // When operation is opened then we can not change history
131 if (aMgr->isOperation())
133 XGUI_DataModel* aModel = dataModel();
134 if (aModel->flags(theIndex) == 0)
136 ObjectPtr aObj = aModel->object(theIndex);
138 DocumentPtr aDoc = aMgr->activeDocument();
140 std::string aOpName = tr("History change").toStdString();
142 if (aObj->document() != aDoc)
144 aMgr->startOperation(aOpName);
145 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
146 aMgr->finishOperation();
148 // Ignore clicks on folders outside current document
149 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
150 // Clicked folder under root but active document is another
152 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
153 // Cliced not on active document folder
156 aMgr->startOperation(aOpName);
157 aDoc->setCurrentFeature(FeaturePtr(), true);
158 aMgr->finishOperation();
160 QModelIndex aNewIndex = aModel->lastHistoryIndex();
161 QModelIndex aParent = theIndex.parent();
162 int aSize = aModel->rowCount(aParent);
163 for (int i = 0; i < aSize; i++) {
164 update(aModel->index(i, 0, aParent));
168 //********************************************************************
169 //********************************************************************
170 //********************************************************************
171 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
172 : QWidget(theParent), myDocModel(0)
174 QVBoxLayout* aLayout = new QVBoxLayout(this);
175 ModuleBase_Tools::zeroMargins(aLayout);
176 aLayout->setSpacing(0);
178 QFrame* aLabelWgt = new QFrame(this);
179 //QWidget* aLabelWgt = new QWidget(this);
180 aLabelWgt->setAutoFillBackground(true);
181 QPalette aPalet = aLabelWgt->palette();
182 aPalet.setColor(QPalette::Window, Qt::white);
183 aLabelWgt->setPalette(aPalet);
185 aLayout->addWidget(aLabelWgt);
186 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
187 ModuleBase_Tools::zeroMargins(aLabelLay);
188 aLabelLay->setSpacing(0);
190 QLabel* aLbl = new QLabel(aLabelWgt);
191 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
194 aLbl->setAutoFillBackground(true);
196 aLabelLay->addWidget(aLbl);
198 SessionPtr aMgr = ModelAPI_Session::get();
199 DocumentPtr aDoc = aMgr->moduleDocument();
200 // TODO: Find a name of the root document
202 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
203 myActiveDocLbl->setReadOnly(true);
204 myActiveDocLbl->setFrame(false);
205 //myActiveDocLbl->setMargin(2);
206 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
208 myActiveDocLbl->installEventFilter(this);
210 aLabelLay->addWidget(myActiveDocLbl);
211 aLabelLay->setStretch(1, 1);
213 myTreeView = new XGUI_DataTree(this);
214 //myTreeView->setFrameShape(QFrame::NoFrame);
215 aLayout->addWidget(myTreeView);
217 aLabelWgt->setFrameShape(myTreeView->frameShape());
218 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
220 myDocModel = new XGUI_DataModel(this);
221 myTreeView->setModel(myDocModel);
222 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
223 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
224 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
226 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
227 SLOT(onLabelContextMenuRequested(const QPoint&)));
228 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
229 SLOT(onContextMenuRequested(QContextMenuEvent*)));
232 //***************************************************
233 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
237 //***************************************************
238 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
240 if (obj == myActiveDocLbl) {
241 if (!myActiveDocLbl->isReadOnly()) {
242 // End of editing by mouse click
243 if (theEvent->type() == QEvent::MouseButtonRelease) {
244 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
245 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
246 if (childAt(aPnt) != myActiveDocLbl) {
247 closeDocNameEditing(true);
249 } else if (theEvent->type() == QEvent::KeyRelease) {
250 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
251 switch (aEvent->key()) {
253 case Qt::Key_Enter: // Accept current input
254 closeDocNameEditing(true);
256 case Qt::Key_Escape: // Cancel the input
257 closeDocNameEditing(false);
262 if (theEvent->type() == QEvent::MouseButtonDblClick) {
263 emit headerMouseDblClicked(QModelIndex());
268 return QWidget::eventFilter(obj, theEvent);
271 //***************************************************
272 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
274 myActiveDocLbl->deselect();
275 myActiveDocLbl->clearFocus();
276 myActiveDocLbl->releaseMouse();
277 myActiveDocLbl->setReadOnly(true);
279 // TODO: Save the name of root document
280 SessionPtr aMgr = ModelAPI_Session::get();
281 DocumentPtr aDoc = aMgr->moduleDocument();
283 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
287 //***************************************************
288 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
290 QModelIndexList aIndexes;
291 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
292 bool toEnable = false;
294 if (aSelectedData.size() == 1) {
295 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
296 if (!aIndexes.contains(aSelected))
297 return; // menu is called on non selected item
299 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
300 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
302 foreach(QAction* aCmd, actions()) {
303 aCmd->setEnabled(toEnable);
305 emit contextMenuRequested(theEvent);
308 //***************************************************
309 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
311 myTreeView->selectionModel()->clearSelection();
312 //Empty feature pointer means that selected root document
313 foreach(QAction* aCmd, actions()) {
314 aCmd->setEnabled(true);
316 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
317 emit contextMenuRequested(&aEvent);
320 //***************************************************
321 void XGUI_ObjectsBrowser::onEditItem()
323 QObjectPtrList aSelectedData = selectedObjects();
324 if (aSelectedData.size() > 0) {
325 ObjectPtr aFeature = aSelectedData.first();
326 if (aFeature) { // Selection happens in TreeView
327 QObjectPtrList aList;
328 aList.append(aFeature);
329 // check whether the object can be deleted. There should not be parts which are not loaded
330 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
333 // Find index which corresponds the feature
335 foreach(QModelIndex aIdx, selectedIndexes()) {
336 ObjectPtr aFea = dataModel()->object(aIdx);
337 if (dataModel()->object(aIdx)->isSame(aFeature)) {
342 if (aIndex.isValid()) {
343 myTreeView->setCurrentIndex(aIndex);
344 myTreeView->edit(aIndex);
349 //Selection happens in Upper label
350 myActiveDocLbl->setReadOnly(false);
351 myActiveDocLbl->setFocus();
352 myActiveDocLbl->selectAll();
353 myActiveDocLbl->grabMouse();
354 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
357 //***************************************************
358 void XGUI_ObjectsBrowser::rebuildDataTree()
360 myDocModel->rebuildDataTree();
364 //***************************************************
365 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
367 QList<QModelIndex> theIndexes;
368 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
369 aSelectModel->clear();
371 foreach(ObjectPtr aFeature, theObjects)
373 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
374 if (aIndex.isValid()) {
375 aSelectModel->select(aIndex, QItemSelectionModel::Select);
380 //***************************************************
381 void XGUI_ObjectsBrowser::clearContent()
386 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
387 const QItemSelection& theDeselected)
389 emit selectionChanged();
392 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
394 QObjectPtrList aList;
395 QModelIndexList aIndexes = selectedIndexes();
396 XGUI_DataModel* aModel = dataModel();
397 QModelIndexList::const_iterator aIt;
398 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
399 if ((*aIt).column() == 0) {
400 ObjectPtr aObject = aModel->object(*aIt);
402 aList.append(aObject);
404 theIndexes->append(*aIt);