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 <Config_Keywords.h>
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
15 #include <XGUI_SelectionMgr.h>
16 #include <XGUI_OperationMgr.h>
18 #include <GeomAPI_Face.h>
20 #include <ModelAPI_Session.h>
21 #include <ModelAPI_ResultBody.h>
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Validator.h>
26 #include <SketchPlugin_SketchEntity.h>
27 #include <FeaturesPlugin_CompositeBoolean.h>
29 #include <ModuleBase_Tools.h>
30 #include <ModuleBase_Operation.h>
31 #include <ModuleBase_IPropertyPanel.h>
32 #include <ModuleBase_OperationFeature.h>
33 #include <Config_WidgetAPI.h>
37 #include <QFormLayout>
38 #include <QMessageBox>
40 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
41 PartSet_Module* theModule,
42 const Config_WidgetAPI* theData,
43 const std::string& theParentId)
44 : ModuleBase_ModelWidget(theParent, theData, theParentId), myModule(theModule), myUseBody(true)
46 QFormLayout* aLayout = new QFormLayout(this);
47 ModuleBase_Tools::adjustMargins(aLayout);
49 QString aLabelText = QString::fromStdString(theData->widgetLabel());
50 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
51 myLabel = new QLabel(aLabelText, this);
52 if (!aLabelIcon.isEmpty())
53 myLabel->setPixmap(QPixmap(aLabelIcon));
56 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
57 myTextLine = new QLineEdit(this);
58 myTextLine->setReadOnly(true);
59 myTextLine->setToolTip(aToolTip);
60 myTextLine->installEventFilter(this);
62 QString aUseBody = QString::fromStdString(theData->getProperty(USE_BODY));
63 if(!aUseBody.isEmpty()) {
64 myUseBody = QVariant(aUseBody).toBool();
67 aLayout->addRow(myLabel, myTextLine);
70 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
74 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
76 QList<QWidget*> aControls;
77 aControls.append(myTextLine);
81 bool PartSet_WidgetSketchCreator::restoreValueCustom()
83 CompositeFeaturePtr aCompFeature =
84 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
85 if (aCompFeature->numberOfSubs() > 0) {
86 FeaturePtr aSubFeature = aCompFeature->subFeature(0);
87 myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
92 bool PartSet_WidgetSketchCreator::storeValueCustom() const
97 void PartSet_WidgetSketchCreator::activateCustom()
99 CompositeFeaturePtr aCompFeature =
100 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
101 if (aCompFeature->numberOfSubs() == 0)
102 connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
105 void PartSet_WidgetSketchCreator::onStarted()
107 disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
109 // Check that model already has bodies
110 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
111 XGUI_Workshop* aWorkshop = aConnector->workshop();
112 XGUI_Displayer* aDisp = aWorkshop->displayer();
113 QObjectPtrList aObjList = aDisp->displayedObjects();
114 bool aHasBody = !myUseBody;
117 foreach(ObjectPtr aObj, aObjList) {
118 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
119 if (aBody.get() != NULL) {
127 // Launch Sketch operation
128 CompositeFeaturePtr aCompFeature =
129 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
130 FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
132 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
133 (myModule->createOperation("Sketch"));
135 aFOperation->setFeature(aSketch);
136 myModule->sendOperation(aFOperation);
137 //connect(anOperation, SIGNAL(aborted()), aWorkshop->operationMgr(), SLOT(abortAllOperations()));
139 // Break current operation
140 std::string anOperationName = feature()->getKind();
141 QString aTitle = tr( anOperationName.c_str() );
142 QMessageBox::warning(this, aTitle,
143 tr("There are no bodies found. Operation aborted."), QMessageBox::Ok);
144 ModuleBase_Operation* aOp = myModule->workshop()->currentOperation();
149 bool PartSet_WidgetSketchCreator::focusTo()
151 CompositeFeaturePtr aCompFeature =
152 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
153 if (aCompFeature->numberOfSubs() == 0)
154 return ModuleBase_ModelWidget::focusTo();
156 connect(myModule, SIGNAL(operationResumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
157 SessionPtr aMgr = ModelAPI_Session::get();
158 // Open transaction that is general for the previous nested one: it will be closed on nested commit
159 bool aIsOp = aMgr->isOperation();
161 const static std::string aNestedOpID("Parameters modification");
162 aMgr->startOperation(aNestedOpID, true);
169 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
171 CompositeFeaturePtr aCompFeature =
172 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
173 CompositeFeaturePtr aSketchFeature =
174 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
175 if (aSketchFeature->numberOfSubs() == 0) {
177 SessionPtr aMgr = ModelAPI_Session::get();
180 bool aIsOp = aMgr->isOperation();
182 const static std::string aNestedOpID("Parameters cancelation");
183 aMgr->startOperation(aNestedOpID, true);
188 // Hide sketcher result
189 std::list<ResultPtr> aResults = aSketchFeature->results();
190 std::list<ResultPtr>::const_iterator aIt;
191 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
192 (*aIt)->setDisplayed(false);
194 aSketchFeature->setDisplayed(false);
197 // Add Selected body were created the sketcher to list of selected objects
198 DataPtr aData = aSketchFeature->data();
199 AttributeSelectionPtr aSelAttr =
200 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
201 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
202 if (aSelAttr.get()) {
203 ResultPtr aRes = aSelAttr->context();
204 GeomShapePtr aShape = aSelAttr->value();
206 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::BOOLEAN_OBJECTS_ID();
207 SessionPtr aMgr = ModelAPI_Session::get();
208 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
209 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
210 std::string aValidatorID, anError;
211 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
212 aSelList->append(aRes, GeomShapePtr());
213 if (aFactory->validate(anAttribute, aValidatorID, anError))
214 updateObject(aCompFeature);