1 // Copyright (C) 2006-2014 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 "RuntimeSALOME.hxx"
21 #include "CatalogWidget.hxx"
23 #include "Catalog.hxx"
24 #include "TypeCode.hxx"
25 #include "ComponentDefinition.hxx"
26 #include "ItemMimeData.hxx"
27 #include "QtGuiContext.hxx"
29 #include <QApplication>
36 #include <QMouseEvent>
41 #include "YacsTrace.hxx"
44 using namespace YACS::ENGINE;
45 using namespace YACS::HMI;
47 CatalogWidget::CatalogWidget(QWidget *parent,
48 YACS::ENGINE::Catalog* builtinCatalog,
49 YACS::ENGINE::Catalog* sessionCatalog)
52 DEBTRACE("CatalogWidget::CatalogWidget");
53 _builtinCatalog = builtinCatalog;
54 _sessionCatalog = sessionCatalog;
58 _typeToCataMap.clear();
63 addCatalog(_builtinCatalog, "Built In");
64 addCatalog(_sessionCatalog, "Current Session");
66 setDragDropMode(QAbstractItemView::DragOnly);
68 setDropIndicatorShown(true);
70 setSelectionMode(QAbstractItemView::ExtendedSelection);
73 bool CatalogWidget::addCatalogFromFile(std::string fileName)
75 DEBTRACE("CatalogWidget::addCatalogFromFile " << fileName);
76 QFileInfo afi(fileName.c_str());
79 Catalog *cataProc = YACS::ENGINE::getSALOMERuntime()->loadCatalog("proc", fileName);
80 string aFile = afi.fileName().toStdString();
81 addCatalog(cataProc, aFile);
84 std::map<std::string, YACS::ENGINE::Catalog*> CatalogWidget::getCataMap()
89 YACS::ENGINE::Catalog* CatalogWidget::getCatalog(std::string cataName)
91 YACS::ENGINE::Catalog* catalog = 0;
92 if (_cataMap.count(cataName))
93 catalog = _cataMap[cataName];
97 YACS::ENGINE::Catalog* CatalogWidget::getCatalogFromType(std::string typeName)
99 DEBTRACE("CatalogWidget::getCatalogFromType " << typeName);
100 YACS::ENGINE::Catalog* catalog = 0;
101 if (_typeToCataMap.count(typeName))
102 catalog = _typeToCataMap[typeName];
106 void CatalogWidget::addCatalog(YACS::ENGINE::Catalog* catalog,
109 if (!catalog) return;
111 QTreeWidgetItem *itemCata = 0;
112 QTreeWidgetItem *category = 0;
114 itemCata = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString(name.c_str())));
115 insertTopLevelItem(_idCatalog, itemCata);
118 if (! catalog->_typeMap.empty())
120 category = new QTreeWidgetItem(itemCata, QStringList(QString("Types")));
121 map<string, TypeCode*>::const_iterator it = catalog->_typeMap.begin();
122 for (; it != catalog->_typeMap.end(); ++it)
124 DEBTRACE("Type : " <<(*it).first
125 << " " << (*it).second->getKindRepr()
126 << " " << (*it).second->name()
127 << " " << (*it).second->shortName()
128 << " " << (*it).second->id() );
129 string typeName = it->first;
130 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString(typeName.c_str())));
131 if (! _typeToCataMap.count(typeName))
132 _typeToCataMap[typeName] = catalog;
133 else if ( ! ((*it).second)->isEquivalent(_typeToCataMap[typeName]->_typeMap[typeName]) )
135 DEBTRACE(" ========================================================================================================");
136 DEBTRACE(" type " << typeName << " not compatible with one of same name already present in another catalog, FORCE NEW!");
137 DEBTRACE(" ========================================================================================================");
138 _typeToCataMap[typeName] = catalog;
139 item->setForeground(0,Qt::blue);
144 if (! catalog->_componentMap.empty())
146 category = new QTreeWidgetItem(itemCata, QStringList(QString("Components")));
147 map<string, ComponentDefinition*>::const_iterator it = catalog->_componentMap.begin();
148 for (; it != catalog->_componentMap.end(); ++it)
150 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
151 map<string, ServiceNode *>::const_iterator its = (it->second)->_serviceMap.begin();
152 for (; its != (it->second)->_serviceMap.end(); ++its)
153 QTreeWidgetItem *sitem = new QTreeWidgetItem(item, QStringList(QString((its->first).c_str())));
157 if (! catalog->_nodeMap.empty())
159 category = new QTreeWidgetItem(itemCata, QStringList(QString("Elementary Nodes")));
160 map<string, Node*>::const_iterator it = catalog->_nodeMap.begin();
161 for (; it != catalog->_nodeMap.end(); ++it)
162 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
165 if (! catalog->_composednodeMap.empty())
167 category = new QTreeWidgetItem(itemCata, QStringList(QString("Composed Nodes")));
168 map<string, ComposedNode*>::const_iterator it = catalog->_composednodeMap.begin();
169 for (; it != catalog->_composednodeMap.end(); ++it)
170 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
172 _cataMap[name] = catalog;
175 CatalogWidget::~CatalogWidget()
179 void CatalogWidget::startDrag(Qt::DropActions supportedActions)
181 DEBTRACE("startDrag " << supportedActions);
182 if (! QtGuiContext::getQtCurrent()) return;
183 if (! QtGuiContext::getQtCurrent()->isEdition()) return;
186 ItemMimeData *mime = 0;
189 QList<QTreeWidgetItem*> selectList = selectedItems();
190 for (int i=0; i<selectList.size(); i++)
192 QTreeWidgetItem *parent = selectList[i]->parent();
193 if (!parent) continue;
194 QTreeWidgetItem *grandPa = parent->parent();
195 if (!grandPa) continue;
196 QTreeWidgetItem *grandGrandPa = grandPa->parent();
199 cataName = grandGrandPa->text(0).toStdString();
201 cataName = grandPa->text(0).toStdString();
202 DEBTRACE("cataName=" << cataName);
203 YASSERT(_cataMap.count(cataName));
204 YACS::ENGINE::Catalog *catalog = _cataMap[cataName];
208 string definition = "";
210 if (! parent->text(0).compare("Types"))
213 definition = selectList[i]->text(0).toStdString();
214 pixmap.load("icons:data_link.png");
216 else if (parent->text(0).contains("Nodes"))
219 definition = selectList[i]->text(0).toStdString();
220 pixmap.load("icons:add_node.png");
222 else if (! grandPa->text(0).compare("Components"))
224 mimeInfo = "Service";
225 definition = selectList[i]->text(0).toStdString();
226 compo = parent->text(0).toStdString();
227 pixmap.load("icons:new_salome_service_node.png");
231 mimeInfo = "Component";
232 compo = selectList[i]->text(0).toStdString();
233 pixmap.load("icons:component.png");
235 QString mimeType = "yacs/cata" + mimeInfo;
237 if (!drag) // --- intialize mime data with the first selected item
239 DEBTRACE("mimeInfo=" << mimeInfo.toStdString() << " definition=" << definition << " compo=" << compo);
240 drag = new QDrag(this);
241 mime = new ItemMimeData;
242 drag->setMimeData(mime);
243 mime->setData(mimeType, mimeInfo.toAscii());
244 drag->setPixmap(pixmap);
246 theMimeInfo = mimeInfo;
248 mime->setCatalog(catalog);
249 mime->setCataName(cataName);
250 mime->setCompo(compo);
251 mime->setType(definition);
253 else // --- push only selected item of the same mimeType than the first
255 if (theMimeInfo == mimeInfo)
257 DEBTRACE("mimeInfo=" << mimeInfo.toStdString() << " definition=" << definition << " compo=" << compo);
258 mime->setCatalog(catalog);
259 mime->setCataName(cataName);
260 mime->setCompo(compo);
261 mime->setType(definition);
269 mime->setControl(true);
271 mime->setControl(false);
273 drag->exec(supportedActions);
277 void CatalogWidget::mousePressEvent(QMouseEvent *event)
279 DEBTRACE("CatalogWidget::mousePressEvent ");
280 _dragModifier= false;
281 if(event->button() == Qt::MidButton)
283 QTreeWidget::mousePressEvent(event);