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 = 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 myListControl = new CustomListWidget(this);
128 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
129 aMainLay->setRowStretch(2, 1);
130 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
131 //aMainLay->setRowMinimumHeight(3, 20);
132 //this->setLayout(aMainLay);
133 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
135 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
136 myCopyAction->setShortcut(QKeySequence::Copy);
137 myCopyAction->setEnabled(false);
138 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
139 myListControl->addAction(myCopyAction);
140 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
141 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
144 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
148 //********************************************************************
149 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
151 // the value is stored on the selection changed signal processing
152 // A rare case when plugin was not loaded.
156 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
157 if (aSelectionListAttr.get()) {
158 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
163 //********************************************************************
164 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
166 // A rare case when plugin was not loaded.
170 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
171 if (aSelectionListAttr.get()) {
172 // Restore shape type
173 if (!aSelectionListAttr->selectionType().empty())
174 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
176 updateSelectionList();
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
183 ModuleBase_WidgetValidated::storeAttributeValue();
185 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
186 if (aSelectionListAttr.get()) {
187 mySelectionType = aSelectionListAttr->selectionType();
188 mySelectionCount = aSelectionListAttr->size();
191 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
192 mySelectionCount = aRefListAttr->size();
196 //********************************************************************
197 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
199 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
202 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
203 if (aSelectionListAttr.get()) {
204 aSelectionListAttr->setSelectionType(mySelectionType);
206 // restore selection in the attribute. Indeed there is only one stored object
207 int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
208 for (int i = 0; i < aCountAppened; i++)
209 aSelectionListAttr->removeLast();
212 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
213 // restore objects in the attribute. Indeed there is only one stored object
214 int aCountAppened = aRefListAttr->size() - mySelectionCount;
215 for (int i = 0; i < aCountAppened; i++)
216 aRefListAttr->removeLast();
220 //********************************************************************
221 void ModuleBase_WidgetMultiSelector::clearAttribute()
223 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
224 if (aSelectionListAttr.get())
225 aSelectionListAttr->clear();
227 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
228 aRefListAttr->clear();
232 //********************************************************************
233 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
234 GeomShapePtr theShape)
236 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
237 if (aSelectionListAttr.get()) {
238 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
239 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
242 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
243 aRefListAttr->append(theSelectedObject);
247 //********************************************************************
248 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
249 const bool theToValidate)
251 QList<ModuleBase_ViewerPrs> aSkippedValues;
253 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
255 for (; anIt != aLast; anIt++) {
256 ModuleBase_ViewerPrs aValue = *anIt;
257 bool aProcessed = false;
258 if (!theToValidate || isValidInFilters(aValue)) {
259 aProcessed = setSelectionCustom(aValue);
262 aSkippedValues.append(aValue);
263 // if there is at least one set, the result is true
264 isDone = isDone || aProcessed;
266 // updateObject - to update/redisplay feature
267 // it is commented in order to perfom it outside the method
269 //updateObject(myFeature);
270 // this emit is necessary to call store/restore method an restore type of selection
271 //emit valuesChanged();
274 if (!aSkippedValues.empty())
275 theValues.append(aSkippedValues);
280 //********************************************************************
281 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
283 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
285 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
286 aValid = aResult.get() != NULL;
289 // We can not select a result of our feature
290 const std::list<ResultPtr>& aResList = myFeature->results();
291 std::list<ResultPtr>::const_iterator aIt;
292 bool isSkipSelf = false;
293 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
294 if ((*aIt) == aResult) {
307 //********************************************************************
308 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
310 QList<QWidget*> result;
311 //result << myTypeCombo;
312 result << myListControl;
316 //********************************************************************
317 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
319 activateSelectionAndFilters(true);
320 QList<ModuleBase_ViewerPrs> anEmptyList;
321 // This method will call Selection changed event which will call onSelectionChanged
322 // To clear mySelection, myListControl and storeValue()
323 // So, we don't need to call it
324 myWorkshop->setSelected(anEmptyList);
327 void ModuleBase_WidgetMultiSelector::updateFocus()
329 // Set focus to List control in order to make possible
330 // to use Tab key for transfer the focus to next widgets
331 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
332 myListControl->setFocus();
335 //********************************************************************
336 void ModuleBase_WidgetMultiSelector::updateSelectionName()
340 //********************************************************************
341 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
343 QIntList aShapeTypes;
345 if (myTypeCombo->count() > 1 && myIsUseChoice) {
346 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
349 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
350 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
351 aShapeTypes.append(aType);
352 if (aType == TopAbs_SOLID)
353 aShapeTypes.append(TopAbs_COMPSOLID);
359 //********************************************************************
360 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
362 QString aShapeTypeName;
364 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
365 aShapeTypeName = myTypeCombo->itemText(idx);
366 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
367 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
368 activateSelectionAndFilters(false);
369 bool isBlocked = myTypeCombo->blockSignals(true);
370 myTypeCombo->setCurrentIndex(idx);
371 myTypeCombo->blockSignals(isBlocked);
373 activateSelectionAndFilters(true);
379 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
381 QList<ModuleBase_ViewerPrs> aSelected;
382 // Restore selection in the viewer by the attribute selection list
384 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
385 if (aSelectionListAttr.get()) {
386 for (int i = 0; i < aSelectionListAttr->size(); i++) {
387 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
388 ResultPtr anObject = anAttr->context();
389 if (anObject.get()) {
391 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
392 if (aShapePtr.get()) {
393 aShape = aShapePtr->impl<TopoDS_Shape>();
395 aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
400 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
401 if (aRefListAttr.get()) {
402 for (int i = 0; i < aRefListAttr->size(); i++) {
403 ObjectPtr anObject = aRefListAttr->object(i);
404 if (anObject.get()) {
405 aSelected.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
414 //********************************************************************
415 void ModuleBase_WidgetMultiSelector::updateSelectionList()
417 myListControl->clear();
419 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
420 if (aSelectionListAttr.get()) {
421 for (int i = 0; i < aSelectionListAttr->size(); i++) {
422 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
423 myListControl->addItem(aAttr->namingName().c_str());
427 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
428 for (int i = 0; i < aRefListAttr->size(); i++) {
429 ObjectPtr anObject = aRefListAttr->object(i);
431 myListControl->addItem(anObject->data()->name().c_str());
434 // We have to call repaint because sometimes the List control is not updated
435 myListControl->repaint();
438 //********************************************************************
439 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
443 if (theType == "Vertices")
445 else if (theType == "Edges")
447 else if (theType == "Faces")
449 else if (theType == "Solids")
455 //********************************************************************
456 void ModuleBase_WidgetMultiSelector::onCopyItem()
458 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
460 foreach(QListWidgetItem* aItem, aItems) {
463 aRes += aItem->text();
465 if (!aRes.isEmpty()) {
466 QClipboard *clipboard = QApplication::clipboard();
467 clipboard->setText(aRes);
471 //********************************************************************
472 void ModuleBase_WidgetMultiSelector::onListSelection()
474 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
475 myCopyAction->setEnabled(!aItems.isEmpty());