1 // Copyright (C) 2006-2015 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SalomeWrap_DataModel.hxx"
22 #include <SalomeApp_DataObject.h>
23 #include <SalomeApp_Study.h>
24 #include <SalomeApp_Module.h>
25 #include <SalomeApp_Application.h>
26 #include <SUIT_DataBrowser.h>
27 #include <SUIT_ViewManager.h>
28 #include <QxScene_ViewWindow.h>
33 #include "YacsTrace.hxx"
37 SalomeWrap_DataModel::SalomeWrap_DataModel(CAM_Module* theModule)
38 : SalomeApp_DataModel(theModule)
40 DEBTRACE("SalomeWrap_DataModel::SalomeWrap_DataModel");
41 _viewEntryMap.clear();
42 _entryViewMap.clear();
45 SalomeWrap_DataModel::~SalomeWrap_DataModel()
47 DEBTRACE("SalomeWrap_DataModel::~SalomeWrap_DataModel");
50 void SalomeWrap_DataModel::createNewSchema(const QString& schemaName,
53 DEBTRACE("SalomeWrap_DataModel::createNewSchema : " << schemaName.toStdString());
54 SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
57 DEBTRACE("SalomeApp_ModuleObject* aRoot =0");
61 _PTR(SComponent) aSComp(aRoot->object());
62 _PTR(Study) aStudy = getStudy()->studyDS();
63 _PTR(StudyBuilder) aBuilder(aStudy->NewBuilder());
64 _PTR(GenericAttribute) anAttr;
65 _PTR(AttributeName) aName;
66 _PTR(AttributePixMap) aPixmap;
67 _PTR(AttributeParameter) aType;
69 // --- create a new schema SObject
72 aSObj = aBuilder->NewObject(aSComp);
74 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeName");
75 aName = _PTR(AttributeName)(anAttr);
76 aName->SetValue(QFileInfo(schemaName).baseName().toStdString());
78 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributePixMap");
79 aPixmap = _PTR(AttributePixMap)(anAttr);
80 aPixmap->SetPixMap("schema.png");
82 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeParameter");
83 aType = _PTR(AttributeParameter)(anAttr);
84 aType->SetInt("ObjectType", SchemaObject);
85 string filePath = schemaName.toStdString();
86 aType->SetString("FilePath", filePath.c_str());
87 DEBTRACE("DataModel : FilePath = " << filePath);
89 _viewEntryMap[viewWindow] = aSObj->GetID();
91 _entryViewMap[aSObj->GetID()] = viewWindow;
92 DEBTRACE("--- " << viewWindow << " "<< aSObj->GetID());
93 SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
94 if (mod) mod->updateObjBrowser();
96 QxScene_ViewWindow *swv = dynamic_cast<QxScene_ViewWindow*>(viewWindow);
98 QString tabName = QFileInfo(schemaName).baseName();
99 swv->getViewManager()->setTitle(tabName);
102 bool SalomeWrap_DataModel::renameSchema(const QString& oldName,
103 const QString& newName,
106 DEBTRACE("SalomeWrap_DataModel::renameSchema " << oldName.toStdString()
107 << " " << newName.toStdString());
108 SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
109 if (!aRoot) return false;
110 if (!_viewEntryMap.count(viewWindow)) return false;
112 _PTR(SComponent) aSComp(aRoot->object());
113 _PTR(Study) aStudy = getStudy()->studyDS();
115 string id = _viewEntryMap[viewWindow];
116 _PTR(SObject) aSObj = aStudy->FindObjectID(id);
118 _PTR(StudyBuilder) aBuilder(aStudy->NewBuilder());
119 _PTR(GenericAttribute) anAttr;
120 _PTR(AttributeName) aName;
121 _PTR(AttributeParameter) aType;
123 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeName");
124 aName = _PTR(AttributeName)(anAttr);
125 aName->SetValue(QFileInfo(newName).baseName().toStdString());
127 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeParameter");
128 aType = _PTR(AttributeParameter)(anAttr);
129 aType->SetInt("ObjectType", SchemaObject);
130 string filePath = newName.toStdString();
131 aType->SetString("FilePath", filePath.c_str());
132 DEBTRACE("DataModel : FilePath = " << filePath);
134 SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
135 if (mod) mod->updateObjBrowser();
137 QxScene_ViewWindow *swv = dynamic_cast<QxScene_ViewWindow*>(viewWindow);
138 QString tabName = QFileInfo(newName).baseName();
139 if (swv) swv->getViewManager()->setTitle(tabName);
143 bool SalomeWrap_DataModel::deleteSchema(QWidget* viewWindow)
145 DEBTRACE("SalomeWrap_DataModel::deleteSchema");
146 SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
147 if (!aRoot) return false;
148 if (!_viewEntryMap.count(viewWindow)) return false;
150 _PTR(SComponent) aSComp(aRoot->object());
151 _PTR(Study) aStudy = getStudy()->studyDS();
153 string id = _viewEntryMap[viewWindow];
154 _PTR(SObject) aSObj = aStudy->FindObjectID(id);
156 _PTR(StudyBuilder) aBuilder(aStudy->NewBuilder());
157 aBuilder->RemoveObject(aSObj);
159 SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
160 if (mod) mod->updateObjBrowser();
164 void SalomeWrap_DataModel::createNewRun(const QString& schemaName,
165 const QString& runName,
169 DEBTRACE("SalomeWrap_DataModel::createNewRun");
170 SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
173 if (!_viewEntryMap.count(refWindow)) return;
176 _PTR(SComponent) aSComp(aRoot->object());
177 _PTR(Study) aStudy = getStudy()->studyDS();
179 _PTR(StudyBuilder) aBuilder(aStudy->NewBuilder());
180 _PTR(GenericAttribute) anAttr;
181 _PTR(AttributeName) aName;
182 _PTR(AttributePixMap) aPixmap;
183 _PTR(AttributeParameter) aType;
185 string id = _viewEntryMap[refWindow];
186 _PTR(SObject) aRefObj = aStudy->FindObjectID(id);
188 // --- create a new schema SObject
191 aSObj = aBuilder->NewObject(aRefObj);
193 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeName");
194 aName = _PTR(AttributeName)(anAttr);
195 aName->SetValue(QFileInfo(runName).baseName().toStdString());
197 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributePixMap");
198 aPixmap = _PTR(AttributePixMap)(anAttr);
199 aPixmap->SetPixMap("schema.png");
201 anAttr = aBuilder->FindOrCreateAttribute(aSObj, "AttributeParameter");
202 aType = _PTR(AttributeParameter)(anAttr);
203 aType->SetInt("ObjectType", SchemaObject);
204 string filePath = runName.toStdString();
205 aType->SetString("FilePath", filePath.c_str());
206 DEBTRACE("DataModel : FilePath = " << filePath);
208 _viewEntryMap[viewWindow] = aSObj->GetID();
209 _entryViewMap[aSObj->GetID()] = viewWindow;
210 DEBTRACE("--- " << viewWindow << " "<< aSObj->GetID());
211 SalomeApp_Module *mod = dynamic_cast<SalomeApp_Module*>(module());
212 if (mod) mod->updateObjBrowser();
214 QxScene_ViewWindow *swv = dynamic_cast<QxScene_ViewWindow*>(viewWindow);
218 if (_runCountMap.count(schemaName.toStdString()))
219 count = ++_runCountMap[schemaName.toStdString()];
221 _runCountMap[schemaName.toStdString()] = count;
223 QString tabName = QFileInfo(schemaName).baseName() +QString("_run%1").arg(count);
224 swv->getViewManager()->setTitle(tabName);
227 pixmap.load("icons:run_active.png");
228 swv->getViewManager()->setIcon(pixmap);
231 void SalomeWrap_DataModel::setSelected(QWidget* viewWindow)
233 DEBTRACE("SalomeWrap_DataModel::setSelected");
234 if (!_viewEntryMap.count(viewWindow)) return;
235 string entry = _viewEntryMap[viewWindow];
236 SUIT_DataObject* item = getDataObject(entry);
237 if(item) getModule()->getApp()->objectBrowser()->setSelected(item);
240 SUIT_DataObject* SalomeWrap_DataModel::getDataObject(std::string entry)
242 DEBTRACE("SalomeWrap_DataModel::getDataObject " << entry);
243 QString refEntry = entry.c_str();
244 SUIT_DataObject* sdo = 0;
245 SalomeApp_ModuleObject* aRoot = dynamic_cast<SalomeApp_ModuleObject*>(root());
246 if (!aRoot) return 0;
248 QList<SUIT_DataObject*> objList = root()->children(true);
249 for (int i = 0; i < objList.size(); ++i)
251 SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>(objList.at(i));
252 if (obj && (!QString::compare(refEntry, obj->entry())))
261 QWidget* SalomeWrap_DataModel::getViewWindow(std::string entry)
263 if (! _entryViewMap.count(entry)) return 0;
264 return _entryViewMap[entry];