Salome HOME
3659165565a476f415ec5eb1cf8a1c8aec3fe461
[modules/yacs.git] / src / salomewrap / SalomeWrap_DataModel.cxx
1 // Copyright (C) 2006-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SalomeWrap_DataModel.hxx"
21
22 #include <SalomeApp_DataObject.h>
23 #include <SalomeApp_Study.h>
24 #include <SalomeApp_Module.h>
25 #include <SalomeApp_Application.h>
26 #include <LightApp_SelectionMgr.h>
27 #include <SALOME_ListIO.hxx>
28 #include <SUIT_DataBrowser.h>
29 #include <SUIT_ViewManager.h>
30 #include <QxScene_ViewWindow.h>
31
32 #include <QFileInfo>
33
34 //#define _DEVDEBUG_
35 #include "YacsTrace.hxx"
36
37 using namespace std;
38
39 SalomeWrap_DataModel::SalomeWrap_DataModel(CAM_Module* theModule)
40   : SalomeApp_DataModel(theModule)
41 {
42   DEBTRACE("SalomeWrap_DataModel::SalomeWrap_DataModel");
43   _viewEntryMap.clear();
44   _entryViewMap.clear();
45 }
46
47 SalomeWrap_DataModel::~SalomeWrap_DataModel()
48 {
49   DEBTRACE("SalomeWrap_DataModel::~SalomeWrap_DataModel");
50 }
51
52 void SalomeWrap_DataModel::createNewSchema(const QString& schemaName,
53                                            QWidget* viewWindow)
54 {
55   DEBTRACE("SalomeWrap_DataModel::createNewSchema : " << schemaName.toStdString());
56   SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
57   if (!aRoot)
58     {
59       DEBTRACE("SalomeApp_ModuleObject* aRoot =0");
60       return;
61     }
62   DEBTRACE("aRoot OK");
63   _PTR(SComponent)         aSComp(aRoot->object());
64   _PTR(Study)              aStudy = getStudy()->studyDS();
65   _PTR(StudyBuilder)       aBuilder(aStudy->NewBuilder());
66   _PTR(GenericAttribute)   anAttr;
67   _PTR(AttributeName)      aName;
68   _PTR(AttributePixMap)    aPixmap;
69   _PTR(AttributeParameter) aType;
70
71   // --- create a new schema SObject
72
73   _PTR(SObject) aSObj;
74   aSObj = aBuilder->NewObject(aSComp);
75
76   anAttr =  aBuilder->FindOrCreateAttribute(aSObj, "AttributeName");
77   aName = _PTR(AttributeName)(anAttr);
78   aName->SetValue(QFileInfo(schemaName).baseName().toStdString());
79
80   anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributePixMap");
81   aPixmap = _PTR(AttributePixMap)(anAttr);
82   aPixmap->SetPixMap("schema.png");
83
84   anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeParameter");
85   aType = _PTR(AttributeParameter)(anAttr);
86   aType->SetInt("ObjectType", SchemaObject);
87   string filePath = schemaName.toStdString();
88   aType->SetString("FilePath", filePath.c_str());
89   DEBTRACE("DataModel : FilePath = " << filePath);
90
91   _viewEntryMap[viewWindow] = aSObj->GetID();
92   DEBTRACE(viewWindow);
93   _entryViewMap[aSObj->GetID()] = viewWindow;
94   DEBTRACE("--- " << viewWindow << " "<< aSObj->GetID());
95   SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
96   if (mod) mod->updateObjBrowser();
97
98   QxScene_ViewWindow *swv = dynamic_cast<QxScene_ViewWindow*>(viewWindow);
99   if (!swv) return;
100   QString tabName = QFileInfo(schemaName).baseName();
101   swv->getViewManager()->setTitle(tabName);
102 }
103
104 bool SalomeWrap_DataModel::renameSchema(const QString& oldName,
105                                         const QString& newName,
106                                         QWidget* viewWindow)
107 {
108   DEBTRACE("SalomeWrap_DataModel::renameSchema " << oldName.toStdString()
109            << " " << newName.toStdString());
110   SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
111   if (!aRoot) return false;
112   if (!_viewEntryMap.count(viewWindow)) return false;
113
114   _PTR(SComponent)         aSComp(aRoot->object());
115   _PTR(Study)              aStudy = getStudy()->studyDS();
116
117   string id = _viewEntryMap[viewWindow];
118   _PTR(SObject) aSObj = aStudy->FindObjectID(id);
119
120   _PTR(StudyBuilder)       aBuilder(aStudy->NewBuilder());
121   _PTR(GenericAttribute)   anAttr;
122   _PTR(AttributeName)      aName;
123   _PTR(AttributeParameter) aType;
124
125   anAttr =  aBuilder->FindOrCreateAttribute(aSObj, "AttributeName");
126   aName = _PTR(AttributeName)(anAttr);
127   aName->SetValue(QFileInfo(newName).baseName().toStdString());
128
129   anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeParameter");
130   aType = _PTR(AttributeParameter)(anAttr);
131   aType->SetInt("ObjectType", SchemaObject);
132   string filePath = newName.toStdString();
133   aType->SetString("FilePath", filePath.c_str());
134   DEBTRACE("DataModel : FilePath = " << filePath);
135
136   SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
137   if (mod) mod->updateObjBrowser();
138
139   QxScene_ViewWindow *swv = dynamic_cast<QxScene_ViewWindow*>(viewWindow);
140   QString tabName = QFileInfo(newName).baseName();
141   if (swv) swv->getViewManager()->setTitle(tabName);
142   return true;
143 }
144
145 bool SalomeWrap_DataModel::deleteSchema(QWidget* viewWindow)
146 {
147   DEBTRACE("SalomeWrap_DataModel::deleteSchema");
148   SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
149   if (!aRoot) return false;
150   if (!_viewEntryMap.count(viewWindow)) return false;
151
152   _PTR(SComponent)         aSComp(aRoot->object());
153   _PTR(Study)              aStudy = getStudy()->studyDS();
154
155   string id = _viewEntryMap[viewWindow];
156   _PTR(SObject) aSObj = aStudy->FindObjectID(id);
157
158   _PTR(StudyBuilder)       aBuilder(aStudy->NewBuilder());
159   aBuilder->RemoveObject(aSObj);
160
161   SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
162   if (mod) mod->updateObjBrowser();
163   return true;
164 }
165
166 void SalomeWrap_DataModel::createNewRun(const QString& schemaName,
167                                         const QString& runName,
168                                         QWidget* refWindow,
169                                         QWidget* viewWindow)
170 {
171   DEBTRACE("SalomeWrap_DataModel::createNewRun");
172   SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
173   if (!aRoot) return;
174   DEBTRACE(refWindow);
175   if (!_viewEntryMap.count(refWindow)) return;
176   DEBTRACE("---");
177   
178   _PTR(SComponent)         aSComp(aRoot->object());
179   _PTR(Study)              aStudy = getStudy()->studyDS();
180
181   _PTR(StudyBuilder)       aBuilder(aStudy->NewBuilder());
182   _PTR(GenericAttribute)   anAttr;
183   _PTR(AttributeName)      aName;
184   _PTR(AttributePixMap)    aPixmap;
185   _PTR(AttributeParameter) aType;
186
187   string id = _viewEntryMap[refWindow];
188   _PTR(SObject) aRefObj = aStudy->FindObjectID(id);
189
190   // --- create a new schema SObject
191
192   _PTR(SObject) aSObj;
193   aSObj = aBuilder->NewObject(aRefObj);
194
195   anAttr =  aBuilder->FindOrCreateAttribute(aSObj, "AttributeName");
196   aName = _PTR(AttributeName)(anAttr);
197   aName->SetValue(QFileInfo(runName).baseName().toStdString());
198
199   anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributePixMap");
200   aPixmap = _PTR(AttributePixMap)(anAttr);
201   aPixmap->SetPixMap("schema.png");
202
203   anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeParameter");
204   aType = _PTR(AttributeParameter)(anAttr);
205   aType->SetInt("ObjectType", SchemaObject);
206   string filePath = runName.toStdString();
207   aType->SetString("FilePath", filePath.c_str());
208   DEBTRACE("DataModel : FilePath = " << filePath);
209
210   _viewEntryMap[viewWindow] = aSObj->GetID();
211   _entryViewMap[aSObj->GetID()] = viewWindow;
212   DEBTRACE("--- " << viewWindow << " "<< aSObj->GetID());
213   SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
214   if (mod) mod->updateObjBrowser();
215
216   QxScene_ViewWindow *swv = dynamic_cast<QxScene_ViewWindow*>(viewWindow);
217   if (!swv) return;
218
219   int count = 0;
220   if (_runCountMap.count(schemaName.toStdString()))
221     count = ++_runCountMap[schemaName.toStdString()];
222   else
223     _runCountMap[schemaName.toStdString()] = count;
224
225   QString tabName = QFileInfo(schemaName).baseName() +QString("_run%1").arg(count);
226   swv->getViewManager()->setTitle(tabName);
227
228   QPixmap pixmap;
229   pixmap.load("icons:run_active.png");
230   swv->getViewManager()->setIcon(pixmap);
231 }
232
233 void SalomeWrap_DataModel::setSelected(QWidget* viewWindow)
234 {
235   DEBTRACE("SalomeWrap_DataModel::setSelected");
236   if (!_viewEntryMap.count(viewWindow)) return;
237   string entry = _viewEntryMap[viewWindow];
238   LightApp_SelectionMgr* selMgr = getModule()->getApp()->selectionMgr();
239   SALOME_ListIO ioList;
240   ioList.Append( new SALOME_InteractiveObject( entry.c_str(), "", "" ) );
241   selMgr->setSelectedObjects( ioList, false );
242 }
243
244 SUIT_DataObject* SalomeWrap_DataModel::getDataObject(std::string entry)
245 {
246   DEBTRACE("SalomeWrap_DataModel::getDataObject " << entry);
247   QString refEntry = entry.c_str();
248   SUIT_DataObject* sdo = 0;
249   SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
250   if (!aRoot) return 0;
251
252   QList<SUIT_DataObject*> objList = root()->children(true);
253   for (int i = 0; i < objList.size(); ++i)
254     {
255       SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>(objList.at(i));
256       if (obj && (!QString::compare(refEntry, obj->entry())))
257         {
258           sdo = obj;
259           break;
260         }
261     }
262   return sdo;
263 }
264
265 QWidget* SalomeWrap_DataModel::getViewWindow(std::string entry)
266 {
267   if (! _entryViewMap.count(entry)) return 0;
268   return _entryViewMap[entry];
269 }