1 // Copyright (C) 2006-2008 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.
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
19 #include "RuntimeSALOME.hxx"
20 #include "CatalogWidget.hxx"
22 #include "Catalog.hxx"
23 #include "TypeCode.hxx"
24 #include "ComponentDefinition.hxx"
25 #include "ItemMimeData.hxx"
37 #include "YacsTrace.hxx"
40 using namespace YACS::ENGINE;
41 using namespace YACS::HMI;
43 CatalogWidget::CatalogWidget(QWidget *parent,
44 YACS::ENGINE::Catalog* builtinCatalog,
45 YACS::ENGINE::Catalog* sessionCatalog)
48 DEBTRACE("CatalogWidget::CatalogWidget");
49 _builtinCatalog = builtinCatalog;
50 _sessionCatalog = sessionCatalog;
54 _typeToCataMap.clear();
58 addCatalog(_builtinCatalog, "Built In");
59 addCatalog(_sessionCatalog, "Current Session");
61 setDragDropMode(QAbstractItemView::DragOnly);
63 setDropIndicatorShown(true);
66 bool CatalogWidget::addCatalogFromFile(std::string fileName)
68 DEBTRACE("CatalogWidget::addCatalogFromFile " << fileName);
69 Catalog *cataProc = YACS::ENGINE::getSALOMERuntime()->loadCatalog("proc", fileName);
70 QFileInfo afi(fileName.c_str());
71 string aFile = afi.fileName().toStdString();
72 addCatalog(cataProc, aFile);
75 std::map<std::string, YACS::ENGINE::Catalog*> CatalogWidget::getCataMap()
80 YACS::ENGINE::Catalog* CatalogWidget::getCatalog(std::string cataName)
82 YACS::ENGINE::Catalog* catalog = 0;
83 if (_cataMap.count(cataName))
84 catalog = _cataMap[cataName];
88 YACS::ENGINE::Catalog* CatalogWidget::getCatalogFromType(std::string typeName)
90 DEBTRACE("CatalogWidget::getCatalogFromType " << typeName);
91 YACS::ENGINE::Catalog* catalog = 0;
92 if (_typeToCataMap.count(typeName))
93 catalog = _typeToCataMap[typeName];
97 void CatalogWidget::addCatalog(YACS::ENGINE::Catalog* catalog,
100 if (!catalog) return;
102 QTreeWidgetItem *itemCata = 0;
103 QTreeWidgetItem *category = 0;
105 itemCata = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString(name.c_str())));
106 insertTopLevelItem(_idCatalog, itemCata);
109 if (! catalog->_typeMap.empty())
111 category = new QTreeWidgetItem(itemCata, QStringList(QString("Types")));
112 map<string, TypeCode*>::const_iterator it = catalog->_typeMap.begin();
113 for (; it != catalog->_typeMap.end(); ++it)
115 DEBTRACE("Type : " <<(*it).first
116 << " " << (*it).second->getKindRepr()
117 << " " << (*it).second->name()
118 << " " << (*it).second->shortName()
119 << " " << (*it).second->id() );
120 string typeName = it->first;
121 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString(typeName.c_str())));
122 if (! _typeToCataMap.count(typeName))
123 _typeToCataMap[typeName] = catalog;
124 else if ( ! ((*it).second)->isEquivalent(_typeToCataMap[typeName]->_typeMap[typeName]) )
126 DEBTRACE(" ========================================================================================================");
127 DEBTRACE(" type " << typeName << " not compatible with one of same name already present in another catalog, FORCE NEW!");
128 DEBTRACE(" ========================================================================================================");
129 _typeToCataMap[typeName] = catalog;
130 item->setForeground(0,Qt::blue);
135 if (! catalog->_componentMap.empty())
137 category = new QTreeWidgetItem(itemCata, QStringList(QString("Components")));
138 map<string, ComponentDefinition*>::const_iterator it = catalog->_componentMap.begin();
139 for (; it != catalog->_componentMap.end(); ++it)
141 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
142 map<string, ServiceNode *>::const_iterator its = (it->second)->_serviceMap.begin();
143 for (; its != (it->second)->_serviceMap.end(); ++its)
144 QTreeWidgetItem *sitem = new QTreeWidgetItem(item, QStringList(QString((its->first).c_str())));
148 if (! catalog->_nodeMap.empty())
150 category = new QTreeWidgetItem(itemCata, QStringList(QString("Elementary Nodes")));
151 map<string, Node*>::const_iterator it = catalog->_nodeMap.begin();
152 for (; it != catalog->_nodeMap.end(); ++it)
153 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
156 if (! catalog->_composednodeMap.empty())
158 category = new QTreeWidgetItem(itemCata, QStringList(QString("Composed Nodes")));
159 map<string, ComposedNode*>::const_iterator it = catalog->_composednodeMap.begin();
160 for (; it != catalog->_composednodeMap.end(); ++it)
161 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
163 _cataMap[name] = catalog;
166 CatalogWidget::~CatalogWidget()
170 void CatalogWidget::startDrag(Qt::DropActions supportedActions)
172 DEBTRACE("startDrag " << supportedActions);
173 QTreeWidgetItem *item = currentItem();
175 QTreeWidgetItem *parent = item->parent();
177 QTreeWidgetItem *grandPa = parent->parent();
178 if (!grandPa) return;
179 QTreeWidgetItem *grandGrandPa = grandPa->parent();
182 cataName = grandGrandPa->text(0).toStdString();
184 cataName = grandPa->text(0).toStdString();
186 DEBTRACE("cataName=" << cataName);
187 assert(_cataMap.count(cataName));
188 YACS::ENGINE::Catalog *catalog = _cataMap[cataName];
192 string definition = "";
194 if (! parent->text(0).compare("Types"))
197 definition = item->text(0).toStdString();
198 pixmap.load("icons:data_link.png");
200 else if (parent->text(0).contains("Nodes"))
203 definition = item->text(0).toStdString();
204 pixmap.load("icons:add_node.png");
206 else if (! grandPa->text(0).compare("Components"))
208 mimeInfo = "Service";
209 definition = item->text(0).toStdString();
210 compo = parent->text(0).toStdString();
211 pixmap.load("icons:new_salome_service_node.png");
215 mimeInfo = "Component";
216 compo = item->text(0).toStdString();
217 pixmap.load("icons:component.png");
219 DEBTRACE("mimeInfo=" << mimeInfo.toStdString() << " definition=" << definition << " compo=" << compo);
220 QString mimeType = "yacs/cata" + mimeInfo;
222 QDrag *drag = new QDrag(this);
223 ItemMimeData *mime = new ItemMimeData;
224 drag->setMimeData(mime);
225 mime->setData(mimeType, mimeInfo.toAscii());
226 mime->setCatalog(catalog);
227 mime->setCataName(cataName);
228 mime->setCompo(compo);
229 mime->setType(definition);
230 drag->setPixmap(pixmap);
232 drag->exec(supportedActions);