X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetMultiSelector.cpp;h=5ee8ac64a38bd0bab91c132eb4b3a475fdebfac9;hb=a2982d2108f929cf9e7f996cfd590c4ce59dc21c;hp=189b659c4d2f89a281b1f8e895fb608f9d2ac8b9;hpb=d4267cafa3b0f01f3ed6c4d98f0133a8dec6f0d9;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp index 189b659c4..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 * @@ -11,22 +13,23 @@ #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, @@ -34,30 +37,41 @@ ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParen const Config_WidgetAPI* theData, const std::string& theParentId) : ModuleBase_ModelWidget(theParent, theData, theParentId), - myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false) + myWorkshop(theWorkshop), myIsActive(false) { myMainWidget = new QWidget(theParent); QGridLayout* aMainLay = new QGridLayout(myMainWidget); ModuleBase_Tools::adjustMargins(aMainLay); + QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget); aMainLay->addWidget(aTypeLabel, 0, 0); + myTypeCombo = new QComboBox(myMainWidget); - std::string aTypes = theData->getProperty("type_choice"); - myShapeTypes = QString::fromStdString(aTypes).split(' '); - myTypeCombo->addItems(myShapeTypes); + // 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 QTextEdit(myMainWidget); - myListControl->setReadOnly(true); + + myListControl = new QListWidget(myMainWidget); aMainLay->addWidget(myListControl, 2, 0, 2, -1); - aMainLay->setColumnStretch(1, 1); + aMainLay->setRowStretch(2, 1); + aMainLay->addWidget(new QLabel(myMainWidget)); + aMainLay->setRowMinimumHeight(3, 20); myMainWidget->setLayout(aMainLay); - - //TODO: Move into the base class - myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); - //TODO_END connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged())); + + 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); } @@ -66,44 +80,70 @@ 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() { - return false; // 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 << myTypeCombo; result << myListControl; return result; } @@ -111,26 +151,72 @@ QList ModuleBase_WidgetMultiSelector::getControls() const //******************************************************************** bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent) { - if (theObj == myListControl) { - if (theEvent->type() == QEvent::FocusIn) - activateSelection(true); - } + //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; - aSelection->selectedShapes(aSelectedShapes); - QString aText; - if (!aSelectedShapes.IsEmpty()) { - filterShapes(aSelectedShapes, aFilteredShapes); - aText = QString("Items selected: %1").arg(aFilteredShapes.Size()); + 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)); } - myListControl->setText(aText); + //updateSelectionList(); + emit valuesChanged(); } +//******************************************************************** void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List& theShapesToFilter, NCollection_List& theResult) { @@ -147,22 +233,63 @@ void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_ListdeactivateSubShapesSelection(); + 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::onSelectionTypeChanged() +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()); +} +