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>
39 class CustomListWidget : public QListWidget
42 CustomListWidget( QWidget* theParent )
43 : QListWidget( theParent )
47 virtual QSize sizeHint() const
49 int aHeight = 2*QFontMetrics( font() ).height();
50 QSize aSize = QListWidget::sizeHint();
51 return QSize( aSize.width(), aHeight );
54 virtual QSize minimumSizeHint() const
56 int aHeight = 2*QFontMetrics( font() ).height();
57 QSize aSize = QListWidget::minimumSizeHint();
58 return QSize( aSize.width(), aHeight );
62 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
63 ModuleBase_IWorkshop* theWorkshop,
64 const Config_WidgetAPI* theData,
65 const std::string& theParentId)
66 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId),
69 QGridLayout* aMainLay = new QGridLayout(this);
70 ModuleBase_Tools::adjustMargins(aMainLay);
72 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
73 aMainLay->addWidget(aTypeLabel, 0, 0);
75 myTypeCombo = new QComboBox(this);
76 // There is no sense to parameterize list of types while we can not parameterize selection mode
78 std::string aPropertyTypes = theData->getProperty("type_choice");
79 QString aTypesStr = aPropertyTypes.c_str();
80 QStringList aShapeTypes = aTypesStr.split(' ');
82 myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
84 myTypeCombo->addItems(aShapeTypes);
85 aMainLay->addWidget(myTypeCombo, 0, 1);
86 // if the xml definition contains one type, the controls to select a type should not be shown
87 if (aShapeTypes.size() == 1 || !myIsUseChoice) {
88 aTypeLabel->setVisible(false);
89 myTypeCombo->setVisible(false);
92 std::string aLabelText = theData->getProperty("label");
93 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
94 : tr("Selected objects:"), this);
95 aMainLay->addWidget(aListLabel, 1, 0);
96 // if the xml definition contains one type, an information label should be shown near to the latest
97 if (aShapeTypes.size() == 1) {
98 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
99 if (!aLabelIcon.isEmpty()) {
100 QLabel* aSelectedLabel = new QLabel("", this);
101 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
102 aMainLay->addWidget(aSelectedLabel, 1, 1);
104 aMainLay->setColumnStretch(2, 1);
107 myListControl = new CustomListWidget(this);
108 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
109 aMainLay->setRowStretch(2, 1);
110 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
111 //aMainLay->setRowMinimumHeight(3, 20);
112 //this->setLayout(aMainLay);
113 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
115 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
116 myCopyAction->setShortcut(QKeySequence::Copy);
117 myCopyAction->setEnabled(false);
118 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
119 myListControl->addAction(myCopyAction);
120 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
121 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
124 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
128 //********************************************************************
129 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
131 // the value is stored on the selection changed signal processing
132 // A rare case when plugin was not loaded.
136 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
137 if (aSelectionListAttr.get()) {
138 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
143 //********************************************************************
144 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
146 // A rare case when plugin was not loaded.
150 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
151 if (aSelectionListAttr.get()) {
152 // Restore shape type
153 if (!aSelectionListAttr->selectionType().empty())
154 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
156 updateSelectionList();
160 //********************************************************************
161 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
163 ModuleBase_WidgetValidated::storeAttributeValue();
165 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
166 if (aSelectionListAttr.get()) {
167 mySelectionType = aSelectionListAttr->selectionType();
168 mySelectionCount = aSelectionListAttr->size();
171 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
172 mySelectionCount = aRefListAttr->size();
176 //********************************************************************
177 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
179 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
182 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
183 if (aSelectionListAttr.get()) {
184 aSelectionListAttr->setSelectionType(mySelectionType);
186 // restore selection in the attribute. Indeed there is only one stored object
187 int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
188 for (int i = 0; i < aCountAppened; i++)
189 aSelectionListAttr->removeLast();
192 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
193 // restore objects in the attribute. Indeed there is only one stored object
194 int aCountAppened = aRefListAttr->size() - mySelectionCount;
195 for (int i = 0; i < aCountAppened; i++)
196 aRefListAttr->removeLast();
200 //********************************************************************
201 void ModuleBase_WidgetMultiSelector::clearAttribute()
203 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
204 if (aSelectionListAttr.get())
205 aSelectionListAttr->clear();
207 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
208 aRefListAttr->clear();
212 //********************************************************************
213 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
214 GeomShapePtr theShape)
216 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
217 if (aSelectionListAttr.get()) {
218 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
219 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
222 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
223 aRefListAttr->append(theSelectedObject);
227 //********************************************************************
228 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
229 const bool theToValidate)
231 QList<ModuleBase_ViewerPrs> aSkippedValues;
233 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
235 for (; anIt != aLast; anIt++) {
236 ModuleBase_ViewerPrs aValue = *anIt;
237 bool aProcessed = false;
238 if (!theToValidate || isValidInFilters(aValue)) {
239 aProcessed = setSelectionCustom(aValue);
242 aSkippedValues.append(aValue);
243 // if there is at least one set, the result is true
244 isDone = isDone || aProcessed;
246 // updateObject - to update/redisplay feature
247 // it is commented in order to perfom it outside the method
249 //updateObject(myFeature);
250 // this emit is necessary to call store/restore method an restore type of selection
251 //emit valuesChanged();
254 if (!aSkippedValues.empty())
255 theValues.append(aSkippedValues);
260 //********************************************************************
261 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
263 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
265 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
266 aValid = aResult.get() != NULL;
269 // We can not select a result of our feature
270 const std::list<ResultPtr>& aResList = myFeature->results();
271 std::list<ResultPtr>::const_iterator aIt;
272 bool isSkipSelf = false;
273 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
274 if ((*aIt) == aResult) {
287 //********************************************************************
288 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
290 QList<QWidget*> result;
291 //result << myTypeCombo;
292 result << myListControl;
296 //********************************************************************
297 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
299 activateSelection(true);
300 activateFilters(true);
301 QList<ModuleBase_ViewerPrs> anEmptyList;
302 // This method will call Selection changed event which will call onSelectionChanged
303 // To clear mySelection, myListControl and storeValue()
304 // So, we don't need to call it
305 myWorkshop->setSelected(anEmptyList);
308 void ModuleBase_WidgetMultiSelector::updateFocus()
310 // Set focus to List control in order to make possible
311 // to use Tab key for transfer the focus to next widgets
312 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
313 myListControl->setFocus();
316 //********************************************************************
317 void ModuleBase_WidgetMultiSelector::updateSelectionName()
321 //********************************************************************
322 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
324 QIntList aShapeTypes;
326 if (myTypeCombo->count() > 1 && myIsUseChoice) {
327 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
330 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
331 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
332 aShapeTypes.append(aType);
333 if (aType == TopAbs_SOLID)
334 aShapeTypes.append(TopAbs_COMPSOLID);
340 //********************************************************************
341 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
343 QString aShapeTypeName;
345 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
346 aShapeTypeName = myTypeCombo->itemText(idx);
347 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
348 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
349 activateSelection(false);
350 activateFilters(false);
351 bool isBlocked = myTypeCombo->blockSignals(true);
352 myTypeCombo->setCurrentIndex(idx);
353 myTypeCombo->blockSignals(isBlocked);
355 activateSelection(true);
356 activateFilters(true);
362 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
364 QList<ModuleBase_ViewerPrs> aSelected;
365 // Restore selection in the viewer by the attribute selection list
367 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
368 if (aSelectionListAttr.get()) {
369 for (int i = 0; i < aSelectionListAttr->size(); i++) {
370 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
371 ResultPtr anObject = anAttr->context();
372 if (anObject.get()) {
374 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
375 if (aShapePtr.get()) {
376 aShape = aShapePtr->impl<TopoDS_Shape>();
378 aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
383 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
384 if (aRefListAttr.get()) {
385 for (int i = 0; i < aRefListAttr->size(); i++) {
386 ObjectPtr anObject = aRefListAttr->object(i);
387 if (anObject.get()) {
388 aSelected.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
397 //********************************************************************
398 void ModuleBase_WidgetMultiSelector::updateSelectionList()
400 myListControl->clear();
402 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
403 if (aSelectionListAttr.get()) {
404 for (int i = 0; i < aSelectionListAttr->size(); i++) {
405 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
406 myListControl->addItem(aAttr->namingName().c_str());
410 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
411 for (int i = 0; i < aRefListAttr->size(); i++) {
412 ObjectPtr anObject = aRefListAttr->object(i);
414 myListControl->addItem(anObject->data()->name().c_str());
417 // We have to call repaint because sometimes the List control is not updated
418 myListControl->repaint();
421 //********************************************************************
422 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
426 if (theType == "Vertices")
428 else if (theType == "Edges")
430 else if (theType == "Faces")
432 else if (theType == "Solids")
438 //********************************************************************
439 void ModuleBase_WidgetMultiSelector::onCopyItem()
441 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
443 foreach(QListWidgetItem* aItem, aItems) {
446 aRes += aItem->text();
448 if (!aRes.isEmpty()) {
449 QClipboard *clipboard = QApplication::clipboard();
450 clipboard->setText(aRes);
454 //********************************************************************
455 void ModuleBase_WidgetMultiSelector::onListSelection()
457 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
458 myCopyAction->setEnabled(!aItems.isEmpty());