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>
13 #include <ModuleBase_IDocumentDataModel.h>
20 #include <QMouseEvent>
22 #include <QStyledItemDelegate>
23 #include <QMessageBox>
25 /// Width of second column (minimum acceptable = 27)
26 #define SECOND_COL_WIDTH 30
31 * Tree item delegate for definition of data in column items editor
33 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
37 /// \param theParent a parent of the delegate
38 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
40 /// Set data for item editor (name of the item)
41 /// \param editor a widget of editor
42 /// \param index the tree item index
43 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
45 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
47 ModuleBase_IDocumentDataModel* aModel = myTreedView->dataModel();
48 ObjectPtr aObj = aModel->object(index);
49 if (aObj.get() != NULL) {
50 aEditor->setText(aObj->data()->name().c_str());
54 QStyledItemDelegate::setEditorData(editor, index);
58 XGUI_DataTree* myTreedView;
62 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
63 : QTreeView(theParent)
65 setHeaderHidden(true);
66 setEditTriggers(QAbstractItemView::NoEditTriggers);
67 setSelectionBehavior(QAbstractItemView::SelectRows);
68 setSelectionMode(QAbstractItemView::ExtendedSelection);
70 setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
72 #ifndef ModuleDataModel
73 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
74 SLOT(onDoubleClick(const QModelIndex&)));
78 XGUI_DataTree::~XGUI_DataTree()
82 ModuleBase_IDocumentDataModel* XGUI_DataTree::dataModel() const
84 return static_cast<ModuleBase_IDocumentDataModel*>(model());
87 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
89 emit contextMenuRequested(theEvent);
92 void XGUI_DataTree::commitData(QWidget* theEditor)
94 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
96 QString aName = aEditor->text();
97 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
98 ModuleBase_IDocumentDataModel* aModel = dataModel();
99 ObjectPtr aObj = aModel->object(aIndexList.first());
100 SessionPtr aMgr = ModelAPI_Session::get();
101 aMgr->startOperation("Rename");
103 if (!XGUI_Tools::canRename(this, aObj, aName)) {
104 aMgr->abortOperation();
108 aObj->data()->setName(qPrintable(aName));
109 aMgr->finishOperation();
113 void XGUI_DataTree::clear()
115 ModuleBase_IDocumentDataModel* aModel = dataModel();
120 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
122 QSize aSize = theEvent->size();
123 if (aSize.isValid()) {
124 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
125 setColumnWidth(1, SECOND_COL_WIDTH);
129 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
131 if (theIndex.column() != 1)
133 SessionPtr aMgr = ModelAPI_Session::get();
134 // When operation is opened then we can not change history
135 if (aMgr->isOperation())
137 ModuleBase_IDocumentDataModel* aModel = dataModel();
138 if (aModel->flags(theIndex) == 0)
140 ObjectPtr aObj = aModel->object(theIndex);
142 DocumentPtr aDoc = aMgr->activeDocument();
144 std::string aOpName = tr("History change").toStdString();
146 if (aObj->document() != aDoc)
148 aMgr->startOperation(aOpName);
149 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
150 aMgr->finishOperation();
152 // Ignore clicks on folders outside current document
153 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
154 // Clicked folder under root but active document is another
156 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
157 // Cliced not on active document folder
160 aMgr->startOperation(aOpName);
161 aDoc->setCurrentFeature(FeaturePtr(), true);
162 aMgr->finishOperation();
164 QModelIndex aNewIndex = aModel->lastHistoryIndex();
165 QModelIndex aParent = theIndex.parent();
166 int aSize = aModel->rowCount(aParent);
167 for (int i = 0; i < aSize; i++) {
168 update(aModel->index(i, 0, aParent));
172 //********************************************************************
173 //********************************************************************
174 //********************************************************************
175 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
176 : QWidget(theParent), myDocModel(0)
178 QVBoxLayout* aLayout = new QVBoxLayout(this);
179 ModuleBase_Tools::zeroMargins(aLayout);
180 aLayout->setSpacing(0);
182 QFrame* aLabelWgt = new QFrame(this);
183 aLabelWgt->setAutoFillBackground(true);
184 QPalette aPalet = aLabelWgt->palette();
185 aPalet.setColor(QPalette::Window, Qt::white);
186 aLabelWgt->setPalette(aPalet);
188 aLayout->addWidget(aLabelWgt);
189 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
190 ModuleBase_Tools::zeroMargins(aLabelLay);
191 aLabelLay->setSpacing(0);
193 QLabel* aLbl = new QLabel(aLabelWgt);
194 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
197 aLbl->setAutoFillBackground(true);
199 aLabelLay->addWidget(aLbl);
201 SessionPtr aMgr = ModelAPI_Session::get();
202 DocumentPtr aDoc = aMgr->moduleDocument();
203 // TODO: Find a name of the root document
205 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
206 myActiveDocLbl->setReadOnly(true);
207 myActiveDocLbl->setFrame(false);
208 //myActiveDocLbl->setMargin(2);
209 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
211 myActiveDocLbl->installEventFilter(this);
213 aLabelLay->addWidget(myActiveDocLbl);
214 aLabelLay->setStretch(1, 1);
216 myTreeView = new XGUI_DataTree(this);
217 aLayout->addWidget(myTreeView);
219 aLabelWgt->setFrameShape(myTreeView->frameShape());
220 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
222 #ifndef ModuleDataModel
223 myDocModel = new XGUI_DataModel(this);
224 myTreeView->setModel(myDocModel);
225 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
226 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
227 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
230 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
231 SLOT(onLabelContextMenuRequested(const QPoint&)));
232 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
233 SLOT(onContextMenuRequested(QContextMenuEvent*)));
236 //***************************************************
237 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
241 //***************************************************
242 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
244 if (obj == myActiveDocLbl) {
245 if (!myActiveDocLbl->isReadOnly()) {
246 // End of editing by mouse click
247 if (theEvent->type() == QEvent::MouseButtonRelease) {
248 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
249 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
250 if (childAt(aPnt) != myActiveDocLbl) {
251 closeDocNameEditing(true);
253 } else if (theEvent->type() == QEvent::KeyRelease) {
254 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
255 switch (aEvent->key()) {
257 case Qt::Key_Enter: // Accept current input
258 closeDocNameEditing(true);
260 case Qt::Key_Escape: // Cancel the input
261 closeDocNameEditing(false);
266 if (theEvent->type() == QEvent::MouseButtonDblClick) {
267 emit headerMouseDblClicked(QModelIndex());
272 return QWidget::eventFilter(obj, theEvent);
275 //***************************************************
276 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
278 myActiveDocLbl->deselect();
279 myActiveDocLbl->clearFocus();
280 myActiveDocLbl->releaseMouse();
281 myActiveDocLbl->setReadOnly(true);
283 // TODO: Save the name of root document
284 SessionPtr aMgr = ModelAPI_Session::get();
285 DocumentPtr aDoc = aMgr->moduleDocument();
287 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
291 //***************************************************
292 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
294 QModelIndexList aIndexes;
295 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
296 bool toEnable = false;
297 if (aSelectedData.size() == 1) {
298 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
299 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
301 foreach(QAction* aCmd, actions()) {
302 aCmd->setEnabled(toEnable);
304 emit contextMenuRequested(theEvent);
307 //***************************************************
308 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
310 myTreeView->selectionModel()->clearSelection();
311 //Empty feature pointer means that selected root document
312 foreach(QAction* aCmd, actions()) {
313 aCmd->setEnabled(true);
315 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
316 emit contextMenuRequested(&aEvent);
319 //***************************************************
320 void XGUI_ObjectsBrowser::onEditItem()
322 QObjectPtrList aSelectedData = selectedObjects();
323 if (aSelectedData.size() > 0) {
324 ObjectPtr aFeature = aSelectedData.first();
325 if (aFeature) { // Selection happens in TreeView
326 QObjectPtrList aList;
327 aList.append(aFeature);
328 // check whether the object can be deleted. There should not be parts which are not loaded
329 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
332 // Find index which corresponds the feature
334 foreach(QModelIndex aIdx, selectedIndexes()) {
335 ObjectPtr aFea = dataModel()->object(aIdx);
336 if (dataModel()->object(aIdx)->isSame(aFeature)) {
341 if (aIndex.isValid()) {
342 myTreeView->setCurrentIndex(aIndex);
343 myTreeView->edit(aIndex);
348 //Selection happens in Upper label
349 myActiveDocLbl->setReadOnly(false);
350 myActiveDocLbl->setFocus();
351 myActiveDocLbl->selectAll();
352 myActiveDocLbl->grabMouse();
353 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
356 //***************************************************
357 void XGUI_ObjectsBrowser::rebuildDataTree()
359 myDocModel->rebuildDataTree();
363 //***************************************************
364 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
366 QList<QModelIndex> theIndexes;
367 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
368 aSelectModel->clear();
370 foreach(ObjectPtr aFeature, theObjects)
372 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
373 if (aIndex.isValid()) {
374 aSelectModel->select(aIndex, QItemSelectionModel::Select);
379 //***************************************************
380 void XGUI_ObjectsBrowser::clearContent()
385 #ifdef ModuleDataModel
386 void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
388 myDocModel = theModel;
389 //myDocModel = new XGUI_DataModel(this);
390 myTreeView->setModel(myDocModel);
391 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
392 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
393 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
397 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
398 const QItemSelection& theDeselected)
400 emit selectionChanged();
403 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
405 QObjectPtrList aList;
406 QModelIndexList aIndexes = selectedIndexes();
407 #ifdef ModuleDataModel
408 ModuleBase_IDocumentDataModel* aModel = dataModel();
410 XGUI_DataModel* aModel = dataModel();
412 QModelIndexList::const_iterator aIt;
413 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
414 if ((*aIt).column() == 0) {
415 ObjectPtr aObject = aModel->object(*aIt);
417 aList.append(aObject);
419 theIndexes->append(*aIt);