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_FilterNoDegeneratedEdge.h>
13 #include <ModuleBase_ISelection.h>
14 #include <ModuleBase_IWorkshop.h>
15 #include <ModuleBase_IViewer.h>
16 #include <ModuleBase_Tools.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_ModelWidget(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
52 QString aTypesStr("Vertices Edges Faces Solids");
53 QStringList aShapeTypes = aTypesStr.split(' ');
54 myTypeCombo->addItems(aShapeTypes);
55 aMainLay->addWidget(myTypeCombo, 0, 1);
57 QLabel* aListLabel = new QLabel(tr("Selected objects:"), this);
58 aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
60 myListControl = new QListWidget(this);
61 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
62 aMainLay->setRowStretch(2, 1);
63 aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
64 aMainLay->setRowMinimumHeight(3, 20);
65 this->setLayout(aMainLay);
66 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
68 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
69 myCopyAction->setShortcut(QKeySequence::Copy);
70 myCopyAction->setEnabled(false);
71 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
72 myListControl->addAction(myCopyAction);
73 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
74 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
76 activateSelection(true);
79 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
81 activateSelection(false);
84 //********************************************************************
85 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
87 // A rare case when plugin was not loaded.
90 DataPtr aData = myFeature->data();
91 AttributeSelectionListPtr aSelectionListAttr =
92 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
94 if (aSelectionListAttr) {
95 aSelectionListAttr->clear();
97 TopAbs_ShapeEnum aCurrentType =
98 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
99 aSelectionListAttr->setSelectionType((int) aCurrentType);
100 // Store selection in the attribute
101 foreach (GeomSelection aSelec, mySelection) {
102 aSelectionListAttr->append(aSelec.first, aSelec.second);
104 //updateSelectionList(aSelectionListAttr);
105 updateObject(myFeature);
111 //********************************************************************
112 bool ModuleBase_WidgetMultiSelector::restoreValue()
114 // A rare case when plugin was not loaded.
117 DataPtr aData = myFeature->data();
118 AttributeSelectionListPtr aSelectionListAttr =
119 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
121 if (aSelectionListAttr) {
123 // Restore shape type
124 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum) aSelectionListAttr->selectionType();
125 setCurrentShapeType(aShapeType);
126 // Restore selection in the list
127 for (int i = 0; i < aSelectionListAttr->size(); i++) {
128 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
129 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
131 updateSelectionList(aSelectionListAttr);
137 //********************************************************************
138 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
140 QList<QWidget*> result;
141 //result << myTypeCombo;
142 result << myListControl;
146 //********************************************************************
147 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
149 //TODO: Remove maybe?
150 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
153 //********************************************************************
154 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
156 myIsActive = toActivate;
158 connect(myWorkshop, SIGNAL(selectionChanged()),
159 this, SLOT(onSelectionChanged()),
160 Qt::UniqueConnection);
161 activateShapeSelection();
163 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
164 myWorkshop->deactivateSubShapesSelection();
166 myWorkshop->viewer()->removeSelectionFilter(myEdgesTypeFilter);
170 //********************************************************************
171 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
173 activateShapeSelection();
174 QObjectPtrList anEmptyList;
175 myWorkshop->setSelected(anEmptyList);
176 // Clear mySelection, myListControl and storeValue()
177 onSelectionChanged();
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
183 ModuleBase_ISelection* aSelection = myWorkshop->selection();
184 NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
185 std::list<ObjectPtr> aOwnersList;
186 aSelection->selectedShapes(aSelectedShapes, aOwnersList);
189 std::list<ObjectPtr>::const_iterator aIt;
190 NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
192 for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
193 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
195 // We can not select a result of our feature
196 const std::list<ResultPtr>& aResList = myFeature->results();
197 std::list<ResultPtr>::const_iterator aIt;
198 bool isSkipSelf = false;
199 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
200 if ((*aIt) == aResult) {
208 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
209 aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
210 mySelection.append(GeomSelection(aResult, aShape));
212 //updateSelectionList();
213 emit valuesChanged();
216 //********************************************************************
217 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
218 NCollection_List<TopoDS_Shape>& theResult)
220 if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
222 TopAbs_ShapeEnum aReferenceType =
223 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
224 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
225 for (; anIter.More(); anIter.Next()) {
226 TopoDS_Shape aShape = anIter.Value();
227 if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
229 theResult.Append(aShape);
233 //********************************************************************
234 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
236 QString aShapeTypeName;
238 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
239 aShapeTypeName = myTypeCombo->itemText(idx);
240 TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
241 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
242 activateSelection(false);
243 bool isBlocked = myTypeCombo->blockSignals(true);
244 myTypeCombo->setCurrentIndex(idx);
245 myTypeCombo->blockSignals(isBlocked);
246 activateSelection(true);
252 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
254 QString aNewType = myTypeCombo->currentText();
256 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
257 myWorkshop->activateSubShapesSelection(aList);
259 // it is necessary to filter the selected edges to be non-degenerated
260 // it is not possible to build naming name for such edges
261 if (aNewType == "Edges") {
262 myEdgesTypeFilter = new ModuleBase_FilterNoDegeneratedEdge();
263 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
264 aViewer->addSelectionFilter(myEdgesTypeFilter);
267 myWorkshop->viewer()->removeSelectionFilter(myEdgesTypeFilter);
271 //********************************************************************
272 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
274 myListControl->clear();
275 for (int i = 0; i < theList->size(); i++) {
276 AttributeSelectionPtr aAttr = theList->value(i);
277 myListControl->addItem(aAttr->namingName().c_str());
281 //********************************************************************
282 void ModuleBase_WidgetMultiSelector::onCopyItem()
284 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
286 foreach(QListWidgetItem* aItem, aItems) {
289 aRes += aItem->text();
291 if (!aRes.isEmpty()) {
292 QClipboard *clipboard = QApplication::clipboard();
293 clipboard->setText(aRes);
297 //********************************************************************
298 void ModuleBase_WidgetMultiSelector::onListSelection()
300 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
301 myCopyAction->setEnabled(!aItems.isEmpty());