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>
11 #include <Events_Error.h>
13 #include <ModuleBase_Tools.h>
19 #include <QMouseEvent>
21 #include <QStyledItemDelegate>
22 #include <QMessageBox>
26 #include <QWindowsStyle>
31 /// Width of second column (minimum acceptable = 27)
32 #define SECOND_COL_WIDTH 30
37 * Tree item delegate for definition of data in column items editor
39 class XGUI_TreeViewItemDelegate: public QStyledItemDelegate
43 /// \param theParent a parent of the delegate
44 XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {}
46 /// Set data for item editor (name of the item)
47 /// \param editor a widget of editor
48 /// \param index the tree item index
49 virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const
51 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(editor);
53 XGUI_DataModel* aModel = myTreedView->dataModel();
54 ObjectPtr aObj = aModel->object(index);
55 if (aObj.get() != NULL) {
56 aEditor->setText(aObj->data()->name().c_str());
60 QStyledItemDelegate::setEditorData(editor, index);
64 XGUI_DataTree* myTreedView;
68 XGUI_DataTree::XGUI_DataTree(QWidget* theParent)
69 : QTreeView(theParent)
73 setStyle(new QWindowsStyle());
75 myStyle = new XGUI_TreeViewStyle();
80 setHeaderHidden(true);
81 setEditTriggers(QAbstractItemView::NoEditTriggers);
82 setSelectionBehavior(QAbstractItemView::SelectRows);
83 setSelectionMode(QAbstractItemView::ExtendedSelection);
85 setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this));
87 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
88 SLOT(onDoubleClick(const QModelIndex&)));
91 XGUI_DataTree::~XGUI_DataTree()
95 XGUI_DataModel* XGUI_DataTree::dataModel() const
97 return static_cast<XGUI_DataModel*>(model());
100 void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
102 emit contextMenuRequested(theEvent);
105 void XGUI_DataTree::commitData(QWidget* theEditor)
107 static int aEntrance = 0;
108 if (aEntrance == 0) {
109 // We have to check number of enter and exit of this function because it can be called recursively by Qt
110 // in order to avoid double modifying of a data
112 QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
114 QString aName = aEditor->text();
115 QModelIndexList aIndexList = selectionModel()->selectedIndexes();
116 XGUI_DataModel* aModel = dataModel();
117 ObjectPtr aObj = aModel->object(aIndexList.first());
119 if (XGUI_Tools::canRename(aObj, aName)) {
120 SessionPtr aMgr = ModelAPI_Session::get();
121 aMgr->startOperation("Rename");
122 aObj->data()->setName(qPrintable(aName));
123 aMgr->finishOperation();
130 void XGUI_DataTree::clear()
132 dataModel()->clear();
136 void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
138 QSize aSize = theEvent->size();
139 if (aSize.isValid()) {
140 setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH);
141 setColumnWidth(1, SECOND_COL_WIDTH);
143 QTreeView::resizeEvent(theEvent);
146 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
148 if (theIndex.column() != 1)
150 SessionPtr aMgr = ModelAPI_Session::get();
151 // When operation is opened then we can not change history
152 if (aMgr->isOperation())
154 XGUI_DataModel* aModel = dataModel();
155 if (aModel->flags(theIndex) == 0)
157 ObjectPtr aObj = aModel->object(theIndex);
159 DocumentPtr aDoc = aMgr->activeDocument();
161 std::string aOpName = tr("History change").toStdString();
163 if (aObj->document() != aDoc)
165 aMgr->startOperation(aOpName);
166 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
167 aMgr->finishOperation();
169 // Ignore clicks on folders outside current document
170 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
171 // Clicked folder under root but active document is another
173 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
174 // Cliced not on active document folder
177 aMgr->startOperation(aOpName);
178 aDoc->setCurrentFeature(FeaturePtr(), true);
179 aMgr->finishOperation();
181 QModelIndex aNewIndex = aModel->lastHistoryIndex();
182 QModelIndex aParent = theIndex.parent();
183 int aSize = aModel->rowCount(aParent);
184 for (int i = 0; i < aSize; i++) {
185 update(aModel->index(i, 0, aParent));
186 update(aModel->index(i, 1, aParent));
190 #if (!defined HAVE_SALOME) && (defined WIN32)
191 void XGUI_DataTree::drawRow(QPainter* thePainter,
192 const QStyleOptionViewItem& theOptions,
193 const QModelIndex& theIndex) const
195 QStyleOptionViewItemV4 aOptions = theOptions;
196 myStyle->setIndex(theIndex);
197 QTreeView::drawRow(thePainter, aOptions, theIndex);
200 //********************************************************************
201 //********************************************************************
202 //********************************************************************
203 void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement,
204 const QStyleOption* theOption,
205 QPainter* thePainter, const QWidget* theWidget) const
207 if ((theElement == QStyle::PE_PanelItemViewRow) || (theElement == QStyle::PE_PanelItemViewItem)) {
208 const QStyleOptionViewItemV4* aOptions = qstyleoption_cast<const QStyleOptionViewItemV4 *>(theOption);
209 if (myIndex.isValid() && ((myIndex.flags() & Qt::ItemIsSelectable) == 0)) {
210 QStyle::State aState = aOptions->state;
211 if ((aState & QStyle::State_MouseOver) != 0)
212 aState &= ~QStyle::State_MouseOver;
213 QStyleOptionViewItemV4* aOpt = (QStyleOptionViewItemV4*) aOptions;
214 aOpt->state = aState;
215 QWindowsVistaStyle::drawPrimitive(theElement, aOpt, thePainter, theWidget);
218 QWindowsVistaStyle::drawPrimitive(theElement, theOption, thePainter, theWidget);
223 //********************************************************************
224 //********************************************************************
225 //********************************************************************
226 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
227 : QLabel(theText, theParent),
228 myPreSelectionStyle(""),
230 mySelectionStyle(""),
235 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
237 myTreeView = theView;
238 setFont(myTreeView->font());
240 QPalette aPalet = myTreeView->palette();
241 QColor aHighlight = aPalet.highlight().color();
242 QColor aHighlightText = aPalet.highlightedText().color();
244 myPreSelectionStyle = "QLabel {background-color: ";
245 myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");";
246 myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
248 QString aName = aPalet.color(QPalette::Base).name();
249 myNeutralStyle = "QLabel { border: 1px solid " + aName + " }";
252 #if (!defined HAVE_SALOME) && (defined WIN32)
253 mySelectionStyle = "QLabel {background-color: ";
254 mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
255 mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
256 mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
258 mySelectionStyle = "QLabel {background-color: " + aHighlight.name();
259 mySelectionStyle += "; color : " + aHighlightText.name() + "}";
262 myTreeView->viewport()->installEventFilter(this);
266 #if (!defined HAVE_SALOME) && (defined WIN32)
267 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
269 switch (theEvent->type()) {
272 setStyleSheet(myPreSelectionStyle);
276 setStyleSheet(myNeutralStyle);
279 return QLabel::event(theEvent);
283 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
285 if (theObj == myTreeView->viewport()) {
286 if (theEvent->type() == QEvent::MouseButtonRelease)
289 return QLabel::eventFilter(theObj, theEvent);
292 static bool MYClearing = false;
293 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
297 setStyleSheet(mySelectionStyle);
298 // We can not block signals because on
299 // clear selection the View state will not be updated
300 myTreeView->clearSelection();
301 QLabel::mouseReleaseEvent(e);
305 void XGUI_ActiveDocLbl::unselect()
308 myIsSelected = false;
309 setStyleSheet(myNeutralStyle);
314 //********************************************************************
315 //********************************************************************
316 //********************************************************************
317 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
318 : QWidget(theParent), myDocModel(0)
320 QVBoxLayout* aLayout = new QVBoxLayout(this);
321 ModuleBase_Tools::zeroMargins(aLayout);
322 aLayout->setSpacing(0);
324 QWidget* aLabelWgt = new QWidget(this);
325 aLabelWgt->setAutoFillBackground(true);
327 aLayout->addWidget(aLabelWgt);
328 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
329 ModuleBase_Tools::zeroMargins(aLabelLay);
330 aLabelLay->setSpacing(0);
332 QLabel* aLbl = new QLabel(aLabelWgt);
333 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
335 // Do not paint background of the label (in order to show icon)
336 aLbl->setAutoFillBackground(false);
338 aLabelLay->addWidget(aLbl);
340 SessionPtr aMgr = ModelAPI_Session::get();
341 DocumentPtr aDoc = aMgr->moduleDocument();
343 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
344 // myActiveDocLbl->setReadOnly(true);
345 // myActiveDocLbl->setFrame(false);
346 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
348 aLabelLay->addWidget(myActiveDocLbl);
349 aLabelLay->setStretch(1, 1);
351 myTreeView = new XGUI_DataTree(this);
352 myTreeView->setFrameShape(QFrame::NoFrame);
353 aLayout->addWidget(myTreeView);
355 QPalette aTreePalet = myTreeView->palette();
356 QColor aTreeBack = aTreePalet.color(QPalette::Base);
359 aPalet.setColor(QPalette::Base, aTreeBack);
360 aPalet.setColor(QPalette::Window, aTreeBack);
361 aLabelWgt->setPalette(aPalet);
363 myDocModel = new XGUI_DataModel(this);
364 connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
365 connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
367 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
368 SLOT(onContextMenuRequested(QContextMenuEvent*)));
371 //***************************************************
372 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
376 void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
378 myDocModel->setXMLReader(theReader);
379 myTreeView->setModel(myDocModel);
381 // It has to be done after setting of model
382 myActiveDocLbl->setTreeView(myTreeView);
384 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
385 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
386 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
389 //***************************************************
390 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
392 QModelIndexList aIndexes;
393 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
394 bool toEnable = false;
396 if (aSelectedData.size() == 1) {
397 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
398 if (!aIndexes.contains(aSelected))
399 return; // menu is called on non selected item
401 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
402 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
404 foreach(QAction* aCmd, actions()) {
405 aCmd->setEnabled(toEnable);
407 emit contextMenuRequested(theEvent);
410 //***************************************************
411 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
413 myTreeView->selectionModel()->clearSelection();
414 //Empty feature pointer means that selected root document
415 foreach(QAction* aCmd, actions()) {
416 aCmd->setEnabled(true);
418 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
419 emit contextMenuRequested(&aEvent);
422 //***************************************************
423 void XGUI_ObjectsBrowser::onEditItem()
425 QObjectPtrList aSelectedData = selectedObjects();
426 if (aSelectedData.size() > 0) {
427 ObjectPtr anObject = aSelectedData.first();
428 if (anObject.get()) { // Selection happens in TreeView
429 // check whether the object can be renamed. There should not be parts which are not loaded
430 std::set<FeaturePtr> aFeatures;
431 aFeatures.insert(ModelAPI_Feature::feature(anObject));
432 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aFeatures))
435 // Find index which corresponds the feature
437 foreach(QModelIndex aIdx, selectedIndexes()) {
438 ObjectPtr aFea = dataModel()->object(aIdx);
439 if (dataModel()->object(aIdx)->isSame(anObject)) {
444 if (aIndex.isValid()) {
445 myTreeView->setCurrentIndex(aIndex);
446 myTreeView->edit(aIndex);
453 //***************************************************
454 QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
456 QModelIndexList aIndexes;
458 for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
459 aIndex = myDocModel->index(i, 0, theParent);
460 if (myDocModel->hasChildren(aIndex)) {
461 if (myTreeView->isExpanded(aIndex)) {
462 aIndexes.append(aIndex);
463 QModelIndexList aSubIndexes = expandedItems(aIndex);
464 if (!aSubIndexes.isEmpty())
465 aIndexes.append(aSubIndexes);
472 //***************************************************
473 void XGUI_ObjectsBrowser::rebuildDataTree()
475 myDocModel->rebuildDataTree();
479 //***************************************************
480 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
482 QList<QModelIndex> theIndexes;
483 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
484 aSelectModel->clear();
486 foreach(ObjectPtr aFeature, theObjects)
488 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
489 if (aIndex.isValid()) {
490 aSelectModel->select(aIndex, QItemSelectionModel::Select);
495 //***************************************************
496 void XGUI_ObjectsBrowser::clearContent()
501 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
502 const QItemSelection& theDeselected)
504 emit selectionChanged();
507 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
509 QObjectPtrList aList;
510 QModelIndexList aIndexes = selectedIndexes();
511 XGUI_DataModel* aModel = dataModel();
512 QModelIndexList::const_iterator aIt;
513 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
514 if ((*aIt).column() == 0) {
515 ObjectPtr aObject = aModel->object(*aIt);
517 aList.append(aObject);
519 theIndexes->append(*aIt);
526 void XGUI_ObjectsBrowser::onBeforeReset()
528 myExpandedItems = expandedItems();
531 void XGUI_ObjectsBrowser::onAfterModelReset()
533 foreach(QModelIndex aIndex, myExpandedItems) {
534 myTreeView->setExpanded(aIndex, true);