Salome HOME
27.10.2014. Naming data structure for Extrusion feature.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
1 /*
2  * ModuleBase_WidgetMultiSelector.cpp
3  *
4  *  Created on: Aug 28, 2014
5  *      Author: sbh
6  */
7
8 #include <ModuleBase_WidgetMultiSelector.h>
9 #include <ModuleBase_WidgetShapeSelector.h>
10 #include <ModuleBase_ISelection.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_Tools.h>
13
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Validator.h>
18
19 #include <Config_WidgetAPI.h>
20
21 #include <QGridLayout>
22 #include <QLabel>
23 #include <QTextEdit>
24 #include <QObject>
25 #include <QString>
26 #include <QComboBox>
27 #include <QEvent>
28
29 #include <boost/smart_ptr/shared_ptr.hpp>
30 #include <string>
31
32 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
33                                                                ModuleBase_IWorkshop* theWorkshop,
34                                                                const Config_WidgetAPI* theData,
35                                                                const std::string& theParentId)
36     : ModuleBase_ModelWidget(theParent, theData, theParentId),
37       myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
38 {
39   myMainWidget = new QWidget(theParent);
40   QGridLayout* aMainLay = new QGridLayout(myMainWidget);
41   ModuleBase_Tools::adjustMargins(aMainLay);
42   QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget);
43   aMainLay->addWidget(aTypeLabel, 0, 0);
44   myTypeCombo = new QComboBox(myMainWidget);
45   std::string aTypes = theData->getProperty("type_choice");
46   myShapeTypes = QString::fromStdString(aTypes).split(' ');
47   myTypeCombo->addItems(myShapeTypes);
48   aMainLay->addWidget(myTypeCombo, 0, 1);
49   QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget);
50   aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
51   myListControl = new QTextEdit(myMainWidget);
52   myListControl->setReadOnly(true);
53   aMainLay->addWidget(myListControl, 2, 0, 2, -1);
54   aMainLay->setColumnStretch(1, 1);
55   myMainWidget->setLayout(aMainLay);
56
57   //TODO: Move into the base class
58   myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
59   //TODO_END
60   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
61   activateSelection(true);
62 }
63
64 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
65 {
66   activateSelection(false);
67 }
68
69 bool ModuleBase_WidgetMultiSelector::storeValue() const
70 {
71   // A rare case when plugin was not loaded. 
72   if(!myFeature)
73     return false;
74   DataPtr aData = myFeature->data();
75   AttributeStringPtr aStringAttr = aData->string(attributeID());
76   QString aWidgetValue = myListControl->toPlainText();
77   aStringAttr->setValue(aWidgetValue.toStdString());
78   updateObject(myFeature);
79   return true;
80 }
81
82 bool ModuleBase_WidgetMultiSelector::restoreValue()
83 {
84   return false;
85   // A rare case when plugin was not loaded. 
86   if(!myFeature)
87     return false;
88   DataPtr aData = myFeature->data();
89   AttributeStringPtr aStringAttr = aData->string(attributeID());
90
91   bool isBlocked = myListControl->blockSignals(true);
92   myListControl->setText(QString::fromStdString(aStringAttr->value()));
93   myListControl->blockSignals(isBlocked);
94
95   return true;
96 }
97
98 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
99 {
100   return myMainWidget;
101 }
102
103 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
104 {
105   QList<QWidget*> result;
106   result << myTypeCombo;
107   result << myListControl;
108   return result;
109 }
110
111 //********************************************************************
112 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
113 {
114   if (theObj == myListControl) {
115     if (theEvent->type() == QEvent::FocusIn)
116       activateSelection(true);
117   }
118   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
119 }
120
121 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
122 {
123   ModuleBase_ISelection* aSelection = myWorkshop->selection();
124   NCollection_List<TopoDS_Shape> aSelectedShapes, aFilteredShapes;
125   aSelection->selectedShapes(aSelectedShapes);
126   QString aText;
127   if (!aSelectedShapes.IsEmpty()) {
128     filterShapes(aSelectedShapes, aFilteredShapes);
129     aText = QString("Items selected: %1").arg(aFilteredShapes.Size());
130   }
131   myListControl->setText(aText);
132 }
133
134 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
135                                                   NCollection_List<TopoDS_Shape>& theResult)
136 {
137   if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
138     return;
139   TopAbs_ShapeEnum aReferenceType =
140       ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
141   NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
142   for (; anIter.More(); anIter.Next()) {
143     TopoDS_Shape aShape = anIter.Value();
144     if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
145       continue;
146     theResult.Append(aShape);
147   }
148 }
149
150 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
151 {
152   myIsActive = toActivate;
153   if (myIsActive) {
154     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
155     onSelectionTypeChanged();
156   } else {
157     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
158     myWorkshop->deactivateSubShapesSelection();
159   }
160 }
161
162 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
163 {
164   QString aNewType = myTypeCombo->currentText();
165   QIntList aList;
166   aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
167   myWorkshop->activateSubShapesSelection(aList);
168 }