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>
41 * Customization of a List Widget to make it to be placed on full width of container
43 class CustomListWidget : public QListWidget
47 /// \param theParent a parent widget
48 CustomListWidget( QWidget* theParent )
49 : QListWidget( theParent )
53 /// Redefinition of virtual method
54 virtual QSize sizeHint() const
56 int aHeight = 2*QFontMetrics( font() ).height();
57 QSize aSize = QListWidget::sizeHint();
58 return QSize( aSize.width(), aHeight );
61 /// Redefinition of virtual method
62 virtual QSize minimumSizeHint() const
64 int aHeight = 4/*2*/*QFontMetrics( font() ).height();
65 QSize aSize = QListWidget::minimumSizeHint();
66 return QSize( aSize.width(), aHeight );
70 // The code is necessary only for Linux because
71 //it can not update viewport on widget resize
73 void resizeEvent(QResizeEvent* theEvent)
75 QListWidget::resizeEvent(theEvent);
76 QTimer::singleShot(5, viewport(), SLOT(repaint()));
81 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
82 ModuleBase_IWorkshop* theWorkshop,
83 const Config_WidgetAPI* theData,
84 const std::string& theParentId)
85 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId),
88 QGridLayout* aMainLay = new QGridLayout(this);
89 ModuleBase_Tools::adjustMargins(aMainLay);
91 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
92 aMainLay->addWidget(aTypeLabel, 0, 0);
94 myTypeCombo = new QComboBox(this);
95 // There is no sense to parameterize list of types while we can not parameterize selection mode
97 std::string aPropertyTypes = theData->getProperty("type_choice");
98 QString aTypesStr = aPropertyTypes.c_str();
99 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
101 myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
103 if (!aShapeTypes.empty())
104 myTypeCombo->addItems(aShapeTypes);
105 aMainLay->addWidget(myTypeCombo, 0, 1);
106 // if the xml definition contains one type, the controls to select a type should not be shown
107 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
108 aTypeLabel->setVisible(false);
109 myTypeCombo->setVisible(false);
112 std::string aLabelText = theData->getProperty("label");
113 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
114 : tr("Selected objects:"), this);
115 aMainLay->addWidget(aListLabel, 1, 0);
116 // if the xml definition contains one type, an information label should be shown near to the latest
117 if (aShapeTypes.size() <= 1) {
118 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
119 if (!aLabelIcon.isEmpty()) {
120 QLabel* aSelectedLabel = new QLabel("", this);
121 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
122 aMainLay->addWidget(aSelectedLabel, 1, 1);
124 aMainLay->setColumnStretch(2, 1);
127 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
128 myListControl = new CustomListWidget(this);
129 QString anObjName = QString::fromStdString(attributeID());
130 myListControl->setObjectName(anObjName);
131 myListControl->setToolTip(aToolTip);
133 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
134 aMainLay->setRowStretch(2, 1);
135 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
136 //aMainLay->setRowMinimumHeight(3, 20);
137 //this->setLayout(aMainLay);
138 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
140 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
141 myCopyAction->setShortcut(QKeySequence::Copy);
142 myCopyAction->setEnabled(false);
143 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
144 myListControl->addAction(myCopyAction);
145 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
146 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
149 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
153 //********************************************************************
154 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
156 // the value is stored on the selection changed signal processing
157 // A rare case when plugin was not loaded.
161 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
162 if (aSelectionListAttr.get()) {
163 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
168 //********************************************************************
169 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
171 // A rare case when plugin was not loaded.
175 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
176 if (aSelectionListAttr.get()) {
177 // Restore shape type
178 if (!aSelectionListAttr->selectionType().empty())
179 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
181 updateSelectionList();
185 //********************************************************************
186 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
188 ModuleBase_WidgetValidated::storeAttributeValue();
190 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
191 if (aSelectionListAttr.get()) {
192 mySelectionType = aSelectionListAttr->selectionType();
193 mySelectionCount = aSelectionListAttr->size();
196 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
197 mySelectionCount = aRefListAttr->size();
201 //********************************************************************
202 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
204 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
207 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
208 if (aSelectionListAttr.get()) {
209 aSelectionListAttr->setSelectionType(mySelectionType);
211 // restore selection in the attribute. Indeed there is only one stored object
212 int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
213 for (int i = 0; i < aCountAppened; i++)
214 aSelectionListAttr->removeLast();
217 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
218 // restore objects in the attribute. Indeed there is only one stored object
219 int aCountAppened = aRefListAttr->size() - mySelectionCount;
220 for (int i = 0; i < aCountAppened; i++)
221 aRefListAttr->removeLast();
225 //********************************************************************
226 void ModuleBase_WidgetMultiSelector::clearAttribute()
228 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
229 if (aSelectionListAttr.get())
230 aSelectionListAttr->clear();
232 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
233 aRefListAttr->clear();
237 //********************************************************************
238 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
239 GeomShapePtr theShape)
241 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
242 if (aSelectionListAttr.get()) {
243 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
244 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
247 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
248 aRefListAttr->append(theSelectedObject);
252 //********************************************************************
253 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
254 const bool theToValidate)
256 QList<ModuleBase_ViewerPrs> aSkippedValues;
258 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
260 for (; anIt != aLast; anIt++) {
261 ModuleBase_ViewerPrs aValue = *anIt;
262 bool aProcessed = false;
263 if (!theToValidate || isValidInFilters(aValue)) {
264 aProcessed = setSelectionCustom(aValue);
267 aSkippedValues.append(aValue);
268 // if there is at least one set, the result is true
269 isDone = isDone || aProcessed;
271 // updateObject - to update/redisplay feature
272 // it is commented in order to perfom it outside the method
274 //updateObject(myFeature);
275 // this emit is necessary to call store/restore method an restore type of selection
276 //emit valuesChanged();
279 if (!aSkippedValues.empty())
280 theValues.append(aSkippedValues);
285 //********************************************************************
286 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
288 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
290 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
291 aValid = aResult.get() != NULL;
294 // We can not select a result of our feature
295 const std::list<ResultPtr>& aResList = myFeature->results();
296 std::list<ResultPtr>::const_iterator aIt;
297 bool isSkipSelf = false;
298 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
299 if ((*aIt) == aResult) {
312 //********************************************************************
313 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
315 QList<QWidget*> result;
316 //result << myTypeCombo;
317 result << myListControl;
321 //********************************************************************
322 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
324 activateSelectionAndFilters(true);
325 QList<ModuleBase_ViewerPrs> anEmptyList;
326 // This method will call Selection changed event which will call onSelectionChanged
327 // To clear mySelection, myListControl and storeValue()
328 // So, we don't need to call it
329 myWorkshop->setSelected(anEmptyList);
332 void ModuleBase_WidgetMultiSelector::updateFocus()
334 // Set focus to List control in order to make possible
335 // to use Tab key for transfer the focus to next widgets
336 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
337 ModuleBase_Tools::setFocus(myListControl,
338 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
341 //********************************************************************
342 void ModuleBase_WidgetMultiSelector::updateSelectionName()
346 //********************************************************************
347 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
349 QIntList aShapeTypes;
351 if (myTypeCombo->count() > 1 && myIsUseChoice) {
352 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
355 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
356 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
357 aShapeTypes.append(aType);
358 if (aType == TopAbs_SOLID)
359 aShapeTypes.append(TopAbs_COMPSOLID);
365 //********************************************************************
366 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
368 QString aShapeTypeName;
370 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
371 aShapeTypeName = myTypeCombo->itemText(idx);
372 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
373 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
374 activateSelectionAndFilters(false);
375 bool isBlocked = myTypeCombo->blockSignals(true);
376 myTypeCombo->setCurrentIndex(idx);
377 myTypeCombo->blockSignals(isBlocked);
379 activateSelectionAndFilters(true);
385 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
387 QList<ModuleBase_ViewerPrs> aSelected;
388 // Restore selection in the viewer by the attribute selection list
390 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
391 if (aSelectionListAttr.get()) {
392 for (int i = 0; i < aSelectionListAttr->size(); i++) {
393 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
394 ResultPtr anObject = anAttr->context();
395 if (anObject.get()) {
397 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
398 if (aShapePtr.get()) {
399 aShape = aShapePtr->impl<TopoDS_Shape>();
401 aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
406 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
407 if (aRefListAttr.get()) {
408 for (int i = 0; i < aRefListAttr->size(); i++) {
409 ObjectPtr anObject = aRefListAttr->object(i);
410 if (anObject.get()) {
411 aSelected.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
420 //********************************************************************
421 void ModuleBase_WidgetMultiSelector::updateSelectionList()
423 myListControl->clear();
425 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
426 if (aSelectionListAttr.get()) {
427 for (int i = 0; i < aSelectionListAttr->size(); i++) {
428 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
429 myListControl->addItem(aAttr->namingName().c_str());
433 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
434 for (int i = 0; i < aRefListAttr->size(); i++) {
435 ObjectPtr anObject = aRefListAttr->object(i);
437 myListControl->addItem(anObject->data()->name().c_str());
440 // We have to call repaint because sometimes the List control is not updated
441 myListControl->repaint();
444 //********************************************************************
445 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
449 if (theType == "Vertices")
451 else if (theType == "Edges")
453 else if (theType == "Faces")
455 else if (theType == "Solids")
461 //********************************************************************
462 void ModuleBase_WidgetMultiSelector::onCopyItem()
464 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
466 foreach(QListWidgetItem* aItem, aItems) {
469 aRes += aItem->text();
471 if (!aRes.isEmpty()) {
472 QClipboard *clipboard = QApplication::clipboard();
473 clipboard->setText(aRes);
477 //********************************************************************
478 void ModuleBase_WidgetMultiSelector::onListSelection()
480 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
481 myCopyAction->setEnabled(!aItems.isEmpty());