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>
19 #include <QMouseEvent>
21 #include <QStyledItemDelegate>
22 #include <QMessageBox>
24 /// Width of second column (minimum acceptable = 27)
25 #define SECOND_COL_WIDTH 30
30 * Tree item delegate for definition of data in column items editor
32 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
36 /// \param theParent a parent of the delegate
37 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
39 /// Set data for item editor (name of the item)
40 /// \param editor a widget of editor
41 /// \param index the tree item index
42 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
44 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
46 XGUI_DataModel* aModel = myTreedView->dataModel();
47 ObjectPtr aObj = aModel->object(index);
48 if (aObj.get() != NULL) {
49 aEditor->setText(aObj->data()->name().c_str());
53 QStyledItemDelegate::setEditorData(editor, index);
57 XGUI_DataTree* myTreedView;
61 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
62 : QTreeView(theParent)
64 setHeaderHidden(true);
65 setEditTriggers(QAbstractItemView::NoEditTriggers);
66 setSelectionBehavior(QAbstractItemView::SelectRows);
67 setSelectionMode(QAbstractItemView::ExtendedSelection);
69 setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
71 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
72 SLOT(onDoubleClick(const QModelIndex&)));
75 XGUI_DataTree::~XGUI_DataTree()
79 XGUI_DataModel* XGUI_DataTree::dataModel() const
81 return static_cast<XGUI_DataModel*>(model());
84 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
86 emit contextMenuRequested(theEvent);
89 void XGUI_DataTree::commitData(QWidget* theEditor)
91 static int aEntrance = 0;
93 // We have to check number of enter and exit of this function because it can be called recursively by Qt
94 // in order to avoid double modifying of a data
96 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
98 QString aName = aEditor->text();
99 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
100 XGUI_DataModel* aModel = dataModel();
101 ObjectPtr aObj = aModel->object(aIndexList.first());
103 if (XGUI_Tools::canRename(aObj, aName)) {
104 SessionPtr aMgr = ModelAPI_Session::get();
105 aMgr->startOperation("Rename");
106 aObj->data()->setName(qPrintable(aName));
107 aMgr->finishOperation();
114 void XGUI_DataTree::clear()
116 dataModel()->clear();
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 XGUI_DataModel* 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_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
176 : QLineEdit(theText, theParent),
177 myPreSelectionStyle(""),
179 mySelectionStyle(""),
184 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
186 myTreeView = theView;
187 QPalette aPalet = myTreeView->palette();
188 QColor aHighlight = aPalet.highlight().color();
190 myPreSelectionStyle = "QLineEdit {background-color: ";
191 myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");";
192 myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
194 QString aName = aPalet.color(QPalette::Base).name();
195 myNeutralStyle = "QLineEdit { border: 1px solid " + aName + " }";
197 mySelectionStyle = "QLineEdit {background-color: ";
198 mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 " + aHighlight.lighter(170).name();
199 mySelectionStyle += ", stop:1 " + aHighlight.lighter().name() + ");";
200 mySelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
202 connect(myTreeView->selectionModel(),
203 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
208 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
210 switch (theEvent->type()) {
213 setStyleSheet(myPreSelectionStyle);
217 setStyleSheet(myNeutralStyle);
220 return QLineEdit::event(theEvent);
223 static bool MYClearing = false;
224 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
228 setStyleSheet(mySelectionStyle);
229 // We can not block signals because on
230 // clear selection the View state will not be updated
231 myTreeView->clearSelection();
232 QLineEdit::mouseReleaseEvent(e);
236 void XGUI_ActiveDocLbl::unselect()
239 myIsSelected = false;
240 setStyleSheet(myNeutralStyle);
245 //********************************************************************
246 //********************************************************************
247 //********************************************************************
248 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
249 : QWidget(theParent), myDocModel(0)
251 QVBoxLayout* aLayout = new QVBoxLayout(this);
252 ModuleBase_Tools::zeroMargins(aLayout);
253 aLayout->setSpacing(0);
255 QWidget* aLabelWgt = new QWidget(this);
256 aLabelWgt->setAutoFillBackground(true);
258 aLayout->addWidget(aLabelWgt);
259 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
260 ModuleBase_Tools::zeroMargins(aLabelLay);
261 aLabelLay->setSpacing(0);
263 QLabel* aLbl = new QLabel(aLabelWgt);
264 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
266 // Do not paint background of the label (in order to show icon)
267 aLbl->setAutoFillBackground(false);
269 aLabelLay->addWidget(aLbl);
271 SessionPtr aMgr = ModelAPI_Session::get();
272 DocumentPtr aDoc = aMgr->moduleDocument();
274 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
275 myActiveDocLbl->setReadOnly(true);
276 myActiveDocLbl->setFrame(false);
277 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
279 aLabelLay->addWidget(myActiveDocLbl);
280 aLabelLay->setStretch(1, 1);
282 myTreeView = new XGUI_DataTree(this);
283 myTreeView->setFrameShape(QFrame::NoFrame);
284 aLayout->addWidget(myTreeView);
286 QPalette aTreePalet = myTreeView->palette();
287 QColor aTreeBack = aTreePalet.color(QPalette::Base);
290 aPalet.setColor(QPalette::Base, aTreeBack);
291 aPalet.setColor(QPalette::Window, aTreeBack);
292 aLabelWgt->setPalette(aPalet);
294 myDocModel = new XGUI_DataModel(this);
295 myTreeView->setModel(myDocModel);
297 // It has to be done after setting of model
298 myActiveDocLbl->setTreeView(myTreeView);
300 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
301 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
302 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
304 connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this,
305 SLOT(onLabelContextMenuRequested(const QPoint&)));
306 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
307 SLOT(onContextMenuRequested(QContextMenuEvent*)));
310 //***************************************************
311 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
316 //***************************************************
317 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
319 QModelIndexList aIndexes;
320 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
321 bool toEnable = false;
323 if (aSelectedData.size() == 1) {
324 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
325 if (!aIndexes.contains(aSelected))
326 return; // menu is called on non selected item
328 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
329 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
331 foreach(QAction* aCmd, actions()) {
332 aCmd->setEnabled(toEnable);
334 emit contextMenuRequested(theEvent);
337 //***************************************************
338 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
340 myTreeView->selectionModel()->clearSelection();
341 //Empty feature pointer means that selected root document
342 foreach(QAction* aCmd, actions()) {
343 aCmd->setEnabled(true);
345 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
346 emit contextMenuRequested(&aEvent);
349 //***************************************************
350 void XGUI_ObjectsBrowser::onEditItem()
352 QObjectPtrList aSelectedData = selectedObjects();
353 if (aSelectedData.size() > 0) {
354 ObjectPtr aFeature = aSelectedData.first();
355 if (aFeature) { // Selection happens in TreeView
356 QObjectPtrList aList;
357 aList.append(aFeature);
358 // check whether the object can be deleted. There should not be parts which are not loaded
359 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
362 // Find index which corresponds the feature
364 foreach(QModelIndex aIdx, selectedIndexes()) {
365 ObjectPtr aFea = dataModel()->object(aIdx);
366 if (dataModel()->object(aIdx)->isSame(aFeature)) {
371 if (aIndex.isValid()) {
372 myTreeView->setCurrentIndex(aIndex);
373 myTreeView->edit(aIndex);
380 //***************************************************
381 void XGUI_ObjectsBrowser::rebuildDataTree()
383 myDocModel->rebuildDataTree();
387 //***************************************************
388 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
390 QList<QModelIndex> theIndexes;
391 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
392 aSelectModel->clear();
394 foreach(ObjectPtr aFeature, theObjects)
396 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
397 if (aIndex.isValid()) {
398 aSelectModel->select(aIndex, QItemSelectionModel::Select);
403 //***************************************************
404 void XGUI_ObjectsBrowser::clearContent()
409 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
410 const QItemSelection& theDeselected)
412 emit selectionChanged();
415 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
417 QObjectPtrList aList;
418 QModelIndexList aIndexes = selectedIndexes();
419 XGUI_DataModel* aModel = dataModel();
420 QModelIndexList::const_iterator aIt;
421 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
422 if ((*aIt).column() == 0) {
423 ObjectPtr aObject = aModel->object(*aIt);
425 aList.append(aObject);
427 theIndexes->append(*aIt);