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