Salome HOME
Issue #1330 correction for attribute ref attr list. Crash of rectangle creation
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "ModuleBase_IModule.h"
5 #include "ModuleBase_IViewer.h"
6 #include "ModuleBase_ViewerPrs.h"
7 #include "ModuleBase_Operation.h"
8 #include "ModuleBase_ISelection.h"
9 #include "ModuleBase_OperationDescription.h"
10 #include "ModuleBase_OperationFeature.h"
11 #include "ModuleBase_ModelWidget.h"
12
13 #include <Events_Loop.h>
14
15 #include <ModelAPI_Events.h>
16 #include <ModelAPI_CompositeFeature.h>
17 #include <ModelAPI_Session.h>
18 #include "ModelAPI_Tools.h"
19
20 #include <Config_PointerMessage.h>
21 #include <Config_WidgetReader.h>
22 #include <Config_ModuleReader.h>
23
24 #include <QAction>
25
26 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
27   : QObject(theParent), myWorkshop(theParent) 
28 {
29   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
30
31
32   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
33   //        SLOT(onMousePressed(QMouseEvent*)));
34   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
35   //        SLOT(onMouseReleased(QMouseEvent*)));
36   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
37   //        SLOT(onMouseMoved(QMouseEvent*)));
38   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
39   //        SLOT(onKeyRelease(QKeyEvent*)));
40   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
41   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
42 }
43
44 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
45 {
46   if (!myWorkshop->canStartOperation(theCmdId))
47     return;
48
49   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
50                                              (createOperation(theCmdId.toStdString()));
51   if (aFOperation) {
52     ModuleBase_ISelection* aSelection = myWorkshop->selection();
53     // Initialise operation with preliminary selection
54     aFOperation->initSelection(aSelection, myWorkshop->viewer());
55     sendOperation(aFOperation);
56   }
57 }
58
59
60 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
61 {
62   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
63   std::shared_ptr<Config_PointerMessage> aMessage =
64       std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
65   aMessage->setPointer(theOperation);
66   Events_Loop::loop()->send(aMessage);
67 }
68
69 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
70 {
71   return ModelAPI_Tools::getFeatureError(theFeature).c_str();
72 }
73
74 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
75                                              QStringList& theIds) const
76 {
77 }
78
79 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
80 {
81   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
82 }
83
84 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
85                                          const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
86                                          const bool theUpdateViewer)
87 {
88   return false;
89 }
90
91 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
92 {
93   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
94                                                           (getNewOperation(theFeatureId));
95   // If the operation is launched as sub-operation of another then we have to initialize
96   // parent feature
97   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
98                                                          (myWorkshop->currentOperation());
99   if (aCurOperation) {
100     FeaturePtr aFeature = aCurOperation->feature();
101     CompositeFeaturePtr aCompFeature =
102         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
103     if (aCompFeature) {
104       aFOperation->setParentFeature(aCompFeature);
105     }
106   }
107
108   std::string aXmlCfg, aDescription;
109   getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
110   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
111   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
112
113   return aFOperation;
114 }
115
116 void ModuleBase_IModule::createFeatures()
117 {
118   registerValidators();
119   registerFilters();
120   registerProperties();
121
122   Config_ModuleReader aXMLReader = Config_ModuleReader();
123   aXMLReader.readAll();
124   myFeaturesInFiles = aXMLReader.featuresInFiles();
125 }
126
127
128 void ModuleBase_IModule::actionCreated(QAction* theFeature)
129 {
130   theFeature->setStatusTip(theFeature->text());
131   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
132 }
133
134 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
135 {
136   return true;
137 }
138
139 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
140 {
141   return true;
142 }
143
144 bool ModuleBase_IModule::canUndo() const
145 {
146   SessionPtr aMgr = ModelAPI_Session::get();
147   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
148 }
149
150 bool ModuleBase_IModule::canRedo() const
151 {
152   SessionPtr aMgr = ModelAPI_Session::get();
153   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
154 }
155
156 void ModuleBase_IModule::onFeatureTriggered()
157 {
158   QAction* aCmd = dynamic_cast<QAction*>(sender());
159   //Do nothing on uncheck
160   if (aCmd->isCheckable() && !aCmd->isChecked()) {
161     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
162     if (myWorkshop->canStopOperation(anOperation))
163       myWorkshop->abortOperation(anOperation);
164     else {
165       aCmd->setChecked(true);
166     }
167   }
168   else {
169     launchOperation(aCmd->data().toString());
170     emit operationLaunched();
171   }
172 }
173
174 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
175 {
176   std::string aFeatureId = theFeature->getKind();
177   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
178     return;
179
180   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
181                                                          (createOperation(aFeatureId));
182   if (aFOperation) {
183     aFOperation->setFeature(theFeature);
184     sendOperation(aFOperation);
185   }
186 }
187
188 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
189 {
190   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
191                                                      (myWorkshop->currentOperation());
192   return !aFOperation || !aFOperation->hasObject(theObject);
193 }
194
195 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation) 
196 {
197   emit resumed(theOperation);
198 }
199
200 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
201                                               std::string& theXmlCfg, std::string& theDescription)
202 {
203   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
204   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
205   aWdgReader.readAll();
206
207   theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
208   theDescription = aWdgReader.featureDescription(theFeatureId);
209 }