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 SessionPtr aMgr = ModelAPI_Session::get();
106 aMgr->startOperation("Rename");
107 aObj->data()->setName(qPrintable(aName));
108 aMgr->finishOperation();
115 void XGUI_DataTree::clear()
117 dataModel()->clear();
121 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
123 QSize aSize = theEvent->size();
124 if (aSize.isValid()) {
125 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
126 setColumnWidth(1, SECOND_COL_WIDTH);
130 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
132 if (theIndex.column() != 1)
134 SessionPtr aMgr = ModelAPI_Session::get();
135 // When operation is opened then we can not change history
136 if (aMgr->isOperation())
138 XGUI_DataModel* aModel = dataModel();
139 if (aModel->flags(theIndex) == 0)
141 ObjectPtr aObj = aModel->object(theIndex);
143 DocumentPtr aDoc = aMgr->activeDocument();
145 std::string aOpName = tr("History change").toStdString();
147 if (aObj->document() != aDoc)
149 aMgr->startOperation(aOpName);
150 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
151 aMgr->finishOperation();
153 // Ignore clicks on folders outside current document
154 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
155 // Clicked folder under root but active document is another
157 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
158 // Cliced not on active document folder
161 aMgr->startOperation(aOpName);
162 aDoc->setCurrentFeature(FeaturePtr(), true);
163 aMgr->finishOperation();
165 QModelIndex aNewIndex = aModel->lastHistoryIndex();
166 QModelIndex aParent = theIndex.parent();
167 int aSize = aModel->rowCount(aParent);
168 for (int i = 0; i < aSize; i++) {
169 update(aModel->index(i, 0, aParent));
173 //********************************************************************
174 //********************************************************************
175 //********************************************************************
176 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
177 : QWidget(theParent), myDocModel(0)
179 QVBoxLayout* aLayout = new QVBoxLayout(this);
180 ModuleBase_Tools::zeroMargins(aLayout);
181 aLayout->setSpacing(0);
183 QFrame* aLabelWgt = new QFrame(this);
184 //QWidget* aLabelWgt = new QWidget(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 //myTreeView->setFrameShape(QFrame::NoFrame);
220 aLayout->addWidget(myTreeView);
222 aLabelWgt->setFrameShape(myTreeView->frameShape());
223 aLabelWgt->setFrameShadow(myTreeView->frameShadow());
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&)));
231 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
232 SLOT(onLabelContextMenuRequested(const QPoint&)));
233 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
234 SLOT(onContextMenuRequested(QContextMenuEvent*)));
237 //***************************************************
238 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
242 //***************************************************
243 bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent)
245 if (obj == myActiveDocLbl) {
246 if (!myActiveDocLbl->isReadOnly()) {
247 // End of editing by mouse click
248 if (theEvent->type() == QEvent::MouseButtonRelease) {
249 QMouseEvent* aEvent = (QMouseEvent*) theEvent;
250 QPoint aPnt = mapFromGlobal(aEvent->globalPos());
251 if (childAt(aPnt) != myActiveDocLbl) {
252 closeDocNameEditing(true);
254 } else if (theEvent->type() == QEvent::KeyRelease) {
255 QKeyEvent* aEvent = (QKeyEvent*) theEvent;
256 switch (aEvent->key()) {
258 case Qt::Key_Enter: // Accept current input
259 closeDocNameEditing(true);
261 case Qt::Key_Escape: // Cancel the input
262 closeDocNameEditing(false);
267 if (theEvent->type() == QEvent::MouseButtonDblClick) {
268 emit headerMouseDblClicked(QModelIndex());
273 return QWidget::eventFilter(obj, theEvent);
276 //***************************************************
277 void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave)
279 myActiveDocLbl->deselect();
280 myActiveDocLbl->clearFocus();
281 myActiveDocLbl->releaseMouse();
282 myActiveDocLbl->setReadOnly(true);
284 // TODO: Save the name of root document
285 SessionPtr aMgr = ModelAPI_Session::get();
286 DocumentPtr aDoc = aMgr->moduleDocument();
288 myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString());
292 //***************************************************
293 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
295 QModelIndexList aIndexes;
296 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
297 bool toEnable = false;
299 if (aSelectedData.size() == 1) {
300 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
301 if (!aIndexes.contains(aSelected))
302 return; // menu is called on non selected item
304 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
305 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
307 foreach(QAction* aCmd, actions()) {
308 aCmd->setEnabled(toEnable);
310 emit contextMenuRequested(theEvent);
313 //***************************************************
314 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
316 myTreeView->selectionModel()->clearSelection();
317 //Empty feature pointer means that selected root document
318 foreach(QAction* aCmd, actions()) {
319 aCmd->setEnabled(true);
321 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
322 emit contextMenuRequested(&aEvent);
325 //***************************************************
326 void XGUI_ObjectsBrowser::onEditItem()
328 QObjectPtrList aSelectedData = selectedObjects();
329 if (aSelectedData.size() > 0) {
330 ObjectPtr aFeature = aSelectedData.first();
331 if (aFeature) { // Selection happens in TreeView
332 QObjectPtrList aList;
333 aList.append(aFeature);
334 // check whether the object can be deleted. There should not be parts which are not loaded
335 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
338 // Find index which corresponds the feature
340 foreach(QModelIndex aIdx, selectedIndexes()) {
341 ObjectPtr aFea = dataModel()->object(aIdx);
342 if (dataModel()->object(aIdx)->isSame(aFeature)) {
347 if (aIndex.isValid()) {
348 myTreeView->setCurrentIndex(aIndex);
349 myTreeView->edit(aIndex);
354 //Selection happens in Upper label
355 myActiveDocLbl->setReadOnly(false);
356 myActiveDocLbl->setFocus();
357 myActiveDocLbl->selectAll();
358 myActiveDocLbl->grabMouse();
359 myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text());
362 //***************************************************
363 void XGUI_ObjectsBrowser::rebuildDataTree()
365 myDocModel->rebuildDataTree();
369 //***************************************************
370 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
372 QList<QModelIndex> theIndexes;
373 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
374 aSelectModel->clear();
376 foreach(ObjectPtr aFeature, theObjects)
378 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
379 if (aIndex.isValid()) {
380 aSelectModel->select(aIndex, QItemSelectionModel::Select);
385 //***************************************************
386 void XGUI_ObjectsBrowser::clearContent()
391 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
392 const QItemSelection& theDeselected)
394 emit selectionChanged();
397 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
399 QObjectPtrList aList;
400 QModelIndexList aIndexes = selectedIndexes();
401 XGUI_DataModel* aModel = dataModel();
402 QModelIndexList::const_iterator aIt;
403 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
404 if ((*aIt).column() == 0) {
405 ObjectPtr aObject = aModel->object(*aIt);
407 aList.append(aObject);
409 theIndexes->append(*aIt);