1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetSketchCreator.cpp
4 // Created: 08 June 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_WidgetSketchCreator.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetSketchLabel.h"
10 #include "PartSet_PreviewPlanes.h"
12 #include <Config_Keywords.h>
14 #include <XGUI_ModuleConnector.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_Displayer.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <XGUI_OperationMgr.h>
19 #include <XGUI_PropertyPanel.h>
20 #include <XGUI_Tools.h>
21 #include <XGUI_ViewerProxy.h>
23 #include <GeomAPI_Face.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Events.h>
32 #include <SketchPlugin_SketchEntity.h>
33 #include <FeaturesPlugin_CompositeBoolean.h>
35 #include <ModuleBase_Tools.h>
36 #include <ModuleBase_Operation.h>
37 #include <ModuleBase_IPropertyPanel.h>
38 #include <ModuleBase_OperationFeature.h>
39 #include <Config_WidgetAPI.h>
41 #include <Events_Loop.h>
45 //#include <QFormLayout>
46 #include <QVBoxLayout>
47 #include <QMessageBox>
49 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
50 PartSet_Module* theModule,
51 const Config_WidgetAPI* theData)
52 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
55 myAttributeListID = theData->getProperty("attribute_list_id");
57 //QFormLayout* aLayout = new QFormLayout(this);
58 QVBoxLayout* aLayout = new QVBoxLayout(this);
59 ModuleBase_Tools::zeroMargins(aLayout);
61 ModuleBase_Tools::adjustMargins(aLayout);
63 QString aLabelText = QString::fromStdString(theData->widgetLabel());
64 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
66 myLabel = new QLabel(aLabelText, this);
67 myLabel->setWordWrap(true);
68 aLayout->addWidget(myLabel);
69 /*if (!aLabelIcon.isEmpty())
70 myLabel->setPixmap(QPixmap(aLabelIcon));
73 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
74 myTextLine = new QLineEdit(this);
75 myTextLine->setReadOnly(true);
76 myTextLine->setToolTip(aToolTip);
77 myTextLine->installEventFilter(this);
79 myLabel->setToolTip(aToolTip);
81 aLayout->addRow(myLabel, myTextLine);*/
83 std::string aTypes = theData->getProperty("shape_types");
84 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
86 myPreviewPlanes = new PartSet_PreviewPlanes();
89 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
93 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
95 QList<QWidget*> aControls;
96 aControls.append(myLabel);
100 bool PartSet_WidgetSketchCreator::restoreValueCustom()
102 /*CompositeFeaturePtr aCompFeature =
103 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
104 if (aCompFeature->numberOfSubs() > 0) {
105 FeaturePtr aSubFeature = aCompFeature->subFeature(0);
106 myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
111 bool PartSet_WidgetSketchCreator::storeValueCustom() const
116 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
118 // hide current widget, activate the next widget
119 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
120 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
121 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
122 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
123 if (theSelectionControl) { // hide other controls
125 aWidget->setVisible(false);
127 else { // hide current control
129 aWidget->setVisible(false);
131 aWidget->setVisible(true);
135 if (theSelectionControl) {
136 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
137 if (!aBodyIsVisualized) {
138 // We have to select a plane before any operation
139 myPreviewPlanes->showPreviewPlanes(myWorkshop);
142 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
143 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
145 aWorkshop->viewer()->update();
149 QIntList PartSet_WidgetSketchCreator::getShapeTypes() const
151 QIntList aShapeTypes;
152 foreach(QString aType, myShapeTypes) {
153 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
158 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
160 ModuleBase_ModelWidget::setEditingMode(isEditing);
162 setVisibleSelectionControl(false);
165 bool PartSet_WidgetSketchCreator::isSelectionMode() const
167 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
168 bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
170 return !aHasValueInList;
173 void PartSet_WidgetSketchCreator::onSelectionChanged()
175 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
177 if (!startSketchOperation(aSelected)) {
178 QList<ModuleBase_ViewerPrs>::const_iterator anIt = aSelected.begin(), aLast = aSelected.end();
179 bool aProcessed = false;
180 for (; anIt != aLast; anIt++) {
181 ModuleBase_ViewerPrs aValue = *anIt;
182 if (isValidInFilters(aValue))
183 aProcessed = setSelectionCustom(aValue) || aProcessed;
186 emit valuesChanged();
187 updateObject(myFeature);
188 setVisibleSelectionControl(false);
189 // manually deactivation because the widget was not activated as has no focus acceptin controls
191 emit focusOutWidget(this);
196 //********************************************************************
197 void PartSet_WidgetSketchCreator::setObject(ObjectPtr theSelectedObject,
198 GeomShapePtr theShape)
200 std::string anAttributeId = myAttributeListID;
201 DataPtr aData = myFeature->data();
202 AttributePtr anAttribute = aData->attribute(anAttributeId);
203 if (anAttribute.get()) {
204 std::string aType = anAttribute->attributeType();
205 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
206 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(anAttributeId);
207 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
208 if (!aSelectionListAttr->isInList(aResult, theShape, myIsInValidate))
209 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
214 bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrs>& theValues)
216 bool aSketchStarted = false;
218 if (theValues.size() != 1)
219 return aSketchStarted;
221 ModuleBase_ViewerPrs aValue = theValues.front();
222 if (!PartSet_WidgetSketchLabel::canFillSketch(aValue))
223 return aSketchStarted;
225 aSketchStarted = true;
227 // manually deactivation because the widget was not activated as has no focus acceptin controls
229 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
230 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
232 // Launch Sketch operation
233 CompositeFeaturePtr aCompFeature =
234 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
236 /// add sketch feature without current feature change.
237 /// it is important to do not change the current feature in order to
238 /// after sketch edition, the extrusion cut feature becomes current
239 SessionPtr aMgr = ModelAPI_Session::get();
240 DocumentPtr aDoc = aMgr->activeDocument();
241 FeaturePtr aPreviousCurrentFeature = aDoc->currentFeature(false);
242 FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
244 PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(aSketch, aValue);
246 aDoc->setCurrentFeature(aPreviousCurrentFeature, false);
248 // start edit operation for the sketch
249 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
250 (myModule->createOperation("Sketch"));
252 aFOperation->setFeature(aSketch);
253 myModule->sendOperation(aFOperation);
255 return aSketchStarted;
258 bool PartSet_WidgetSketchCreator::focusTo()
260 if (isSelectionMode()) {
261 setVisibleSelectionControl(true);
263 // we need to call activate here as the widget has no focus accepted controls
264 // if these controls are added here, activate will happens automatically after focusIn()
265 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
266 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
267 aPanel->activateWidget(this, false);
271 setVisibleSelectionControl(false);
273 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
274 SessionPtr aMgr = ModelAPI_Session::get();
275 // Open transaction that is general for the previous nested one: it will be closed on nested commit
276 bool aIsOp = aMgr->isOperation();
278 const static std::string aNestedOpID("Parameters modification");
279 aMgr->startOperation(aNestedOpID, true);
286 void PartSet_WidgetSketchCreator::deactivate()
288 ModuleBase_WidgetSelector::deactivate();
290 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
291 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
293 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
297 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
299 CompositeFeaturePtr aCompFeature =
300 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
301 CompositeFeaturePtr aSketchFeature =
302 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
303 if (aSketchFeature->numberOfSubs() == 0) {
304 // do nothing, selection control should be shown
307 //SessionPtr aMgr = ModelAPI_Session::get();
310 bool aIsOp = aMgr->isOperation();
312 const static std::string aNestedOpID("Parameters cancelation");
313 aMgr->startOperation(aNestedOpID, true);
318 // Update value in attribute selection list
319 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
320 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
321 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
322 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
323 if (aWidget->attributeID() == myAttributeListID)
324 aWidget->restoreValue();
327 // Hide sketcher result
328 std::list<ResultPtr> aResults = aSketchFeature->results();
329 std::list<ResultPtr>::const_iterator aIt;
330 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
331 (*aIt)->setDisplayed(false);
333 aSketchFeature->setDisplayed(false);
335 // restore value in the selection control
338 // Add Selected body were created the sketcher to list of selected objects
339 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::BOOLEAN_OBJECTS_ID();
340 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
341 if (aSelList.get()) {
342 DataPtr aData = aSketchFeature->data();
343 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
344 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
345 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
347 SessionPtr aMgr = ModelAPI_Session::get();
348 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
349 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
350 std::string aValidatorID, anError;
351 aSelList->append(aRes, GeomShapePtr());
352 if (aFactory->validate(anAttribute, aValidatorID, anError))
353 updateObject(aCompFeature);