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);
145 void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex)
147 if (theIndex.column() != 1)
149 SessionPtr aMgr = ModelAPI_Session::get();
150 // When operation is opened then we can not change history
151 if (aMgr->isOperation())
153 XGUI_DataModel* aModel = dataModel();
154 if (aModel->flags(theIndex) == 0)
156 ObjectPtr aObj = aModel->object(theIndex);
158 DocumentPtr aDoc = aMgr->activeDocument();
160 std::string aOpName = tr("History change").toStdString();
162 if (aObj->document() != aDoc)
164 aMgr->startOperation(aOpName);
165 aDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObj), true);
166 aMgr->finishOperation();
168 // Ignore clicks on folders outside current document
169 if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument()))
170 // Clicked folder under root but active document is another
172 if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer()))
173 // Cliced not on active document folder
176 aMgr->startOperation(aOpName);
177 aDoc->setCurrentFeature(FeaturePtr(), true);
178 aMgr->finishOperation();
180 QModelIndex aNewIndex = aModel->lastHistoryIndex();
181 QModelIndex aParent = theIndex.parent();
182 int aSize = aModel->rowCount(aParent);
183 for (int i = 0; i < aSize; i++) {
184 update(aModel->index(i, 0, aParent));
188 #if (!defined HAVE_SALOME) && (defined WIN32)
189 void XGUI_DataTree::drawRow(QPainter* thePainter,
190 const QStyleOptionViewItem& theOptions,
191 const QModelIndex& theIndex) const
193 QStyleOptionViewItemV4 aOptions = theOptions;
194 myStyle->setIndex(theIndex);
195 QTreeView::drawRow(thePainter, aOptions, theIndex);
198 //********************************************************************
199 //********************************************************************
200 //********************************************************************
201 void XGUI_TreeViewStyle::drawPrimitive(PrimitiveElement theElement,
202 const QStyleOption* theOption,
203 QPainter* thePainter, const QWidget* theWidget) const
205 if ((theElement == QStyle::PE_PanelItemViewRow) || (theElement == QStyle::PE_PanelItemViewItem)) {
206 const QStyleOptionViewItemV4* aOptions = qstyleoption_cast<const QStyleOptionViewItemV4 *>(theOption);
207 if (myIndex.isValid() && ((myIndex.flags() & Qt::ItemIsSelectable) == 0)) {
208 QStyle::State aState = aOptions->state;
209 if ((aState & QStyle::State_MouseOver) != 0)
210 aState &= ~QStyle::State_MouseOver;
211 QStyleOptionViewItemV4* aOpt = (QStyleOptionViewItemV4*) aOptions;
212 aOpt->state = aState;
213 QWindowsVistaStyle::drawPrimitive(theElement, aOpt, thePainter, theWidget);
216 QWindowsVistaStyle::drawPrimitive(theElement, theOption, thePainter, theWidget);
221 //********************************************************************
222 //********************************************************************
223 //********************************************************************
224 XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent )
225 : QLineEdit(theText, theParent),
226 myPreSelectionStyle(""),
228 mySelectionStyle(""),
233 void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView)
235 myTreeView = theView;
236 QPalette aPalet = myTreeView->palette();
237 QColor aHighlight = aPalet.highlight().color();
238 QColor aHighlightText = aPalet.highlightedText().color();
240 myPreSelectionStyle = "QLineEdit {background-color: ";
241 myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");";
242 myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }";
244 QString aName = aPalet.color(QPalette::Base).name();
245 myNeutralStyle = "QLineEdit { border: 1px solid " + aName + " }";
248 #if (!defined HAVE_SALOME) && (defined WIN32)
249 mySelectionStyle = "QLineEdit {background-color: ";
250 mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(236, 245, 255)";
251 mySelectionStyle += ", stop:1 rgb(208, 229, 255));";
252 mySelectionStyle += "border: 1px solid rgb(132, 172, 221); border-radius: 2px }";
254 mySelectionStyle = "QLineEdit {background-color: " + aHighlight.name();
255 mySelectionStyle += "; color : " + aHighlightText.name() + "}";
258 myTreeView->viewport()->installEventFilter(this);
262 #if (!defined HAVE_SALOME) && (defined WIN32)
263 bool XGUI_ActiveDocLbl::event(QEvent* theEvent)
265 switch (theEvent->type()) {
268 setStyleSheet(myPreSelectionStyle);
272 setStyleSheet(myNeutralStyle);
275 return QLineEdit::event(theEvent);
279 bool XGUI_ActiveDocLbl::eventFilter(QObject* theObj, QEvent* theEvent)
281 if (theObj == myTreeView->viewport()) {
282 if (theEvent->type() == QEvent::MouseButtonRelease)
285 return QLineEdit::eventFilter(theObj, theEvent);
288 static bool MYClearing = false;
289 void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e)
293 setStyleSheet(mySelectionStyle);
294 // We can not block signals because on
295 // clear selection the View state will not be updated
296 myTreeView->clearSelection();
297 QLineEdit::mouseReleaseEvent(e);
301 void XGUI_ActiveDocLbl::unselect()
304 myIsSelected = false;
305 setStyleSheet(myNeutralStyle);
310 //********************************************************************
311 //********************************************************************
312 //********************************************************************
313 XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent)
314 : QWidget(theParent), myDocModel(0)
316 QVBoxLayout* aLayout = new QVBoxLayout(this);
317 ModuleBase_Tools::zeroMargins(aLayout);
318 aLayout->setSpacing(0);
320 QWidget* aLabelWgt = new QWidget(this);
321 aLabelWgt->setAutoFillBackground(true);
323 aLayout->addWidget(aLabelWgt);
324 QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt);
325 ModuleBase_Tools::zeroMargins(aLabelLay);
326 aLabelLay->setSpacing(0);
328 QLabel* aLbl = new QLabel(aLabelWgt);
329 aLbl->setPixmap(QPixmap(":pictures/assembly.png"));
331 // Do not paint background of the label (in order to show icon)
332 aLbl->setAutoFillBackground(false);
334 aLabelLay->addWidget(aLbl);
336 SessionPtr aMgr = ModelAPI_Session::get();
337 DocumentPtr aDoc = aMgr->moduleDocument();
339 myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt);
340 myActiveDocLbl->setReadOnly(true);
341 myActiveDocLbl->setFrame(false);
342 myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu);
344 aLabelLay->addWidget(myActiveDocLbl);
345 aLabelLay->setStretch(1, 1);
347 myTreeView = new XGUI_DataTree(this);
348 myTreeView->setFrameShape(QFrame::NoFrame);
349 aLayout->addWidget(myTreeView);
351 QPalette aTreePalet = myTreeView->palette();
352 QColor aTreeBack = aTreePalet.color(QPalette::Base);
355 aPalet.setColor(QPalette::Base, aTreeBack);
356 aPalet.setColor(QPalette::Window, aTreeBack);
357 aLabelWgt->setPalette(aPalet);
359 myDocModel = new XGUI_DataModel(this);
360 myTreeView->setModel(myDocModel);
362 // It has to be done after setting of model
363 myActiveDocLbl->setTreeView(myTreeView);
365 QItemSelectionModel* aSelMod = myTreeView->selectionModel();
366 connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
367 this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
369 connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
370 SLOT(onContextMenuRequested(QContextMenuEvent*)));
373 //***************************************************
374 XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
379 //***************************************************
380 void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent)
382 QModelIndexList aIndexes;
383 QObjectPtrList aSelectedData = selectedObjects(&aIndexes);
384 bool toEnable = false;
386 if (aSelectedData.size() == 1) {
387 QModelIndex aSelected = myTreeView->indexAt(theEvent->pos());
388 if (!aIndexes.contains(aSelected))
389 return; // menu is called on non selected item
391 Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first());
392 toEnable = ((aFlags & Qt::ItemIsEditable) != 0);
394 foreach(QAction* aCmd, actions()) {
395 aCmd->setEnabled(toEnable);
397 emit contextMenuRequested(theEvent);
400 //***************************************************
401 void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt)
403 myTreeView->selectionModel()->clearSelection();
404 //Empty feature pointer means that selected root document
405 foreach(QAction* aCmd, actions()) {
406 aCmd->setEnabled(true);
408 QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt));
409 emit contextMenuRequested(&aEvent);
412 //***************************************************
413 void XGUI_ObjectsBrowser::onEditItem()
415 QObjectPtrList aSelectedData = selectedObjects();
416 if (aSelectedData.size() > 0) {
417 ObjectPtr aFeature = aSelectedData.first();
418 if (aFeature) { // Selection happens in TreeView
419 QObjectPtrList aList;
420 aList.append(aFeature);
421 // check whether the object can be deleted. There should not be parts which are not loaded
422 if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList))
425 // Find index which corresponds the feature
427 foreach(QModelIndex aIdx, selectedIndexes()) {
428 ObjectPtr aFea = dataModel()->object(aIdx);
429 if (dataModel()->object(aIdx)->isSame(aFeature)) {
434 if (aIndex.isValid()) {
435 myTreeView->setCurrentIndex(aIndex);
436 myTreeView->edit(aIndex);
443 //***************************************************
444 void XGUI_ObjectsBrowser::rebuildDataTree()
446 myDocModel->rebuildDataTree();
450 //***************************************************
451 void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
453 QList<QModelIndex> theIndexes;
454 QItemSelectionModel* aSelectModel = myTreeView->selectionModel();
455 aSelectModel->clear();
457 foreach(ObjectPtr aFeature, theObjects)
459 QModelIndex aIndex = myDocModel->objectIndex(aFeature);
460 if (aIndex.isValid()) {
461 aSelectModel->select(aIndex, QItemSelectionModel::Select);
466 //***************************************************
467 void XGUI_ObjectsBrowser::clearContent()
472 void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected,
473 const QItemSelection& theDeselected)
475 emit selectionChanged();
478 QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const
480 QObjectPtrList aList;
481 QModelIndexList aIndexes = selectedIndexes();
482 XGUI_DataModel* aModel = dataModel();
483 QModelIndexList::const_iterator aIt;
484 for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) {
485 if ((*aIt).column() == 0) {
486 ObjectPtr aObject = aModel->object(*aIt);
488 aList.append(aObject);
490 theIndexes->append(*aIt);