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 ModuleBase_IDocumentDataModel* aModel = dataModel();
134 if (aModel->flags(theIndex) == 0)
136 ObjectPtr aObj = aModel->object(theIndex);
138 SessionPtr aMgr = ModelAPI_Session::get();
139 DocumentPtr aDoc = aMgr->activeDocument();
141 QModelIndex aOldIndex = aModel->lastHistoryIndex();
143 std::string aOpName = tr("History change").toStdString();
145 if (aObj->document() != aDoc)
147 aMgr->startOperation(aOpName);
148 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
149 aMgr->finishOperation();
151 // Ignore clicks on folders outside current document
152 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
153 // Clicked folder under root but active document is another
155 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
156 // Cliced not on active document folder
159 aMgr->startOperation(aOpName);
160 aDoc->setCurrentFeature(FeaturePtr(), true);
161 aMgr->finishOperation();
163 QModelIndex aNewIndex = aModel->lastHistoryIndex();
164 QModelIndex aParent = theIndex.parent();
165 int aStartRow = std::min(aOldIndex.row(), aNewIndex.row());
166 int aEndRow = std::max(aOldIndex.row(), aNewIndex.row());
167 for (int i = aStartRow; i <= aEndRow; i++) {
168 update(aModel->index(i, 0, aParent));
174 //********************************************************************
175 //********************************************************************
176 //********************************************************************
177 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
178 : QWidget(theParent), myDocModel(0)
180 QVBoxLayout* aLayout = new QVBoxLayout(this);
181 ModuleBase_Tools::zeroMargins(aLayout);
182 aLayout->setSpacing(0);
184 QFrame* aLabelWgt = new QFrame(this);
185 aLabelWgt->setAutoFillBackground(true);
186 QPalette aPalet = aLabelWgt->palette();
187 aPalet.setColor(QPalette::Window, Qt::white);
188 aLabelWgt->setPalette(aPalet);
190 aLayout->addWidget(aLabelWgt);
191 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
192 ModuleBase_Tools::zeroMargins(aLabelLay);
193 aLabelLay->setSpacing(0);
195 QLabel* aLbl = new QLabel(aLabelWgt);
196 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
199 aLbl->setAutoFillBackground(true);
201 aLabelLay->addWidget(aLbl);
203 SessionPtr aMgr = ModelAPI_Session::get();
204 DocumentPtr aDoc = aMgr->moduleDocument();
205 // TODO: Find a name of the root document
207 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
208 myActiveDocLbl->setReadOnly(true);
209 myActiveDocLbl->setFrame(false);
210 //myActiveDocLbl->setMargin(2);
211 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
213 myActiveDocLbl->installEventFilter(this);
215 aLabelLay->addWidget(myActiveDocLbl);
216 aLabelLay->setStretch(1, 1);
218 myTreeView = new XGUI_DataTree(this);
219 aLayout->addWidget(myTreeView);
221 aLabelWgt->setFrameShape(myTreeView->frameShape());
222 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
224 #ifndef ModuleDataModel
225 myDocModel = new XGUI_DataModel(this);
226 myTreeView->setModel(myDocModel);
227 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
228 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
229 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
232 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
233 SLOT(onLabelContextMenuRequested(const QPoint&)));
234 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
235 SLOT(onContextMenuRequested(QContextMenuEvent*)));
238 //***************************************************
239 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
243 //***************************************************
244 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
246 if (obj == myActiveDocLbl) {
247 if (!myActiveDocLbl->isReadOnly()) {
248 // End of editing by mouse click
249 if (theEvent->type() == QEvent::MouseButtonRelease) {
250 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
251 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
252 if (childAt(aPnt) != myActiveDocLbl) {
253 closeDocNameEditing(true);
255 } else if (theEvent->type() == QEvent::KeyRelease) {
256 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
257 switch (aEvent->key()) {
259 case Qt::Key_Enter: // Accept current input
260 closeDocNameEditing(true);
262 case Qt::Key_Escape: // Cancel the input
263 closeDocNameEditing(false);
268 if (theEvent->type() == QEvent::MouseButtonDblClick) {
269 emit headerMouseDblClicked(QModelIndex());
274 return QWidget::eventFilter(obj, theEvent);
277 //***************************************************
278 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
280 myActiveDocLbl->deselect();
281 myActiveDocLbl->clearFocus();
282 myActiveDocLbl->releaseMouse();
283 myActiveDocLbl->setReadOnly(true);
285 // TODO: Save the name of root document
286 SessionPtr aMgr = ModelAPI_Session::get();
287 DocumentPtr aDoc = aMgr->moduleDocument();
289 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
293 //***************************************************
294 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
296 QModelIndexList aIndexes;
297 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
298 bool toEnable = false;
299 if (aSelectedData.size() == 1) {
300 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
301 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
303 foreach(QAction* aCmd, actions()) {
304 aCmd->setEnabled(toEnable);
306 emit contextMenuRequested(theEvent);
309 //***************************************************
310 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
312 myTreeView->selectionModel()->clearSelection();
313 //Empty feature pointer means that selected root document
314 foreach(QAction* aCmd, actions()) {
315 aCmd->setEnabled(true);
317 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
318 emit contextMenuRequested(&aEvent);
321 //***************************************************
322 void XGUI_ObjectsBrowser::onEditItem()
324 QObjectPtrList aSelectedData = selectedObjects();
325 if (aSelectedData.size() > 0) {
326 ObjectPtr aFeature = aSelectedData.first();
327 if (aFeature) { // Selection happens in TreeView
328 QObjectPtrList aList;
329 aList.append(aFeature);
330 // check whether the object can be deleted. There should not be parts which are not loaded
331 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
334 // Find index which corresponds the feature
336 foreach(QModelIndex aIdx, selectedIndexes()) {
337 ObjectPtr aFea = dataModel()->object(aIdx);
338 if (dataModel()->object(aIdx)->isSame(aFeature)) {
343 if (aIndex.isValid()) {
344 myTreeView->setCurrentIndex(aIndex);
345 myTreeView->edit(aIndex);
350 //Selection happens in Upper label
351 myActiveDocLbl->setReadOnly(false);
352 myActiveDocLbl->setFocus();
353 myActiveDocLbl->selectAll();
354 myActiveDocLbl->grabMouse();
355 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
358 //***************************************************
359 void XGUI_ObjectsBrowser::rebuildDataTree()
361 myDocModel->rebuildDataTree();
365 //***************************************************
366 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
368 QList<QModelIndex> theIndexes;
369 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
370 aSelectModel->clear();
372 foreach(ObjectPtr aFeature, theObjects)
374 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
375 if (aIndex.isValid()) {
376 aSelectModel->select(aIndex, QItemSelectionModel::Select);
381 //***************************************************
382 void XGUI_ObjectsBrowser::clearContent()
387 #ifdef ModuleDataModel
388 void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
390 myDocModel = theModel;
391 //myDocModel = new XGUI_DataModel(this);
392 myTreeView->setModel(myDocModel);
393 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
394 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
395 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
399 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
400 const QItemSelection& theDeselected)
402 emit selectionChanged();
405 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
407 QObjectPtrList aList;
408 QModelIndexList aIndexes = selectedIndexes();
409 #ifdef ModuleDataModel
410 ModuleBase_IDocumentDataModel* aModel = dataModel();
412 XGUI_DataModel* aModel = dataModel();
414 QModelIndexList::const_iterator aIt;
415 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
416 if ((*aIt).column() == 0) {
417 ObjectPtr aObject = aModel->object(*aIt);
419 aList.append(aObject);
421 theIndexes->append(*aIt);