X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FXGUI%2FXGUI_ObjectsBrowser.cpp;h=c43ca86fa984f2763438d973f0266923fb7f1d79;hb=b4b0748450845966e7ccf5324781d61c992fa86f;hp=699d6e518e2dcd9b40810f1d0bd3970f7bd15c33;hpb=0f05d006a106e11da67743ac851ab6c6170eb80c;p=modules%2Fshaper.git diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index 699d6e518..c43ca86fa 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -1,11 +1,16 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + #include "XGUI_ObjectsBrowser.h" -#include "XGUI_DocumentDataModel.h" #include "XGUI_Tools.h" +#include "XGUI_DataModel.h" #include #include #include -#include +#include + +#include +#include #include #include @@ -14,56 +19,69 @@ #include #include #include +#include +#include + +/// Width of second column (minimum acceptable = 27) +#define SECOND_COL_WIDTH 30 + + +/** +* \ingroup GUI +* Tree item delegate for definition of data in column items editor +*/ +class XGUI_TreeViewItemDelegate: public QStyledItemDelegate +{ +public: + /// Constructor + /// \param theParent a parent of the delegate + XGUI_TreeViewItemDelegate(XGUI_DataTree* theParent):QStyledItemDelegate(theParent), myTreedView(theParent) {} + + /// Set data for item editor (name of the item) + /// \param editor a widget of editor + /// \param index the tree item index + virtual void setEditorData ( QWidget* editor, const QModelIndex& index ) const + { + QLineEdit* aEditor = dynamic_cast(editor); + if (aEditor) { + ModuleBase_IDocumentDataModel* aModel = myTreedView->dataModel(); + ObjectPtr aObj = aModel->object(index); + if (aObj.get() != NULL) { + aEditor->setText(aObj->data()->name().c_str()); + return; + } + } + QStyledItemDelegate::setEditorData(editor, index); + } + +private: + XGUI_DataTree* myTreedView; +}; + XGUI_DataTree::XGUI_DataTree(QWidget* theParent) : QTreeView(theParent) { setHeaderHidden(true); - setModel(new XGUI_DocumentDataModel(this)); setEditTriggers(QAbstractItemView::NoEditTriggers); setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::ExtendedSelection); - connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), - this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&))); -} - -XGUI_DataTree::~XGUI_DataTree() -{ -} + setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this)); -XGUI_DocumentDataModel* XGUI_DataTree::dataModel() const -{ - return static_cast(model()); +#ifndef ModuleDataModel + connect(this, SIGNAL(doubleClicked(const QModelIndex&)), + SLOT(onDoubleClick(const QModelIndex&))); +#endif } -void XGUI_DataTree::onSelectionChanged(const QItemSelection& theSelected, - const QItemSelection& theDeselected) +XGUI_DataTree::~XGUI_DataTree() { - mySelectedData.clear(); - QModelIndexList aIndexes = selectionModel()->selectedIndexes(); - XGUI_DocumentDataModel* aModel = dataModel(); - QModelIndexList::const_iterator aIt; - for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) { - ObjectPtr aObject = aModel->object(*aIt); - if (aObject) - mySelectedData.append(aObject); - } - emit selectionChanged(); } -void XGUI_DataTree::mouseDoubleClickEvent(QMouseEvent* theEvent) +ModuleBase_IDocumentDataModel* XGUI_DataTree::dataModel() const { - if (theEvent->button() == Qt::LeftButton) { - QModelIndex aIndex = currentIndex(); - XGUI_DocumentDataModel* aModel = dataModel(); - ObjectPtr aObject = aModel->object(aIndex); - ResultPartPtr aPart = boost::dynamic_pointer_cast(aObject); - if (aPart) { - aPart->activate(); - } - } else - QTreeView::mouseDoubleClickEvent(theEvent); + return static_cast(model()); } void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent) @@ -75,31 +93,90 @@ void XGUI_DataTree::commitData(QWidget* theEditor) { QLineEdit* aEditor = dynamic_cast(theEditor); if (aEditor) { - QString aRes = aEditor->text(); - ObjectPtr aFeature = mySelectedData.first(); + QString aName = aEditor->text(); + QModelIndexList aIndexList = selectionModel()->selectedIndexes(); + ModuleBase_IDocumentDataModel* aModel = dataModel(); + ObjectPtr aObj = aModel->object(aIndexList.first()); SessionPtr aMgr = ModelAPI_Session::get(); - aMgr->startOperation(); - aFeature->data()->setName(qPrintable(aRes)); + aMgr->startOperation("Rename"); + + if (!XGUI_Tools::canRename(this, aObj, aName)) { + aMgr->abortOperation(); + return; + } + + aObj->data()->setName(qPrintable(aName)); aMgr->finishOperation(); } } void XGUI_DataTree::clear() { - mySelectedData.clear(); - XGUI_DocumentDataModel* aModel = dataModel(); + ModuleBase_IDocumentDataModel* aModel = dataModel(); aModel->clear(); reset(); } +void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent) +{ + QSize aSize = theEvent->size(); + if (aSize.isValid()) { + setColumnWidth(0, aSize.width() - SECOND_COL_WIDTH); + setColumnWidth(1, SECOND_COL_WIDTH); + } +} + +void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex) +{ + if (theIndex.column() != 1) + return; + SessionPtr aMgr = ModelAPI_Session::get(); + // When operation is opened then we can not change history + if (aMgr->isOperation()) + return; + ModuleBase_IDocumentDataModel* aModel = dataModel(); + if (aModel->flags(theIndex) == 0) + return; + ObjectPtr aObj = aModel->object(theIndex); + + DocumentPtr aDoc = aMgr->activeDocument(); + + std::string aOpName = tr("History change").toStdString(); + if (aObj.get()) { + if (aObj->document() != aDoc) + return; + aMgr->startOperation(aOpName); + aDoc->setCurrentFeature(std::dynamic_pointer_cast(aObj), true); + aMgr->finishOperation(); + } else { + // Ignore clicks on folders outside current document + if ((theIndex.internalId() == -1) && (aDoc != aMgr->moduleDocument())) + // Clicked folder under root but active document is another + return; + if ((theIndex.internalId() != -1) && (aDoc.get() != theIndex.internalPointer())) + // Cliced not on active document folder + return; + + aMgr->startOperation(aOpName); + aDoc->setCurrentFeature(FeaturePtr(), true); + aMgr->finishOperation(); + } + QModelIndex aNewIndex = aModel->lastHistoryIndex(); + QModelIndex aParent = theIndex.parent(); + int aSize = aModel->rowCount(aParent); + for (int i = 0; i < aSize; i++) { + update(aModel->index(i, 0, aParent)); + } +} + //******************************************************************** //******************************************************************** //******************************************************************** XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) - : QWidget(theParent) + : QWidget(theParent), myDocModel(0) { QVBoxLayout* aLayout = new QVBoxLayout(this); - aLayout->setContentsMargins(0, 0, 0, 0); + ModuleBase_Tools::zeroMargins(aLayout); aLayout->setSpacing(0); QFrame* aLabelWgt = new QFrame(this); @@ -110,7 +187,7 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) aLayout->addWidget(aLabelWgt); QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt); - aLabelLay->setContentsMargins(0, 0, 0, 0); + ModuleBase_Tools::zeroMargins(aLabelLay); aLabelLay->setSpacing(0); QLabel* aLbl = new QLabel(aLabelWgt); @@ -139,29 +216,21 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) myTreeView = new XGUI_DataTree(this); aLayout->addWidget(myTreeView); - myDocModel = myTreeView->dataModel(); - aLabelWgt->setFrameShape(myTreeView->frameShape()); aLabelWgt->setFrameShadow(myTreeView->frameShadow()); - connect(myTreeView, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, - SLOT(onActivePartChanged(ObjectPtr))); - connect(myTreeView, SIGNAL(activePartChanged(ObjectPtr)), this, - SIGNAL(activePartChanged(ObjectPtr))); +#ifndef ModuleDataModel + myDocModel = new XGUI_DataModel(this); + myTreeView->setModel(myDocModel); + QItemSelectionModel* aSelMod = myTreeView->selectionModel(); + connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), + this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&))); +#endif connect(myActiveDocLbl, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(onLabelContextMenuRequested(const QPoint&))); connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this, SLOT(onContextMenuRequested(QContextMenuEvent*))); - - onActivePartChanged(ObjectPtr()); - - // Create internal actions - QAction* aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this); - aAction->setData("RENAME_CMD"); - connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEditItem())); - addAction(aAction); } //*************************************************** @@ -169,32 +238,11 @@ XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser() { } -//*************************************************** -void XGUI_ObjectsBrowser::onActivePartChanged(ObjectPtr thePart) -{ - QPalette aPalet = myActiveDocLbl->palette(); - if (thePart) { - aPalet.setColor(QPalette::Text, Qt::black); - } else { - aPalet.setColor(QPalette::Text, QColor(0, 72, 140)); - } - myActiveDocLbl->setPalette(aPalet); -} - //*************************************************** bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent) { if (obj == myActiveDocLbl) { - if (myActiveDocLbl->isReadOnly()) { - if (theEvent->type() == QEvent::MouseButtonDblClick) { - if (myDocModel->activePartIndex().isValid()) { - myTreeView->setExpanded(myDocModel->activePartIndex(), false); - } - myDocModel->deactivatePart(); - onActivePartChanged(ObjectPtr()); - emit activePartChanged(ObjectPtr()); - } - } else { + if (!myActiveDocLbl->isReadOnly()) { // End of editing by mouse click if (theEvent->type() == QEvent::MouseButtonRelease) { QMouseEvent* aEvent = (QMouseEvent*) theEvent; @@ -214,6 +262,11 @@ bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent) break; } } + } else { + if (theEvent->type() == QEvent::MouseButtonDblClick) { + emit headerMouseDblClicked(QModelIndex()); + return true; + } } } return QWidget::eventFilter(obj, theEvent); @@ -235,42 +288,17 @@ void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave) } } -//*************************************************** -void XGUI_ObjectsBrowser::activatePart(const ResultPartPtr& thePart) -{ - if (thePart) { - QModelIndex aIndex = myDocModel->partIndex(thePart); - - if ((myDocModel->activePartIndex() != aIndex) && myDocModel->activePartIndex().isValid()) { - myTreeView->setExpanded(myDocModel->activePartIndex(), false); - } - bool isChanged = myDocModel->activatedIndex(aIndex); - if (isChanged) { - if (myDocModel->activePartIndex().isValid()) { - myTreeView->setExpanded(aIndex.parent(), true); - myTreeView->setExpanded(aIndex, true); - onActivePartChanged(myDocModel->object(aIndex)); - } else { - onActivePartChanged(ObjectPtr()); - } - } - } else { - QModelIndex aIndex = myDocModel->activePartIndex(); - if (aIndex.isValid()) { - myDocModel->activatedIndex(aIndex); - myTreeView->setExpanded(aIndex, false); - onActivePartChanged(ObjectPtr()); - } - } -} - //*************************************************** void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) { - myObjectsList = myTreeView->selectedObjects(); - bool toEnable = myObjectsList.size() == 1; - foreach(QAction* aCmd, actions()) - { + QModelIndexList aIndexes; + QObjectPtrList aSelectedData = selectedObjects(&aIndexes); + bool toEnable = false; + if (aSelectedData.size() == 1) { + Qt::ItemFlags aFlags = dataModel()->flags(aIndexes.first()); + toEnable = ((aFlags & Qt::ItemIsEditable) != 0); + } + foreach(QAction* aCmd, actions()) { aCmd->setEnabled(toEnable); } emit contextMenuRequested(theEvent); @@ -279,12 +307,9 @@ void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) //*************************************************** void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt) { - myObjectsList.clear(); + myTreeView->selectionModel()->clearSelection(); //Empty feature pointer means that selected root document - myObjectsList.append(ObjectPtr()); - - foreach(QAction* aCmd, actions()) - { + foreach(QAction* aCmd, actions()) { aCmd->setEnabled(true); } QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt)); @@ -294,13 +319,19 @@ void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt) //*************************************************** void XGUI_ObjectsBrowser::onEditItem() { - if (myObjectsList.size() > 0) { - ObjectPtr aFeature = myObjectsList.first(); + QObjectPtrList aSelectedData = selectedObjects(); + if (aSelectedData.size() > 0) { + ObjectPtr aFeature = aSelectedData.first(); if (aFeature) { // Selection happens in TreeView + QObjectPtrList aList; + aList.append(aFeature); + // check whether the object can be deleted. There should not be parts which are not loaded + if (!XGUI_Tools::canRemoveOrRename((QWidget*)parent(), aList)) + return; + // Find index which corresponds the feature QModelIndex aIndex; - foreach(QModelIndex aIdx, selectedIndexes()) - { + foreach(QModelIndex aIdx, selectedIndexes()) { ObjectPtr aFea = dataModel()->object(aIdx); if (dataModel()->object(aIdx)->isSame(aFeature)) { aIndex = aIdx; @@ -311,21 +342,15 @@ void XGUI_ObjectsBrowser::onEditItem() myTreeView->setCurrentIndex(aIndex); myTreeView->edit(aIndex); } - } else { //Selection happens in Upper label - myActiveDocLbl->setReadOnly(false); - myActiveDocLbl->setFocus(); - myActiveDocLbl->selectAll(); - myActiveDocLbl->grabMouse(); - myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text()); + return; } } -} - -//*************************************************** -void XGUI_ObjectsBrowser::onSelectionChanged() -{ - myObjectsList = myTreeView->selectedObjects(); - emit selectionChanged(); + //Selection happens in Upper label + myActiveDocLbl->setReadOnly(false); + myActiveDocLbl->setFocus(); + myActiveDocLbl->selectAll(); + myActiveDocLbl->grabMouse(); + myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text()); } //*************************************************** @@ -336,7 +361,7 @@ void XGUI_ObjectsBrowser::rebuildDataTree() } //*************************************************** -void XGUI_ObjectsBrowser::setObjectsSelected(const QList& theObjects) +void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects) { QList theIndexes; QItemSelectionModel* aSelectModel = myTreeView->selectionModel(); @@ -352,15 +377,48 @@ void XGUI_ObjectsBrowser::setObjectsSelected(const QList& theObjects) } //*************************************************** -void XGUI_ObjectsBrowser::processEvent(const boost::shared_ptr& theMessage) +void XGUI_ObjectsBrowser::clearContent() { - myDocModel->processEvent(theMessage); + myTreeView->clear(); } +#ifdef ModuleDataModel +void XGUI_ObjectsBrowser::setDataModel(ModuleBase_IDocumentDataModel* theModel) +{ + myDocModel = theModel; + //myDocModel = new XGUI_DataModel(this); + myTreeView->setModel(myDocModel); + QItemSelectionModel* aSelMod = myTreeView->selectionModel(); + connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), + this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&))); +} +#endif -//*************************************************** -void XGUI_ObjectsBrowser::clearContent() -{ - myObjectsList.clear(); - myTreeView->clear(); +void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected, + const QItemSelection& theDeselected) +{ + emit selectionChanged(); } + +QObjectPtrList XGUI_ObjectsBrowser::selectedObjects(QModelIndexList* theIndexes) const +{ + QObjectPtrList aList; + QModelIndexList aIndexes = selectedIndexes(); +#ifdef ModuleDataModel + ModuleBase_IDocumentDataModel* aModel = dataModel(); +#else + XGUI_DataModel* aModel = dataModel(); +#endif + QModelIndexList::const_iterator aIt; + for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) { + if ((*aIt).column() == 0) { + ObjectPtr aObject = aModel->object(*aIt); + if (aObject) { + aList.append(aObject); + if (theIndexes) + theIndexes->append(*aIt); + } + } + } + return aList; +} \ No newline at end of file