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>
20 #include <ModelAPI_AttributeSelectionList.h>
21 #include <ModelAPI_AttributeRefList.h>
23 #include <Config_WidgetAPI.h>
25 #include <QGridLayout>
27 #include <QListWidget>
33 #include <QApplication>
40 * Customization of a List Widget to make it to be placed on full width of container
42 class CustomListWidget : public QListWidget
46 /// \param theParent a parent widget
47 CustomListWidget( QWidget* theParent )
48 : QListWidget( theParent )
52 /// Redefinition of virtual method
53 virtual QSize sizeHint() const
55 int aHeight = 2*QFontMetrics( font() ).height();
56 QSize aSize = QListWidget::sizeHint();
57 return QSize( aSize.width(), aHeight );
60 /// Redefinition of virtual method
61 virtual QSize minimumSizeHint() const
63 int aHeight = 2*QFontMetrics( font() ).height();
64 QSize aSize = QListWidget::minimumSizeHint();
65 return QSize( aSize.width(), aHeight );
69 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
70 ModuleBase_IWorkshop* theWorkshop,
71 const Config_WidgetAPI* theData,
72 const std::string& theParentId)
73 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId),
76 QGridLayout* aMainLay = new QGridLayout(this);
77 ModuleBase_Tools::adjustMargins(aMainLay);
79 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
80 aMainLay->addWidget(aTypeLabel, 0, 0);
82 myTypeCombo = new QComboBox(this);
83 // There is no sense to parameterize list of types while we can not parameterize selection mode
85 std::string aPropertyTypes = theData->getProperty("type_choice");
86 QString aTypesStr = aPropertyTypes.c_str();
87 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
89 myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
91 if (!aShapeTypes.empty())
92 myTypeCombo->addItems(aShapeTypes);
93 aMainLay->addWidget(myTypeCombo, 0, 1);
94 // if the xml definition contains one type, the controls to select a type should not be shown
95 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
96 aTypeLabel->setVisible(false);
97 myTypeCombo->setVisible(false);
100 std::string aLabelText = theData->getProperty("label");
101 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
102 : tr("Selected objects:"), this);
103 aMainLay->addWidget(aListLabel, 1, 0);
104 // if the xml definition contains one type, an information label should be shown near to the latest
105 if (aShapeTypes.size() <= 1) {
106 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
107 if (!aLabelIcon.isEmpty()) {
108 QLabel* aSelectedLabel = new QLabel("", this);
109 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
110 aMainLay->addWidget(aSelectedLabel, 1, 1);
112 aMainLay->setColumnStretch(2, 1);
115 myListControl = new CustomListWidget(this);
116 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
117 aMainLay->setRowStretch(2, 1);
118 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
119 //aMainLay->setRowMinimumHeight(3, 20);
120 //this->setLayout(aMainLay);
121 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
123 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
124 myCopyAction->setShortcut(QKeySequence::Copy);
125 myCopyAction->setEnabled(false);
126 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
127 myListControl->addAction(myCopyAction);
128 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
129 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
132 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
136 //********************************************************************
137 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
139 // the value is stored on the selection changed signal processing
140 // A rare case when plugin was not loaded.
144 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
145 if (aSelectionListAttr.get()) {
146 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
151 //********************************************************************
152 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
154 // A rare case when plugin was not loaded.
158 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
159 if (aSelectionListAttr.get()) {
160 // Restore shape type
161 if (!aSelectionListAttr->selectionType().empty())
162 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
164 updateSelectionList();
168 //********************************************************************
169 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
171 ModuleBase_WidgetValidated::storeAttributeValue();
173 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
174 if (aSelectionListAttr.get()) {
175 mySelectionType = aSelectionListAttr->selectionType();
176 mySelectionCount = aSelectionListAttr->size();
179 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
180 mySelectionCount = aRefListAttr->size();
184 //********************************************************************
185 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
187 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
190 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
191 if (aSelectionListAttr.get()) {
192 aSelectionListAttr->setSelectionType(mySelectionType);
194 // restore selection in the attribute. Indeed there is only one stored object
195 int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
196 for (int i = 0; i < aCountAppened; i++)
197 aSelectionListAttr->removeLast();
200 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
201 // restore objects in the attribute. Indeed there is only one stored object
202 int aCountAppened = aRefListAttr->size() - mySelectionCount;
203 for (int i = 0; i < aCountAppened; i++)
204 aRefListAttr->removeLast();
208 //********************************************************************
209 void ModuleBase_WidgetMultiSelector::clearAttribute()
211 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
212 if (aSelectionListAttr.get())
213 aSelectionListAttr->clear();
215 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
216 aRefListAttr->clear();
220 //********************************************************************
221 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
222 GeomShapePtr theShape)
224 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
225 if (aSelectionListAttr.get()) {
226 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
227 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
230 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
231 aRefListAttr->append(theSelectedObject);
235 //********************************************************************
236 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
237 const bool theToValidate)
239 QList<ModuleBase_ViewerPrs> aSkippedValues;
241 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
243 for (; anIt != aLast; anIt++) {
244 ModuleBase_ViewerPrs aValue = *anIt;
245 bool aProcessed = false;
246 if (!theToValidate || isValidInFilters(aValue)) {
247 aProcessed = setSelectionCustom(aValue);
250 aSkippedValues.append(aValue);
251 // if there is at least one set, the result is true
252 isDone = isDone || aProcessed;
254 // updateObject - to update/redisplay feature
255 // it is commented in order to perfom it outside the method
257 //updateObject(myFeature);
258 // this emit is necessary to call store/restore method an restore type of selection
259 //emit valuesChanged();
262 if (!aSkippedValues.empty())
263 theValues.append(aSkippedValues);
268 //********************************************************************
269 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
271 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
273 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
274 aValid = aResult.get() != NULL;
277 // We can not select a result of our feature
278 const std::list<ResultPtr>& aResList = myFeature->results();
279 std::list<ResultPtr>::const_iterator aIt;
280 bool isSkipSelf = false;
281 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
282 if ((*aIt) == aResult) {
295 //********************************************************************
296 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
298 QList<QWidget*> result;
299 //result << myTypeCombo;
300 result << myListControl;
304 //********************************************************************
305 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
307 activateSelection(true);
308 activateFilters(true);
309 QList<ModuleBase_ViewerPrs> anEmptyList;
310 // This method will call Selection changed event which will call onSelectionChanged
311 // To clear mySelection, myListControl and storeValue()
312 // So, we don't need to call it
313 myWorkshop->setSelected(anEmptyList);
316 void ModuleBase_WidgetMultiSelector::updateFocus()
318 // Set focus to List control in order to make possible
319 // to use Tab key for transfer the focus to next widgets
320 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
321 myListControl->setFocus();
324 //********************************************************************
325 void ModuleBase_WidgetMultiSelector::updateSelectionName()
329 //********************************************************************
330 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
332 QIntList aShapeTypes;
334 if (myTypeCombo->count() > 1 && myIsUseChoice) {
335 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
338 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
339 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
340 aShapeTypes.append(aType);
341 if (aType == TopAbs_SOLID)
342 aShapeTypes.append(TopAbs_COMPSOLID);
348 //********************************************************************
349 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
351 QString aShapeTypeName;
353 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
354 aShapeTypeName = myTypeCombo->itemText(idx);
355 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
356 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
357 activateSelection(false);
358 activateFilters(false);
359 bool isBlocked = myTypeCombo->blockSignals(true);
360 myTypeCombo->setCurrentIndex(idx);
361 myTypeCombo->blockSignals(isBlocked);
363 activateSelection(true);
364 activateFilters(true);
370 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
372 QList<ModuleBase_ViewerPrs> aSelected;
373 // Restore selection in the viewer by the attribute selection list
375 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
376 if (aSelectionListAttr.get()) {
377 for (int i = 0; i < aSelectionListAttr->size(); i++) {
378 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
379 ResultPtr anObject = anAttr->context();
380 if (anObject.get()) {
382 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
383 if (aShapePtr.get()) {
384 aShape = aShapePtr->impl<TopoDS_Shape>();
386 aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
391 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
392 if (aRefListAttr.get()) {
393 for (int i = 0; i < aRefListAttr->size(); i++) {
394 ObjectPtr anObject = aRefListAttr->object(i);
395 if (anObject.get()) {
396 aSelected.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
405 //********************************************************************
406 void ModuleBase_WidgetMultiSelector::updateSelectionList()
408 myListControl->clear();
410 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
411 if (aSelectionListAttr.get()) {
412 for (int i = 0; i < aSelectionListAttr->size(); i++) {
413 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
414 myListControl->addItem(aAttr->namingName().c_str());
418 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
419 for (int i = 0; i < aRefListAttr->size(); i++) {
420 ObjectPtr anObject = aRefListAttr->object(i);
422 myListControl->addItem(anObject->data()->name().c_str());
425 // We have to call repaint because sometimes the List control is not updated
426 myListControl->repaint();
429 //********************************************************************
430 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
434 if (theType == "Vertices")
436 else if (theType == "Edges")
438 else if (theType == "Faces")
440 else if (theType == "Solids")
446 //********************************************************************
447 void ModuleBase_WidgetMultiSelector::onCopyItem()
449 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
451 foreach(QListWidgetItem* aItem, aItems) {
454 aRes += aItem->text();
456 if (!aRes.isEmpty()) {
457 QClipboard *clipboard = QApplication::clipboard();
458 clipboard->setText(aRes);
462 //********************************************************************
463 void ModuleBase_WidgetMultiSelector::onListSelection()
465 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
466 myCopyAction->setEnabled(!aItems.isEmpty());