Salome HOME
75a50f44b77c74c076a93289a07174cf716089f3
[modules/yacs.git] / src / hmi / editTree.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 "editTree.h"
21 #include "Proc.hxx"
22 #include "Node.hxx"
23 #include "guiContext.hxx"
24 #include "Catalog.hxx"
25 #include "browseCatalog.h"
26 #include "chooseName.h"
27 #include "ServiceNode.hxx"
28
29 #include <iostream>
30
31 //#define _DEVDEBUG_
32 #include "YacsTrace.hxx"
33
34 #include <qpopupmenu.h>
35 #include <qfiledialog.h>
36 #include <qlabel.h>
37 #include <qcursor.h>
38 #include <qmessagebox.h>
39
40
41 #include <string>
42 #include <sstream>
43 #include <iostream>
44
45 using namespace std;
46
47 using namespace YACS;
48 using namespace YACS::HMI;
49
50
51 /*!
52  *
53  */
54
55 ViewItem::ViewItem(QListView *parent, QString label, Subject* subject)
56   : QListViewItem(parent, label)
57 {
58   _parent = parent;
59   _subject = subject;
60   _cf = Qt::black;
61   _subject->attach(this);
62 }
63
64 /*!
65  *
66  */
67
68 ViewItem::ViewItem(ViewItem *parent, QString label, Subject* subject)
69   : GuiObserver(), QListViewItem(parent, label)
70 {
71   _parent = parent->getParent();
72   _subject = subject;
73   _cf = Qt::black;
74   _subject->attach(this);
75 }
76
77 /*!
78  *
79  */
80
81 ViewItem::~ViewItem()
82 {
83 }
84
85 /*!
86  *
87  */
88
89 void ViewItem::setState(int state)
90 {
91 }
92
93 /*!
94  *
95  */
96
97 QListView *ViewItem::getParent()
98 {
99   return _parent;
100 }
101
102 /*!
103  *
104  */
105
106 Subject* ViewItem::getSubject()
107 {
108   return _subject;
109 }
110
111 /*!
112  *
113  */
114
115 void ViewItem::select(bool isSelected)
116 {
117   DEBTRACE("ViewItem::select() "<< isSelected << " " << this);
118   _parent->setSelected(this, isSelected);
119   _parent->ensureItemVisible(this);
120 }
121
122 void ViewItem::update(GuiEvent event, int type, Subject* son)
123 {
124   DEBTRACE("ViewItem::update");
125   ViewItem *item = 0;
126   switch (event)
127     {
128     case RENAME:
129       DEBTRACE("ViewItem::update RENAME " << _subject->getName());
130       setText(0,_subject->getName());
131       break;
132     case ADDREF:
133       DEBTRACE("ViewItem::update ADDREF " << _subject->getName());
134       item = new ReferenceViewItem(this,
135                                    son->getName(),
136                                    son);
137       break;
138     default:
139       DEBTRACE("ViewItem::update(), event not handled: " << event);
140       GuiObserver::update(event, type, son);
141     }
142 }
143
144 /*!
145  *
146  */
147
148 void ViewItem::paintCell( QPainter *p, const QColorGroup &cg,
149                           int column, int width, int alignment )
150 {
151   QColorGroup _cg( cg );
152   QColor c = _cg.text();
153   if (column == 1) _cg.setColor( QColorGroup::Text, _cf );
154   QListViewItem::paintCell( p, _cg, column, width, alignment );
155   if (column == 1) _cg.setColor( QColorGroup::Text, c );
156 }
157
158 // ----------------------------------------------------------------------------
159
160 /*!
161  *
162  */
163
164 NodeViewItem::NodeViewItem(ViewItem *parent,
165                            QString label,
166                            Subject* subject)
167   : ViewItem::ViewItem(parent, label, subject)
168 {
169 }
170
171 void NodeViewItem::update(GuiEvent event, int type, Subject* son)
172 {
173   DEBTRACE("NodeViewItem::update");
174   ViewItem *item = 0;
175   switch (event)
176     {
177     case YACS::HMI::ADD:
178       switch (type)
179         {
180         case YACS::HMI::INPUTPORT:
181         case YACS::HMI::OUTPUTPORT:
182         case YACS::HMI::INPUTDATASTREAMPORT:
183         case YACS::HMI::OUTPUTDATASTREAMPORT:
184           item =  new PortViewItem(this,
185                                    son->getName(),
186                                    son);
187           break;
188         default:
189           DEBTRACE("NodeViewItem::update(), ADD, type not handled: " << type);
190         }
191       break;
192     default:
193       DEBTRACE("NodeViewItem::update(), event not handled: " << event);
194       ViewItem::update(event, type, son);
195     }
196 }
197
198 // ----------------------------------------------------------------------------
199
200 /*!
201  *
202  */
203
204 ComposedNodeViewItem::ComposedNodeViewItem(QListView *parent,
205                                            QString label,
206                                            Subject* subject)
207   : ViewItem::ViewItem(parent, label, subject)
208 {
209 }
210
211 /*!
212  *
213  */
214
215
216 ComposedNodeViewItem::ComposedNodeViewItem(ViewItem *parent,
217                                            QString label,
218                                            Subject* subject)
219   : ViewItem::ViewItem(parent, label, subject)
220 {
221 }
222
223 void ComposedNodeViewItem::update(GuiEvent event, int type, Subject* son)
224 {
225   DEBTRACE("ComposedNodeViewItem::update");
226   ViewItem *item = 0;
227   switch (event)
228     {
229     case YACS::HMI::ADD:
230       switch (type)
231         {
232         case YACS::HMI::BLOC:
233         case YACS::HMI::FORLOOP:
234         case YACS::HMI::WHILELOOP:
235         case YACS::HMI::SWITCH:
236         case YACS::HMI::FOREACHLOOP:
237         case YACS::HMI::OPTIMIZERLOOP:
238           item =  new ComposedNodeViewItem(this,
239                                            son->getName(),
240                                            son);
241           break;
242         case YACS::HMI::PYTHONNODE:
243         case YACS::HMI::PYFUNCNODE:
244         case YACS::HMI::CORBANODE:
245         case YACS::HMI::CPPNODE:
246         case YACS::HMI::SALOMENODE:
247         case YACS::HMI::SALOMEPYTHONNODE:
248         case YACS::HMI::XMLNODE:
249         case YACS::HMI::PRESETNODE:
250         case YACS::HMI::OUTNODE:
251         case YACS::HMI::STUDYINNODE:
252         case YACS::HMI::STUDYOUTNODE:
253           item =  new NodeViewItem(this,
254                                    son->getName(),
255                                    son);
256           break;
257         case YACS::HMI::INPUTPORT:
258         case YACS::HMI::OUTPUTPORT:
259         case YACS::HMI::INPUTDATASTREAMPORT:
260         case YACS::HMI::OUTPUTDATASTREAMPORT:
261           item =  new PortViewItem(this,
262                                    son->getName(),
263                                    son);
264           break;
265         case YACS::HMI::COMPONENT:
266           item =  new ComponentViewItem(this,
267                                         son->getName(),
268                                         son);
269           break;
270         case YACS::HMI::CONTAINER:
271           item =  new ContainerViewItem(this,
272                                         son->getName(),
273                                         son);
274           break;
275         case YACS::HMI::DATATYPE:
276           item = new DataTypeViewItem(this,
277                                       son->getName(),
278                                       son);
279           break;
280         default:
281           DEBTRACE("ComposedNodeViewItem::update() ADD, type not handled:" << type);
282         }
283       break;
284     case YACS::HMI::ADDLINK:
285     case YACS::HMI::ADDCONTROLLINK:
286       item = new LinkViewItem(this,
287                               son->getName(),
288                               son);
289       break;
290     default:
291       DEBTRACE("ComposedNodeViewItem::update(), event not handled: "<< event);
292       ViewItem::update(event, type, son);
293     }
294 }
295
296 // ----------------------------------------------------------------------------
297
298 PortViewItem::PortViewItem(ViewItem *parent, QString label, Subject* subject)
299   : ViewItem::ViewItem(parent, label, subject)
300 {
301 }
302
303 void PortViewItem::update(GuiEvent event, int type, Subject* son)
304 {
305   DEBTRACE("PortViewItem::update");
306   ViewItem::update(event, type, son);
307 }
308
309 // ----------------------------------------------------------------------------
310
311 LinkViewItem::LinkViewItem(ViewItem *parent, QString label, Subject* subject)
312   : ViewItem::ViewItem(parent, label, subject)
313 {
314 }
315
316 void LinkViewItem::update(GuiEvent event, int type, Subject* son)
317 {
318   DEBTRACE("LinkViewItem::update");
319   ViewItem::update(event, type, son);
320 }
321
322 // ----------------------------------------------------------------------------
323
324 ComponentViewItem::ComponentViewItem(ViewItem *parent, QString label, Subject* subject)
325   : ViewItem::ViewItem(parent, label, subject)
326 {
327 }
328
329 void ComponentViewItem::update(GuiEvent event, int type, Subject* son)
330 {
331   DEBTRACE("ComponentViewItem::update");
332   ViewItem::update(event, type, son);
333 }
334
335 // ----------------------------------------------------------------------------
336
337 ContainerViewItem::ContainerViewItem(ViewItem *parent, QString label, Subject* subject)
338   : ViewItem::ViewItem(parent, label, subject)
339 {
340 }
341
342 void ContainerViewItem::update(GuiEvent event, int type, Subject* son)
343 {
344   DEBTRACE("ContainerViewItem::update");
345   ViewItem::update(event, type, son);
346 }
347  
348 // ----------------------------------------------------------------------------
349
350 DataTypeViewItem::DataTypeViewItem(ViewItem *parent, QString label, Subject* subject)
351   : ViewItem::ViewItem(parent, label, subject)
352 {
353 }
354
355 void DataTypeViewItem::update(GuiEvent event, int type, Subject* son)
356 {
357   DEBTRACE("DataTypeViewItem::update");
358   ViewItem::update(event, type, son);
359 }
360  
361 // ----------------------------------------------------------------------------
362
363 ReferenceViewItem::ReferenceViewItem(ViewItem *parent, QString label, Subject* subject)
364   : ViewItem::ViewItem(parent, label, subject)
365 {
366 }
367
368 void ReferenceViewItem::update(GuiEvent event, int type, Subject* son)
369 {
370   DEBTRACE("ReferenceViewItem::update");
371   ViewItem::update(event, type, son);
372 }
373
374 // ----------------------------------------------------------------------------
375
376
377 /*!
378  *
379  */
380
381 editTree::editTree(YACS::HMI::Subject *context, 
382                    QWidget* parent,
383                    const char* name,
384                    WFlags fl) : GuiObserver(), wiEditTree(parent, name, fl)
385 {
386   _context = context;
387   _parent = parent;
388   _root = 0;
389   _previousSelected = 0;
390   _selectedSubjectOutPort = 0;
391   _selectedSubjectNode = 0;
392   _selectedSubjectComponent = 0;
393   _selectedSubjectService = 0;
394   resetTreeNode(lv);
395   _context->attach(this);
396   connect( lv, SIGNAL(selectionChanged()),
397            this, SLOT(select()) );
398 //   connect( lv, SIGNAL(clicked(QListViewItem *)),
399 //            this, SLOT(select()) );
400 //   connect( lv, SIGNAL(selectionChanged(QListViewItem *)),
401 //            this, SLOT(select(QListViewItem *)) );
402 //   connect( lv, SIGNAL(clicked(QListViewItem *)),
403 //            this, SLOT(select(QListViewItem *)) );
404 }
405
406 /*!
407  *
408  */
409
410 editTree::~editTree()
411 {
412 }
413
414 void editTree::update(GuiEvent event, int type, Subject* son)
415 {
416   DEBTRACE("editTree::update");
417   switch (event)
418     {
419     case YACS::HMI::NEWROOT:
420       setNewRoot(son);
421       break;
422     default:
423       DEBTRACE("editTree::update(), event not handled: "<< event);
424     }
425 }
426
427 void editTree::setNewRoot(YACS::HMI::Subject *root)
428 {
429   _root=root;
430   _previousSelected=0;
431   resetTreeNode(lv);
432 }
433
434 void editTree::addViewItemInMap(YACS::HMI::ViewItem* item, YACS::HMI::Subject* subject)
435 {
436   _viewItemsMap[subject] = item;
437 }
438
439 YACS::HMI::ViewItem* editTree::getViewItem(YACS::HMI::Subject* subject)
440 {
441   return _viewItemsMap[subject];
442 }
443
444 /*!
445  *
446  */
447
448 void editTree::resetTreeNode(QListView *lv)
449 {
450   lv->clear();
451   lv->setColumnWidth(0,400);
452 //   lv->addColumn( "state", 100);
453   lv->setRootIsDecorated( TRUE );
454   if (_root)
455     {
456       string name = _root->getName();
457       ViewItem *start = new ComposedNodeViewItem(lv, name, _root);
458       addViewItemInMap(start, _root);
459     }
460 }
461
462 /*!
463  *
464  */
465
466 void editTree::printName()
467 {
468   QListViewItem *it = lv->selectedItem();
469   if (it)
470     {
471       DEBTRACE("my name is Bond, James Bond... "<< this);
472     }
473   else
474     {
475       DEBTRACE("nobody "<< this);
476     }
477 }
478
479 YACS::HMI::Subject* editTree::getSelectedSubject()
480 {
481   Subject *sub = 0;
482   QListViewItem *it = lv->selectedItem();
483   if (it)
484     {
485       ViewItem *item = dynamic_cast<ViewItem*> (it);
486       if (item) sub = item->getSubject();
487     }
488   return sub;
489 }
490
491 void editTree::destroy()
492 {
493   DEBTRACE("editTree::destroy");
494   Subject *sub =getSelectedSubject();
495   if (sub)
496     {
497       Subject *parent = sub->getParent();
498       if (parent)
499         {
500           parent->destroy(sub);
501         }
502     }
503 }
504
505 void editTree::addLink()
506 {
507   DEBTRACE("editTree::addLink");
508   Subject *sub =getSelectedSubject();
509   if (sub)
510     {
511       if (dynamic_cast<SubjectOutputPort*>(sub) || dynamic_cast<SubjectOutputDataStreamPort*>(sub))
512         _selectedSubjectOutPort = static_cast<SubjectDataPort*>(sub);
513       else if (dynamic_cast<SubjectNode*>(sub))
514         _selectedSubjectNode = static_cast<SubjectNode*>(sub);
515     }
516 }
517
518 void editTree::editPortProperties()
519 {
520   DEBTRACE("editTree::editPortProperties");
521   Subject *sub =getSelectedSubject();
522   if (sub)
523     {
524       if (SubjectOutputDataStreamPort* subODS = dynamic_cast<SubjectOutputDataStreamPort*>(sub))
525         {
526           map<string, string> properties = subODS->getProperties();
527           properties["prop1"] = "val1";
528           properties["prop2"] = "val2";
529           subODS->setProperties(properties);
530           properties = subODS->getProperties();
531           map<string, string>::iterator it;
532           for (it = properties.begin(); it !=properties.end(); ++it)
533             DEBTRACE("OutputDataStreamPort property " << (*it).first << " " << (*it).second);
534         }
535       else if (SubjectInputDataStreamPort* subIDS = dynamic_cast<SubjectInputDataStreamPort*>(sub))
536         {
537           map<string, string> properties = subIDS->getProperties();
538           properties["prop1"] = "val1";
539           properties["prop2"] = "val2";
540           subIDS->setProperties(properties);
541           properties = subIDS->getProperties();
542           map<string, string>::iterator it;
543           for (it = properties.begin(); it !=properties.end(); ++it)
544             DEBTRACE("InputDataStreamPort property " << (*it).first << " " << (*it).second);
545          }
546     }
547 }
548
549 void editTree::addComponent()
550 {
551   DEBTRACE("editTree::addComponent");
552   stringstream name;
553   name << "component";
554   name << "_" << GuiContext::getCurrent()->getNewId();
555   ChooseName chooseName("root", "component", name.str());
556   chooseName.exec();
557   if (chooseName.isOk())
558     {
559       string theName = chooseName.getChoosenName();
560       bool ret = GuiContext::getCurrent()->getSubjectProc()->addComponent(theName);
561       if (!ret) QMessageBox::warning(this, "Component not Created", GuiContext::getCurrent()->_lastErrorMessage );
562     }
563 }
564
565 void editTree::addContainer()
566 {
567   DEBTRACE("editTree::addContainer");
568   std::stringstream name;
569   name << "container" << GuiContext::getCurrent()->getNewId();
570   ChooseName chooseName("root", "container", name.str());
571   chooseName.exec();
572   if (chooseName.isOk())
573     {
574       string theName = chooseName.getChoosenName();
575       bool ret = GuiContext::getCurrent()->getSubjectProc()->addContainer(theName);
576       if (!ret) QMessageBox::warning(this, "Container not Created", GuiContext::getCurrent()->_lastErrorMessage );
577     }
578 }
579
580 void editTree::associateServiceToComponent()
581 {
582   DEBTRACE("editTree::associateServiceToComponent");
583   Subject *sub =getSelectedSubject();
584   if (sub)
585     {
586       if (SubjectServiceNode *subserv = dynamic_cast<SubjectServiceNode*>(sub))
587         _selectedSubjectService = subserv;
588     }
589 }
590
591 void editTree::associateComponentToContainer()
592 {
593   DEBTRACE("editTree::associateComponentToContainer");
594   Subject *sub =getSelectedSubject();
595   if (sub)
596     {
597       if (SubjectComponent *subcomp = dynamic_cast<SubjectComponent*>(sub))
598         _selectedSubjectComponent = subcomp;
599     }
600 }
601
602 void editTree::editContainerProperties()
603 {
604   DEBTRACE("editTree::editContainerProperties");
605   Subject *sub =getSelectedSubject();
606   if (sub)
607     {
608       if (SubjectContainer *subcont = dynamic_cast<SubjectContainer*>(sub))
609         {
610           map<string, string> properties = subcont->getProperties();
611           properties["prop1"] = "val1";
612           properties["prop2"] = "val2";
613           subcont->setProperties(properties);
614           properties = subcont->getProperties();
615           map<string, string>::iterator it;
616           for (it = properties.begin(); it !=properties.end(); ++it)
617             DEBTRACE("container property " << (*it).first << " " << (*it).second);
618         }
619     }
620 }
621
622 void editTree::newNode(int key)
623 {
624   DEBTRACE("editTree::newNode(Catalog* catalog, string type): "<< this);
625   YACS::ENGINE::Catalog* catalog = _catalogItemsMap[key].first;
626   std::string type = _catalogItemsMap[key].second;
627   Subject *sub =getSelectedSubject();
628   if (sub)
629     {
630       YACS::HMI::SubjectComposedNode *subject = dynamic_cast< YACS::HMI::SubjectComposedNode*>(sub);
631       if (subject)
632         {
633           std::stringstream name;
634           name << type << GuiContext::getCurrent()->getNewId();
635           YACS::HMI::SubjectSwitch *aSwitch = dynamic_cast< YACS::HMI::SubjectSwitch*>(subject);
636           if (aSwitch)
637             {
638               map<int, SubjectNode*> bodyMap = aSwitch->getBodyMap();
639               int swCase = 0;
640               if (bodyMap.empty()) swCase = 1;
641               else
642                 {
643                   map<int, SubjectNode*>::reverse_iterator rit = bodyMap.rbegin();
644                   swCase = (*rit).first + 1;
645                 }
646               aSwitch->addNode(catalog, "", type, name.str(), swCase);
647             }
648           else
649             subject->addNode(catalog, "", type, name.str());
650         }
651     }
652   else DEBTRACE("editTree::newNode(Catalog* catalog, string type): no ComposedNode selected  "<< this);
653 }
654
655 void editTree::newNode(YACS::ENGINE::Catalog* catalog,
656                        Subject* sub,
657                        std::pair<std::string,std::string> compoServ)
658 {
659   DEBTRACE("editTree::newNode(catalog, subject, std::pair<std::string,std::string> compoServ)");
660   std::string compo = compoServ.first;
661   std::string service = compoServ.second;
662   std::stringstream name;
663   name << compo << "_" << service << GuiContext::getCurrent()->getNewId();
664   YACS::HMI::SubjectComposedNode *subject = dynamic_cast< YACS::HMI::SubjectComposedNode*>(sub);
665   if (subject)
666     {
667       YACS::HMI::SubjectSwitch *aSwitch = dynamic_cast< YACS::HMI::SubjectSwitch*>(subject);
668       if (aSwitch)
669         {
670           map<int, SubjectNode*> bodyMap = aSwitch->getBodyMap();
671           int swCase = 0;
672           if (bodyMap.empty()) swCase = 1;
673           else
674             {
675               map<int, SubjectNode*>::reverse_iterator rit = bodyMap.rbegin();
676               swCase = (*rit).first + 1;
677             }
678           aSwitch->addNode(catalog, compo, service, name.str(), swCase);
679         }
680       else
681         subject->addNode(catalog, compo, service, name.str());
682     }
683 }
684
685 void editTree::newDataType(YACS::ENGINE::Catalog* catalog,
686                            Subject* sub,
687                            std::string typeName)
688 {
689   DEBTRACE("editTree::newDataType " << sub->getName() << " " << typeName);
690   YACS::HMI::SubjectProc *subproc = GuiContext::getCurrent()->getSubjectProc();
691   subproc->addDataType(catalog, typeName);
692 }
693
694
695 void editTree::newInputPort(int key)
696 {
697   DEBTRACE("editTree::newInputPort(Catalog* catalog, string type): "<< this);
698   YACS::ENGINE::Catalog* catalog = _catalogItemsMap[key].first;
699   std::string type = _catalogItemsMap[key].second;
700   Subject *sub =getSelectedSubject();
701   newDFInputPort(catalog, sub, type);
702 }
703
704 void editTree::newOutputPort(int key)
705 {
706   DEBTRACE("editTree::newOutputPort(Catalog* catalog, string type): "<< this);
707   YACS::ENGINE::Catalog* catalog = _catalogItemsMap[key].first;
708   std::string type = _catalogItemsMap[key].second;
709   Subject *sub =getSelectedSubject();
710   newDFOutputPort(catalog, sub, type);
711 }
712
713 void editTree::newDFInputPort(YACS::ENGINE::Catalog* catalog,
714                               Subject* sub,
715                               std::string typeName)
716 {
717   if (sub)
718     {
719       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
720       if (subject)
721         {
722           std::stringstream name;
723           name << "InDF_" << typeName << GuiContext::getCurrent()->getNewId();
724           ChooseName chooseName(subject->getName(), typeName, name.str());
725           chooseName.exec();
726           if (chooseName.isOk())
727             {
728               string theName = chooseName.getChoosenName();
729               bool ret = subject->addInputPort(catalog, typeName, theName);
730               if (!ret) QMessageBox::warning(this, "Input Port not Created", GuiContext::getCurrent()->_lastErrorMessage );
731             }
732         }
733     }
734 }
735
736 void editTree::newDFOutputPort(YACS::ENGINE::Catalog* catalog,
737                                Subject* sub,
738                                std::string typeName)
739 {
740   if (sub)
741     {
742       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
743       if (subject)
744         {
745           std::stringstream name;
746           name << "OutDF_" << typeName << GuiContext::getCurrent()->getNewId();
747           ChooseName chooseName(subject->getName(), typeName, name.str());
748           chooseName.exec();
749           if (chooseName.isOk())
750             {
751               string theName = chooseName.getChoosenName();
752               bool ret = subject->addOutputPort(catalog, typeName, theName);
753               if (!ret) QMessageBox::warning(this, "Output Port not Created", GuiContext::getCurrent()->_lastErrorMessage );
754             }
755         }
756     }
757 }
758
759 void editTree::newDSInputPort(YACS::ENGINE::Catalog* catalog,
760                               Subject* sub,
761                               std::string typeName)
762 {
763   if (sub)
764     {
765       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
766       if (subject)
767         {
768           std::stringstream name;
769           name << "InDS_" << typeName << GuiContext::getCurrent()->getNewId();
770           ChooseName chooseName(subject->getName(), typeName, name.str());
771           chooseName.exec();
772           if (chooseName.isOk())
773             {
774               string theName = chooseName.getChoosenName();
775               bool ret = subject->addIDSPort(catalog, typeName, theName);
776               if (!ret) QMessageBox::warning(this, "Input Port not Created", GuiContext::getCurrent()->_lastErrorMessage );
777             }
778         }
779     }
780 }
781
782 void editTree::newDSOutputPort(YACS::ENGINE::Catalog* catalog,
783                                Subject* sub,
784                                std::string typeName)
785 {
786   if (sub)
787     {
788       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
789       if (subject)
790         {
791           std::stringstream name;
792           name << "OutDS_" << typeName << GuiContext::getCurrent()->getNewId();
793           ChooseName chooseName(subject->getName(), typeName, name.str());
794           chooseName.exec();
795           if (chooseName.isOk())
796             {
797               string theName = chooseName.getChoosenName();
798               bool ret = subject->addODSPort(catalog, typeName, theName);
799               if (!ret) QMessageBox::warning(this, "Output Port not Created", GuiContext::getCurrent()->_lastErrorMessage );
800             }
801         }
802     }
803 }
804
805
806 void editTree::newDFInputPort()
807 {
808   DEBTRACE("editTree::newDFInputPort");
809   Subject *sub =getSelectedSubject();
810   if (sub)
811     {
812       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
813       if (subject)
814         catalog(CATALOGINPUTPORT);
815     }
816
817 }
818
819 void editTree::newDFOutputPort()
820 {
821   DEBTRACE("editTree::newDFOutputPort");
822   Subject *sub =getSelectedSubject();
823   if (sub)
824     {
825       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
826       if (subject)
827         catalog(CATALOGOUTPUTPORT);
828     }
829 }
830 void editTree::newDSInputPort()
831 {
832   DEBTRACE("editTree::newDSInputPort");
833   Subject *sub =getSelectedSubject();
834   if (sub)
835     {
836       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
837       if (subject)
838         catalog(CATALOGIDSPORT);
839     }
840
841 }
842
843 void editTree::newDSOutputPort()
844 {
845   DEBTRACE("editTree::newDSOutputPort");
846   Subject *sub =getSelectedSubject();
847   if (sub)
848     {
849       YACS::HMI::SubjectElementaryNode *subject = dynamic_cast< YACS::HMI::SubjectElementaryNode*>(sub);
850       if (subject)
851         catalog(CATALOGODSPORT);
852     }
853 }
854
855 void editTree::catalog(int cataType)
856 {
857   Subject *sub =getSelectedSubject();
858   if (sub)
859     {
860       BrowseCatalog *brCat = new BrowseCatalog(this, sub, cataType);
861       brCat->exec();
862     }
863 }
864
865 void editTree::setCatalog(int isproc)
866 {
867   Subject *sub =getSelectedSubject();
868   if (sub)
869     {
870       if (isproc)
871         {
872           QString fn = QFileDialog::getOpenFileName( QString::null,
873                                                      tr( "XML-Files (*.xml);;All Files (*)" ),
874                                                      this,
875                                                      "load YACS scheme file dialog as catalog",
876                                                      "Choose a filename to load"  );
877           if ( !fn.isEmpty() )
878             {
879               YACS::ENGINE::Catalog* aCatalog = YACS::ENGINE::getSALOMERuntime()->loadCatalog( "proc", fn.latin1());
880               GuiContext::getCurrent()->setProcCatalog(aCatalog);
881               GuiContext::getCurrent()->setCurrentCatalog(aCatalog);
882             }
883         }
884       else
885         {
886           GuiContext::getCurrent()->setCurrentCatalog(GuiContext::getCurrent()->getSessionCatalog());
887         }
888     }
889 }
890
891
892 /*!
893  *
894  */
895
896 void editTree::select()
897 {
898   DEBTRACE("editTree::select() "<< this);
899   QListViewItem *it =lv->selectedItem();
900   ViewItem *item = 0;
901   if (it)                          // an item selected
902     {
903       if (it != _previousSelected) // a new item is selected
904         {
905           _previousSelected = it;
906           item = dynamic_cast<ViewItem*> (it);
907         }
908     }
909   else if (_previousSelected)      // nothing selected, deselect previous
910     {
911       it = _previousSelected;
912       _previousSelected = 0;
913       item = dynamic_cast<ViewItem*> (it);
914     }
915   if (item)
916     {
917         item->getSubject()->select(it->isSelected());
918         if (_selectedSubjectOutPort)
919           {
920             Subject *sub = item->getSubject();
921             if (dynamic_cast<SubjectInputPort*>(sub) || dynamic_cast<SubjectInputDataStreamPort*>(sub))
922               {
923                 SubjectDataPort *sdp = dynamic_cast<SubjectDataPort*>(sub);
924                 assert(sdp);
925                 SubjectDataPort::tryCreateLink(_selectedSubjectOutPort, sdp);
926                 _selectedSubjectOutPort = 0;
927               }
928           }
929         if (_selectedSubjectNode)
930           {
931             Subject *sub = item->getSubject();
932             if (SubjectNode* subNode = dynamic_cast<SubjectNode*>(sub))
933               {
934                 SubjectNode::tryCreateLink(_selectedSubjectNode, subNode);
935                 _selectedSubjectNode = 0;
936               }
937           }
938         if (_selectedSubjectService)
939           {
940             Subject *sub = item->getSubject();
941             if (SubjectComponent *subcomp = dynamic_cast<SubjectComponent*>(sub))
942               {
943                 _selectedSubjectService->associateToComponent(subcomp);
944                 _selectedSubjectService = 0;
945               }
946           }
947         if (_selectedSubjectComponent)
948           {
949             Subject *sub = item->getSubject();
950             if (SubjectContainer *subcont = dynamic_cast<SubjectContainer*>(sub))
951               {
952                 _selectedSubjectComponent->associateToContainer(subcont);
953                 _selectedSubjectComponent = 0;
954               }
955           }
956     }
957 }
958
959 /*!
960  *
961  */
962
963 void editTree::select(QListViewItem *it)
964 {
965   DEBTRACE("editTree::select(QListViewItem *item) "<< this);
966   ViewItem *item = dynamic_cast<ViewItem*> (it);
967   if (item) item->getSubject()->select(it->isSelected());
968 }
969
970 /*!
971  *
972  */
973
974 void editTree::contextMenuEvent( QContextMenuEvent * )
975 {
976   QListViewItem *it =lv->selectedItem();
977   ViewItem *item = 0;
978   if (it)
979     {
980       if (item = dynamic_cast<ComposedNodeViewItem*> (it))
981         ComposedNodeContextMenu();
982       else if (item = dynamic_cast<NodeViewItem*> (it))
983         NodeContextMenu();
984       else if (item = dynamic_cast<PortViewItem*> (it))
985         PortContextMenu();
986       else if (item = dynamic_cast<LinkViewItem*> (it))
987         LinkContextMenu();
988       else if (item = dynamic_cast<ComponentViewItem*> (it))
989         ComponentContextMenu();
990       else if (item = dynamic_cast<ContainerViewItem*> (it))
991         ContainerContextMenu();
992     }
993 }
994
995 /*!
996  *
997  */
998
999 void editTree::ComposedNodeContextMenu()
1000 {
1001   DEBTRACE("editTree::ComposedNodeContextMenu");
1002   QPopupMenu*   contextMenu = new QPopupMenu();
1003   Q_CHECK_PTR( contextMenu );
1004   QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
1005                                 "ComposedNode Context Menu</b></u></font>",
1006                                 contextMenu );
1007   caption->setAlignment( Qt::AlignCenter );
1008   int id;
1009   contextMenu->insertItem( caption );
1010   contextMenu->insertItem( "Message", this, SLOT(printName()) );
1011   contextMenu->insertItem( "Delete", this, SLOT(destroy()) );
1012   contextMenu->insertItem( "add control link to other node", this, SLOT(addLink()) );
1013   _keymap = 0;
1014   _catalogItemsMap.clear();
1015   YACS::ENGINE::Catalog *builtinCatalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
1016   {
1017     map<string,YACS::ENGINE::Node*>::iterator it;
1018     for (it=builtinCatalog->_nodeMap.begin(); it!=builtinCatalog->_nodeMap.end(); ++it)
1019       {
1020         string nodeType = "new " + (*it).first;
1021         //DEBTRACE(nodeType);
1022         id =contextMenu->insertItem( nodeType.c_str(), this,
1023                                          SLOT(newNode(int)) );
1024         std::pair<YACS::ENGINE::Catalog*, std::string> p(builtinCatalog, (*it).first);
1025         _catalogItemsMap[_keymap] = p;
1026         contextMenu->setItemParameter(id, _keymap++);
1027       }
1028   }
1029   {
1030     map<string,YACS::ENGINE::ComposedNode*>::iterator it;
1031     for (it=builtinCatalog->_composednodeMap.begin(); it!=builtinCatalog->_composednodeMap.end(); ++it)
1032       {
1033         string nodeType = "new " + (*it).first;
1034         //DEBTRACE(nodeType);
1035         id =contextMenu->insertItem( nodeType.c_str(), this,
1036                                          SLOT(newNode(int)) );
1037         std::pair<YACS::ENGINE::Catalog*, std::string> p(builtinCatalog, (*it).first);
1038         _catalogItemsMap[_keymap] = p;
1039         contextMenu->setItemParameter(id, _keymap++);
1040       }
1041   }
1042   id = contextMenu->insertItem( "set current catalog to session", this, SLOT(setCatalog(int)) );
1043   contextMenu->setItemParameter(id, 0);
1044   id = contextMenu->insertItem( "set current catalog to proc", this, SLOT(setCatalog(int)) );
1045   contextMenu->setItemParameter(id, 1);
1046   id = contextMenu->insertItem( "new node from current Catalog", this, SLOT(catalog(int)) );
1047   contextMenu->setItemParameter(id, CATALOGNODE);
1048   contextMenu->exec( QCursor::pos() );
1049   delete contextMenu;
1050 }
1051
1052 /*!
1053  *
1054  */
1055
1056 void editTree::NodeContextMenu()
1057 {
1058   QPopupMenu*   contextMenu = new QPopupMenu();
1059   Q_CHECK_PTR( contextMenu );
1060   QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
1061                                 "Elementary Node Context Menu</b></u></font>",
1062                                 contextMenu );
1063   caption->setAlignment( Qt::AlignCenter );
1064   contextMenu->insertItem( caption );
1065   contextMenu->insertItem( "Message", this, SLOT(printName()) );
1066   contextMenu->insertItem( "Delete", this, SLOT(destroy()) );
1067   contextMenu->insertItem( "add Component", this, SLOT(addComponent()) );
1068   contextMenu->insertItem( "associate to Component", this, SLOT(associateServiceToComponent()) );
1069   contextMenu->insertItem( "add control link to other node", this, SLOT(addLink()) );
1070   _keymap = 0;
1071   _catalogItemsMap.clear();
1072   YACS::ENGINE::Catalog *builtinCatalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
1073   {
1074     int id;
1075     map<string,YACS::ENGINE::TypeCode*>::iterator it;
1076     for (it=builtinCatalog->_typeMap.begin(); it!=builtinCatalog->_typeMap.end(); ++it)
1077       {
1078         std::pair<YACS::ENGINE::Catalog*, std::string> p(builtinCatalog, (*it).first);
1079         {
1080           string typeCode = "new input port " + (*it).first;
1081           DEBTRACE(typeCode);
1082           id =contextMenu->insertItem( typeCode.c_str(), this,
1083                                        SLOT(newInputPort(int)) );
1084           _catalogItemsMap[_keymap] = p;
1085           contextMenu->setItemParameter(id, _keymap++);
1086         }
1087         {
1088           string typeCode = "new output port " + (*it).first;
1089           DEBTRACE(typeCode);
1090           id =contextMenu->insertItem( typeCode.c_str(), this,
1091                                        SLOT(newOutputPort(int)) );
1092           _catalogItemsMap[_keymap] = p;
1093           contextMenu->setItemParameter(id, _keymap++);
1094         }
1095       }
1096     id = contextMenu->insertItem( "set current catalog to session", this, SLOT(setCatalog(int)) );
1097     contextMenu->setItemParameter(id, 0);
1098     id = contextMenu->insertItem( "set current catalog to proc", this, SLOT(setCatalog(int)) );
1099     contextMenu->setItemParameter(id, 1);
1100     id = contextMenu->insertItem( "new InputPort from catalog", this, SLOT(newDFInputPort()));
1101     id = contextMenu->insertItem( "new OutputPort from catalog", this, SLOT(newDFOutputPort()));
1102     id = contextMenu->insertItem( "new DataStream InputPort from catalog", this, SLOT(newDSInputPort()));
1103     id = contextMenu->insertItem( "new DataStream OutputPort from catalog", this, SLOT(newDSOutputPort()));
1104     id = contextMenu->insertItem( "add Data Type from current Catalog", this, SLOT(catalog(int)) );
1105     contextMenu->setItemParameter(id, CATALOGDATATYPE);
1106   }
1107   contextMenu->exec( QCursor::pos() );
1108   delete contextMenu;
1109 }
1110
1111 void editTree::PortContextMenu()
1112 {
1113   QPopupMenu*   contextMenu = new QPopupMenu();
1114   Q_CHECK_PTR( contextMenu );
1115   QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
1116                                 "Port Context Menu</b></u></font>",
1117                                 contextMenu );
1118   caption->setAlignment( Qt::AlignCenter );
1119   contextMenu->insertItem( caption );
1120   contextMenu->insertItem( "Message", this, SLOT(printName()) );
1121   contextMenu->insertItem( "Delete", this, SLOT(destroy()) );
1122   contextMenu->insertItem( "Add link", this, SLOT(addLink()) );
1123   contextMenu->insertItem( "edit properties", this, SLOT(editPortProperties()) );
1124   contextMenu->exec( QCursor::pos() );
1125   delete contextMenu;
1126 }
1127
1128 void editTree::LinkContextMenu()
1129 {
1130   QPopupMenu*   contextMenu = new QPopupMenu();
1131   Q_CHECK_PTR( contextMenu );
1132   QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
1133                                 "Link Context Menu</b></u></font>",
1134                                 contextMenu );
1135   caption->setAlignment( Qt::AlignCenter );
1136   contextMenu->insertItem( caption );
1137   contextMenu->insertItem( "Message", this, SLOT(printName()) );
1138   contextMenu->insertItem( "Delete", this, SLOT(destroy()) );
1139   contextMenu->exec( QCursor::pos() );
1140   delete contextMenu;
1141 }
1142
1143 void  editTree::ComponentContextMenu()
1144 {
1145   QPopupMenu*   contextMenu = new QPopupMenu();
1146   Q_CHECK_PTR( contextMenu );
1147   QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
1148                                 "Component Context Menu</b></u></font>",
1149                                 contextMenu );
1150   caption->setAlignment( Qt::AlignCenter );
1151   contextMenu->insertItem( caption );
1152   contextMenu->insertItem( "Message", this, SLOT(printName()) );
1153   contextMenu->insertItem( "add Container", this, SLOT(addContainer()) );
1154   contextMenu->insertItem( "associate to Container", this, SLOT(associateComponentToContainer()) );
1155   contextMenu->exec( QCursor::pos() );
1156   delete contextMenu;
1157 }
1158
1159 void  editTree::ContainerContextMenu()
1160 {
1161   QPopupMenu*   contextMenu = new QPopupMenu();
1162   Q_CHECK_PTR( contextMenu );
1163   QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
1164                                 "Container Context Menu</b></u></font>",
1165                                 contextMenu );
1166   caption->setAlignment( Qt::AlignCenter );
1167   contextMenu->insertItem( caption );
1168   contextMenu->insertItem( "Message", this, SLOT(printName()) );
1169   contextMenu->insertItem( "edit container properties", this, SLOT(editContainerProperties()) );
1170   contextMenu->exec( QCursor::pos() );
1171   delete contextMenu;
1172 }