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>
32 #include <XGUI_Workshop.h>
38 #include <QMouseEvent>
40 #include <QStyledItemDelegate>
41 #include <QMessageBox>
47 /// Width of second column (minimum acceptable = 27)
48 #define FIRST_COL_WIDTH 20
49 #define SECOND_COL_WIDTH 30
54 * Tree item delegate for definition of data in column items editor
56 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
60 /// \param theParent a parent of the delegate
61 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
62 myTreedView(theParent) {}
64 /// Set data for item editor (name of the item)
65 /// \param editor a widget of editor
66 /// \param index the tree item index
67 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
69 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
71 XGUI_DataModel* aModel = myTreedView->dataModel();
72 ObjectPtr aObj = aModel->object(index);
73 if (aObj.get() != NULL) {
74 aEditor->setText(aObj->data()->name().c_str());
78 QStyledItemDelegate::setEditorData(editor, index);
82 XGUI_DataTree* myTreedView;
86 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
87 : QTreeView(theParent)
89 setHeaderHidden(true);
91 setEditTriggers(QAbstractItemView::NoEditTriggers);
92 setSelectionBehavior(QAbstractItemView::SelectRows);
93 setSelectionMode(QAbstractItemView::ExtendedSelection);
95 setItemDelegateForColumn(1, new XGUI_TreeViewItemDelegate(this));
97 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
98 SLOT(onDoubleClick(const QModelIndex&)));
101 XGUI_DataTree::~XGUI_DataTree()
105 XGUI_DataModel* XGUI_DataTree::dataModel() const
107 return static_cast<XGUI_DataModel*>(model());
110 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
112 emit contextMenuRequested(theEvent);
115 void XGUI_DataTree::commitData(QWidget* theEditor)
117 static int aEntrance = 0;
118 if (aEntrance == 0) {
119 // We have to check number of enter and exit of this function because it can be called
120 // recursively by Qt in order to avoid double modifying of a data
122 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
124 QString aName = aEditor->text();
125 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
126 XGUI_DataModel* aModel = dataModel();
127 ObjectPtr aObj = aModel->object(aIndexList.first());
129 if (XGUI_Tools::canRename(aObj, aName)) {
130 SessionPtr aMgr = ModelAPI_Session::get();
131 aMgr->startOperation("Rename");
132 aObj->data()->setName(qPrintable(aName));
133 aMgr->finishOperation();
140 void XGUI_DataTree::clear()
142 dataModel()->clear();
146 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
148 QTreeView::resizeEvent(theEvent);
149 QSize aSize = theEvent->size();
150 if (aSize.isValid()) {
151 setColumnWidth(0, FIRST_COL_WIDTH);
152 setColumnWidth(1, aSize.width() - SECOND_COL_WIDTH - FIRST_COL_WIDTH - 10);
153 setColumnWidth(2, SECOND_COL_WIDTH);
158 void XGUI_DataTree::mousePressEvent(QMouseEvent* theEvent)
160 QTreeView::mousePressEvent(theEvent);
161 if (theEvent->button() != Qt::MidButton)
163 QModelIndex aInd = indexAt(theEvent->pos());
165 QString("r=%1 c=%2 p=%3").arg(aInd.row()).arg(aInd.column()).arg((long)aInd.internalPointer());
167 QModelIndex aPar = aInd.parent();
169 QString("r=%1 c=%2 p=%3").arg(aPar.row()).arg(aPar.column()).arg((long)aPar.internalPointer());
170 QToolTip::showText(theEvent->globalPos(), aTxt + '\n' + aTxt1);
174 void XGUI_DataTree::mouseReleaseEvent(QMouseEvent* theEvent)
176 QTreeView::mouseReleaseEvent(theEvent);
178 if (theEvent->button() != Qt::MidButton)
180 QToolTip::hideText();
182 if (theEvent->button() == Qt::LeftButton) {
183 QModelIndex aInd = indexAt(theEvent->pos());
184 if (aInd.column() == 0)
185 processEyeClick(aInd);
189 void XGUI_DataTree::processHistoryChange(const QModelIndex& theIndex)
191 SessionPtr aMgr = ModelAPI_Session::get();
192 // When operation is opened then we can not change history
193 if (aMgr->isOperation())
195 XGUI_DataModel* aModel = dataModel();
196 if (aModel->flags(theIndex) == 0)
198 ObjectPtr aObj = aModel->object(theIndex);
200 DocumentPtr aDoc = aMgr->activeDocument();
202 std::string aOpName = tr("History change").toStdString();
204 if (aObj->document() != aDoc)
206 aMgr->startOperation(aOpName);
207 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
208 aMgr->finishOperation();
210 // Ignore clicks on folders outside current document
211 if ((theIndex.internalId() == 0) && (aDoc != aMgr->moduleDocument()))
212 // Clicked folder under root but active document is another
214 if ((theIndex.internalId() != 0) && (aDoc.get() != theIndex.internalPointer()))
215 // Cliced not on active document folder
218 aMgr->startOperation(aOpName);
219 aDoc->setCurrentFeature(FeaturePtr(), true);
220 aMgr->finishOperation();
222 QModelIndex aNewIndex = aModel->lastHistoryIndex();
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);
242 if (aObjBrowser && !aResObj->isDisplayed() &&
243 !aObjBrowser->workshop()->prepareForDisplay(anObjects))
245 aResObj->setDisplayed(!aResObj->isDisplayed());
246 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
249 // Update list of selected objects because this event happens after
250 // selection event in object browser
252 aObjBrowser->onSelectionChanged();
257 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
259 switch (theIndex.column()) {
261 processHistoryChange(theIndex);
267 //********************************************************************
268 //********************************************************************
269 //********************************************************************
270 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
271 : QLabel(theText, theParent),
272 myPreSelectionStyle(""),
274 mySelectionStyle(""),
279 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
281 myTreeView = theView;
282 setFont(myTreeView->font());
284 QPalette aPalet = myTreeView->palette();
285 QColor aHighlight = aPalet.highlight().color();
286 QColor aHighlightText = aPalet.highlightedText().color();
288 myPreSelectionStyle = "QLabel {background-color: ";
289 myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
290 //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
291 // aHighlight.lighter(170).name() + ");";
292 //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
294 QString aName = aPalet.color(QPalette::Base).name();
295 myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
298 #if (!defined HAVE_SALOME) && (defined WIN32)
299 mySelectionStyle = "QLabel {background-color: ";
300 mySelectionStyle += "rgb(205, 232, 255); ";
301 //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
302 //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
303 //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
304 mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
306 mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
307 mySelectionStyle += "; color : " + aHighlightText.name() + "}";
310 myTreeView->viewport()->installEventFilter(this);
314 #if (!defined HAVE_SALOME) && (defined WIN32)
315 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
317 switch (theEvent->type()) {
320 setStyleSheet(myPreSelectionStyle);
324 setStyleSheet(myNeutralStyle);
327 return QLabel::event(theEvent);
331 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
333 if (theObj == myTreeView->viewport()) {
334 if (theEvent->type() == QEvent::MouseButtonRelease)
337 return QLabel::eventFilter(theObj, theEvent);
340 static bool MYClearing = false;
341 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
345 setStyleSheet(mySelectionStyle);
346 // We can not block signals because on
347 // clear selection the View state will not be updated
348 myTreeView->clearSelection();
349 QLabel::mouseReleaseEvent(e);
353 void XGUI_ActiveDocLbl::unselect()
356 myIsSelected = false;
357 setStyleSheet(myNeutralStyle);
362 //********************************************************************
363 //********************************************************************
364 //********************************************************************
365 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop)
366 : QWidget(theParent), myDocModel(0), myWorkshop(theWorkshop)
368 QVBoxLayout* aLayout = new QVBoxLayout(this);
369 ModuleBase_Tools::zeroMargins(aLayout);
370 aLayout->setSpacing(0);
372 QWidget* aLabelWgt = new QWidget(this);
373 aLabelWgt->setAutoFillBackground(true);
375 aLayout->addWidget(aLabelWgt);
376 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
377 ModuleBase_Tools::zeroMargins(aLabelLay);
378 aLabelLay->setSpacing(0);
380 QLabel* aLbl = new QLabel(aLabelWgt);
381 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
383 // Do not paint background of the label (in order to show icon)
384 aLbl->setAutoFillBackground(false);
386 aLabelLay->addWidget(aLbl);
388 SessionPtr aMgr = ModelAPI_Session::get();
389 DocumentPtr aDoc = aMgr->moduleDocument();
391 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
392 // myActiveDocLbl->setReadOnly(true);
393 // myActiveDocLbl->setFrame(false);
394 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
396 aLabelLay->addWidget(myActiveDocLbl);
397 aLabelLay->setStretch(1, 1);
399 myTreeView = new XGUI_DataTree(this);
400 myTreeView->setFrameShape(QFrame::NoFrame);
401 aLayout->addWidget(myTreeView);
403 QPalette aTreePalet = myTreeView->palette();
404 QColor aTreeBack = aTreePalet.color(QPalette::Base);
407 aPalet.setColor(QPalette::Base, aTreeBack);
408 aPalet.setColor(QPalette::Window, aTreeBack);
409 aLabelWgt->setPalette(aPalet);
411 myDocModel = new XGUI_DataModel(this);
412 connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
413 connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
415 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
416 SLOT(onContextMenuRequested(QContextMenuEvent*)));
419 //***************************************************
420 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
424 void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
426 myDocModel->setXMLReader(theReader);
427 myTreeView->setModel(myDocModel);
429 // It has to be done after setting of model
430 myActiveDocLbl->setTreeView(myTreeView);
432 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
433 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
434 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
437 //***************************************************
438 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
440 QModelIndexList aIndexes;
441 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
442 bool toEnable = false;
444 if (aSelectedData.size() == 1) {
445 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
446 if (!aIndexes.contains(aSelected))
447 return; // menu is called on non selected item
449 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
450 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
452 foreach(QAction* aCmd, actions()) {
453 aCmd->setEnabled(toEnable);
455 emit contextMenuRequested(theEvent);
458 //***************************************************
459 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
461 myTreeView->selectionModel()->clearSelection();
462 //Empty feature pointer means that selected root document
463 foreach(QAction* aCmd, actions()) {
464 aCmd->setEnabled(true);
466 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
467 emit contextMenuRequested(&aEvent);
470 //***************************************************
471 void XGUI_ObjectsBrowser::onEditItem()
473 QObjectPtrList aSelectedData = selectedObjects();
474 if (aSelectedData.size() > 0) {
475 ObjectPtr anObject = aSelectedData.first();
476 if (anObject.get()) { // Selection happens in TreeView
477 // check whether the object can be renamed. There should not be parts which are not loaded
478 std::set<FeaturePtr> aFeatures;
479 aFeatures.insert(ModelAPI_Feature::feature(anObject));
480 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
483 // Find index which corresponds the feature
485 foreach(QModelIndex aIdx, selectedIndexes()) {
486 ObjectPtr aFea = dataModel()->object(aIdx);
487 if (dataModel()->object(aIdx)->isSame(anObject)) {
492 if (aIndex.isValid()) {
493 myTreeView->setCurrentIndex(aIndex);
494 myTreeView->edit(aIndex);
501 //***************************************************
502 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
504 QModelIndexList aIndexes;
506 for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
507 aIndex = myDocModel->index(i, 0, theParent);
508 if (myDocModel->hasChildren(aIndex)) {
509 if (myTreeView->isExpanded(aIndex)) {
510 aIndexes.append(aIndex);
511 QModelIndexList aSubIndexes = expandedItems(aIndex);
512 if (!aSubIndexes.isEmpty())
513 aIndexes.append(aSubIndexes);
520 //***************************************************
521 void XGUI_ObjectsBrowser::rebuildDataTree()
523 myDocModel->rebuildDataTree();
527 //***************************************************
528 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
530 QList<QModelIndex> theIndexes;
531 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
532 aSelectModel->clear();
534 foreach(ObjectPtr aFeature, theObjects)
536 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
537 if (aIndex.isValid()) {
538 aSelectModel->select(aIndex, QItemSelectionModel::Select);
543 //***************************************************
544 void XGUI_ObjectsBrowser::clearContent()
549 //***************************************************
550 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
551 const QItemSelection& theDeselected)
553 onSelectionChanged();
556 //***************************************************
557 void XGUI_ObjectsBrowser::onSelectionChanged()
559 emit selectionChanged();
562 //***************************************************
563 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
565 QObjectPtrList aList;
566 QModelIndexList aIndexes = selectedIndexes();
567 XGUI_DataModel* aModel = dataModel();
568 QModelIndexList::const_iterator aIt;
569 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
570 if ((*aIt).column() == 1) {
571 ObjectPtr aObject = aModel->object(*aIt);
573 aList.append(aObject);
575 theIndexes->append(*aIt);
582 void XGUI_ObjectsBrowser::onBeforeReset()
584 myExpandedItems = expandedItems();
587 void XGUI_ObjectsBrowser::onAfterModelReset()
589 foreach(QModelIndex aIndex, myExpandedItems) {
590 myTreeView->setExpanded(aIndex, true);
594 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
596 std::list<bool> aStates;
597 XGUI_DataModel* aModel = dataModel();
598 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
599 int aNbChild = aModel->rowCount(aRootIdx);
600 for (int i = 0; i < aNbChild; i++) {
601 QModelIndex aIdx = aModel->index(i, 1, aRootIdx);
602 aStates.push_back(myTreeView->isExpanded(aIdx));
607 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
609 if (theStates.size() == 0)
611 XGUI_DataModel* aModel = dataModel();
612 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
613 int aNbChild = aModel->rowCount(aRootIdx);
615 std::list<bool>::const_iterator aIt;
617 for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
620 QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
621 myTreeView->setExpanded(aIdx, (*aIt));