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_Tools.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Object.h>
19 #include <Config_WidgetAPI.h>
21 #include <QGridLayout>
23 #include <QListWidget>
29 #include <QApplication>
35 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
36 ModuleBase_IWorkshop* theWorkshop,
37 const Config_WidgetAPI* theData,
38 const std::string& theParentId)
39 : ModuleBase_ModelWidget(theParent, theData, theParentId),
40 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
42 myMainWidget = new QWidget(theParent);
43 QGridLayout* aMainLay = new QGridLayout(myMainWidget);
44 ModuleBase_Tools::adjustMargins(aMainLay);
46 QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget);
47 aMainLay->addWidget(aTypeLabel, 0, 0);
49 myTypeCombo = new QComboBox(myMainWidget);
50 // There is no sence to paramerize list of types while we can not parametrize selection mode
51 QString aTypesStr("Vertices Edges Faces Solids");
52 QStringList aShapeTypes = aTypesStr.split(' ');
53 myTypeCombo->addItems(aShapeTypes);
54 aMainLay->addWidget(myTypeCombo, 0, 1);
56 QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget);
57 aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
59 myListControl = new QListWidget(myMainWidget);
60 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
61 aMainLay->setRowStretch(2, 1);
62 aMainLay->addWidget(new QLabel(myMainWidget));
63 aMainLay->setRowMinimumHeight(3, 20);
64 myMainWidget->setLayout(aMainLay);
65 //TODO: Move into the base class
66 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
68 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
70 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
71 myCopyAction->setShortcut(QKeySequence::Copy);
72 myCopyAction->setEnabled(false);
73 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
74 myListControl->addAction(myCopyAction);
75 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
76 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
78 activateSelection(true);
81 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
83 activateSelection(false);
86 //********************************************************************
87 bool ModuleBase_WidgetMultiSelector::storeValue() const
89 // A rare case when plugin was not loaded.
92 DataPtr aData = myFeature->data();
93 AttributeSelectionListPtr aSelectionListAttr =
94 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
96 if (aSelectionListAttr) {
97 aSelectionListAttr->clear();
99 TopAbs_ShapeEnum aCurrentType =
100 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
101 aSelectionListAttr->setSelectionType((int) aCurrentType);
102 // Store selection in the attribute
103 foreach (GeomSelection aSelec, mySelection) {
104 aSelectionListAttr->append(aSelec.first, aSelec.second);
106 //updateSelectionList(aSelectionListAttr);
107 updateObject(myFeature);
113 //********************************************************************
114 bool ModuleBase_WidgetMultiSelector::restoreValue()
116 // A rare case when plugin was not loaded.
119 DataPtr aData = myFeature->data();
120 AttributeSelectionListPtr aSelectionListAttr =
121 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
123 if (aSelectionListAttr) {
125 // Restore shape type
126 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum) aSelectionListAttr->selectionType();
127 setCurrentShapeType(aShapeType);
128 // Restore selection in the list
129 for (int i = 0; i < aSelectionListAttr->size(); i++) {
130 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
131 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
133 updateSelectionList(aSelectionListAttr);
139 //********************************************************************
140 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
145 //********************************************************************
146 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
148 QList<QWidget*> result;
149 //result << myTypeCombo;
150 result << myListControl;
154 //********************************************************************
155 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
157 //TODO: Remove maybe?
158 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
161 //********************************************************************
162 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
164 myIsActive = toActivate;
166 connect(myWorkshop, SIGNAL(selectionChanged()),
167 this, SLOT(onSelectionChanged()),
168 Qt::UniqueConnection);
169 activateShapeSelection();
171 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
172 myWorkshop->deactivateSubShapesSelection();
176 //********************************************************************
177 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
179 activateShapeSelection();
180 QObjectPtrList anEmptyList;
181 myWorkshop->setSelected(anEmptyList);
182 // Clear mySelection, myListControl and storeValue()
183 onSelectionChanged();
186 //********************************************************************
187 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
189 ModuleBase_ISelection* aSelection = myWorkshop->selection();
190 NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
191 std::list<ObjectPtr> aOwnersList;
192 aSelection->selectedShapes(aSelectedShapes, aOwnersList);
195 std::list<ObjectPtr>::const_iterator aIt;
196 NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
198 for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
199 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
201 // We can not select a result of our feature
202 const std::list<ResultPtr>& aResList = myFeature->results();
203 std::list<ResultPtr>::const_iterator aIt;
204 bool isSkipSelf = false;
205 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
206 if ((*aIt) == aResult) {
214 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
215 aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
216 mySelection.append(GeomSelection(aResult, aShape));
218 //updateSelectionList();
219 emit valuesChanged();
222 //********************************************************************
223 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
224 NCollection_List<TopoDS_Shape>& theResult)
226 if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
228 TopAbs_ShapeEnum aReferenceType =
229 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
230 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
231 for (; anIter.More(); anIter.Next()) {
232 TopoDS_Shape aShape = anIter.Value();
233 if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
235 theResult.Append(aShape);
239 //********************************************************************
240 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
242 QString aShapeTypeName;
244 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
245 aShapeTypeName = myTypeCombo->itemText(idx);
246 TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
247 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
248 activateSelection(false);
249 bool isBlocked = myTypeCombo->blockSignals(true);
250 myTypeCombo->setCurrentIndex(idx);
251 myTypeCombo->blockSignals(isBlocked);
252 activateSelection(true);
258 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
260 QString aNewType = myTypeCombo->currentText();
262 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
263 myWorkshop->activateSubShapesSelection(aList);
266 //********************************************************************
267 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
269 myListControl->clear();
270 for (int i = 0; i < theList->size(); i++) {
271 AttributeSelectionPtr aAttr = theList->value(i);
272 myListControl->addItem(aAttr->namingName().c_str());
276 //********************************************************************
277 void ModuleBase_WidgetMultiSelector::onCopyItem()
279 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
281 foreach(QListWidgetItem* aItem, aItems) {
284 aRes += aItem->text();
286 if (!aRes.isEmpty()) {
287 QClipboard *clipboard = QApplication::clipboard();
288 clipboard->setText(aRes);
292 //********************************************************************
293 void ModuleBase_WidgetMultiSelector::onListSelection()
295 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
296 myCopyAction->setEnabled(!aItems.isEmpty());