1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_ObjectsBrowser.h"
22 #include "XGUI_Tools.h"
23 #include "XGUI_DataModel.h"
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Tools.h>
30 #include <ModuleBase_Tools.h>
31 #include <ModuleBase_ITreeNode.h>
33 #include <XGUI_Workshop.h>
39 #include <QMouseEvent>
41 #include <QStyledItemDelegate>
42 #include <QMessageBox>
48 /// Width of second column (minimum acceptable = 27)
49 #define FIRST_COL_WIDTH 20
50 #define SECOND_COL_WIDTH 30
55 * Tree item delegate for definition of data in column items editor
57 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
61 /// \param theParent a parent of the delegate
62 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
63 myTreedView(theParent) {}
65 /// Set data for item editor (name of the item)
66 /// \param editor a widget of editor
67 /// \param index the tree item index
68 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
70 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
72 XGUI_DataModel* aModel = myTreedView->dataModel();
73 ObjectPtr aObj = aModel->object(index);
74 if (aObj.get() != NULL) {
75 aEditor->setText(aObj->data()->name().c_str());
79 QStyledItemDelegate::setEditorData(editor, index);
83 XGUI_DataTree* myTreedView;
87 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
88 : QTreeView(theParent)
90 setHeaderHidden(true);
92 setEditTriggers(QAbstractItemView::NoEditTriggers);
93 setSelectionBehavior(QAbstractItemView::SelectRows);
94 setSelectionMode(QAbstractItemView::ExtendedSelection);
96 setItemDelegateForColumn(1, new XGUI_TreeViewItemDelegate(this));
98 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
99 SLOT(onDoubleClick(const QModelIndex&)));
102 XGUI_DataTree::~XGUI_DataTree()
106 XGUI_DataModel* XGUI_DataTree::dataModel() const
108 return static_cast<XGUI_DataModel*>(model());
111 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
113 emit contextMenuRequested(theEvent);
116 void XGUI_DataTree::commitData(QWidget* theEditor)
118 static int aEntrance = 0;
119 if (aEntrance == 0) {
120 // We have to check number of enter and exit of this function because it can be called
121 // recursively by Qt in order to avoid double modifying of a data
123 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
125 QString aName = aEditor->text();
126 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
127 XGUI_DataModel* aModel = dataModel();
128 ObjectPtr aObj = aModel->object(aIndexList.first());
130 if (XGUI_Tools::canRename(aObj, aName)) {
131 SessionPtr aMgr = ModelAPI_Session::get();
132 aMgr->startOperation("Rename");
133 aObj->data()->setName(qPrintable(aName));
134 aMgr->finishOperation();
141 void XGUI_DataTree::clear()
143 dataModel()->clear();
147 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
149 QTreeView::resizeEvent(theEvent);
150 QSize aSize = theEvent->size();
151 if (aSize.isValid()) {
152 setColumnWidth(0, FIRST_COL_WIDTH);
153 setColumnWidth(1, aSize.width() - SECOND_COL_WIDTH - FIRST_COL_WIDTH - 10);
154 setColumnWidth(2, SECOND_COL_WIDTH);
159 void XGUI_DataTree::mousePressEvent(QMouseEvent* theEvent)
161 QTreeView::mousePressEvent(theEvent);
162 if (theEvent->button() != Qt::MidButton)
164 QModelIndex aInd = indexAt(theEvent->pos());
166 QString("r=%1 c=%2 p=%3").arg(aInd.row()).arg(aInd.column()).arg((long)aInd.internalPointer());
168 QModelIndex aPar = aInd.parent();
170 QString("r=%1 c=%2 p=%3").arg(aPar.row()).arg(aPar.column()).arg((long)aPar.internalPointer());
171 QToolTip::showText(theEvent->globalPos(), aTxt + '\n' + aTxt1);
175 void XGUI_DataTree::mouseReleaseEvent(QMouseEvent* theEvent)
177 QTreeView::mouseReleaseEvent(theEvent);
179 if (theEvent->button() != Qt::MidButton)
181 QToolTip::hideText();
183 if (theEvent->button() == Qt::LeftButton) {
184 QModelIndex aInd = indexAt(theEvent->pos());
185 if (aInd.column() == 0)
186 processEyeClick(aInd);
190 void XGUI_DataTree::processHistoryChange(const QModelIndex& theIndex)
192 SessionPtr aMgr = ModelAPI_Session::get();
193 // When operation is opened then we can not change history
194 if (aMgr->isOperation())
196 XGUI_DataModel* aModel = dataModel();
197 if (aModel->flags(theIndex) == 0)
199 ObjectPtr aObj = aModel->object(theIndex);
201 DocumentPtr aDoc = aMgr->activeDocument();
203 std::string aOpName = tr("History change").toStdString();
205 if (aObj->document() != aDoc)
207 aMgr->startOperation(aOpName);
208 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
209 aMgr->finishOperation();
211 // Ignore clicks on folders outside current document
212 if ((theIndex.internalId() == 0) && (aDoc != aMgr->moduleDocument()))
213 // Clicked folder under root but active document is another
215 if ((theIndex.internalId() != 0) && (aDoc != aModel->document(theIndex)))
216 // Cliced not on active document folder
219 aMgr->startOperation(aOpName);
220 aDoc->setCurrentFeature(FeaturePtr(), true);
221 aMgr->finishOperation();
223 QModelIndex aParent = theIndex.parent();
224 int aSize = aModel->rowCount(aParent);
225 for (int i = 0; i < aSize; i++) {
226 update(aModel->index(i, 0, aParent));
227 update(aModel->index(i, 1, aParent));
228 update(aModel->index(i, 2, aParent));
232 void XGUI_DataTree::processEyeClick(const QModelIndex& theIndex)
234 XGUI_DataModel* aModel = dataModel();
235 ObjectPtr aObj = aModel->object(theIndex);
237 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
238 XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
240 std::set<ObjectPtr> anObjects;
241 anObjects.insert(aResObj);
243 bool hasHiddenState = aModel->hasHiddenState(theIndex);
244 if (aObjBrowser && hasHiddenState && !aObjBrowser->workshop()->prepareForDisplay(anObjects))
246 if (hasHiddenState) // #issue 2335(hide all faces then show solid problem)
247 aResObj->setDisplayed(true);
249 aResObj->setDisplayed(!aResObj->isDisplayed());
250 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
253 // Update list of selected objects because this event happens after
254 // selection event in object browser
256 aObjBrowser->onSelectionChanged();
261 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
263 switch (theIndex.column()) {
265 processHistoryChange(theIndex);
271 //********************************************************************
272 //********************************************************************
273 //********************************************************************
274 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
275 : QLabel(theText, theParent),
276 myPreSelectionStyle(""),
278 mySelectionStyle(""),
283 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
285 myTreeView = theView;
286 setFont(myTreeView->font());
288 QPalette aPalet = myTreeView->palette();
289 QColor aHighlight = aPalet.highlight().color();
290 QColor aHighlightText = aPalet.highlightedText().color();
292 myPreSelectionStyle = "QLabel {background-color: ";
293 myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
294 //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
295 // aHighlight.lighter(170).name() + ");";
296 //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
298 QString aName = aPalet.color(QPalette::Base).name();
299 myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
302 #if (!defined HAVE_SALOME) && (defined WIN32)
303 mySelectionStyle = "QLabel {background-color: ";
304 mySelectionStyle += "rgb(205, 232, 255); ";
305 //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
306 //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
307 //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
308 mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
310 mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
311 mySelectionStyle += "; color : " + aHighlightText.name() + "}";
314 myTreeView->viewport()->installEventFilter(this);
318 #if (!defined HAVE_SALOME) && (defined WIN32)
319 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
321 switch (theEvent->type()) {
324 setStyleSheet(myPreSelectionStyle);
328 setStyleSheet(myNeutralStyle);
331 return QLabel::event(theEvent);
335 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
337 if (theObj == myTreeView->viewport()) {
338 if (theEvent->type() == QEvent::MouseButtonRelease)
341 return QLabel::eventFilter(theObj, theEvent);
344 static bool MYClearing = false;
345 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
349 setStyleSheet(mySelectionStyle);
350 // We can not block signals because on
351 // clear selection the View state will not be updated
352 myTreeView->clearSelection();
353 QLabel::mouseReleaseEvent(e);
357 void XGUI_ActiveDocLbl::unselect()
360 myIsSelected = false;
361 setStyleSheet(myNeutralStyle);
366 //********************************************************************
367 //********************************************************************
368 //********************************************************************
369 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop)
370 : QWidget(theParent), myDocModel(0), myWorkshop(theWorkshop)
372 QVBoxLayout* aLayout = new QVBoxLayout(this);
373 ModuleBase_Tools::zeroMargins(aLayout);
374 aLayout->setSpacing(0);
376 QWidget* aLabelWgt = new QWidget(this);
377 aLabelWgt->setAutoFillBackground(true);
379 aLayout->addWidget(aLabelWgt);
380 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
381 ModuleBase_Tools::zeroMargins(aLabelLay);
382 aLabelLay->setSpacing(0);
384 QLabel* aLbl = new QLabel(aLabelWgt);
385 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
387 // Do not paint background of the label (in order to show icon)
388 aLbl->setAutoFillBackground(false);
390 aLabelLay->addWidget(aLbl);
392 SessionPtr aMgr = ModelAPI_Session::get();
393 DocumentPtr aDoc = aMgr->moduleDocument();
395 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
396 // myActiveDocLbl->setReadOnly(true);
397 // myActiveDocLbl->setFrame(false);
398 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
400 aLabelLay->addWidget(myActiveDocLbl);
401 aLabelLay->setStretch(1, 1);
403 myTreeView = new XGUI_DataTree(this);
404 myTreeView->setFrameShape(QFrame::NoFrame);
405 aLayout->addWidget(myTreeView);
407 QPalette aTreePalet = myTreeView->palette();
408 QColor aTreeBack = aTreePalet.color(QPalette::Base);
411 aPalet.setColor(QPalette::Base, aTreeBack);
412 aPalet.setColor(QPalette::Window, aTreeBack);
413 aLabelWgt->setPalette(aPalet);
415 myDocModel = new XGUI_DataModel(this);
416 connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
417 connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
419 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
420 SLOT(onContextMenuRequested(QContextMenuEvent*)));
423 //***************************************************
424 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
428 void XGUI_ObjectsBrowser::initialize(ModuleBase_ITreeNode* theRoot)
430 //myDocModel->setXMLReader(theReader);
431 myDocModel->setRoot(theRoot);
432 myTreeView->setModel(myDocModel);
434 // It has to be done after setting of model
435 myActiveDocLbl->setTreeView(myTreeView);
437 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
438 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
439 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
442 //***************************************************
443 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
445 QModelIndexList aIndexes;
446 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
447 bool toEnable = false;
449 if (aSelectedData.size() == 1) {
450 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
451 if (!aIndexes.contains(aSelected))
452 return; // menu is called on non selected item
454 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
455 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
457 foreach(QAction* aCmd, actions()) {
458 aCmd->setEnabled(toEnable);
460 emit contextMenuRequested(theEvent);
463 //***************************************************
464 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
466 myTreeView->selectionModel()->clearSelection();
467 //Empty feature pointer means that selected root document
468 foreach(QAction* aCmd, actions()) {
469 aCmd->setEnabled(true);
471 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
472 emit contextMenuRequested(&aEvent);
475 //***************************************************
476 void XGUI_ObjectsBrowser::onEditItem()
478 QObjectPtrList aSelectedData = selectedObjects();
479 if (aSelectedData.size() > 0) {
480 ObjectPtr anObject = aSelectedData.first();
481 if (anObject.get()) { // Selection happens in TreeView
482 // check whether the object can be renamed. There should not be parts which are not loaded
483 std::set<FeaturePtr> aFeatures;
484 aFeatures.insert(ModelAPI_Feature::feature(anObject));
485 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
488 // Find index which corresponds the feature
490 foreach(QModelIndex aIdx, selectedIndexes()) {
491 ObjectPtr aFea = dataModel()->object(aIdx);
492 if (dataModel()->object(aIdx)->isSame(anObject)) {
497 if (aIndex.isValid()) {
498 myTreeView->setCurrentIndex(aIndex);
499 myTreeView->edit(aIndex);
506 //***************************************************
507 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
509 QModelIndexList aIndexes;
511 for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
512 aIndex = myDocModel->index(i, 0, theParent);
513 if (myDocModel->hasChildren(aIndex)) {
514 if (myTreeView->isExpanded(aIndex)) {
515 aIndexes.append(aIndex);
516 QModelIndexList aSubIndexes = expandedItems(aIndex);
517 if (!aSubIndexes.isEmpty())
518 aIndexes.append(aSubIndexes);
525 //***************************************************
526 void XGUI_ObjectsBrowser::rebuildDataTree()
528 myDocModel->root()->update();
529 myDocModel->rebuildDataTree();
533 //***************************************************
534 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
536 QList<QModelIndex> theIndexes;
537 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
538 aSelectModel->clear();
540 foreach(ObjectPtr aFeature, theObjects)
542 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
543 if (aIndex.isValid()) {
544 aSelectModel->select(aIndex, QItemSelectionModel::Select);
549 //***************************************************
550 void XGUI_ObjectsBrowser::ensureVisible(const ObjectPtr theObject)
552 QModelIndex aIndex = myDocModel->objectIndex(theObject);
553 if (aIndex.isValid()) {
554 QModelIndex aParent = aIndex.parent();
555 while (aParent.isValid()) {
556 myTreeView->expand(aParent);
557 aParent = aParent.parent();
559 myTreeView->scrollTo(aIndex);
563 //***************************************************
564 void XGUI_ObjectsBrowser::clearContent()
569 //***************************************************
570 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
571 const QItemSelection& theDeselected)
573 onSelectionChanged();
576 //***************************************************
577 void XGUI_ObjectsBrowser::onSelectionChanged()
579 emit selectionChanged();
582 //***************************************************
583 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
585 QObjectPtrList aList;
586 QModelIndexList aIndexes = selectedIndexes();
587 XGUI_DataModel* aModel = dataModel();
588 QModelIndexList::const_iterator aIt;
589 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
590 if ((*aIt).column() == 1) {
591 ObjectPtr aObject = aModel->object(*aIt);
593 aList.append(aObject);
595 theIndexes->append(*aIt);
602 void XGUI_ObjectsBrowser::onBeforeReset()
604 myExpandedItems = expandedItems();
607 void XGUI_ObjectsBrowser::onAfterModelReset()
609 foreach(QModelIndex aIndex, myExpandedItems) {
610 if (myTreeView->dataModel()->hasIndex(aIndex))
611 myTreeView->setExpanded(aIndex, true);
615 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
617 std::list<bool> aStates;
618 XGUI_DataModel* aModel = dataModel();
619 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
620 int aNbChild = aModel->rowCount(aRootIdx);
621 for (int i = 0; i < aNbChild; i++) {
622 QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
623 aStates.push_back(myTreeView->isExpanded(aIdx));
628 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
630 if (theStates.size() == 0)
632 XGUI_DataModel* aModel = dataModel();
633 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
634 int aNbChild = aModel->rowCount(aRootIdx);
636 std::list<bool>::const_iterator aIt;
638 for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
641 QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
642 myTreeView->setExpanded(aIdx, (*aIt));
646 void XGUI_ObjectsBrowser::updateAllIndexes(int theColumn, const QModelIndex& theParent)
648 int aNb = myDocModel->rowCount(theParent);
649 for (int i = 0; i < aNb; i++) {
650 QModelIndex aIdx = myDocModel->index(i, theColumn, theParent);
651 if (aIdx.isValid() && myDocModel->hasIndex(aIdx)) {
652 myTreeView->update(aIdx);
653 if (myTreeView->isExpanded(aIdx))
654 updateAllIndexes(theColumn, aIdx);
659 QMap<ObjectPtr, bool> XGUI_ObjectsBrowser::getFoldersState(DocumentPtr theDoc) const
661 QMap<ObjectPtr, bool> aMap;
663 int aNb = theDoc->size(ModelAPI_Folder::group());
665 for (int i = 0; i < aNb; i++) {
666 aObj = theDoc->object(ModelAPI_Folder::group(), i);
667 QModelIndex aIdx = myDocModel->objectIndex(aObj, 0);
668 aMap[aObj] = myTreeView->isExpanded(aIdx);
673 void XGUI_ObjectsBrowser::setFoldersState(const QMap<ObjectPtr, bool>& theStates)
675 QMap<ObjectPtr, bool>::const_iterator aIt;
676 for (aIt = theStates.constBegin(); aIt != theStates.constEnd(); aIt++) {
677 QModelIndex aIdx = myDocModel->objectIndex(aIt.key(), 0);
678 myTreeView->setExpanded(aIdx, aIt.value());