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_ISelection.h>
13 #include <ModuleBase_IWorkshop.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_Definitions.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();
112 QObjectPtrList anObjects;
113 // Restore selection in the viewer by the attribute selection list
115 DataPtr aData = myFeature->data();
116 AttributeSelectionListPtr aListAttr =
117 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
119 for (int i = 0; i < aListAttr->size(); i++) {
120 AttributeSelectionPtr anAttr = aListAttr->value(i);
121 ResultPtr anObject = anAttr->context();
123 anObjects.append(anObject);
127 myWorkshop->setSelected(anObjects);
130 //********************************************************************
131 void ModuleBase_WidgetMultiSelector::deactivate()
133 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
135 activateShapeSelection();
138 //********************************************************************
139 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
141 // the value is stored on the selection changed signal processing
142 // A rare case when plugin was not loaded.
145 DataPtr aData = myFeature->data();
146 AttributeSelectionListPtr aSelectionListAttr =
147 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
149 if (aSelectionListAttr) {
151 TopAbs_ShapeEnum aCurrentType =
152 ModuleBase_Tools::shapeType(myTypeCombo->currentText());
153 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
158 //********************************************************************
159 bool ModuleBase_WidgetMultiSelector::restoreValue()
161 // A rare case when plugin was not loaded.
164 DataPtr aData = myFeature->data();
165 AttributeSelectionListPtr aSelectionListAttr =
166 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
168 if (aSelectionListAttr) {
169 // Restore shape type
170 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
171 updateSelectionList(aSelectionListAttr);
177 //********************************************************************
178 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
180 DataPtr aData = myFeature->data();
181 AttributeSelectionListPtr aSelectionListAttr =
182 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
183 if (aSelectionListAttr.get() == NULL)
186 mySelectionType = aSelectionListAttr->selectionType();
188 int aSize = aSelectionListAttr->size();
189 for (int i = 0; i < aSelectionListAttr->size(); i++) {
190 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
191 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
195 //********************************************************************
196 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool/* theValid*/)
198 DataPtr aData = myFeature->data();
199 AttributeSelectionListPtr aSelectionListAttr =
200 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
201 if (aSelectionListAttr.get() == NULL)
203 aSelectionListAttr->clear();
206 aSelectionListAttr->setSelectionType(mySelectionType);
208 // Store selection in the attribute
209 int aSize = mySelection.size();
210 foreach (GeomSelection aSelec, mySelection) {
211 aSelectionListAttr->append(aSelec.first, aSelec.second);
215 //********************************************************************
216 bool ModuleBase_WidgetMultiSelector::setSelection(const QList<ModuleBase_ViewerPrs>& theValues,
222 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
224 for (int i = thePosition; i < theValues.size(); i++) {
225 ModuleBase_ViewerPrs aValue = theValues[i];
227 bool aProcessed = false;
228 if (isValidSelection(aValue)) {
229 aProcessed = setSelectionCustom(aValue);
231 // if there is at least one set, the result is true
232 isDone = isDone || aProcessed;
233 // when an object, which do not satisfy the validating process, stop set selection
238 updateObject(myFeature);
239 emit valuesChanged();
244 //********************************************************************
245 bool ModuleBase_WidgetMultiSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
247 TopoDS_Shape aShape = thePrs.shape();
248 if ((myTypeCombo->count() > 1) && (!aShape.IsNull())) {
249 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->currentText());
250 if (aShape.ShapeType() != aType)
254 if (!thePrs.owner().IsNull()) {
255 ObjectPtr anObject = myWorkshop->selection()->getSelectableObject(thePrs.owner());
256 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
259 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
264 // We can not select a result of our feature
265 const std::list<ResultPtr>& aResList = myFeature->results();
266 std::list<ResultPtr>::const_iterator aIt;
267 bool isSkipSelf = false;
268 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
269 if ((*aIt) == aResult) {
278 // if the result has the similar shap as the parameter shape, just the context is set to the
279 // selection list attribute.
280 DataPtr aData = myFeature->data();
281 AttributeSelectionListPtr aSelectionListAttr =
282 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
284 const TopoDS_Shape& aTDSShape = thePrs.shape();
285 // if only result is selected, an empty shape is set to the model
286 if (aTDSShape.IsNull()) {
287 aSelectionListAttr->append(aResult, GeomShapePtr());
290 GeomShapePtr aShape(new GeomAPI_Shape());
291 aShape->setImpl(new TopoDS_Shape(aTDSShape));
292 // We can not select a result of our feature
293 if (aShape->isEqual(aResult->shape()))
294 aSelectionListAttr->append(aResult, GeomShapePtr());
296 aSelectionListAttr->append(aResult, aShape);
301 //********************************************************************
302 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
304 QList<QWidget*> result;
305 //result << myTypeCombo;
306 result << myListControl;
310 //********************************************************************
311 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
313 //TODO: Remove maybe?
314 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
317 //********************************************************************
318 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
320 activateShapeSelection();
321 QObjectPtrList anEmptyList;
322 // This method will call Selection changed event which will call onSelectionChanged
323 // To clear mySelection, myListControl and storeValue()
324 // So, we don't need to call it
325 myWorkshop->setSelected(anEmptyList);
328 //********************************************************************
329 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
331 QList<ModuleBase_ViewerPrs> aSelected = getSelectedEntitiesOrObjects(myWorkshop->selection());
333 DataPtr aData = myFeature->data();
334 AttributeSelectionListPtr aSelectionListAttr =
335 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
337 aSelectionListAttr->clear();
338 if (aSelected.size() > 0) {
339 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
340 if (isValidSelection(aPrs)) {
341 setSelectionCustom(aPrs);
345 emit valuesChanged();
346 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
347 // calls validators for the feature and, as a result, updates the Apply button state.
348 updateObject(myFeature);
351 //********************************************************************
352 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
354 QString aShapeTypeName;
356 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
357 aShapeTypeName = myTypeCombo->itemText(idx);
358 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
359 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
361 activateShapeSelection();
362 bool isBlocked = myTypeCombo->blockSignals(true);
363 myTypeCombo->setCurrentIndex(idx);
365 myTypeCombo->blockSignals(isBlocked);
366 activateShapeSelection();
372 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
374 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
377 QString aNewType = myTypeCombo->currentText();
379 aList.append(ModuleBase_Tools::shapeType(aNewType));
380 myWorkshop->activateSubShapesSelection(aList);
382 myWorkshop->deactivateSubShapesSelection();
385 activateFilters(myWorkshop, myIsActive);
388 //********************************************************************
389 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
391 myListControl->clear();
392 for (int i = 0; i < theList->size(); i++) {
393 AttributeSelectionPtr aAttr = theList->value(i);
394 myListControl->addItem(aAttr->namingName().c_str());
396 // We have to call repaint because sometimes the List control is not updated
397 myListControl->repaint();
400 //********************************************************************
401 void ModuleBase_WidgetMultiSelector::onCopyItem()
403 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
405 foreach(QListWidgetItem* aItem, aItems) {
408 aRes += aItem->text();
410 if (!aRes.isEmpty()) {
411 QClipboard *clipboard = QApplication::clipboard();
412 clipboard->setText(aRes);
416 //********************************************************************
417 void ModuleBase_WidgetMultiSelector::onListSelection()
419 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
420 myCopyAction->setEnabled(!aItems.isEmpty());