1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetShapeSelector.h>
21 #include <ModuleBase_Definitions.h>
22 #include <ModuleBase_ISelection.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_IViewer.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_Filter.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_ViewerPrs.h>
29 #include <ModuleBase_IconFactory.h>
31 #include <Config_Translator.h>
32 #include <Config_WidgetAPI.h>
33 #include <Events_Loop.h>
34 #include <Events_Message.h>
35 #include <GeomAPI_Interface.h>
36 #include <GeomAPI_Shape.h>
38 #include <ModelAPI_AttributeReference.h>
39 #include <ModelAPI_Data.h>
40 #include <ModelAPI_Document.h>
41 #include <ModelAPI_Events.h>
42 #include <ModelAPI_Feature.h>
43 #include <ModelAPI_Result.h>
44 #include <ModelAPI_ResultConstruction.h>
45 #include <ModelAPI_AttributeReference.h>
46 #include <ModelAPI_AttributeSelection.h>
47 #include <ModelAPI_Session.h>
48 #include <ModelAPI_Tools.h>
49 #include <ModelAPI_ResultBody.h>
50 #include <ModelAPI_AttributeRefAttr.h>
51 #include <ModelAPI_Validator.h>
52 #include <ModelAPI_AttributeValidator.h>
54 #include <Config_WidgetAPI.h>
56 #include <GeomAPI_Shape.h>
58 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <TopExp_Explorer.hxx>
66 #include <QToolButton>
69 #include <QDockWidget>
70 #include <QApplication>
71 #include <QFormLayout>
73 #include <TopExp_Explorer.hxx>
74 #include <TopoDS_Shape.hxx>
81 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
82 ModuleBase_IWorkshop* theWorkshop,
83 const Config_WidgetAPI* theData)
84 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
86 QFormLayout* aLayout = new QFormLayout(this);
87 ModuleBase_Tools::adjustMargins(aLayout);
89 QString aLabelText = translate(theData->widgetLabel());
90 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
91 myLabel = new QLabel(aLabelText, this);
92 if (!aLabelIcon.isEmpty())
93 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
96 QString aToolTip = translate(theData->widgetTooltip());
97 myTextLine = new QLineEdit(this);
98 QString anObjName = QString::fromStdString(attributeID());
99 myTextLine->setObjectName(anObjName);
100 myTextLine->setReadOnly(true);
101 myTextLine->setToolTip(aToolTip);
102 myTextLine->installEventFilter(this);
104 aLayout->addRow(myLabel, myTextLine);
105 myLabel->setToolTip(aToolTip);
107 std::string aTypes = theData->getProperty("shape_types");
108 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
111 //********************************************************************
112 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
116 //********************************************************************
117 bool ModuleBase_WidgetShapeSelector::storeValueCustom()
119 // the value is stored on the selection changed signal processing
123 //********************************************************************
124 bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
125 const bool theToValidate)
127 if (theValues.empty()) {
128 // In order to make reselection possible, set empty object and shape should be done
129 setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
130 ObjectPtr(), GeomShapePtr(), NULL)));
133 // it removes the processed value from the parameters list
134 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
137 if (!theToValidate || isValidInFilters(aValue)) {
138 isDone = setSelectionCustom(aValue);
139 // updateObject - to update/redisplay feature
140 // it is commented in order to perfom it outside the method
141 //updateObject(myFeature);
143 //emit valuesChanged();
148 //********************************************************************
149 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
151 QList<ModuleBase_ViewerPrsPtr> aSelected;
153 DataPtr aData = myFeature->data();
154 AttributePtr anAttribute = myFeature->attribute(attributeID());
156 ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
157 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
158 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
159 aSelected.append(aPrs);
164 //********************************************************************
165 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
167 bool isBlocked = this->blockSignals(true);
168 updateSelectionName();
169 this->blockSignals(isBlocked);
174 //********************************************************************
175 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
177 QList<QWidget*> aControls;
178 aControls.append(myTextLine);
182 void ModuleBase_WidgetShapeSelector::updateFocus()
184 emit focusOutWidget(this);
187 //********************************************************************
188 QIntList ModuleBase_WidgetShapeSelector::shapeTypes() const
190 QIntList aShapeTypes;
191 foreach(QString aType, myShapeTypes) {
192 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
197 //********************************************************************
198 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
201 DataPtr aData = myFeature->data();
202 if (aData->isValid()) {
203 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
204 aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
209 //********************************************************************
210 void ModuleBase_WidgetShapeSelector::updateSelectionName()
212 DataPtr aData = myFeature->data();
213 if (!aData->isValid())
216 bool isNameUpdated = false;
217 AttributeSelectionPtr aSelect = aData->selection(attributeID());
219 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
220 isNameUpdated = true;
222 if (!isNameUpdated) {
223 ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
224 if (anObject.get() != NULL) {
225 std::string aName = anObject->data()->name();
226 myTextLine->setText(QString::fromStdString(aName));
228 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
229 if (aRefAttr && aRefAttr->attr().get() != NULL) {
230 //myIsObject = aRefAttr->isObject();
231 std::string anAttrName = ModuleBase_Tools::generateName(aRefAttr->attr(), myWorkshop);
232 myTextLine->setText(QString::fromStdString(anAttrName));
235 myTextLine->setText(getDefaultValue().c_str());