1 // Copyright (C) 2006-2016 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();
62 setHeaderHidden( true );
64 addCatalog(_builtinCatalog, "Built In");
65 addCatalog(_sessionCatalog, "Current Session");
67 setDragDropMode(QAbstractItemView::DragOnly);
69 setDropIndicatorShown(true);
71 setSelectionMode(QAbstractItemView::ExtendedSelection);
74 bool CatalogWidget::addCatalogFromFile(std::string fileName)
76 DEBTRACE("CatalogWidget::addCatalogFromFile " << fileName);
77 QFileInfo afi(fileName.c_str());
80 Catalog *cataProc = YACS::ENGINE::getSALOMERuntime()->loadCatalog("proc", fileName);
81 string aFile = afi.fileName().toStdString();
82 addCatalog(cataProc, aFile);
85 std::map<std::string, YACS::ENGINE::Catalog*> CatalogWidget::getCataMap()
90 YACS::ENGINE::Catalog* CatalogWidget::getCatalog(std::string cataName)
92 YACS::ENGINE::Catalog* catalog = 0;
93 if (_cataMap.count(cataName))
94 catalog = _cataMap[cataName];
98 YACS::ENGINE::Catalog* CatalogWidget::getCatalogFromType(std::string typeName)
100 DEBTRACE("CatalogWidget::getCatalogFromType " << typeName);
101 YACS::ENGINE::Catalog* catalog = 0;
102 if (_typeToCataMap.count(typeName))
103 catalog = _typeToCataMap[typeName];
107 void CatalogWidget::addCatalog(YACS::ENGINE::Catalog* catalog,
110 if (!catalog) return;
112 QTreeWidgetItem *itemCata = 0;
113 QTreeWidgetItem *category = 0;
115 itemCata = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString(name.c_str())));
116 insertTopLevelItem(_idCatalog, itemCata);
119 if (! catalog->_typeMap.empty())
121 category = new QTreeWidgetItem(itemCata, QStringList(QString("Types")));
122 map<string, TypeCode*>::const_iterator it = catalog->_typeMap.begin();
123 for (; it != catalog->_typeMap.end(); ++it)
125 DEBTRACE("Type : " <<(*it).first
126 << " " << (*it).second->getKindRepr()
127 << " " << (*it).second->name()
128 << " " << (*it).second->shortName()
129 << " " << (*it).second->id() );
130 string typeName = it->first;
131 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString(typeName.c_str())));
132 if (! _typeToCataMap.count(typeName))
133 _typeToCataMap[typeName] = catalog;
134 else if ( ! ((*it).second)->isEquivalent(_typeToCataMap[typeName]->_typeMap[typeName]) )
136 DEBTRACE(" ========================================================================================================");
137 DEBTRACE(" type " << typeName << " not compatible with one of same name already present in another catalog, FORCE NEW!");
138 DEBTRACE(" ========================================================================================================");
139 _typeToCataMap[typeName] = catalog;
140 item->setForeground(0,Qt::blue);
145 if (! catalog->_componentMap.empty())
147 category = new QTreeWidgetItem(itemCata, QStringList(QString("Components")));
148 map<string, ComponentDefinition*>::const_iterator it = catalog->_componentMap.begin();
149 for (; it != catalog->_componentMap.end(); ++it)
151 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
152 map<string, ServiceNode *>::const_iterator its = (it->second)->_serviceMap.begin();
153 for (; its != (it->second)->_serviceMap.end(); ++its)
154 QTreeWidgetItem *sitem = new QTreeWidgetItem(item, QStringList(QString((its->first).c_str())));
158 if (! catalog->_nodeMap.empty())
160 category = new QTreeWidgetItem(itemCata, QStringList(QString("Elementary Nodes")));
161 map<string, Node*>::const_iterator it = catalog->_nodeMap.begin();
162 for (; it != catalog->_nodeMap.end(); ++it)
163 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
166 if (! catalog->_composednodeMap.empty())
168 category = new QTreeWidgetItem(itemCata, QStringList(QString("Composed Nodes")));
169 map<string, ComposedNode*>::const_iterator it = catalog->_composednodeMap.begin();
170 for (; it != catalog->_composednodeMap.end(); ++it)
171 QTreeWidgetItem *item = new QTreeWidgetItem(category, QStringList(QString((it->first).c_str())));
173 _cataMap[name] = catalog;
176 CatalogWidget::~CatalogWidget()
180 void CatalogWidget::startDrag(Qt::DropActions supportedActions)
182 DEBTRACE("startDrag " << supportedActions);
183 if (! QtGuiContext::getQtCurrent()) return;
184 if (! QtGuiContext::getQtCurrent()->isEdition()) return;
187 ItemMimeData *mime = 0;
190 QList<QTreeWidgetItem*> selectList = selectedItems();
191 for (int i=0; i<selectList.size(); i++)
193 QTreeWidgetItem *parent = selectList[i]->parent();
194 if (!parent) continue;
195 QTreeWidgetItem *grandPa = parent->parent();
196 if (!grandPa) continue;
197 QTreeWidgetItem *grandGrandPa = grandPa->parent();
200 cataName = grandGrandPa->text(0).toStdString();
202 cataName = grandPa->text(0).toStdString();
203 DEBTRACE("cataName=" << cataName);
204 YASSERT(_cataMap.count(cataName));
205 YACS::ENGINE::Catalog *catalog = _cataMap[cataName];
209 string definition = "";
211 if (! parent->text(0).compare("Types"))
214 definition = selectList[i]->text(0).toStdString();
215 pixmap.load("icons:data_link.png");
217 else if (parent->text(0).contains("Nodes"))
220 definition = selectList[i]->text(0).toStdString();
221 pixmap.load("icons:add_node.png");
223 else if (! grandPa->text(0).compare("Components"))
225 mimeInfo = "Service";
226 definition = selectList[i]->text(0).toStdString();
227 compo = parent->text(0).toStdString();
228 pixmap.load("icons:new_salome_service_node.png");
232 mimeInfo = "Component";
233 compo = selectList[i]->text(0).toStdString();
234 pixmap.load("icons:component.png");
236 QString mimeType = "yacs/cata" + mimeInfo;
238 if (!drag) // --- intialize mime data with the first selected item
240 DEBTRACE("mimeInfo=" << mimeInfo.toStdString() << " definition=" << definition << " compo=" << compo);
241 drag = new QDrag(this);
242 mime = new ItemMimeData;
243 drag->setMimeData(mime);
244 mime->setData(mimeType, mimeInfo.toLatin1());
245 drag->setPixmap(pixmap);
247 theMimeInfo = mimeInfo;
249 mime->setCatalog(catalog);
250 mime->setCataName(cataName);
251 mime->setCompo(compo);
252 mime->setType(definition);
254 else // --- push only selected item of the same mimeType than the first
256 if (theMimeInfo == mimeInfo)
258 DEBTRACE("mimeInfo=" << mimeInfo.toStdString() << " definition=" << definition << " compo=" << compo);
259 mime->setCatalog(catalog);
260 mime->setCataName(cataName);
261 mime->setCompo(compo);
262 mime->setType(definition);
270 mime->setControl(true);
272 mime->setControl(false);
274 drag->exec(supportedActions);
278 void CatalogWidget::mousePressEvent(QMouseEvent *event)
280 DEBTRACE("CatalogWidget::mousePressEvent ");
281 _dragModifier= false;
282 if(event->button() == Qt::MidButton)
284 QTreeWidget::mousePressEvent(event);