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 // PORTING_TO_SALOME_8
26 //#include <QWindowsStyle>
27 #include <QCommonStyle>
32 /// Width of second column (minimum acceptable = 27)
33 #define SECOND_COL_WIDTH 30
38 * Tree item delegate for definition of data in column items editor
40 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
44 /// \param theParent a parent of the delegate
45 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
46 myTreedView(theParent) {}
48 /// Set data for item editor (name of the item)
49 /// \param editor a widget of editor
50 /// \param index the tree item index
51 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
53 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
55 XGUI_DataModel* aModel = myTreedView->dataModel();
56 ObjectPtr aObj = aModel->object(index);
57 if (aObj.get() != NULL) {
58 aEditor->setText(aObj->data()->name().c_str());
62 QStyledItemDelegate::setEditorData(editor, index);
66 XGUI_DataTree* myTreedView;
70 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
71 : QTreeView(theParent)
75 setStyle(new QCommonStyle());
77 myStyle = new XGUI_TreeViewStyle();
82 setHeaderHidden(true);
83 setEditTriggers(QAbstractItemView::NoEditTriggers);
84 setSelectionBehavior(QAbstractItemView::SelectRows);
85 setSelectionMode(QAbstractItemView::ExtendedSelection);
87 setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
89 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
90 SLOT(onDoubleClick(const QModelIndex&)));
93 XGUI_DataTree::~XGUI_DataTree()
97 XGUI_DataModel* XGUI_DataTree::dataModel() const
99 return static_cast<XGUI_DataModel*>(model());
102 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
104 emit contextMenuRequested(theEvent);
107 void XGUI_DataTree::commitData(QWidget* theEditor)
109 static int aEntrance = 0;
110 if (aEntrance == 0) {
111 // We have to check number of enter and exit of this function because it can be called
112 // recursively by Qt in order to avoid double modifying of a data
114 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
116 QString aName = aEditor->text();
117 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
118 XGUI_DataModel* aModel = dataModel();
119 ObjectPtr aObj = aModel->object(aIndexList.first());
121 if (XGUI_Tools::canRename(aObj, aName)) {
122 SessionPtr aMgr = ModelAPI_Session::get();
123 aMgr->startOperation("Rename");
124 aObj->data()->setName(qPrintable(aName));
125 aMgr->finishOperation();
132 void XGUI_DataTree::clear()
134 dataModel()->clear();
138 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
140 QTreeView::resizeEvent(theEvent);
141 QSize aSize = theEvent->size();
142 if (aSize.isValid()) {
143 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH - 6);
144 setColumnWidth(1, SECOND_COL_WIDTH);
148 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
150 if (theIndex.column() != 1)
152 SessionPtr aMgr = ModelAPI_Session::get();
153 // When operation is opened then we can not change history
154 if (aMgr->isOperation())
156 XGUI_DataModel* aModel = dataModel();
157 if (aModel->flags(theIndex) == 0)
159 ObjectPtr aObj = aModel->object(theIndex);
161 DocumentPtr aDoc = aMgr->activeDocument();
163 std::string aOpName = tr("History change").toStdString();
165 if (aObj->document() != aDoc)
167 aMgr->startOperation(aOpName);
168 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
169 aMgr->finishOperation();
171 // Ignore clicks on folders outside current document
172 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
173 // Clicked folder under root but active document is another
175 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
176 // Cliced not on active document folder
179 aMgr->startOperation(aOpName);
180 aDoc->setCurrentFeature(FeaturePtr(), true);
181 aMgr->finishOperation();
183 QModelIndex aNewIndex = aModel->lastHistoryIndex();
184 QModelIndex aParent = theIndex.parent();
185 int aSize = aModel->rowCount(aParent);
186 for (int i = 0; i < aSize; i++) {
187 update(aModel->index(i, 0, aParent));
188 update(aModel->index(i, 1, aParent));
192 #if (!defined HAVE_SALOME) && (defined WIN32)
193 void XGUI_DataTree::drawRow(QPainter* thePainter,
194 const QStyleOptionViewItem& theOptions,
195 const QModelIndex& theIndex) const
197 QStyleOptionViewItemV4 aOptions = theOptions;
198 myStyle->setIndex(theIndex);
199 QTreeView::drawRow(thePainter, aOptions, theIndex);
202 //********************************************************************
203 //********************************************************************
204 //********************************************************************
205 void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement,
206 const QStyleOption* theOption,
207 QPainter* thePainter, const QWidget* theWidget) const
209 if ((theElement == QStyle::PE_PanelItemViewRow) || (theElement == QStyle::PE_PanelItemViewItem)) {
210 const QStyleOptionViewItemV4* aOptions =
211 qstyleoption_cast<const QStyleOptionViewItemV4 *>(theOption);
212 if (myIndex.isValid() && ((myIndex.flags() & Qt::ItemIsSelectable) == 0)) {
213 QStyle::State aState = aOptions->state;
214 if ((aState & QStyle::State_MouseOver) != 0)
215 aState &= ~QStyle::State_MouseOver;
216 QStyleOptionViewItemV4* aOpt = (QStyleOptionViewItemV4*) aOptions;
217 aOpt->state = aState;
218 QCommonStyle/*QWindowsVistaStyle*/::drawPrimitive(theElement, aOpt, thePainter, theWidget);
221 QCommonStyle/*QWindowsVistaStyle*/::drawPrimitive(theElement, theOption, thePainter, theWidget);
226 //********************************************************************
227 //********************************************************************
228 //********************************************************************
229 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
230 : QLabel(theText, theParent),
231 myPreSelectionStyle(""),
233 mySelectionStyle(""),
238 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
240 myTreeView = theView;
241 setFont(myTreeView->font());
243 QPalette aPalet = myTreeView->palette();
244 QColor aHighlight = aPalet.highlight().color();
245 QColor aHighlightText = aPalet.highlightedText().color();
247 myPreSelectionStyle = "QLabel {background-color: ";
248 myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
249 aHighlight.lighter(170).name() + ");";
250 myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
252 QString aName = aPalet.color(QPalette::Base).name();
253 myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
256 #if (!defined HAVE_SALOME) && (defined WIN32)
257 mySelectionStyle = "QLabel {background-color: ";
258 mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
259 mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
260 mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
262 mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
263 mySelectionStyle += "; color : " + aHighlightText.name() + "}";
266 myTreeView->viewport()->installEventFilter(this);
270 #if (!defined HAVE_SALOME) && (defined WIN32)
271 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
273 switch (theEvent->type()) {
276 setStyleSheet(myPreSelectionStyle);
280 setStyleSheet(myNeutralStyle);
283 return QLabel::event(theEvent);
287 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
289 if (theObj == myTreeView->viewport()) {
290 if (theEvent->type() == QEvent::MouseButtonRelease)
293 return QLabel::eventFilter(theObj, theEvent);
296 static bool MYClearing = false;
297 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
301 setStyleSheet(mySelectionStyle);
302 // We can not block signals because on
303 // clear selection the View state will not be updated
304 myTreeView->clearSelection();
305 QLabel::mouseReleaseEvent(e);
309 void XGUI_ActiveDocLbl::unselect()
312 myIsSelected = false;
313 setStyleSheet(myNeutralStyle);
318 //********************************************************************
319 //********************************************************************
320 //********************************************************************
321 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
322 : QWidget(theParent), myDocModel(0)
324 QVBoxLayout* aLayout = new QVBoxLayout(this);
325 ModuleBase_Tools::zeroMargins(aLayout);
326 aLayout->setSpacing(0);
328 QWidget* aLabelWgt = new QWidget(this);
329 aLabelWgt->setAutoFillBackground(true);
331 aLayout->addWidget(aLabelWgt);
332 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
333 ModuleBase_Tools::zeroMargins(aLabelLay);
334 aLabelLay->setSpacing(0);
336 QLabel* aLbl = new QLabel(aLabelWgt);
337 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
339 // Do not paint background of the label (in order to show icon)
340 aLbl->setAutoFillBackground(false);
342 aLabelLay->addWidget(aLbl);
344 SessionPtr aMgr = ModelAPI_Session::get();
345 DocumentPtr aDoc = aMgr->moduleDocument();
347 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
348 // myActiveDocLbl->setReadOnly(true);
349 // myActiveDocLbl->setFrame(false);
350 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
352 aLabelLay->addWidget(myActiveDocLbl);
353 aLabelLay->setStretch(1, 1);
355 myTreeView = new XGUI_DataTree(this);
356 myTreeView->setFrameShape(QFrame::NoFrame);
357 aLayout->addWidget(myTreeView);
359 QPalette aTreePalet = myTreeView->palette();
360 QColor aTreeBack = aTreePalet.color(QPalette::Base);
363 aPalet.setColor(QPalette::Base, aTreeBack);
364 aPalet.setColor(QPalette::Window, aTreeBack);
365 aLabelWgt->setPalette(aPalet);
367 myDocModel = new XGUI_DataModel(this);
368 connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
369 connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
371 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
372 SLOT(onContextMenuRequested(QContextMenuEvent*)));
375 //***************************************************
376 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
380 void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
382 myDocModel->setXMLReader(theReader);
383 myTreeView->setModel(myDocModel);
385 // It has to be done after setting of model
386 myActiveDocLbl->setTreeView(myTreeView);
388 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
389 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
390 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
393 //***************************************************
394 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
396 QModelIndexList aIndexes;
397 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
398 bool toEnable = false;
400 if (aSelectedData.size() == 1) {
401 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
402 if (!aIndexes.contains(aSelected))
403 return; // menu is called on non selected item
405 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
406 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
408 foreach(QAction* aCmd, actions()) {
409 aCmd->setEnabled(toEnable);
411 emit contextMenuRequested(theEvent);
414 //***************************************************
415 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
417 myTreeView->selectionModel()->clearSelection();
418 //Empty feature pointer means that selected root document
419 foreach(QAction* aCmd, actions()) {
420 aCmd->setEnabled(true);
422 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
423 emit contextMenuRequested(&aEvent);
426 //***************************************************
427 void XGUI_ObjectsBrowser::onEditItem()
429 QObjectPtrList aSelectedData = selectedObjects();
430 if (aSelectedData.size() > 0) {
431 ObjectPtr anObject = aSelectedData.first();
432 if (anObject.get()) { // Selection happens in TreeView
433 // check whether the object can be renamed. There should not be parts which are not loaded
434 std::set<FeaturePtr> aFeatures;
435 aFeatures.insert(ModelAPI_Feature::feature(anObject));
436 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
439 // Find index which corresponds the feature
441 foreach(QModelIndex aIdx, selectedIndexes()) {
442 ObjectPtr aFea = dataModel()->object(aIdx);
443 if (dataModel()->object(aIdx)->isSame(anObject)) {
448 if (aIndex.isValid()) {
449 myTreeView->setCurrentIndex(aIndex);
450 myTreeView->edit(aIndex);
457 //***************************************************
458 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
460 QModelIndexList aIndexes;
462 for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
463 aIndex = myDocModel->index(i, 0, theParent);
464 if (myDocModel->hasChildren(aIndex)) {
465 if (myTreeView->isExpanded(aIndex)) {
466 aIndexes.append(aIndex);
467 QModelIndexList aSubIndexes = expandedItems(aIndex);
468 if (!aSubIndexes.isEmpty())
469 aIndexes.append(aSubIndexes);
476 //***************************************************
477 void XGUI_ObjectsBrowser::rebuildDataTree()
479 myDocModel->rebuildDataTree();
483 //***************************************************
484 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
486 QList<QModelIndex> theIndexes;
487 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
488 aSelectModel->clear();
490 foreach(ObjectPtr aFeature, theObjects)
492 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
493 if (aIndex.isValid()) {
494 aSelectModel->select(aIndex, QItemSelectionModel::Select);
499 //***************************************************
500 void XGUI_ObjectsBrowser::clearContent()
505 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
506 const QItemSelection& theDeselected)
508 emit selectionChanged();
511 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
513 QObjectPtrList aList;
514 QModelIndexList aIndexes = selectedIndexes();
515 XGUI_DataModel* aModel = dataModel();
516 QModelIndexList::const_iterator aIt;
517 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
518 if ((*aIt).column() == 0) {
519 ObjectPtr aObject = aModel->object(*aIt);
521 aList.append(aObject);
523 theIndexes->append(*aIt);
530 void XGUI_ObjectsBrowser::onBeforeReset()
532 myExpandedItems = expandedItems();
535 void XGUI_ObjectsBrowser::onAfterModelReset()
537 foreach(QModelIndex aIndex, myExpandedItems) {
538 myTreeView->setExpanded(aIndex, true);
542 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
544 std::list<bool> aStates;
545 XGUI_DataModel* aModel = dataModel();
546 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
547 int aNbChild = aModel->rowCount(aRootIdx);
548 for (int i = 0; i < aNbChild; i++) {
549 QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
550 aStates.push_back(myTreeView->isExpanded(aIdx));
555 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
557 if (theStates.size() == 0)
559 XGUI_DataModel* aModel = dataModel();
560 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
561 int aNbChild = aModel->rowCount(aRootIdx);
563 std::list<bool>::const_iterator aIt;
565 for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
568 QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
569 myTreeView->setExpanded(aIdx, (*aIt));