Salome HOME
Roll back the modification, not yet approved
[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   QString aUseBody = QString::fromStdString(theData->getProperty(USE_BODY));
63   if(!aUseBody.isEmpty()) {
64     myUseBody = QVariant(aUseBody).toBool();
65   }
66
67   aLayout->addRow(myLabel, myTextLine);
68 }
69
70 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
71 {
72 }
73
74 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
75 {
76   QList<QWidget*> aControls;
77   aControls.append(myTextLine);
78   return aControls;
79 }
80
81 bool PartSet_WidgetSketchCreator::restoreValueCustom()
82 {
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()));
88   }
89   return true;
90 }
91
92 bool PartSet_WidgetSketchCreator::storeValueCustom() const
93 {
94   return true;
95 }
96
97 void PartSet_WidgetSketchCreator::activateCustom()
98 {
99   CompositeFeaturePtr aCompFeature = 
100     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
101   if (aCompFeature->numberOfSubs() == 0)
102     connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
103 }
104
105 void PartSet_WidgetSketchCreator::onStarted()
106 {
107   disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
108
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;
115   ResultBodyPtr aBody;
116   if(!aHasBody) {
117     foreach(ObjectPtr aObj, aObjList) {
118       aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
119       if (aBody.get() != NULL) {
120         aHasBody = true;
121         break;
122       }
123     }
124   }
125
126   if (aHasBody) {
127     // Launch Sketch operation
128     CompositeFeaturePtr aCompFeature = 
129       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
130     FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
131
132     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
133                                                              (myModule->createOperation("Sketch"));
134     if (aFOperation)
135       aFOperation->setFeature(aSketch);
136     myModule->sendOperation(aFOperation);
137     //connect(anOperation, SIGNAL(aborted()), aWorkshop->operationMgr(), SLOT(abortAllOperations()));
138   } else {
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();
145     aOp->abort();
146   }
147 }
148
149 bool PartSet_WidgetSketchCreator::focusTo()
150 {
151   CompositeFeaturePtr aCompFeature = 
152     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
153   if (aCompFeature->numberOfSubs() == 0)
154     return ModuleBase_ModelWidget::focusTo(); 
155
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();
160   if (!aIsOp) {
161     const static std::string aNestedOpID("Parameters modification");
162     aMgr->startOperation(aNestedOpID, true);
163   }
164
165   restoreValue();
166   return false;
167 }
168
169 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
170 {
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) {
176     // Abort operation
177     SessionPtr aMgr = ModelAPI_Session::get();
178     // Close transaction
179     /*
180     bool aIsOp = aMgr->isOperation();
181     if (aIsOp) {
182       const static std::string aNestedOpID("Parameters cancelation");
183       aMgr->startOperation(aNestedOpID, true);
184     }
185     */
186     theOp->abort();
187   } else {
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);
193     }
194     aSketchFeature->setDisplayed(false);
195
196     if(myUseBody) {
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();
205         if (aRes.get()) {
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);
215           else
216             aSelList->clear();
217         }
218       }
219     }
220   }
221 }