X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FXGUI%2FXGUI_ObjectsBrowser.cpp;h=c43ca86fa984f2763438d973f0266923fb7f1d79;hb=65a616a3bb6cbdf09c61fed7eb91d1f5d9667988;hp=15539c3e64ddfc0158ddff749d17a6d9559cc035;hpb=50d85b6bd3f3dd43e11cb6a9ccb3446f5f6d49b6;p=modules%2Fshaper.git diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index 15539c3e6..c43ca86fa 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -1,51 +1,424 @@ +// 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" -XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) - : QTreeView(theParent) +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#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); - myDocModel = new XGUI_DocumentDataModel(this); - setModel(myDocModel); + setEditTriggers(QAbstractItemView::NoEditTriggers); + setSelectionBehavior(QAbstractItemView::SelectRows); + setSelectionMode(QAbstractItemView::ExtendedSelection); - connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), - this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&))); + setItemDelegateForColumn(0, new XGUI_TreeViewItemDelegate(this)); + +#ifndef ModuleDataModel + connect(this, SIGNAL(doubleClicked(const QModelIndex&)), + SLOT(onDoubleClick(const QModelIndex&))); +#endif } +XGUI_DataTree::~XGUI_DataTree() +{ +} -XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser() +ModuleBase_IDocumentDataModel* XGUI_DataTree::dataModel() const +{ + return static_cast(model()); +} + +void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent) { + emit contextMenuRequested(theEvent); } +void XGUI_DataTree::commitData(QWidget* theEditor) +{ + QLineEdit* aEditor = dynamic_cast(theEditor); + if (aEditor) { + 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("Rename"); + + if (!XGUI_Tools::canRename(this, aObj, aName)) { + aMgr->abortOperation(); + return; + } + aObj->data()->setName(qPrintable(aName)); + aMgr->finishOperation(); + } +} -void XGUI_ObjectsBrowser::onSelectionChanged(const QItemSelection& theSelected, - const QItemSelection& theDeselected) +void XGUI_DataTree::clear() { - mySelectedData.clear(); - QModelIndexList aIndexes = selectionModel()->selectedIndexes(); - QModelIndexList::const_iterator aIt; - for (aIt = aIndexes.constBegin(); aIt != aIndexes.constEnd(); ++aIt) { - FeaturePtr aFeature = myDocModel->feature(*aIt); - if (aFeature) - mySelectedData.append(aFeature); + 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); } - emit selectionChanged(); } -void XGUI_ObjectsBrowser::mouseDoubleClickEvent(QMouseEvent* theEvent) +void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex) { - QModelIndex aIndex = currentIndex(); - bool isChanged = myDocModel->activatedIndex(aIndex); - QTreeView::mouseDoubleClickEvent(theEvent); - if (isChanged) { - setExpanded(aIndex.parent(), true); - setExpanded(aIndex, myDocModel->hasChildren(aIndex)); - emit activePartChanged(myDocModel->activePart()); + 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)); } } -void XGUI_ObjectsBrowser::contextMenuEvent(QContextMenuEvent* theEvent) +//******************************************************************** +//******************************************************************** +//******************************************************************** +XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) + : QWidget(theParent), myDocModel(0) { + QVBoxLayout* aLayout = new QVBoxLayout(this); + ModuleBase_Tools::zeroMargins(aLayout); + aLayout->setSpacing(0); + + QFrame* aLabelWgt = new QFrame(this); + aLabelWgt->setAutoFillBackground(true); + QPalette aPalet = aLabelWgt->palette(); + aPalet.setColor(QPalette::Window, Qt::white); + aLabelWgt->setPalette(aPalet); + + aLayout->addWidget(aLabelWgt); + QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt); + ModuleBase_Tools::zeroMargins(aLabelLay); + aLabelLay->setSpacing(0); + + QLabel* aLbl = new QLabel(aLabelWgt); + aLbl->setPixmap(QPixmap(":pictures/assembly.png")); + aLbl->setMargin(2); + + aLbl->setAutoFillBackground(true); + + aLabelLay->addWidget(aLbl); + + SessionPtr aMgr = ModelAPI_Session::get(); + DocumentPtr aDoc = aMgr->moduleDocument(); + // TODO: Find a name of the root document + + myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt); + myActiveDocLbl->setReadOnly(true); + myActiveDocLbl->setFrame(false); + //myActiveDocLbl->setMargin(2); + myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu); + + myActiveDocLbl->installEventFilter(this); + + aLabelLay->addWidget(myActiveDocLbl); + aLabelLay->setStretch(1, 1); + + myTreeView = new XGUI_DataTree(this); + aLayout->addWidget(myTreeView); + + aLabelWgt->setFrameShape(myTreeView->frameShape()); + aLabelWgt->setFrameShadow(myTreeView->frameShadow()); + +#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*))); +} + +//*************************************************** +XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser() +{ +} + +//*************************************************** +bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent) +{ + if (obj == myActiveDocLbl) { + if (!myActiveDocLbl->isReadOnly()) { + // End of editing by mouse click + if (theEvent->type() == QEvent::MouseButtonRelease) { + QMouseEvent* aEvent = (QMouseEvent*) theEvent; + QPoint aPnt = mapFromGlobal(aEvent->globalPos()); + if (childAt(aPnt) != myActiveDocLbl) { + closeDocNameEditing(true); + } + } else if (theEvent->type() == QEvent::KeyRelease) { + QKeyEvent* aEvent = (QKeyEvent*) theEvent; + switch (aEvent->key()) { + case Qt::Key_Return: + case Qt::Key_Enter: // Accept current input + closeDocNameEditing(true); + break; + case Qt::Key_Escape: // Cancel the input + closeDocNameEditing(false); + break; + } + } + } else { + if (theEvent->type() == QEvent::MouseButtonDblClick) { + emit headerMouseDblClicked(QModelIndex()); + return true; + } + } + } + return QWidget::eventFilter(obj, theEvent); +} + +//*************************************************** +void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave) +{ + myActiveDocLbl->deselect(); + myActiveDocLbl->clearFocus(); + myActiveDocLbl->releaseMouse(); + myActiveDocLbl->setReadOnly(true); + if (toSave) { + // TODO: Save the name of root document + SessionPtr aMgr = ModelAPI_Session::get(); + DocumentPtr aDoc = aMgr->moduleDocument(); + } else { + myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString()); + } +} + +//*************************************************** +void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) +{ + 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); +} + +//*************************************************** +void XGUI_ObjectsBrowser::onLabelContextMenuRequested(const QPoint& thePnt) +{ + myTreeView->selectionModel()->clearSelection(); + //Empty feature pointer means that selected root document + foreach(QAction* aCmd, actions()) { + aCmd->setEnabled(true); + } + QContextMenuEvent aEvent(QContextMenuEvent::Mouse, thePnt, myActiveDocLbl->mapToGlobal(thePnt)); + emit contextMenuRequested(&aEvent); +} + +//*************************************************** +void XGUI_ObjectsBrowser::onEditItem() +{ + 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()) { + ObjectPtr aFea = dataModel()->object(aIdx); + if (dataModel()->object(aIdx)->isSame(aFeature)) { + aIndex = aIdx; + break; + } + } + if (aIndex.isValid()) { + myTreeView->setCurrentIndex(aIndex); + myTreeView->edit(aIndex); + } + return; + } + } + //Selection happens in Upper label + myActiveDocLbl->setReadOnly(false); + myActiveDocLbl->setFocus(); + myActiveDocLbl->selectAll(); + myActiveDocLbl->grabMouse(); + myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text()); +} + +//*************************************************** +void XGUI_ObjectsBrowser::rebuildDataTree() +{ + myDocModel->rebuildDataTree(); + update(); +} + +//*************************************************** +void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects) +{ + QList theIndexes; + QItemSelectionModel* aSelectModel = myTreeView->selectionModel(); + aSelectModel->clear(); + + foreach(ObjectPtr aFeature, theObjects) + { + QModelIndex aIndex = myDocModel->objectIndex(aFeature); + if (aIndex.isValid()) { + aSelectModel->select(aIndex, QItemSelectionModel::Select); + } + } +} + +//*************************************************** +void XGUI_ObjectsBrowser::clearContent() +{ + 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::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