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>
36 #include <QMouseEvent>
38 #include <QStyledItemDelegate>
39 #include <QMessageBox>
45 /// Width of second column (minimum acceptable = 27)
46 #define FIRST_COL_WIDTH 30
47 #define SECOND_COL_WIDTH 30
52 * Tree item delegate for definition of data in column items editor
54 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
58 /// \param theParent a parent of the delegate
59 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent),
60 myTreedView(theParent) {}
62 /// Set data for item editor (name of the item)
63 /// \param editor a widget of editor
64 /// \param index the tree item index
65 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
67 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
69 XGUI_DataModel* aModel = myTreedView->dataModel();
70 ObjectPtr aObj = aModel->object(index);
71 if (aObj.get() != NULL) {
72 aEditor->setText(aObj->data()->name().c_str());
76 QStyledItemDelegate::setEditorData(editor, index);
80 XGUI_DataTree* myTreedView;
84 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
85 : QTreeView(theParent)
87 setHeaderHidden(true);
89 setEditTriggers(QAbstractItemView::NoEditTriggers);
90 setSelectionBehavior(QAbstractItemView::SelectRows);
91 setSelectionMode(QAbstractItemView::ExtendedSelection);
93 setItemDelegateForColumn(1, new XGUI_TreeViewItemDelegate(this));
95 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
96 SLOT(onDoubleClick(const QModelIndex&)));
99 XGUI_DataTree::~XGUI_DataTree()
103 XGUI_DataModel* XGUI_DataTree::dataModel() const
105 return static_cast<XGUI_DataModel*>(model());
108 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
110 emit contextMenuRequested(theEvent);
113 void XGUI_DataTree::commitData(QWidget* theEditor)
115 static int aEntrance = 0;
116 if (aEntrance == 0) {
117 // We have to check number of enter and exit of this function because it can be called
118 // recursively by Qt in order to avoid double modifying of a data
120 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
122 QString aName = aEditor->text();
123 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
124 XGUI_DataModel* aModel = dataModel();
125 ObjectPtr aObj = aModel->object(aIndexList.first());
127 if (XGUI_Tools::canRename(aObj, aName)) {
128 SessionPtr aMgr = ModelAPI_Session::get();
129 aMgr->startOperation("Rename");
130 aObj->data()->setName(qPrintable(aName));
131 aMgr->finishOperation();
138 void XGUI_DataTree::clear()
140 dataModel()->clear();
144 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
146 QTreeView::resizeEvent(theEvent);
147 QSize aSize = theEvent->size();
148 if (aSize.isValid()) {
149 setColumnWidth(0, FIRST_COL_WIDTH);
150 setColumnWidth(1, aSize.width() - SECOND_COL_WIDTH - FIRST_COL_WIDTH - 12);
151 setColumnWidth(2, SECOND_COL_WIDTH);
156 void XGUI_DataTree::mousePressEvent(QMouseEvent* theEvent)
158 QTreeView::mousePressEvent(theEvent);
159 if (theEvent->button() != Qt::MidButton)
161 QModelIndex aInd = indexAt(theEvent->pos());
162 QString aTxt = QString("r=%1 c=%2 p=%3").arg(aInd.row()).arg(aInd.column()).arg((long)aInd.internalPointer());
164 QModelIndex aPar = aInd.parent();
165 QString aTxt1 = QString("r=%1 c=%2 p=%3").arg(aPar.row()).arg(aPar.column()).arg((long)aPar.internalPointer());
166 QToolTip::showText(theEvent->globalPos(), aTxt + '\n' + aTxt1);
170 void XGUI_DataTree::mouseReleaseEvent(QMouseEvent* theEvent)
172 QTreeView::mouseReleaseEvent(theEvent);
174 if (theEvent->button() != Qt::MidButton)
176 QToolTip::hideText();
178 if (theEvent->button() == Qt::LeftButton) {
179 QModelIndex aInd = indexAt(theEvent->pos());
180 if (aInd.column() == 0)
181 processEyeClick(aInd);
185 void XGUI_DataTree::processHistoryChange(const QModelIndex& theIndex)
187 SessionPtr aMgr = ModelAPI_Session::get();
188 // When operation is opened then we can not change history
189 if (aMgr->isOperation())
191 XGUI_DataModel* aModel = dataModel();
192 if (aModel->flags(theIndex) == 0)
194 ObjectPtr aObj = aModel->object(theIndex);
196 DocumentPtr aDoc = aMgr->activeDocument();
198 std::string aOpName = tr("History change").toStdString();
200 if (aObj->document() != aDoc)
202 aMgr->startOperation(aOpName);
203 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
204 aMgr->finishOperation();
206 // Ignore clicks on folders outside current document
207 if ((theIndex.internalId() == 0) && (aDoc != aMgr->moduleDocument()))
208 // Clicked folder under root but active document is another
210 if ((theIndex.internalId() != 0) && (aDoc.get() != theIndex.internalPointer()))
211 // Cliced not on active document folder
214 aMgr->startOperation(aOpName);
215 aDoc->setCurrentFeature(FeaturePtr(), true);
216 aMgr->finishOperation();
218 QModelIndex aNewIndex = aModel->lastHistoryIndex();
219 QModelIndex aParent = theIndex.parent();
220 int aSize = aModel->rowCount(aParent);
221 for (int i = 0; i < aSize; i++) {
222 update(aModel->index(i, 0, aParent));
223 update(aModel->index(i, 1, aParent));
224 update(aModel->index(i, 2, aParent));
228 void XGUI_DataTree::processEyeClick(const QModelIndex& theIndex)
230 XGUI_DataModel* aModel = dataModel();
231 ObjectPtr aObj = aModel->object(theIndex);
233 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
235 aResObj->setDisplayed(!aResObj->isDisplayed());
236 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
239 // Update list of selected objects because this event happens after selection event in object browser
240 XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
242 aObjBrowser->onSelectionChanged();
247 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
249 switch (theIndex.column()) {
251 processHistoryChange(theIndex);
257 //********************************************************************
258 //********************************************************************
259 //********************************************************************
260 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
261 : QLabel(theText, theParent),
262 myPreSelectionStyle(""),
264 mySelectionStyle(""),
269 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
271 myTreeView = theView;
272 setFont(myTreeView->font());
274 QPalette aPalet = myTreeView->palette();
275 QColor aHighlight = aPalet.highlight().color();
276 QColor aHighlightText = aPalet.highlightedText().color();
278 myPreSelectionStyle = "QLabel {background-color: ";
279 myPreSelectionStyle += aHighlight.lighter(170).name() + "}";
280 //myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " +
281 // aHighlight.lighter(170).name() + ");";
282 //myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
284 QString aName = aPalet.color(QPalette::Base).name();
285 myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
288 #if (!defined HAVE_SALOME) && (defined WIN32)
289 mySelectionStyle = "QLabel {background-color: ";
290 mySelectionStyle += "rgb(205, 232, 255); ";
291 //mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
292 //mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
293 //mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
294 mySelectionStyle += "border: 1px solid rgb(153, 209, 255) }";
296 mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
297 mySelectionStyle += "; color : " + aHighlightText.name() + "}";
300 myTreeView->viewport()->installEventFilter(this);
304 #if (!defined HAVE_SALOME) && (defined WIN32)
305 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
307 switch (theEvent->type()) {
310 setStyleSheet(myPreSelectionStyle);
314 setStyleSheet(myNeutralStyle);
317 return QLabel::event(theEvent);
321 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
323 if (theObj == myTreeView->viewport()) {
324 if (theEvent->type() == QEvent::MouseButtonRelease)
327 return QLabel::eventFilter(theObj, theEvent);
330 static bool MYClearing = false;
331 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
335 setStyleSheet(mySelectionStyle);
336 // We can not block signals because on
337 // clear selection the View state will not be updated
338 myTreeView->clearSelection();
339 QLabel::mouseReleaseEvent(e);
343 void XGUI_ActiveDocLbl::unselect()
346 myIsSelected = false;
347 setStyleSheet(myNeutralStyle);
352 //********************************************************************
353 //********************************************************************
354 //********************************************************************
355 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop)
356 : QWidget(theParent), myDocModel(0), myWorkshop(theWorkshop)
358 QVBoxLayout* aLayout = new QVBoxLayout(this);
359 ModuleBase_Tools::zeroMargins(aLayout);
360 aLayout->setSpacing(0);
362 QWidget* aLabelWgt = new QWidget(this);
363 aLabelWgt->setAutoFillBackground(true);
365 aLayout->addWidget(aLabelWgt);
366 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
367 ModuleBase_Tools::zeroMargins(aLabelLay);
368 aLabelLay->setSpacing(0);
370 QLabel* aLbl = new QLabel(aLabelWgt);
371 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
373 // Do not paint background of the label (in order to show icon)
374 aLbl->setAutoFillBackground(false);
376 aLabelLay->addWidget(aLbl);
378 SessionPtr aMgr = ModelAPI_Session::get();
379 DocumentPtr aDoc = aMgr->moduleDocument();
381 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
382 // myActiveDocLbl->setReadOnly(true);
383 // myActiveDocLbl->setFrame(false);
384 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
386 aLabelLay->addWidget(myActiveDocLbl);
387 aLabelLay->setStretch(1, 1);
389 myTreeView = new XGUI_DataTree(this);
390 myTreeView->setFrameShape(QFrame::NoFrame);
391 aLayout->addWidget(myTreeView);
393 QPalette aTreePalet = myTreeView->palette();
394 QColor aTreeBack = aTreePalet.color(QPalette::Base);
397 aPalet.setColor(QPalette::Base, aTreeBack);
398 aPalet.setColor(QPalette::Window, aTreeBack);
399 aLabelWgt->setPalette(aPalet);
401 myDocModel = new XGUI_DataModel(this);
402 connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
403 connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
405 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
406 SLOT(onContextMenuRequested(QContextMenuEvent*)));
409 //***************************************************
410 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
414 void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
416 myDocModel->setXMLReader(theReader);
417 myTreeView->setModel(myDocModel);
419 // It has to be done after setting of model
420 myActiveDocLbl->setTreeView(myTreeView);
422 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
423 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
424 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
427 //***************************************************
428 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
430 QModelIndexList aIndexes;
431 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
432 bool toEnable = false;
434 if (aSelectedData.size() == 1) {
435 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
436 if (!aIndexes.contains(aSelected))
437 return; // menu is called on non selected item
439 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
440 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
442 foreach(QAction* aCmd, actions()) {
443 aCmd->setEnabled(toEnable);
445 emit contextMenuRequested(theEvent);
448 //***************************************************
449 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
451 myTreeView->selectionModel()->clearSelection();
452 //Empty feature pointer means that selected root document
453 foreach(QAction* aCmd, actions()) {
454 aCmd->setEnabled(true);
456 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
457 emit contextMenuRequested(&aEvent);
460 //***************************************************
461 void XGUI_ObjectsBrowser::onEditItem()
463 QObjectPtrList aSelectedData = selectedObjects();
464 if (aSelectedData.size() > 0) {
465 ObjectPtr anObject = aSelectedData.first();
466 if (anObject.get()) { // Selection happens in TreeView
467 // check whether the object can be renamed. There should not be parts which are not loaded
468 std::set<FeaturePtr> aFeatures;
469 aFeatures.insert(ModelAPI_Feature::feature(anObject));
470 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
473 // Find index which corresponds the feature
475 foreach(QModelIndex aIdx, selectedIndexes()) {
476 ObjectPtr aFea = dataModel()->object(aIdx);
477 if (dataModel()->object(aIdx)->isSame(anObject)) {
482 if (aIndex.isValid()) {
483 myTreeView->setCurrentIndex(aIndex);
484 myTreeView->edit(aIndex);
491 //***************************************************
492 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
494 QModelIndexList aIndexes;
496 for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
497 aIndex = myDocModel->index(i, 0, theParent);
498 if (myDocModel->hasChildren(aIndex)) {
499 if (myTreeView->isExpanded(aIndex)) {
500 aIndexes.append(aIndex);
501 QModelIndexList aSubIndexes = expandedItems(aIndex);
502 if (!aSubIndexes.isEmpty())
503 aIndexes.append(aSubIndexes);
510 //***************************************************
511 void XGUI_ObjectsBrowser::rebuildDataTree()
513 myDocModel->rebuildDataTree();
517 //***************************************************
518 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
520 QList<QModelIndex> theIndexes;
521 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
522 aSelectModel->clear();
524 foreach(ObjectPtr aFeature, theObjects)
526 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
527 if (aIndex.isValid()) {
528 aSelectModel->select(aIndex, QItemSelectionModel::Select);
533 //***************************************************
534 void XGUI_ObjectsBrowser::clearContent()
539 //***************************************************
540 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
541 const QItemSelection& theDeselected)
543 onSelectionChanged();
546 //***************************************************
547 void XGUI_ObjectsBrowser::onSelectionChanged()
549 emit selectionChanged();
552 //***************************************************
553 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
555 QObjectPtrList aList;
556 QModelIndexList aIndexes = selectedIndexes();
557 XGUI_DataModel* aModel = dataModel();
558 QModelIndexList::const_iterator aIt;
559 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
560 if ((*aIt).column() == 1) {
561 ObjectPtr aObject = aModel->object(*aIt);
563 aList.append(aObject);
565 theIndexes->append(*aIt);
572 void XGUI_ObjectsBrowser::onBeforeReset()
574 myExpandedItems = expandedItems();
577 void XGUI_ObjectsBrowser::onAfterModelReset()
579 foreach(QModelIndex aIndex, myExpandedItems) {
580 myTreeView->setExpanded(aIndex, true);
584 std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
586 std::list<bool> aStates;
587 XGUI_DataModel* aModel = dataModel();
588 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
589 int aNbChild = aModel->rowCount(aRootIdx);
590 for (int i = 0; i < aNbChild; i++) {
591 QModelIndex aIdx = aModel->index(i, 1, aRootIdx);
592 aStates.push_back(myTreeView->isExpanded(aIdx));
597 void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates)
599 if (theStates.size() == 0)
601 XGUI_DataModel* aModel = dataModel();
602 QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
603 int aNbChild = aModel->rowCount(aRootIdx);
605 std::list<bool>::const_iterator aIt;
607 for (aIt = theStates.cbegin(); aIt != theStates.cend(); aIt++, i++) {
610 QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
611 myTreeView->setExpanded(aIdx, (*aIt));