Salome HOME
44bbe84a0c184f4c9d1ec93bd061981a87dfbcab
[modules/yacs.git] / src / genericgui / CatalogWidget.cxx
1 // Copyright (C) 2006-2013  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
20 #include "RuntimeSALOME.hxx"
21 #include "CatalogWidget.hxx"
22
23 #include "Catalog.hxx"
24 #include "TypeCode.hxx"
25 #include "ComponentDefinition.hxx"
26 #include "ItemMimeData.hxx"
27 #include "QtGuiContext.hxx"
28
29 #include <QApplication>
30 #include <QMimeData>
31 #include <QDrag>
32 #include <QPainter>
33 #include <QBitmap>
34 #include <QString>
35 #include <QFileInfo>
36 #include <QMouseEvent>
37
38 #include <cassert>
39
40 //#define _DEVDEBUG_
41 #include "YacsTrace.hxx"
42
43 using namespace std;
44 using namespace YACS::ENGINE;
45 using namespace YACS::HMI;
46
47 CatalogWidget::CatalogWidget(QWidget *parent,
48                              YACS::ENGINE::Catalog* builtinCatalog,
49                              YACS::ENGINE::Catalog* sessionCatalog)
50   : QTreeWidget(parent)
51 {
52   DEBTRACE("CatalogWidget::CatalogWidget");
53   _builtinCatalog = builtinCatalog;
54   _sessionCatalog = sessionCatalog;
55
56   _idCatalog = 0;
57   _cataMap.clear();
58   _typeToCataMap.clear();
59   _dragModifier=false;
60
61   setColumnCount(1);
62
63   addCatalog(_builtinCatalog, "Built In");
64   addCatalog(_sessionCatalog, "Current Session");
65
66   setDragDropMode(QAbstractItemView::DragOnly);
67   setDragEnabled(true);
68   setDropIndicatorShown(true);
69
70   setSelectionMode(QAbstractItemView::ExtendedSelection);
71 }
72
73 bool CatalogWidget::addCatalogFromFile(std::string fileName)
74 {
75   DEBTRACE("CatalogWidget::addCatalogFromFile " << fileName);
76   QFileInfo afi(fileName.c_str());
77   if(!afi.exists())
78     return false;
79   Catalog *cataProc = YACS::ENGINE::getSALOMERuntime()->loadCatalog("proc", fileName);
80   string aFile = afi.fileName().toStdString();
81   addCatalog(cataProc, aFile);
82 }
83
84 std::map<std::string, YACS::ENGINE::Catalog*> CatalogWidget::getCataMap()
85 {
86   return _cataMap;
87 }
88
89 YACS::ENGINE::Catalog* CatalogWidget::getCatalog(std::string cataName)
90 {
91   YACS::ENGINE::Catalog* catalog = 0;
92   if (_cataMap.count(cataName))
93     catalog = _cataMap[cataName];
94   return catalog;
95 }
96
97 YACS::ENGINE::Catalog* CatalogWidget::getCatalogFromType(std::string typeName)
98 {
99   DEBTRACE("CatalogWidget::getCatalogFromType " << typeName);
100   YACS::ENGINE::Catalog* catalog = 0;
101   if (_typeToCataMap.count(typeName))
102     catalog = _typeToCataMap[typeName];
103   return catalog;
104 }
105
106 void CatalogWidget::addCatalog(YACS::ENGINE::Catalog* catalog,
107                               std::string name)
108 {
109   if (!catalog) return;
110
111   QTreeWidgetItem *itemCata = 0;
112   QTreeWidgetItem *category = 0;
113
114   itemCata = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString(name.c_str())));
115   insertTopLevelItem(_idCatalog, itemCata);
116   _idCatalog++;
117
118   if (! catalog->_typeMap.empty())
119     {
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)
123         {
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]) )
134             {
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);
140            }
141         }
142     }
143
144   if (! catalog->_componentMap.empty())
145     {
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)
149         {
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())));
154         }
155     }
156
157   if (! catalog->_nodeMap.empty())
158     {
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())));
163     }
164
165   if (! catalog->_composednodeMap.empty())
166     {
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())));
171     }
172   _cataMap[name] = catalog;
173 }
174
175 CatalogWidget::~CatalogWidget()
176 {
177 }
178
179 void CatalogWidget::startDrag(Qt::DropActions supportedActions)
180 {
181   DEBTRACE("startDrag " << supportedActions);
182   if (! QtGuiContext::getQtCurrent()) return;
183   if (! QtGuiContext::getQtCurrent()->isEdition()) return;
184
185   QDrag *drag = 0;
186   ItemMimeData *mime = 0;
187   QString theMimeInfo;
188
189   QList<QTreeWidgetItem*> selectList = selectedItems();
190   for (int i=0; i<selectList.size(); i++)
191     {
192       QTreeWidgetItem *parent = selectList[i]->parent();
193       if (!parent) continue;
194       QTreeWidgetItem *grandPa = parent->parent();
195       if (!grandPa) continue;
196       QTreeWidgetItem *grandGrandPa = grandPa->parent();
197       string cataName ="";
198       if (grandGrandPa)
199         cataName = grandGrandPa->text(0).toStdString();
200       else
201         cataName = grandPa->text(0).toStdString();
202       DEBTRACE("cataName=" << cataName);
203       YASSERT(_cataMap.count(cataName));
204       YACS::ENGINE::Catalog *catalog = _cataMap[cataName];
205
206       QString mimeInfo;
207       string compo = "";
208       string definition = "";
209       QPixmap pixmap;
210       if (! parent->text(0).compare("Types"))
211         {
212           mimeInfo = "Type";
213           definition = selectList[i]->text(0).toStdString();
214           pixmap.load("icons:data_link.png");
215         }
216       else if (parent->text(0).contains("Nodes"))
217         {
218           mimeInfo = "Node";
219           definition = selectList[i]->text(0).toStdString();
220           pixmap.load("icons:add_node.png");
221         }
222       else if (! grandPa->text(0).compare("Components"))
223         {
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");
228         }
229       else
230         {
231           mimeInfo = "Component";
232           compo = selectList[i]->text(0).toStdString();
233           pixmap.load("icons:component.png");
234         }
235       QString mimeType = "yacs/cata" + mimeInfo;
236       
237       if (!drag) // --- intialize mime data with the first selected item
238         {
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);
245
246           theMimeInfo = mimeInfo;
247
248           mime->setCatalog(catalog);
249           mime->setCataName(cataName);
250           mime->setCompo(compo);
251           mime->setType(definition);
252         }
253       else       // --- push only selected item of the same mimeType than the first
254         {
255           if (theMimeInfo == mimeInfo)
256             {
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);
262             }          
263         }
264     }
265
266   if (drag)
267     {
268       if(_dragModifier)
269         mime->setControl(true);
270       else
271         mime->setControl(false);
272       
273       drag->exec(supportedActions);
274     }
275 }
276
277 void CatalogWidget::mousePressEvent(QMouseEvent  *event)
278 {
279   DEBTRACE("CatalogWidget::mousePressEvent ");
280   _dragModifier= false;
281   if(event->button() == Qt::MidButton)
282     _dragModifier= true;
283   QTreeWidget::mousePressEvent(event);
284 }
285