Salome HOME
Fix for the issue #910: never fully remove the results, just make them disabled when...
[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 <XGUI_ModuleConnector.h>
11 #include <XGUI_Workshop.h>
12 #include <XGUI_Displayer.h>
13 #include <XGUI_SelectionMgr.h>
14 #include <XGUI_OperationMgr.h>
15
16 #include <GeomAPI_Face.h>
17
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>
23
24 #include <SketchPlugin_SketchEntity.h>
25 #include <FeaturesPlugin_CompositeBoolean.h>
26
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>
32
33 #include <QLabel>
34 #include <QLineEdit>
35 #include <QFormLayout>
36 #include <QMessageBox>
37
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)
43 {
44   QFormLayout* aLayout = new QFormLayout(this);
45   ModuleBase_Tools::adjustMargins(aLayout);
46
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));
52
53
54   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
55   myTextLine = new QLineEdit(this);
56   myTextLine->setReadOnly(true);
57   myTextLine->setToolTip(aToolTip);
58   myTextLine->installEventFilter(this);
59
60   aLayout->addRow(myLabel, myTextLine);
61 }
62
63 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
64 {
65 }
66
67 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
68 {
69   QList<QWidget*> aControls;
70   aControls.append(myTextLine);
71   return aControls;
72 }
73
74 bool PartSet_WidgetSketchCreator::restoreValueCustom()
75 {
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()));
81   }
82   return true;
83 }
84
85 bool PartSet_WidgetSketchCreator::storeValueCustom() const
86 {
87   return true;
88 }
89
90 void PartSet_WidgetSketchCreator::activateCustom()
91 {
92   CompositeFeaturePtr aCompFeature = 
93     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
94   if (aCompFeature->numberOfSubs() == 0)
95     connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
96 }
97
98 void PartSet_WidgetSketchCreator::onStarted()
99 {
100   disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
101
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;
108   ResultBodyPtr aBody;
109   foreach(ObjectPtr aObj, aObjList) {
110     aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
111     if (aBody.get() != NULL) {
112       aHasBody = true;
113       break;
114     }
115   }
116
117   if (aHasBody) {
118     // Launch Sketch operation
119     CompositeFeaturePtr aCompFeature = 
120       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
121     FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
122
123     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
124                                                              (myModule->createOperation("Sketch"));
125     if (aFOperation)
126       aFOperation->setFeature(aSketch);
127     myModule->sendOperation(aFOperation);
128     //connect(anOperation, SIGNAL(aborted()), aWorkshop->operationMgr(), SLOT(abortAllOperations()));
129   } else {
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();
136     aOp->abort();
137   }
138 }
139
140 bool PartSet_WidgetSketchCreator::focusTo()
141 {
142   CompositeFeaturePtr aCompFeature = 
143     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
144   if (aCompFeature->numberOfSubs() == 0)
145     return ModuleBase_ModelWidget::focusTo(); 
146
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();
151   if (!aIsOp) {
152     const static std::string aNestedOpID("Parameters modification");
153     aMgr->startOperation(aNestedOpID, true);
154   }
155
156   restoreValue();
157   return false;
158 }
159
160 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
161 {
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) {
167     // Abort operation
168     SessionPtr aMgr = ModelAPI_Session::get();
169     // Close transaction
170     /*
171     bool aIsOp = aMgr->isOperation();
172     if (aIsOp) {
173       const static std::string aNestedOpID("Parameters cancelation");
174       aMgr->startOperation(aNestedOpID, true);
175     }
176     */
177     theOp->abort();
178   } else {
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);
184     }
185     aSketchFeature->setDisplayed(false);
186
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();
195       if (aRes.get()) {
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);
205         else
206           aSelList->clear();
207       }
208     }
209   }
210 }