]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSketchCreator.cpp
Salome HOME
Value state is provided in ModelWidget to remove 'myIsCurrentValueUnderModification...
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchCreator.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetSketchCreator.cpp
4 // Created:     08 June 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_WidgetSketchCreator.h"
8 #include "PartSet_Module.h"
9
10 #include <Config_Keywords.h>
11
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>
17
18 #include <GeomAPI_Face.h>
19
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>
25
26 #include <SketchPlugin_SketchEntity.h>
27 #include <FeaturesPlugin_CompositeBoolean.h>
28
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>
34
35 #include <QLabel>
36 #include <QLineEdit>
37 #include <QFormLayout>
38 #include <QMessageBox>
39
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)
45 {
46   QFormLayout* aLayout = new QFormLayout(this);
47   ModuleBase_Tools::adjustMargins(aLayout);
48
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));
54
55
56   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
57   myTextLine = new QLineEdit(this);
58   myTextLine->setReadOnly(true);
59   myTextLine->setToolTip(aToolTip);
60   myTextLine->installEventFilter(this);
61
62   myLabel->setToolTip(aToolTip);
63
64   QString aUseBody = QString::fromStdString(theData->getProperty(USE_BODY));
65   if(!aUseBody.isEmpty()) {
66     myUseBody = QVariant(aUseBody).toBool();
67   }
68
69   aLayout->addRow(myLabel, myTextLine);
70 }
71
72 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
73 {
74 }
75
76 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
77 {
78   QList<QWidget*> aControls;
79   aControls.append(myTextLine);
80   return aControls;
81 }
82
83 bool PartSet_WidgetSketchCreator::restoreValueCustom()
84 {
85   CompositeFeaturePtr aCompFeature = 
86     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
87   if (aCompFeature->numberOfSubs() > 0) {
88     FeaturePtr aSubFeature = aCompFeature->subFeature(0);
89     myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
90   }
91   return true;
92 }
93
94 bool PartSet_WidgetSketchCreator::storeValueCustom() const
95 {
96   return true;
97 }
98
99 void PartSet_WidgetSketchCreator::activateCustom()
100 {
101   CompositeFeaturePtr aCompFeature = 
102     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
103   if (aCompFeature->numberOfSubs() == 0)
104     connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
105 }
106
107 void PartSet_WidgetSketchCreator::onStarted()
108 {
109   disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
110
111   // Check that model already has bodies
112   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
113   XGUI_Workshop* aWorkshop = aConnector->workshop();
114   XGUI_Displayer* aDisp = aWorkshop->displayer();
115   QObjectPtrList aObjList = aDisp->displayedObjects();
116   bool aHasBody = !myUseBody;
117   ResultBodyPtr aBody;
118   if(!aHasBody) {
119     foreach(ObjectPtr aObj, aObjList) {
120       aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
121       if (aBody.get() != NULL) {
122         aHasBody = true;
123         break;
124       }
125     }
126   }
127
128   if (aHasBody) {
129     // Launch Sketch operation
130     CompositeFeaturePtr aCompFeature = 
131       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
132
133     /// add sketch feature without current feature change.
134     /// it is important to do not change the current feature in order to
135     /// after sketch edition, the extrusion cut feature becomes current
136     SessionPtr aMgr = ModelAPI_Session::get();
137     DocumentPtr aDoc = aMgr->activeDocument();
138     FeaturePtr aPreviousCurrentFeature = aDoc->currentFeature(false);
139     FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
140     aDoc->setCurrentFeature(aPreviousCurrentFeature, false);
141
142     // start edit operation for the sketch
143     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
144                                                              (myModule->createOperation("Sketch"));
145     if (aFOperation)
146       aFOperation->setFeature(aSketch);
147     myModule->sendOperation(aFOperation);
148     //connect(anOperation, SIGNAL(aborted()), aWorkshop->operationMgr(), SLOT(abortAllOperations()));
149   } else {
150     // Break current operation
151     std::string anOperationName = feature()->getKind();
152     QString aTitle = tr( anOperationName.c_str() );
153     QMessageBox::warning(this, aTitle,
154         tr("There are no bodies found. Operation aborted."), QMessageBox::Ok);
155     ModuleBase_Operation* aOp = myModule->workshop()->currentOperation();
156     aOp->abort();
157   }
158 }
159
160 bool PartSet_WidgetSketchCreator::focusTo()
161 {
162   CompositeFeaturePtr aCompFeature = 
163     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
164   if (aCompFeature->numberOfSubs() == 0)
165     return ModuleBase_ModelWidget::focusTo(); 
166
167   connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
168   SessionPtr aMgr = ModelAPI_Session::get();
169   // Open transaction that is general for the previous nested one: it will be closed on nested commit
170   bool aIsOp = aMgr->isOperation();
171   if (!aIsOp) {
172     const static std::string aNestedOpID("Parameters modification");
173     aMgr->startOperation(aNestedOpID, true);
174   }
175
176   restoreValue();
177   return false;
178 }
179
180 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
181 {
182   CompositeFeaturePtr aCompFeature = 
183     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
184   CompositeFeaturePtr aSketchFeature = 
185     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
186   if (aSketchFeature->numberOfSubs() == 0) {
187     // Abort operation
188     SessionPtr aMgr = ModelAPI_Session::get();
189     // Close transaction
190     /*
191     bool aIsOp = aMgr->isOperation();
192     if (aIsOp) {
193       const static std::string aNestedOpID("Parameters cancelation");
194       aMgr->startOperation(aNestedOpID, true);
195     }
196     */
197     theOp->abort();
198   } else {
199     // Hide sketcher result
200     std::list<ResultPtr> aResults = aSketchFeature->results();
201     std::list<ResultPtr>::const_iterator aIt;
202     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
203       (*aIt)->setDisplayed(false);
204     }
205     aSketchFeature->setDisplayed(false);
206
207     if(myUseBody) {
208       // Add Selected body were created the sketcher to list of selected objects
209       DataPtr aData = aSketchFeature->data();
210       AttributeSelectionPtr aSelAttr = 
211         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
212         (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
213       if (aSelAttr.get()) {
214         ResultPtr aRes = aSelAttr->context();
215         GeomShapePtr aShape = aSelAttr->value();
216         if (aRes.get()) {
217           std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::BOOLEAN_OBJECTS_ID();
218           SessionPtr aMgr = ModelAPI_Session::get();
219           ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
220           AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
221           std::string aValidatorID, anError;
222           AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
223           aSelList->append(aRes, GeomShapePtr());
224           if (aFactory->validate(anAttribute, aValidatorID, anError))
225             updateObject(aCompFeature);
226           else
227             aSelList->clear();
228         }
229       }
230     }
231   }
232 }