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>
18 #include <QMouseEvent>
20 #include <QStyledItemDelegate>
21 #include <QMessageBox>
25 #include <QWindowsStyle>
30 /// Width of second column (minimum acceptable = 27)
31 #define SECOND_COL_WIDTH 30
36 * Tree item delegate for definition of data in column items editor
38 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
42 /// \param theParent a parent of the delegate
43 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
45 /// Set data for item editor (name of the item)
46 /// \param editor a widget of editor
47 /// \param index the tree item index
48 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
50 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
52 XGUI_DataModel* aModel = myTreedView->dataModel();
53 ObjectPtr aObj = aModel->object(index);
54 if (aObj.get() != NULL) {
55 aEditor->setText(aObj->data()->name().c_str());
59 QStyledItemDelegate::setEditorData(editor, index);
63 XGUI_DataTree* myTreedView;
67 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
68 : QTreeView(theParent)
72 setStyle(new QWindowsStyle());
74 myStyle = new XGUI_TreeViewStyle();
79 setHeaderHidden(true);
80 setEditTriggers(QAbstractItemView::NoEditTriggers);
81 setSelectionBehavior(QAbstractItemView::SelectRows);
82 setSelectionMode(QAbstractItemView::ExtendedSelection);
84 setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
86 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
87 SLOT(onDoubleClick(const QModelIndex&)));
90 XGUI_DataTree::~XGUI_DataTree()
94 XGUI_DataModel* XGUI_DataTree::dataModel() const
96 return static_cast<XGUI_DataModel*>(model());
99 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
101 emit contextMenuRequested(theEvent);
104 void XGUI_DataTree::commitData(QWidget* theEditor)
106 static int aEntrance = 0;
107 if (aEntrance == 0) {
108 // We have to check number of enter and exit of this function because it can be called recursively by Qt
109 // in order to avoid double modifying of a data
111 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
113 QString aName = aEditor->text();
114 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
115 XGUI_DataModel* aModel = dataModel();
116 ObjectPtr aObj = aModel->object(aIndexList.first());
118 if (XGUI_Tools::canRename(aObj, aName)) {
119 SessionPtr aMgr = ModelAPI_Session::get();
120 aMgr->startOperation("Rename");
121 aObj->data()->setName(qPrintable(aName));
122 aMgr->finishOperation();
129 void XGUI_DataTree::clear()
131 dataModel()->clear();
135 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
137 QTreeView::resizeEvent(theEvent);
138 QSize aSize = theEvent->size();
139 if (aSize.isValid()) {
140 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH - 6);
141 setColumnWidth(1, SECOND_COL_WIDTH);
145 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
147 if (theIndex.column() != 1)
149 SessionPtr aMgr = ModelAPI_Session::get();
150 // When operation is opened then we can not change history
151 if (aMgr->isOperation())
153 XGUI_DataModel* aModel = dataModel();
154 if (aModel->flags(theIndex) == 0)
156 ObjectPtr aObj = aModel->object(theIndex);
158 DocumentPtr aDoc = aMgr->activeDocument();
160 std::string aOpName = tr("History change").toStdString();
162 if (aObj->document() != aDoc)
164 aMgr->startOperation(aOpName);
165 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
166 aMgr->finishOperation();
168 // Ignore clicks on folders outside current document
169 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
170 // Clicked folder under root but active document is another
172 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
173 // Cliced not on active document folder
176 aMgr->startOperation(aOpName);
177 aDoc->setCurrentFeature(FeaturePtr(), true);
178 aMgr->finishOperation();
180 QModelIndex aNewIndex = aModel->lastHistoryIndex();
181 QModelIndex aParent = theIndex.parent();
182 int aSize = aModel->rowCount(aParent);
183 for (int i = 0; i < aSize; i++) {
184 update(aModel->index(i, 0, aParent));
185 update(aModel->index(i, 1, aParent));
189 #if (!defined HAVE_SALOME) && (defined WIN32)
190 void XGUI_DataTree::drawRow(QPainter* thePainter,
191 const QStyleOptionViewItem& theOptions,
192 const QModelIndex& theIndex) const
194 QStyleOptionViewItemV4 aOptions = theOptions;
195 myStyle->setIndex(theIndex);
196 QTreeView::drawRow(thePainter, aOptions, theIndex);
199 //********************************************************************
200 //********************************************************************
201 //********************************************************************
202 void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement,
203 const QStyleOption* theOption,
204 QPainter* thePainter, const QWidget* theWidget) const
206 if ((theElement == QStyle::PE_PanelItemViewRow) || (theElement == QStyle::PE_PanelItemViewItem)) {
207 const QStyleOptionViewItemV4* aOptions = qstyleoption_cast<const QStyleOptionViewItemV4 *>(theOption);
208 if (myIndex.isValid() && ((myIndex.flags() & Qt::ItemIsSelectable) == 0)) {
209 QStyle::State aState = aOptions->state;
210 if ((aState & QStyle::State_MouseOver) != 0)
211 aState &= ~QStyle::State_MouseOver;
212 QStyleOptionViewItemV4* aOpt = (QStyleOptionViewItemV4*) aOptions;
213 aOpt->state = aState;
214 QWindowsVistaStyle::drawPrimitive(theElement, aOpt, thePainter, theWidget);
217 QWindowsVistaStyle::drawPrimitive(theElement, theOption, thePainter, theWidget);
222 //********************************************************************
223 //********************************************************************
224 //********************************************************************
225 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
226 : QLabel(theText, theParent),
227 myPreSelectionStyle(""),
229 mySelectionStyle(""),
234 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
236 myTreeView = theView;
237 setFont(myTreeView->font());
239 QPalette aPalet = myTreeView->palette();
240 QColor aHighlight = aPalet.highlight().color();
241 QColor aHighlightText = aPalet.highlightedText().color();
243 myPreSelectionStyle = "QLabel {background-color: ";
244 myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");";
245 myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
247 QString aName = aPalet.color(QPalette::Base).name();
248 myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
251 #if (!defined HAVE_SALOME) && (defined WIN32)
252 mySelectionStyle = "QLabel {background-color: ";
253 mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
254 mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
255 mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
257 mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
258 mySelectionStyle += "; color : " + aHighlightText.name() + "}";
261 myTreeView->viewport()->installEventFilter(this);
265 #if (!defined HAVE_SALOME) && (defined WIN32)
266 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
268 switch (theEvent->type()) {
271 setStyleSheet(myPreSelectionStyle);
275 setStyleSheet(myNeutralStyle);
278 return QLabel::event(theEvent);
282 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
284 if (theObj == myTreeView->viewport()) {
285 if (theEvent->type() == QEvent::MouseButtonRelease)
288 return QLabel::eventFilter(theObj, theEvent);
291 static bool MYClearing = false;
292 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
296 setStyleSheet(mySelectionStyle);
297 // We can not block signals because on
298 // clear selection the View state will not be updated
299 myTreeView->clearSelection();
300 QLabel::mouseReleaseEvent(e);
304 void XGUI_ActiveDocLbl::unselect()
307 myIsSelected = false;
308 setStyleSheet(myNeutralStyle);
313 //********************************************************************
314 //********************************************************************
315 //********************************************************************
316 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
317 : QWidget(theParent), myDocModel(0)
319 QVBoxLayout* aLayout = new QVBoxLayout(this);
320 ModuleBase_Tools::zeroMargins(aLayout);
321 aLayout->setSpacing(0);
323 QWidget* aLabelWgt = new QWidget(this);
324 aLabelWgt->setAutoFillBackground(true);
326 aLayout->addWidget(aLabelWgt);
327 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
328 ModuleBase_Tools::zeroMargins(aLabelLay);
329 aLabelLay->setSpacing(0);
331 QLabel* aLbl = new QLabel(aLabelWgt);
332 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
334 // Do not paint background of the label (in order to show icon)
335 aLbl->setAutoFillBackground(false);
337 aLabelLay->addWidget(aLbl);
339 SessionPtr aMgr = ModelAPI_Session::get();
340 DocumentPtr aDoc = aMgr->moduleDocument();
342 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
343 // myActiveDocLbl->setReadOnly(true);
344 // myActiveDocLbl->setFrame(false);
345 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
347 aLabelLay->addWidget(myActiveDocLbl);
348 aLabelLay->setStretch(1, 1);
350 myTreeView = new XGUI_DataTree(this);
351 myTreeView->setFrameShape(QFrame::NoFrame);
352 aLayout->addWidget(myTreeView);
354 QPalette aTreePalet = myTreeView->palette();
355 QColor aTreeBack = aTreePalet.color(QPalette::Base);
358 aPalet.setColor(QPalette::Base, aTreeBack);
359 aPalet.setColor(QPalette::Window, aTreeBack);
360 aLabelWgt->setPalette(aPalet);
362 myDocModel = new XGUI_DataModel(this);
363 connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
364 connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
366 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
367 SLOT(onContextMenuRequested(QContextMenuEvent*)));
370 //***************************************************
371 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
375 void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
377 myDocModel->setXMLReader(theReader);
378 myTreeView->setModel(myDocModel);
380 // It has to be done after setting of model
381 myActiveDocLbl->setTreeView(myTreeView);
383 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
384 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
385 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
388 //***************************************************
389 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
391 QModelIndexList aIndexes;
392 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
393 bool toEnable = false;
395 if (aSelectedData.size() == 1) {
396 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
397 if (!aIndexes.contains(aSelected))
398 return; // menu is called on non selected item
400 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
401 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
403 foreach(QAction* aCmd, actions()) {
404 aCmd->setEnabled(toEnable);
406 emit contextMenuRequested(theEvent);
409 //***************************************************
410 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
412 myTreeView->selectionModel()->clearSelection();
413 //Empty feature pointer means that selected root document
414 foreach(QAction* aCmd, actions()) {
415 aCmd->setEnabled(true);
417 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
418 emit contextMenuRequested(&aEvent);
421 //***************************************************
422 void XGUI_ObjectsBrowser::onEditItem()
424 QObjectPtrList aSelectedData = selectedObjects();
425 if (aSelectedData.size() > 0) {
426 ObjectPtr anObject = aSelectedData.first();
427 if (anObject.get()) { // Selection happens in TreeView
428 // check whether the object can be renamed. There should not be parts which are not loaded
429 std::set<FeaturePtr> aFeatures;
430 aFeatures.insert(ModelAPI_Feature::feature(anObject));
431 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
434 // Find index which corresponds the feature
436 foreach(QModelIndex aIdx, selectedIndexes()) {
437 ObjectPtr aFea = dataModel()->object(aIdx);
438 if (dataModel()->object(aIdx)->isSame(anObject)) {
443 if (aIndex.isValid()) {
444 myTreeView->setCurrentIndex(aIndex);
445 myTreeView->edit(aIndex);
452 //***************************************************
453 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
455 QModelIndexList aIndexes;
457 for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
458 aIndex = myDocModel->index(i, 0, theParent);
459 if (myDocModel->hasChildren(aIndex)) {
460 if (myTreeView->isExpanded(aIndex)) {
461 aIndexes.append(aIndex);
462 QModelIndexList aSubIndexes = expandedItems(aIndex);
463 if (!aSubIndexes.isEmpty())
464 aIndexes.append(aSubIndexes);
471 //***************************************************
472 void XGUI_ObjectsBrowser::rebuildDataTree()
474 myDocModel->rebuildDataTree();
478 //***************************************************
479 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
481 QList<QModelIndex> theIndexes;
482 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
483 aSelectModel->clear();
485 foreach(ObjectPtr aFeature, theObjects)
487 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
488 if (aIndex.isValid()) {
489 aSelectModel->select(aIndex, QItemSelectionModel::Select);
494 //***************************************************
495 void XGUI_ObjectsBrowser::clearContent()
500 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
501 const QItemSelection& theDeselected)
503 emit selectionChanged();
506 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
508 QObjectPtrList aList;
509 QModelIndexList aIndexes = selectedIndexes();
510 XGUI_DataModel* aModel = dataModel();
511 QModelIndexList::const_iterator aIt;
512 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
513 if ((*aIt).column() == 0) {
514 ObjectPtr aObject = aModel->object(*aIt);
516 aList.append(aObject);
518 theIndexes->append(*aIt);
525 void XGUI_ObjectsBrowser::onBeforeReset()
527 myExpandedItems = expandedItems();
530 void XGUI_ObjectsBrowser::onAfterModelReset()
532 foreach(QModelIndex aIndex, myExpandedItems) {
533 myTreeView->setExpanded(aIndex, true);