Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetShapeSelector.cpp
4 // Created:     2 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include <ModuleBase_WidgetShapeSelector.h>
8 #include <ModuleBase_Definitions.h>
9 #include <ModuleBase_ISelection.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_Tools.h>
13 #include <ModuleBase_FilterFactory.h>
14 #include <ModuleBase_Filter.h>
15 #include <ModuleBase_IModule.h>
16 #include <ModuleBase_ViewerPrs.h>
17 #include <ModuleBase_IconFactory.h>
18
19 #include <Config_WidgetAPI.h>
20 #include <Events_Loop.h>
21 #include <Events_Message.h>
22 #include <GeomAPI_Interface.h>
23 #include <GeomAPI_Shape.h>
24
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_AttributeReference.h>
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_AttributeValidator.h>
40
41 #include <Config_WidgetAPI.h>
42
43 #include <GeomAPI_Shape.h>
44
45 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopExp_Explorer.hxx>
48
49 #include <QWidget>
50 #include <QLayout>
51 #include <QLabel>
52 #include <QLineEdit>
53 #include <QToolButton>
54 #include <QString>
55 #include <QEvent>
56 #include <QDockWidget>
57 #include <QApplication>
58 #include <QFormLayout>
59
60 #include <TopExp_Explorer.hxx>
61 #include <TopoDS_Shape.hxx>
62
63 #include <memory>
64
65 #include <list>
66 #include <string>
67
68 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
69                                                      ModuleBase_IWorkshop* theWorkshop,
70                                                      const Config_WidgetAPI* theData)
71 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
72 {
73   QFormLayout* aLayout = new QFormLayout(this);
74   ModuleBase_Tools::adjustMargins(aLayout);
75
76   QString aLabelText = QString::fromStdString(theData->widgetLabel());
77   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
78   myLabel = new QLabel(aLabelText, this);
79   if (!aLabelIcon.isEmpty())
80     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
81
82
83   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
84   myTextLine = new QLineEdit(this);
85   QString anObjName = QString::fromStdString(attributeID());
86   myTextLine->setObjectName(anObjName);
87   myTextLine->setReadOnly(true);
88   myTextLine->setToolTip(aToolTip);
89   myTextLine->installEventFilter(this);
90
91   aLayout->addRow(myLabel, myTextLine);
92   myLabel->setToolTip(aToolTip);
93
94   std::string aTypes = theData->getProperty("shape_types");
95   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
96 }
97
98 //********************************************************************
99 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
100 {
101 }
102
103 //********************************************************************
104 bool ModuleBase_WidgetShapeSelector::storeValueCustom()
105 {
106   // the value is stored on the selection changed signal processing
107   return true;
108 }
109
110 //********************************************************************
111 bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
112                                                   const bool theToValidate)
113 {
114   if (theValues.empty()) {
115     // In order to make reselection possible, set empty object and shape should be done
116     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
117                                                   ObjectPtr(), GeomShapePtr(), NULL)));
118     return false;
119   }
120   // it removes the processed value from the parameters list
121   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
122   bool isDone = false;
123
124   if (!theToValidate || isValidInFilters(aValue)) {
125     isDone = setSelectionCustom(aValue);
126     // updateObject - to update/redisplay feature
127     // it is commented in order to perfom it outside the method
128     //updateObject(myFeature);
129     // to storeValue()
130     //emit valuesChanged();
131   }
132   return isDone;
133 }
134
135 //********************************************************************
136 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
137 {
138   QList<ModuleBase_ViewerPrsPtr> aSelected;
139   if(myFeature) {
140     DataPtr aData = myFeature->data();
141     AttributePtr anAttribute = myFeature->attribute(attributeID());
142
143     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
144     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
145     ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
146     aSelected.append(aPrs);
147   }
148   return aSelected;
149 }
150
151 //********************************************************************
152 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
153 {
154   bool isBlocked = this->blockSignals(true);
155   updateSelectionName();
156   this->blockSignals(isBlocked);
157
158   return true;
159 }
160
161 //********************************************************************
162 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
163 {
164   QList<QWidget*> aControls;
165   aControls.append(myTextLine);
166   return aControls;
167 }
168
169 void ModuleBase_WidgetShapeSelector::updateFocus()
170 {
171   emit focusOutWidget(this);
172 }
173
174 //********************************************************************
175 QIntList ModuleBase_WidgetShapeSelector::shapeTypes() const
176 {
177   QIntList aShapeTypes;
178   foreach(QString aType, myShapeTypes) {
179     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
180   }
181   return aShapeTypes;
182 }
183
184 //********************************************************************
185 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
186 {
187   GeomShapePtr aShape;
188   DataPtr aData = myFeature->data();
189   if (aData->isValid()) {
190     AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
191     aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
192   }
193   return aShape;
194 }
195
196 //********************************************************************
197 void ModuleBase_WidgetShapeSelector::updateSelectionName()
198 {
199   DataPtr aData = myFeature->data();
200   if (!aData->isValid())
201     return;
202
203   bool isNameUpdated = false;
204   AttributeSelectionPtr aSelect = aData->selection(attributeID());
205   if (aSelect) {
206     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
207     isNameUpdated = true;
208   }
209   if (!isNameUpdated) {
210     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
211     if (anObject.get() != NULL) {
212       std::string aName = anObject->data()->name();
213       myTextLine->setText(QString::fromStdString(aName));
214     } else {
215       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
216       if (aRefAttr && aRefAttr->attr().get() != NULL) {
217         //myIsObject = aRefAttr->isObject();
218         std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
219         myTextLine->setText(QString::fromStdString(anAttrName));
220       }
221       else {
222         myTextLine->setText(getDefaultValue().c_str());
223       }
224     }
225   }
226 }