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"
10 #include <XGUI_ModuleConnector.h>
11 #include <XGUI_Workshop.h>
12 #include <XGUI_Displayer.h>
13 #include <XGUI_SelectionMgr.h>
14 #include <XGUI_OperationMgr.h>
16 #include <GeomAPI_Face.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_ResultBody.h>
20 #include <ModelAPI_AttributeSelection.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_Validator.h>
24 #include <SketchPlugin_SketchEntity.h>
25 #include <FeaturesPlugin_CompositeBoolean.h>
27 #include <ModuleBase_Tools.h>
28 #include <ModuleBase_Operation.h>
29 #include <ModuleBase_IPropertyPanel.h>
30 #include <ModuleBase_OperationFeature.h>
31 #include <Config_WidgetAPI.h>
35 #include <QFormLayout>
36 #include <QMessageBox>
38 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
39 PartSet_Module* theModule,
40 const Config_WidgetAPI* theData,
41 const std::string& theParentId)
42 : ModuleBase_ModelWidget(theParent, theData, theParentId), myModule(theModule)
44 QFormLayout* aLayout = new QFormLayout(this);
45 ModuleBase_Tools::adjustMargins(aLayout);
47 QString aLabelText = QString::fromStdString(theData->widgetLabel());
48 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
49 myLabel = new QLabel(aLabelText, this);
50 if (!aLabelIcon.isEmpty())
51 myLabel->setPixmap(QPixmap(aLabelIcon));
54 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
55 myTextLine = new QLineEdit(this);
56 myTextLine->setReadOnly(true);
57 myTextLine->setToolTip(aToolTip);
58 myTextLine->installEventFilter(this);
60 aLayout->addRow(myLabel, myTextLine);
63 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
67 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
69 QList<QWidget*> aControls;
70 aControls.append(myTextLine);
74 bool PartSet_WidgetSketchCreator::restoreValueCustom()
76 CompositeFeaturePtr aCompFeature =
77 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
78 if (aCompFeature->numberOfSubs() > 0) {
79 FeaturePtr aSubFeature = aCompFeature->subFeature(0);
80 myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
85 bool PartSet_WidgetSketchCreator::storeValueCustom() const
90 void PartSet_WidgetSketchCreator::activateCustom()
92 CompositeFeaturePtr aCompFeature =
93 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
94 if (aCompFeature->numberOfSubs() == 0)
95 connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
98 void PartSet_WidgetSketchCreator::onStarted()
100 disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
102 // Check that model already has bodies
103 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
104 XGUI_Workshop* aWorkshop = aConnector->workshop();
105 XGUI_Displayer* aDisp = aWorkshop->displayer();
106 QObjectPtrList aObjList = aDisp->displayedObjects();
107 bool aHasBody = false;
109 foreach(ObjectPtr aObj, aObjList) {
110 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
111 if (aBody.get() != NULL) {
118 // Launch Sketch operation
119 CompositeFeaturePtr aCompFeature =
120 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
121 FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
123 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
124 (myModule->createOperation("Sketch"));
126 aFOperation->setFeature(aSketch);
127 myModule->sendOperation(aFOperation);
128 //connect(anOperation, SIGNAL(aborted()), aWorkshop->operationMgr(), SLOT(abortAllOperations()));
130 // Break current operation
131 std::string anOperationName = feature()->getKind();
132 QString aTitle = tr( anOperationName.c_str() );
133 QMessageBox::warning(this, aTitle,
134 tr("There are no bodies found. Operation aborted."), QMessageBox::Ok);
135 ModuleBase_Operation* aOp = myModule->workshop()->currentOperation();
140 bool PartSet_WidgetSketchCreator::focusTo()
142 CompositeFeaturePtr aCompFeature =
143 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
144 if (aCompFeature->numberOfSubs() == 0)
145 return ModuleBase_ModelWidget::focusTo();
147 connect(myModule, SIGNAL(operationResumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
148 SessionPtr aMgr = ModelAPI_Session::get();
149 // Open transaction that is general for the previous nested one: it will be closed on nested commit
150 bool aIsOp = aMgr->isOperation();
152 const static std::string aNestedOpID("Parameters modification");
153 aMgr->startOperation(aNestedOpID, true);
160 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
162 CompositeFeaturePtr aCompFeature =
163 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
164 CompositeFeaturePtr aSketchFeature =
165 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
166 if (aSketchFeature->numberOfSubs() == 0) {
168 SessionPtr aMgr = ModelAPI_Session::get();
171 bool aIsOp = aMgr->isOperation();
173 const static std::string aNestedOpID("Parameters cancelation");
174 aMgr->startOperation(aNestedOpID, true);
179 // Hide sketcher result
180 std::list<ResultPtr> aResults = aSketchFeature->results();
181 std::list<ResultPtr>::const_iterator aIt;
182 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
183 (*aIt)->setDisplayed(false);
185 aSketchFeature->setDisplayed(false);
187 // Add Selected body were created the sketcher to list of selected objects
188 DataPtr aData = aSketchFeature->data();
189 AttributeSelectionPtr aSelAttr =
190 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
191 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
192 if (aSelAttr.get()) {
193 ResultPtr aRes = aSelAttr->context();
194 GeomShapePtr aShape = aSelAttr->value();
196 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::BOOLEAN_OBJECTS_ID();
197 SessionPtr aMgr = ModelAPI_Session::get();
198 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
199 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
200 std::string aValidatorID, anError;
201 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
202 aSelList->append(aRes, GeomShapePtr());
203 if (aFactory->validate(anAttribute, aValidatorID, anError))
204 updateObject(aCompFeature);