]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Provide Copy command for items in Group operation
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetMultiSelector.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
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>
15
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Object.h>
18
19 #include <Config_WidgetAPI.h>
20
21 #include <QGridLayout>
22 #include <QLabel>
23 #include <QListWidget>
24 #include <QObject>
25 #include <QString>
26 #include <QComboBox>
27 #include <QEvent>
28 #include <QAction>
29 #include <QApplication>
30 #include <QClipboard>
31
32 #include <memory>
33 #include <string>
34
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)
41 {
42   myMainWidget = new QWidget(theParent);
43   QGridLayout* aMainLay = new QGridLayout(myMainWidget);
44   ModuleBase_Tools::adjustMargins(aMainLay);
45
46   QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget);
47   aMainLay->addWidget(aTypeLabel, 0, 0);
48
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);
55
56   QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget);
57   aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
58
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);
67   //TODO_END
68   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
69
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()));
77
78   activateSelection(true);
79 }
80
81 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
82 {
83   activateSelection(false);
84 }
85
86 //********************************************************************
87 bool ModuleBase_WidgetMultiSelector::storeValue() const
88 {
89   // A rare case when plugin was not loaded. 
90   if(!myFeature)
91     return false;
92   DataPtr aData = myFeature->data();
93   AttributeSelectionListPtr aSelectionListAttr = 
94     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
95
96   if (aSelectionListAttr) {
97     aSelectionListAttr->clear();
98     // Store shapes type
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);
105     }
106     //updateSelectionList(aSelectionListAttr);
107     updateObject(myFeature);
108     return true;
109   }
110   return false;
111 }
112
113 //********************************************************************
114 bool ModuleBase_WidgetMultiSelector::restoreValue()
115 {
116   // A rare case when plugin was not loaded. 
117   if(!myFeature)
118     return false;
119   DataPtr aData = myFeature->data();
120   AttributeSelectionListPtr aSelectionListAttr = 
121     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
122
123   if (aSelectionListAttr) {
124     mySelection.clear();
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()));
132     }
133     updateSelectionList(aSelectionListAttr);
134     return true;
135   }
136   return false;
137 }
138
139 //********************************************************************
140 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
141 {
142   return myMainWidget;
143 }
144
145 //********************************************************************
146 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
147 {
148   QList<QWidget*> result;
149   //result << myTypeCombo;
150   result << myListControl;
151   return result;
152 }
153
154 //********************************************************************
155 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
156 {
157   //TODO: Remove maybe?
158   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
159 }
160
161 //********************************************************************
162 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
163 {
164   myIsActive = toActivate;
165   if (myIsActive) {
166     connect(myWorkshop, SIGNAL(selectionChanged()), 
167             this,       SLOT(onSelectionChanged()), 
168             Qt::UniqueConnection);
169     activateShapeSelection();
170   } else {
171     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
172     myWorkshop->deactivateSubShapesSelection();
173   }
174 }
175
176 //********************************************************************
177 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
178 {
179   activateShapeSelection();
180   QObjectPtrList anEmptyList;
181   myWorkshop->setSelected(anEmptyList);
182   // Clear mySelection, myListControl and storeValue()
183   onSelectionChanged();
184 }
185
186 //********************************************************************
187 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
188 {
189   ModuleBase_ISelection* aSelection = myWorkshop->selection();
190   NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
191   std::list<ObjectPtr> aOwnersList;
192   aSelection->selectedShapes(aSelectedShapes, aOwnersList);
193
194   mySelection.clear();
195   std::list<ObjectPtr>::const_iterator aIt;
196   NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
197   GeomShapePtr aShape;
198   for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
199     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
200     if (myFeature) {
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) {
207           isSkipSelf = true;
208           break;
209         }
210       }
211       if(isSkipSelf)
212         continue;
213     }
214     aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
215     aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
216     mySelection.append(GeomSelection(aResult, aShape));
217   }
218   //updateSelectionList();
219   emit valuesChanged();
220 }
221
222 //********************************************************************
223 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
224                                                   NCollection_List<TopoDS_Shape>& theResult)
225 {
226   if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
227     return;
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)
234       continue;
235     theResult.Append(aShape);
236   }
237 }
238
239 //********************************************************************
240 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
241 {
242   QString aShapeTypeName;
243   
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);
253       break;
254     }
255   }
256 }
257
258 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
259 {
260   QString aNewType = myTypeCombo->currentText();
261   QIntList aList;
262   aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
263   myWorkshop->activateSubShapesSelection(aList);
264 }
265
266 //********************************************************************
267 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
268 {
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());
273   }
274 }
275
276 //********************************************************************
277 void ModuleBase_WidgetMultiSelector::onCopyItem()
278 {
279   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
280   QString aRes;
281   foreach(QListWidgetItem* aItem, aItems) {
282     if (!aRes.isEmpty())
283       aRes += "\n";
284     aRes += aItem->text();
285   }
286   if (!aRes.isEmpty()) {
287     QClipboard *clipboard = QApplication::clipboard();
288     clipboard->setText(aRes);
289   }
290 }
291
292 //********************************************************************
293 void ModuleBase_WidgetMultiSelector::onListSelection()
294 {
295   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
296   myCopyAction->setEnabled(!aItems.isEmpty());
297 }
298