Salome HOME
Make ModuleBase_ModelWidget::restoreValue() non-virtual and create virtual ModuleBase...
[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
23 #include <SketchPlugin_SketchEntity.h>
24 #include <FeaturesPlugin_CompositeBoolean.h>
25
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_Operation.h>
28 #include <ModuleBase_IPropertyPanel.h>
29 #include <Config_WidgetAPI.h>
30
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QFormLayout>
34 #include <QMessageBox>
35
36 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent, 
37                                                          PartSet_Module* theModule,
38                                                          const Config_WidgetAPI* theData,
39                                                          const std::string& theParentId)
40 : ModuleBase_ModelWidget(theParent, theData, theParentId), myModule(theModule)
41 {
42   QFormLayout* aLayout = new QFormLayout(this);
43   ModuleBase_Tools::adjustMargins(aLayout);
44
45   QString aLabelText = QString::fromStdString(theData->widgetLabel());
46   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
47   myLabel = new QLabel(aLabelText, this);
48   if (!aLabelIcon.isEmpty())
49     myLabel->setPixmap(QPixmap(aLabelIcon));
50
51
52   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
53   myTextLine = new QLineEdit(this);
54   myTextLine->setReadOnly(true);
55   myTextLine->setToolTip(aToolTip);
56   myTextLine->installEventFilter(this);
57
58   aLayout->addRow(myLabel, myTextLine);
59 }
60
61 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
62 {
63 }
64
65 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
66 {
67   QList<QWidget*> aControls;
68   aControls.append(myTextLine);
69   return aControls;
70 }
71
72 bool PartSet_WidgetSketchCreator::restoreValueCustom()
73 {
74   CompositeFeaturePtr aCompFeature = 
75     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
76   if (aCompFeature->numberOfSubs() > 0) {
77     FeaturePtr aSubFeature = aCompFeature->subFeature(0);
78     myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
79   }
80   return true;
81 }
82
83 bool PartSet_WidgetSketchCreator::storeValueCustom() const
84 {
85   return true;
86 }
87
88 void PartSet_WidgetSketchCreator::activateCustom()
89 {
90   CompositeFeaturePtr aCompFeature = 
91     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
92   if (aCompFeature->numberOfSubs() == 0)
93     connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
94 }
95
96 void PartSet_WidgetSketchCreator::onStarted()
97 {
98   disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
99
100   // Check that model already has bodies
101   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
102   XGUI_Workshop* aWorkshop = aConnector->workshop();
103   XGUI_Displayer* aDisp = aWorkshop->displayer();
104   QObjectPtrList aObjList = aDisp->displayedObjects();
105   bool aHasBody = false;
106   ResultBodyPtr aBody;
107   foreach(ObjectPtr aObj, aObjList) {
108     aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
109     if (aBody.get() != NULL) {
110       aHasBody = true;
111       break;
112     }
113   }
114
115   if (aHasBody) {
116     // Launch Sketch operation
117     CompositeFeaturePtr aCompFeature = 
118       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
119     FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
120
121     ModuleBase_Operation* anOperation = myModule->createOperation("Sketch");
122     anOperation->setFeature(aSketch);
123     myModule->sendOperation(anOperation);
124     //connect(anOperation, SIGNAL(aborted()), aWorkshop->operationMgr(), SLOT(abortAllOperations()));
125   } else {
126     // Break current operation
127     QMessageBox::warning(this, tr("Extrusion Cut"),
128         tr("There are no bodies found. Operation aborted."), QMessageBox::Ok);
129     ModuleBase_Operation* aOp = myModule->workshop()->currentOperation();
130     aOp->abort();
131   }
132 }
133
134 bool PartSet_WidgetSketchCreator::focusTo()
135 {
136   CompositeFeaturePtr aCompFeature = 
137     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
138   if (aCompFeature->numberOfSubs() == 0)
139     return ModuleBase_ModelWidget::focusTo(); 
140
141   connect(myModule, SIGNAL(operationResumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
142   SessionPtr aMgr = ModelAPI_Session::get();
143   // Open transaction that is general for the previous nested one: it will be closed on nested commit
144   bool aIsOp = aMgr->isOperation();
145   if (!aIsOp) {
146     const static std::string aNestedOpID("Parameters modification");
147     aMgr->startOperation(aNestedOpID, true);
148   }
149
150   restoreValue();
151   return false;
152 }
153
154 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
155 {
156   CompositeFeaturePtr aCompFeature = 
157     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
158   CompositeFeaturePtr aSketchFeature = 
159     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
160   if (aSketchFeature->numberOfSubs() == 0) {
161     // Abort operation
162     SessionPtr aMgr = ModelAPI_Session::get();
163     // Close transaction
164     /*
165     bool aIsOp = aMgr->isOperation();
166     if (aIsOp) {
167       const static std::string aNestedOpID("Parameters cancelation");
168       aMgr->startOperation(aNestedOpID, true);
169     }
170     */
171     theOp->abort();
172   } else {
173     // Hide sketcher result
174     std::list<ResultPtr> aResults = aSketchFeature->results();
175     std::list<ResultPtr>::const_iterator aIt;
176     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
177       (*aIt)->setDisplayed(false);
178     }
179     aSketchFeature->setDisplayed(false);
180
181     // Add Selected body were created the sketcher to list of selected objects
182     DataPtr aData = aSketchFeature->data();
183     AttributeSelectionPtr aSelAttr = 
184       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
185       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
186     if (aSelAttr.get()) {
187       ResultPtr aRes = aSelAttr->context();
188       GeomShapePtr aShape = aSelAttr->value();
189       if (aRes.get()) {
190         AttributeSelectionListPtr aSelList = 
191           aCompFeature->data()->selectionList(FeaturesPlugin_CompositeBoolean::BOOLEAN_OBJECTS_ID());
192         aSelList->append(aRes, GeomShapePtr());
193         updateObject(aCompFeature);
194       }
195     }
196   }
197 }