]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSketchCreator.cpp
Salome HOME
Cancel ExtrusionCut if there are no bodies
[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
15 #include <GeomAPI_Face.h>
16
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_ResultBody.h>
19
20 #include <ModuleBase_Tools.h>
21 #include <ModuleBase_Operation.h>
22 #include <ModuleBase_IPropertyPanel.h>
23 #include <Config_WidgetAPI.h>
24
25 #include <QLabel>
26 #include <QLineEdit>
27 #include <QFormLayout>
28 #include <QMessageBox>
29
30 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent, 
31                                                          PartSet_Module* theModule,
32                                                          const Config_WidgetAPI* theData,
33                                                          const std::string& theParentId)
34 : ModuleBase_ModelWidget(theParent, theData, theParentId), myModule(theModule)
35 {
36   QFormLayout* aLayout = new QFormLayout(this);
37   ModuleBase_Tools::adjustMargins(aLayout);
38
39   QString aLabelText = QString::fromStdString(theData->widgetLabel());
40   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
41   myLabel = new QLabel(aLabelText, this);
42   if (!aLabelIcon.isEmpty())
43     myLabel->setPixmap(QPixmap(aLabelIcon));
44
45
46   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
47   myTextLine = new QLineEdit(this);
48   myTextLine->setReadOnly(true);
49   myTextLine->setToolTip(aToolTip);
50   myTextLine->installEventFilter(this);
51
52   aLayout->addRow(myLabel, myTextLine);
53 }
54
55 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
56 {
57 }
58
59 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
60 {
61   QList<QWidget*> aControls;
62   aControls.append(myTextLine);
63   return aControls;
64 }
65
66 bool PartSet_WidgetSketchCreator::restoreValue()
67 {
68   CompositeFeaturePtr aCompFeature = 
69     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
70   if (aCompFeature->numberOfSubs() > 0) {
71     FeaturePtr aSubFeature = aCompFeature->subFeature(0);
72     myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
73   }
74   return true;
75 }
76
77 bool PartSet_WidgetSketchCreator::storeValueCustom() const
78 {
79   return true;
80 }
81
82 void PartSet_WidgetSketchCreator::activateCustom()
83 {
84   CompositeFeaturePtr aCompFeature = 
85     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
86   if (aCompFeature->numberOfSubs() == 0)
87     connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
88 }
89
90 void PartSet_WidgetSketchCreator::onStarted()
91 {
92   disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
93
94   // Check that model already has bodies
95   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
96   XGUI_Workshop* aWorkshop = aConnector->workshop();
97   XGUI_Displayer* aDisp = aWorkshop->displayer();
98   QObjectPtrList aObjList = aDisp->displayedObjects();
99   bool aHasBody = false;
100   ResultBodyPtr aBody;
101   foreach(ObjectPtr aObj, aObjList) {
102     aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
103     if (aBody.get() != NULL) {
104       aHasBody = true;
105       break;
106     }
107   }
108
109   if (aHasBody) {
110     // Launch Sketch operation
111     CompositeFeaturePtr aCompFeature = 
112       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
113     FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
114
115     ModuleBase_Operation* anOperation = myModule->createOperation("Sketch");
116     anOperation->setFeature(aSketch);
117     myModule->sendOperation(anOperation);
118   } else {
119     // Break current operation
120     QMessageBox::warning(this, tr("Extrusion Cut"),
121         tr("There are no bodies found. Operation aborted."), QMessageBox::Ok);
122     ModuleBase_Operation* aOp = myModule->workshop()->currentOperation();
123     aOp->abort();
124   }
125 }
126
127 bool PartSet_WidgetSketchCreator::focusTo()
128 {
129   CompositeFeaturePtr aCompFeature = 
130     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
131   if (aCompFeature->numberOfSubs() == 0)
132     return ModuleBase_ModelWidget::focusTo(); 
133
134   SessionPtr aMgr = ModelAPI_Session::get();
135   bool aIsOp = aMgr->isOperation();
136   // Open transaction if it was closed before
137   if (!aIsOp)
138     aMgr->startOperation();
139
140   restoreValue();
141   return false;
142 }