X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetMultiSelector.cpp;h=5ee8ac64a38bd0bab91c132eb4b3a475fdebfac9;hb=a2982d2108f929cf9e7f996cfd590c4ce59dc21c;hp=4ee9a4b253029170957a8d7d70e5b536a1aaaf94;hpb=87c2d038fe6feaae3951850cbfb43313015aa1f7;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp index 4ee9a4b25..5ee8ac64a 100644 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp @@ -1,3 +1,5 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + /* * ModuleBase_WidgetMultiSelector.cpp * @@ -6,84 +8,288 @@ */ #include +#include +#include +#include #include -#include #include #include -#include #include -#include +#include #include -#include +#include #include #include +#include +#include +#include +#include +#include -#include +#include #include ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent, - const Config_WidgetAPI* theData, - const std::string& theParentId) - : ModuleBase_ModelWidget(theParent, theData, theParentId) + ModuleBase_IWorkshop* theWorkshop, + const Config_WidgetAPI* theData, + const std::string& theParentId) + : ModuleBase_ModelWidget(theParent, theData, theParentId), + myWorkshop(theWorkshop), myIsActive(false) { myMainWidget = new QWidget(theParent); - QVBoxLayout* aMainLay = new QVBoxLayout(myMainWidget); + QGridLayout* aMainLay = new QGridLayout(myMainWidget); ModuleBase_Tools::adjustMargins(aMainLay); - QString aTitle = QString::fromStdString(theData->widgetLabel()); - QLabel* aTitleLabel = new QLabel(aTitle, myMainWidget); - aMainLay->addWidget(aTitleLabel); - myListControl = new QTextEdit(myMainWidget); - myListControl->setReadOnly(true); - aMainLay->addWidget(myListControl); - myListControl->setMinimumHeight(20); + + QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget); + aMainLay->addWidget(aTypeLabel, 0, 0); + + myTypeCombo = new QComboBox(myMainWidget); + // There is no sence to paramerize list of types while we can not parametrize selection mode + QString aTypesStr("Vertices Edges Faces Solids"); + QStringList aShapeTypes = aTypesStr.split(' '); + myTypeCombo->addItems(aShapeTypes); + aMainLay->addWidget(myTypeCombo, 0, 1); + + QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget); + aMainLay->addWidget(aListLabel, 1, 0, 1, -1); + + myListControl = new QListWidget(myMainWidget); + aMainLay->addWidget(myListControl, 2, 0, 2, -1); + aMainLay->setRowStretch(2, 1); + aMainLay->addWidget(new QLabel(myMainWidget)); + aMainLay->setRowMinimumHeight(3, 20); myMainWidget->setLayout(aMainLay); + connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged())); - connect(myListControl, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged())); + myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this); + myCopyAction->setShortcut(QKeySequence::Copy); + myCopyAction->setEnabled(false); + connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem())); + myListControl->addAction(myCopyAction); + myListControl->setContextMenuPolicy(Qt::ActionsContextMenu); + connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection())); + + activateSelection(true); } ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector() { + activateSelection(false); } +//******************************************************************** bool ModuleBase_WidgetMultiSelector::storeValue() const { // A rare case when plugin was not loaded. if(!myFeature) return false; DataPtr aData = myFeature->data(); - AttributeStringPtr aStringAttr = aData->string(attributeID()); - QString aWidgetValue = myListControl->toPlainText(); - aStringAttr->setValue(aWidgetValue.toStdString()); - updateObject(myFeature); - return true; + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(aData->attribute(attributeID())); + + if (aSelectionListAttr) { + aSelectionListAttr->clear(); + // Store shapes type + TopAbs_ShapeEnum aCurrentType = + ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText()); + aSelectionListAttr->setSelectionType((int) aCurrentType); + // Store selection in the attribute + foreach (GeomSelection aSelec, mySelection) { + aSelectionListAttr->append(aSelec.first, aSelec.second); + } + //updateSelectionList(aSelectionListAttr); + updateObject(myFeature); + return true; + } + return false; } +//******************************************************************** bool ModuleBase_WidgetMultiSelector::restoreValue() { // A rare case when plugin was not loaded. if(!myFeature) return false; DataPtr aData = myFeature->data(); - AttributeStringPtr aStringAttr = aData->string(attributeID()); - - bool isBlocked = myListControl->blockSignals(true); - myListControl->setText(QString::fromStdString(aStringAttr->value())); - myListControl->blockSignals(isBlocked); + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(aData->attribute(attributeID())); - return true; + if (aSelectionListAttr) { + mySelection.clear(); + // Restore shape type + TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum) aSelectionListAttr->selectionType(); + setCurrentShapeType(aShapeType); + // Restore selection in the list + for (int i = 0; i < aSelectionListAttr->size(); i++) { + AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i); + mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value())); + } + updateSelectionList(aSelectionListAttr); + return true; + } + return false; } +//******************************************************************** QWidget* ModuleBase_WidgetMultiSelector::getControl() const { return myMainWidget; } +//******************************************************************** QList ModuleBase_WidgetMultiSelector::getControls() const { QList result; + //result << myTypeCombo; result << myListControl; return result; } + +//******************************************************************** +bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent) +{ + //TODO: Remove maybe? + return ModuleBase_ModelWidget::eventFilter(theObj, theEvent); +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate) +{ + myIsActive = toActivate; + if (myIsActive) { + connect(myWorkshop, SIGNAL(selectionChanged()), + this, SLOT(onSelectionChanged()), + Qt::UniqueConnection); + activateShapeSelection(); + } else { + disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); + myWorkshop->deactivateSubShapesSelection(); + } +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged() +{ + activateShapeSelection(); + QObjectPtrList anEmptyList; + myWorkshop->setSelected(anEmptyList); + // Clear mySelection, myListControl and storeValue() + onSelectionChanged(); +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::onSelectionChanged() +{ + ModuleBase_ISelection* aSelection = myWorkshop->selection(); + NCollection_List aSelectedShapes; //, aFilteredShapes; + std::list aOwnersList; + aSelection->selectedShapes(aSelectedShapes, aOwnersList); + + mySelection.clear(); + std::list::const_iterator aIt; + NCollection_List::Iterator aShpIt(aSelectedShapes); + GeomShapePtr aShape; + for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) { + ResultPtr aResult = std::dynamic_pointer_cast(*aIt); + if (myFeature) { + // We can not select a result of our feature + const std::list& aResList = myFeature->results(); + std::list::const_iterator aIt; + bool isSkipSelf = false; + for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) { + if ((*aIt) == aResult) { + isSkipSelf = true; + break; + } + } + if(isSkipSelf) + continue; + } + aShape = std::shared_ptr(new GeomAPI_Shape()); + aShape->setImpl(new TopoDS_Shape(aShpIt.Value())); + mySelection.append(GeomSelection(aResult, aShape)); + } + //updateSelectionList(); + emit valuesChanged(); +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List& theShapesToFilter, + NCollection_List& theResult) +{ + if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty()) + return; + TopAbs_ShapeEnum aReferenceType = + ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText()); + NCollection_List::Iterator anIter(theShapesToFilter); + for (; anIter.More(); anIter.Next()) { + TopoDS_Shape aShape = anIter.Value(); + if (aShape.IsNull() || aShape.ShapeType() != aReferenceType) + continue; + theResult.Append(aShape); + } +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType) +{ + QString aShapeTypeName; + + for (int idx = 0; idx < myTypeCombo->count(); ++idx) { + aShapeTypeName = myTypeCombo->itemText(idx); + TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName); + if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) { + activateSelection(false); + bool isBlocked = myTypeCombo->blockSignals(true); + myTypeCombo->setCurrentIndex(idx); + myTypeCombo->blockSignals(isBlocked); + activateSelection(true); + break; + } + } +} + +void ModuleBase_WidgetMultiSelector::activateShapeSelection() +{ + QString aNewType = myTypeCombo->currentText(); + QIntList aList; + aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType)); + myWorkshop->activateSubShapesSelection(aList); +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList) +{ + myListControl->clear(); + for (int i = 0; i < theList->size(); i++) { + AttributeSelectionPtr aAttr = theList->value(i); + myListControl->addItem(aAttr->namingName().c_str()); + } +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::onCopyItem() +{ + QList aItems = myListControl->selectedItems(); + QString aRes; + foreach(QListWidgetItem* aItem, aItems) { + if (!aRes.isEmpty()) + aRes += "\n"; + aRes += aItem->text(); + } + if (!aRes.isEmpty()) { + QClipboard *clipboard = QApplication::clipboard(); + clipboard->setText(aRes); + } +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::onListSelection() +{ + QList aItems = myListControl->selectedItems(); + myCopyAction->setEnabled(!aItems.isEmpty()); +} +