1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetMultiSelector.cpp
6 * Created on: Aug 28, 2014
10 #include <ModuleBase_WidgetMultiSelector.h>
11 #include <ModuleBase_WidgetShapeSelector.h>
12 #include <ModuleBase_FilterNoDegeneratedEdge.h>
13 #include <ModuleBase_ISelection.h>
14 #include <ModuleBase_IWorkshop.h>
15 #include <ModuleBase_IViewer.h>
16 #include <ModuleBase_Tools.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
21 #include <Config_WidgetAPI.h>
23 #include <QGridLayout>
25 #include <QListWidget>
31 #include <QApplication>
37 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
38 ModuleBase_IWorkshop* theWorkshop,
39 const Config_WidgetAPI* theData,
40 const std::string& theParentId)
41 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
42 myWorkshop(theWorkshop), myIsActive(false)
44 QGridLayout* aMainLay = new QGridLayout(this);
45 ModuleBase_Tools::adjustMargins(aMainLay);
47 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
48 aMainLay->addWidget(aTypeLabel, 0, 0);
50 myTypeCombo = new QComboBox(this);
51 // There is no sence to paramerize list of types while we can not parametrize selection mode
53 std::string aPropertyTypes = theData->getProperty("type_choice");
54 QString aTypesStr = aPropertyTypes.c_str();
55 QStringList aShapeTypes = aTypesStr.split(' ');
57 myTypeCombo->addItems(aShapeTypes);
58 aMainLay->addWidget(myTypeCombo, 0, 1);
59 // if the xml definition contains one type, the controls to select a type should not be shown
60 if (aShapeTypes.size() == 1) {
61 aTypeLabel->setVisible(false);
62 myTypeCombo->setVisible(false);
65 QLabel* aListLabel = new QLabel(tr("Selected objects:"), this);
66 aMainLay->addWidget(aListLabel, 1, 0);
67 // if the xml definition contains one type, an information label should be shown near to the latest
68 if (aShapeTypes.size() == 1) {
69 QString aLabelText = QString::fromStdString(theData->widgetLabel());
70 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
71 QLabel* aSelectedLabel = new QLabel(aLabelText, this);
72 if (!aLabelIcon.isEmpty())
73 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
74 aMainLay->addWidget(aSelectedLabel, 1, 1);
75 aMainLay->setColumnStretch(2, 1);
78 myListControl = new QListWidget(this);
79 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
80 aMainLay->setRowStretch(2, 1);
81 aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
82 aMainLay->setRowMinimumHeight(3, 20);
83 this->setLayout(aMainLay);
84 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
86 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
87 myCopyAction->setShortcut(QKeySequence::Copy);
88 myCopyAction->setEnabled(false);
89 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
90 myListControl->addAction(myCopyAction);
91 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
92 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
95 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
98 activateShapeSelection();
101 //********************************************************************
102 void ModuleBase_WidgetMultiSelector::activateCustom()
104 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
105 connect(myWorkshop, SIGNAL(selectionChanged()),
106 this, SLOT(onSelectionChanged()),
107 Qt::UniqueConnection);
110 activateShapeSelection();
113 //********************************************************************
114 void ModuleBase_WidgetMultiSelector::deactivate()
116 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
118 activateShapeSelection();
121 //********************************************************************
122 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
124 // A rare case when plugin was not loaded.
127 DataPtr aData = myFeature->data();
128 AttributeSelectionListPtr aSelectionListAttr =
129 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
131 if (aSelectionListAttr) {
132 aSelectionListAttr->clear();
134 TopAbs_ShapeEnum aCurrentType =
135 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
136 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
137 // Store selection in the attribute
138 foreach (GeomSelection aSelec, mySelection) {
139 aSelectionListAttr->append(aSelec.first, aSelec.second);
141 //updateSelectionList(aSelectionListAttr);
142 updateObject(myFeature);
148 //********************************************************************
149 bool ModuleBase_WidgetMultiSelector::restoreValue()
151 // A rare case when plugin was not loaded.
154 DataPtr aData = myFeature->data();
155 AttributeSelectionListPtr aSelectionListAttr =
156 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
158 if (aSelectionListAttr) {
160 // Restore shape type
162 ModuleBase_WidgetShapeSelector::shapeType(aSelectionListAttr->selectionType().c_str()));
163 // Restore selection in the list
164 for (int i = 0; i < aSelectionListAttr->size(); i++) {
165 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
166 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
168 updateSelectionList(aSelectionListAttr);
174 //********************************************************************
175 void ModuleBase_WidgetMultiSelector::backupAttributeValue(const bool isBackup)
180 //********************************************************************
181 bool ModuleBase_WidgetMultiSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
186 //********************************************************************
187 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
189 QList<QWidget*> result;
190 //result << myTypeCombo;
191 result << myListControl;
195 //********************************************************************
196 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
198 //TODO: Remove maybe?
199 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
202 //********************************************************************
203 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
205 activateShapeSelection();
206 QObjectPtrList anEmptyList;
207 myWorkshop->setSelected(anEmptyList);
208 // Clear mySelection, myListControl and storeValue()
209 onSelectionChanged();
212 //********************************************************************
213 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
215 ModuleBase_ISelection* aSelection = myWorkshop->selection();
216 NCollection_List<TopoDS_Shape> aSelectedShapes;
217 std::list<ObjectPtr> aOwnersList;
218 aSelection->selectedShapes(aSelectedShapes, aOwnersList);
221 std::list<ObjectPtr>::const_iterator aIt;
222 NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
224 for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
225 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
227 // We can not select a result of our feature
228 const std::list<ResultPtr>& aResList = myFeature->results();
229 std::list<ResultPtr>::const_iterator aIt;
230 bool isSkipSelf = false;
231 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
232 if ((*aIt) == aResult) {
240 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
241 aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
243 // if the result has the similar shap as the parameter shape, just the context is set to the
244 // selection list attribute.
245 if (aShape->isEqual(aResult->shape()))
246 mySelection.append(GeomSelection(aResult, NULL));
248 mySelection.append(GeomSelection(aResult, aShape));
250 //updateSelectionList();
251 emit valuesChanged();
254 //********************************************************************
255 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
257 QString aShapeTypeName;
259 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
260 aShapeTypeName = myTypeCombo->itemText(idx);
261 TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
262 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
264 activateShapeSelection();
265 bool isBlocked = myTypeCombo->blockSignals(true);
266 myTypeCombo->setCurrentIndex(idx);
268 myTypeCombo->blockSignals(isBlocked);
269 activateShapeSelection();
275 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
277 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
280 QString aNewType = myTypeCombo->currentText();
282 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
283 myWorkshop->activateSubShapesSelection(aList);
285 // it is necessary to filter the selected edges to be non-degenerated
286 // it is not possible to build naming name for such edges
287 if (aNewType == "Edges") {
288 myEdgesTypeFilter = new ModuleBase_FilterNoDegeneratedEdge();
289 aViewer->addSelectionFilter(myEdgesTypeFilter);
292 aViewer->removeSelectionFilter(myEdgesTypeFilter);
296 myWorkshop->deactivateSubShapesSelection();
297 aViewer->removeSelectionFilter(myEdgesTypeFilter);
300 activateFilters(myWorkshop, myIsActive);
303 //********************************************************************
304 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
306 myListControl->clear();
307 for (int i = 0; i < theList->size(); i++) {
308 AttributeSelectionPtr aAttr = theList->value(i);
309 myListControl->addItem(aAttr->namingName().c_str());
313 //********************************************************************
314 void ModuleBase_WidgetMultiSelector::onCopyItem()
316 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
318 foreach(QListWidgetItem* aItem, aItems) {
321 aRes += aItem->text();
323 if (!aRes.isEmpty()) {
324 QClipboard *clipboard = QApplication::clipboard();
325 clipboard->setText(aRes);
329 //********************************************************************
330 void ModuleBase_WidgetMultiSelector::onListSelection()
332 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
333 myCopyAction->setEnabled(!aItems.isEmpty());