Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / genericgui / CatalogWidget.cxx
1 //  Copyright (C) 2006-2008  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.
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 #include "RuntimeSALOME.hxx"
20 #include "CatalogWidget.hxx"
21
22 #include "Catalog.hxx"
23 #include "TypeCode.hxx"
24 #include "ComponentDefinition.hxx"
25 #include "ItemMimeData.hxx"
26
27 #include <QMimeData>
28 #include <QDrag>
29 #include <QPainter>
30 #include <QBitmap>
31 #include <QString>
32 #include <QFileInfo>
33
34 #include <cassert>
35
36 //#define _DEVDEBUG_
37 #include "YacsTrace.hxx"
38
39 using namespace std;
40 using namespace YACS::ENGINE;
41 using namespace YACS::HMI;
42
43 CatalogWidget::CatalogWidget(QWidget *parent,
44                              YACS::ENGINE::Catalog* builtinCatalog,
45                              YACS::ENGINE::Catalog* sessionCatalog)
46   : QTreeWidget(parent)
47 {
48   DEBTRACE("CatalogWidget::CatalogWidget");
49   _builtinCatalog = builtinCatalog;
50   _sessionCatalog = sessionCatalog;
51
52   _idCatalog = 0;
53   _cataMap.clear();
54   _typeToCataMap.clear();
55
56   setColumnCount(1);
57
58   addCatalog(_builtinCatalog, "Built In");
59   addCatalog(_sessionCatalog, "Current Session");
60
61   setDragDropMode(QAbstractItemView::DragOnly);
62   setDragEnabled(true);
63   setDropIndicatorShown(true);
64 }
65
66 bool CatalogWidget::addCatalogFromFile(std::string fileName)
67 {
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);
73 }
74
75 std::map<std::string, YACS::ENGINE::Catalog*> CatalogWidget::getCataMap()
76 {
77   return _cataMap;
78 }
79
80 YACS::ENGINE::Catalog* CatalogWidget::getCatalog(std::string cataName)
81 {
82   YACS::ENGINE::Catalog* catalog = 0;
83   if (_cataMap.count(cataName))
84     catalog = _cataMap[cataName];
85   return catalog;
86 }
87
88 YACS::ENGINE::Catalog* CatalogWidget::getCatalogFromType(std::string typeName)
89 {
90   DEBTRACE("CatalogWidget::getCatalogFromType " << typeName);
91   YACS::ENGINE::Catalog* catalog = 0;
92   if (_typeToCataMap.count(typeName))
93     catalog = _typeToCataMap[typeName];
94   return catalog;
95 }
96
97 void CatalogWidget::addCatalog(YACS::ENGINE::Catalog* catalog,
98                               std::string name)
99 {
100   if (!catalog) return;
101
102   QTreeWidgetItem *itemCata = 0;
103   QTreeWidgetItem *category = 0;
104
105   itemCata = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString(name.c_str())));
106   insertTopLevelItem(_idCatalog, itemCata);
107   _idCatalog++;
108
109   if (! catalog->_typeMap.empty())
110     {
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)
114         {
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]) )
125             {
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);
131            }
132         }
133     }
134
135   if (! catalog->_componentMap.empty())
136     {
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)
140         {
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())));
145         }
146     }
147
148   if (! catalog->_nodeMap.empty())
149     {
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())));
154     }
155
156   if (! catalog->_composednodeMap.empty())
157     {
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())));
162     }
163   _cataMap[name] = catalog;
164 }
165
166 CatalogWidget::~CatalogWidget()
167 {
168 }
169
170 void CatalogWidget::startDrag(Qt::DropActions supportedActions)
171 {
172   DEBTRACE("startDrag " << supportedActions);
173   QTreeWidgetItem *item = currentItem();
174   assert(item);
175   QTreeWidgetItem *parent = item->parent();
176   if (!parent) return;
177   QTreeWidgetItem *grandPa = parent->parent();
178   if (!grandPa) return;
179   QTreeWidgetItem *grandGrandPa = grandPa->parent();
180   string cataName ="";
181   if (grandGrandPa)
182     cataName = grandGrandPa->text(0).toStdString();
183   else
184     cataName = grandPa->text(0).toStdString();
185
186   DEBTRACE("cataName=" << cataName);
187   assert(_cataMap.count(cataName));
188   YACS::ENGINE::Catalog *catalog = _cataMap[cataName];
189
190   QString mimeInfo;
191   string compo = "";
192   string definition = "";
193   QPixmap pixmap;
194   if (! parent->text(0).compare("Types"))
195     {
196       mimeInfo = "Type";
197       definition = item->text(0).toStdString();
198       pixmap.load("icons:data_link.png");
199     }
200   else if (parent->text(0).contains("Nodes"))
201     {
202       mimeInfo = "Node";
203       definition = item->text(0).toStdString();
204       pixmap.load("icons:add_node.png");
205     }
206   else if (! grandPa->text(0).compare("Components"))
207     {
208       mimeInfo = "Service";
209       definition = item->text(0).toStdString();
210       compo = parent->text(0).toStdString();
211       pixmap.load("icons:new_salome_service_node.png");
212     }
213   else
214     {
215       mimeInfo = "Component";
216       compo = item->text(0).toStdString();
217       pixmap.load("icons:component.png");
218     }
219   DEBTRACE("mimeInfo=" << mimeInfo.toStdString() << " definition=" << definition << " compo=" << compo);
220   QString mimeType = "yacs/cata" + mimeInfo;
221
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);
231   
232   drag->exec(supportedActions);
233 }