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>
11 #include <Events_Error.h>
13 #include <ModuleBase_Tools.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 XGUI_DataModel* 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 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
73 SLOT(onDoubleClick(const QModelIndex&)));
76 XGUI_DataTree::~XGUI_DataTree()
80 XGUI_DataModel* XGUI_DataTree::dataModel() const
82 return static_cast<XGUI_DataModel*>(model());
85 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
87 emit contextMenuRequested(theEvent);
90 void XGUI_DataTree::commitData(QWidget* theEditor)
92 static int aEntrance = 0;
94 // We have to check number of enter and exit of this function because it can be called recursively by Qt
95 // in order to avoid double modifying of a data
97 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
99 QString aName = aEditor->text();
100 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
101 XGUI_DataModel* aModel = dataModel();
102 ObjectPtr aObj = aModel->object(aIndexList.first());
104 if (XGUI_Tools::canRename(aObj, aName))
105 aObj->data()->setName(qPrintable(aName));
111 void XGUI_DataTree::clear()
113 dataModel()->clear();
117 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
119 QSize aSize = theEvent->size();
120 if (aSize.isValid()) {
121 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
122 setColumnWidth(1, SECOND_COL_WIDTH);
126 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
128 if (theIndex.column() != 1)
130 SessionPtr aMgr = ModelAPI_Session::get();
131 // When operation is opened then we can not change history
132 if (aMgr->isOperation())
134 XGUI_DataModel* aModel = dataModel();
135 if (aModel->flags(theIndex) == 0)
137 ObjectPtr aObj = aModel->object(theIndex);
139 DocumentPtr aDoc = aMgr->activeDocument();
141 std::string aOpName = tr("History change").toStdString();
143 if (aObj->document() != aDoc)
145 aMgr->startOperation(aOpName);
146 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
147 aMgr->finishOperation();
149 // Ignore clicks on folders outside current document
150 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
151 // Clicked folder under root but active document is another
153 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
154 // Cliced not on active document folder
157 aMgr->startOperation(aOpName);
158 aDoc->setCurrentFeature(FeaturePtr(), true);
159 aMgr->finishOperation();
161 QModelIndex aNewIndex = aModel->lastHistoryIndex();
162 QModelIndex aParent = theIndex.parent();
163 int aSize = aModel->rowCount(aParent);
164 for (int i = 0; i < aSize; i++) {
165 update(aModel->index(i, 0, aParent));
169 //********************************************************************
170 //********************************************************************
171 //********************************************************************
172 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
173 : QWidget(theParent), myDocModel(0)
175 QVBoxLayout* aLayout = new QVBoxLayout(this);
176 ModuleBase_Tools::zeroMargins(aLayout);
177 aLayout->setSpacing(0);
179 QFrame* aLabelWgt = new QFrame(this);
180 //QWidget* aLabelWgt = new QWidget(this);
181 aLabelWgt->setAutoFillBackground(true);
182 QPalette aPalet = aLabelWgt->palette();
183 aPalet.setColor(QPalette::Window, Qt::white);
184 aLabelWgt->setPalette(aPalet);
186 aLayout->addWidget(aLabelWgt);
187 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
188 ModuleBase_Tools::zeroMargins(aLabelLay);
189 aLabelLay->setSpacing(0);
191 QLabel* aLbl = new QLabel(aLabelWgt);
192 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
195 aLbl->setAutoFillBackground(true);
197 aLabelLay->addWidget(aLbl);
199 SessionPtr aMgr = ModelAPI_Session::get();
200 DocumentPtr aDoc = aMgr->moduleDocument();
201 // TODO: Find a name of the root document
203 myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt);
204 myActiveDocLbl->setReadOnly(true);
205 myActiveDocLbl->setFrame(false);
206 //myActiveDocLbl->setMargin(2);
207 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
209 myActiveDocLbl->installEventFilter(this);
211 aLabelLay->addWidget(myActiveDocLbl);
212 aLabelLay->setStretch(1, 1);
214 myTreeView = new XGUI_DataTree(this);
215 //myTreeView->setFrameShape(QFrame::NoFrame);
216 aLayout->addWidget(myTreeView);
218 aLabelWgt->setFrameShape(myTreeView->frameShape());
219 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
221 myDocModel = new XGUI_DataModel(this);
222 myTreeView->setModel(myDocModel);
223 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
224 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
225 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
227 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
228 SLOT(onLabelContextMenuRequested(const QPoint&)));
229 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
230 SLOT(onContextMenuRequested(QContextMenuEvent*)));
233 //***************************************************
234 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
238 //***************************************************
239 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
241 if (obj == myActiveDocLbl) {
242 if (!myActiveDocLbl->isReadOnly()) {
243 // End of editing by mouse click
244 if (theEvent->type() == QEvent::MouseButtonRelease) {
245 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
246 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
247 if (childAt(aPnt) != myActiveDocLbl) {
248 closeDocNameEditing(true);
250 } else if (theEvent->type() == QEvent::KeyRelease) {
251 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
252 switch (aEvent->key()) {
254 case Qt::Key_Enter: // Accept current input
255 closeDocNameEditing(true);
257 case Qt::Key_Escape: // Cancel the input
258 closeDocNameEditing(false);
263 if (theEvent->type() == QEvent::MouseButtonDblClick) {
264 emit headerMouseDblClicked(QModelIndex());
269 return QWidget::eventFilter(obj, theEvent);
272 //***************************************************
273 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
275 myActiveDocLbl->deselect();
276 myActiveDocLbl->clearFocus();
277 myActiveDocLbl->releaseMouse();
278 myActiveDocLbl->setReadOnly(true);
280 // TODO: Save the name of root document
281 SessionPtr aMgr = ModelAPI_Session::get();
282 DocumentPtr aDoc = aMgr->moduleDocument();
284 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
288 //***************************************************
289 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
291 QModelIndexList aIndexes;
292 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
293 bool toEnable = false;
295 if (aSelectedData.size() == 1) {
296 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
297 if (!aIndexes.contains(aSelected))
298 return; // menu is called on non selected item
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 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
388 const QItemSelection& theDeselected)
390 emit selectionChanged();
393 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
395 QObjectPtrList aList;
396 QModelIndexList aIndexes = selectedIndexes();
397 XGUI_DataModel* aModel = dataModel();
398 QModelIndexList::const_iterator aIt;
399 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
400 if ((*aIt).column() == 0) {
401 ObjectPtr aObject = aModel->object(*aIt);
403 aList.append(aObject);
405 theIndexes->append(*aIt);