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 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
225 SLOT(onLabelContextMenuRequested(const QPoint&)));
226 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
227 SLOT(onContextMenuRequested(QContextMenuEvent*)));
229 // Create internal actions
230 QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
231 aAction->setData("RENAME_CMD");
232 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem()));
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 void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel)
387 #ifdef ModuleDataModel
388 myDocModel = theModel;
390 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&)));
398 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
399 const QItemSelection& theDeselected)
401 emit selectionChanged();
404 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
406 QObjectPtrList aList;
407 QModelIndexList aIndexes = selectedIndexes();
408 #ifdef ModuleDataModel
409 ModuleBase_IDocumentDataModel* aModel = dataModel();
411 XGUI_DataModel* aModel = dataModel();
413 QModelIndexList::const_iterator aIt;
414 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
415 if ((*aIt).column() == 0) {
416 ObjectPtr aObject = aModel->object(*aIt);
418 aList.append(aObject);
420 theIndexes->append(*aIt);