1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetShapeSelector.h>
22 #include <ModuleBase_Definitions.h>
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_Filter.h>
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_ViewerPrs.h>
30 #include <ModuleBase_IconFactory.h>
32 #include <Config_Translator.h>
33 #include <Config_WidgetAPI.h>
34 #include <Events_Loop.h>
35 #include <Events_Message.h>
36 #include <GeomAPI_Interface.h>
37 #include <GeomAPI_Shape.h>
39 #include <ModelAPI_AttributeReference.h>
40 #include <ModelAPI_Data.h>
41 #include <ModelAPI_Document.h>
42 #include <ModelAPI_Events.h>
43 #include <ModelAPI_Feature.h>
44 #include <ModelAPI_Result.h>
45 #include <ModelAPI_ResultConstruction.h>
46 #include <ModelAPI_AttributeReference.h>
47 #include <ModelAPI_AttributeSelection.h>
48 #include <ModelAPI_Session.h>
49 #include <ModelAPI_Tools.h>
50 #include <ModelAPI_ResultBody.h>
51 #include <ModelAPI_AttributeRefAttr.h>
52 #include <ModelAPI_Validator.h>
53 #include <ModelAPI_AttributeValidator.h>
55 #include <Config_WidgetAPI.h>
57 #include <GeomAPI_Shape.h>
59 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <TopExp_Explorer.hxx>
67 #include <QToolButton>
70 #include <QDockWidget>
71 #include <QApplication>
72 #include <QFormLayout>
74 #include <TopExp_Explorer.hxx>
75 #include <TopoDS_Shape.hxx>
82 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
83 ModuleBase_IWorkshop* theWorkshop,
84 const Config_WidgetAPI* theData)
85 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
87 QFormLayout* aLayout = new QFormLayout(this);
88 ModuleBase_Tools::adjustMargins(aLayout);
90 QString aLabelText = translate(theData->widgetLabel());
91 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
92 myLabel = new QLabel(aLabelText, this);
93 if (!aLabelIcon.isEmpty())
94 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
97 QString aToolTip = translate(theData->widgetTooltip());
98 myTextLine = new QLineEdit(this);
99 QString anObjName = QString::fromStdString(attributeID());
100 myTextLine->setObjectName(anObjName);
101 myTextLine->setReadOnly(true);
102 myTextLine->setToolTip(aToolTip);
103 myTextLine->installEventFilter(this);
105 aLayout->addRow(myLabel, myTextLine);
106 myLabel->setToolTip(aToolTip);
108 std::string aTypes = theData->getProperty("shape_types");
109 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
112 //********************************************************************
113 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
117 //********************************************************************
118 bool ModuleBase_WidgetShapeSelector::storeValueCustom()
120 // the value is stored on the selection changed signal processing
124 //********************************************************************
125 bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
126 const bool theToValidate)
128 if (theValues.empty()) {
129 // In order to make reselection possible, set empty object and shape should be done
130 setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
131 ObjectPtr(), GeomShapePtr(), NULL)));
134 // it removes the processed value from the parameters list
135 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
138 if (!theToValidate || isValidInFilters(aValue)) {
139 isDone = setSelectionCustom(aValue);
140 // updateObject - to update/redisplay feature
141 // it is commented in order to perfom it outside the method
142 //updateObject(myFeature);
144 //emit valuesChanged();
149 //********************************************************************
150 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
152 QList<ModuleBase_ViewerPrsPtr> aSelected;
154 DataPtr aData = myFeature->data();
155 AttributePtr anAttribute = myFeature->attribute(attributeID());
157 ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
158 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
159 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
160 aSelected.append(aPrs);
165 //********************************************************************
166 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
168 bool isBlocked = this->blockSignals(true);
169 updateSelectionName();
170 this->blockSignals(isBlocked);
175 //********************************************************************
176 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
178 QList<QWidget*> aControls;
179 aControls.append(myTextLine);
183 void ModuleBase_WidgetShapeSelector::updateFocus()
185 emit focusOutWidget(this);
188 //********************************************************************
189 QIntList ModuleBase_WidgetShapeSelector::shapeTypes() const
191 QIntList aShapeTypes;
192 foreach(QString aType, myShapeTypes) {
193 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
198 //********************************************************************
199 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
202 DataPtr aData = myFeature->data();
203 if (aData->isValid()) {
204 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
205 aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
210 //********************************************************************
211 void ModuleBase_WidgetShapeSelector::updateSelectionName()
213 DataPtr aData = myFeature->data();
214 if (!aData->isValid())
217 bool isNameUpdated = false;
218 AttributeSelectionPtr aSelect = aData->selection(attributeID());
220 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
221 isNameUpdated = true;
223 if (!isNameUpdated) {
224 ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
225 if (anObject.get() != NULL) {
226 std::string aName = anObject->data()->name();
227 myTextLine->setText(QString::fromStdString(aName));
229 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
230 if (aRefAttr && aRefAttr->attr().get() != NULL) {
231 //myIsObject = aRefAttr->isObject();
232 std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
233 myTextLine->setText(QString::fromStdString(anAttrName));
236 myTextLine->setText(getDefaultValue().c_str());