2 // File : SUPERVGUI_Service.cxx
3 // Created : 7 / 11 / 2001
4 // Author : Francis KLOSS
9 #include "SUPERVGUI_Service.h"
10 #include "SUPERVGUI_Main.h"
11 #include "SUPERVGUI.h"
12 #include "QAD_Tools.h"
13 #include "QAD_FileDlg.h"
15 #include "SALOME_NamingService.hxx"
16 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
18 #include <qtextstream.h>
21 static const char * ComponentIcon[] = {
28 " .................. ",
37 " .................. ",
41 " .. .. .. .. .. .. ",
43 " .. .. .. .. .. .. ",
47 static const char * InterfaceIcon[] = {
75 SUPERVGUI_Service::SUPERVGUI_Service(SALOME_NamingService* ns):
76 QDialog(QAD_Application::getDesktop(), 0, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
79 setSizeGripEnabled( true );
80 setCaption(tr("TIT_SERVICES"));
82 QVBoxLayout* aMainLayout = new QVBoxLayout(this, 7, 4);
84 myTabPane = new QTabWidget(this);
85 connect(myTabPane, SIGNAL(currentChanged(QWidget *)), this, SLOT(tabChanged(QWidget *)));
87 // Create Tab for Corba services
88 QWidget* aCorbaPane = new QWidget(myTabPane);
89 QVBoxLayout* aBaseLayoutV= new QVBoxLayout(aCorbaPane, 0, 4);
90 aBaseLayoutV->setMargin(5);
91 aBaseLayoutV->setSpacing(10);
93 QHBoxLayout* aBaseLayout = new QHBoxLayout(aCorbaPane); //!!
95 components = new QListView(aCorbaPane);
96 components->addColumn(tr("COL_COMPONENTS"));
97 components->addColumn(tr("COL_PORTTYPE"));
98 components->addColumn(tr("COL_PORTWAY"));
99 components->setColumnAlignment(1, AlignLeft);
100 components->setColumnAlignment(2, AlignLeft);
101 components->setColumnAlignment(3, AlignLeft);
102 components->setSelectionMode(QListView::Extended);
103 components->setRootIsDecorated(true);
104 // aBaseLayout->addWidget(components);
105 aBaseLayoutV->addWidget(components); //!!
107 //QHGroupBox* aAddBox = new QHGroupBox(tr("TIT_ADDNODE"), aCorbaPane); //!!
108 //aAddBox->setInsideSpacing(20); //!!
110 QPushButton* aComputeCBtn = new QPushButton(tr("TIT_ADDCNODE"), aCorbaPane); //!!
111 connect(aComputeCBtn, SIGNAL(clicked()), this, SLOT(addComputeNode())); //!!
112 aComputeCBtn->setDefault(false);
114 QPushButton* aComputeBtn = new QPushButton(tr("TIT_ADDFNODE"), aCorbaPane);
115 connect(aComputeBtn, SIGNAL(clicked()), this, SLOT(addFactoryNode()));
116 aComputeBtn->setDefault(true);
118 aBaseLayout->addWidget(aComputeBtn);
119 aBaseLayout->addWidget(aComputeCBtn); //!!
121 aBaseLayoutV->insertLayout(-1, aBaseLayout);
122 myTabPane->addTab(aCorbaPane, tr("MODULES_PANE"));
125 // Create Tab for Python services
126 QWidget* aPythonPane = new QWidget(myTabPane);
127 QVBoxLayout* aPythonLayout = new QVBoxLayout(aPythonPane, 0, 4);
128 aPythonLayout->setMargin(5);
129 aPythonLayout->setSpacing(10);
132 QHGroupBox* aAddBox2 = new QHGroupBox(tr("TIT_ADDNODE"), aPythonPane);
133 aAddBox2->setInsideSpacing(20);
135 QLabel* aTypeLbl = new QLabel(tr("LBL_NODETYPE"), aAddBox2);
137 myTypeCombo = new QComboBox(aAddBox2);
138 myTypeCombo->insertItem("Computation");
139 myTypeCombo->insertItem("Switch");
140 myTypeCombo->insertItem("Loop");
141 myTypeCombo->insertItem("GoTo");
142 //myTypeCombo->insertItem("Label");
143 connect(myTypeCombo, SIGNAL(activated(int)), this, SLOT(typeNodeSelected(int)));
145 aPythonLayout->addWidget(aAddBox2);
148 myStackWidget = new QWidgetStack(aPythonPane);
151 myScriptPane = new SUPERVGUI_PythonEditPane(myStackWidget);
152 myOtherId = myStackWidget->addWidget(myScriptPane);
155 QTabWidget* aLoopTabPane = new QTabWidget(myStackWidget);
156 myInitPane = new SUPERVGUI_PythonEditPane(myStackWidget);
157 aLoopTabPane->addTab(myInitPane, "Init");
159 myMorePane = new SUPERVGUI_PythonEditPane(myStackWidget);
160 aLoopTabPane->addTab(myMorePane, "More");
162 myNextPane = new SUPERVGUI_PythonEditPane(myStackWidget);
163 aLoopTabPane->addTab(myNextPane, "Next");
164 myLoopId = myStackWidget->addWidget(aLoopTabPane);
166 myStackWidget->raiseWidget(myOtherId);
168 aPythonLayout->addWidget(myStackWidget, 1);
171 QPushButton* aCreateBtn = new QPushButton(tr("TIT_ADDNODE"), aPythonPane);
172 connect(aCreateBtn, SIGNAL(clicked()), this, SLOT(addInlineNode()));
173 aPythonLayout->addWidget(aCreateBtn);
175 myTabPane->addTab(aPythonPane, tr("INLINE_PANE"));
177 aMainLayout->addWidget(myTabPane);
180 QHBox* aBtnBox = new QHBox(this);
181 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnBox->layout());
182 aBtnLayout->addStretch();
184 QPushButton* aCloseBtn = new QPushButton(tr("BUT_CLOSE"), aBtnBox);
185 connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(reject()));
187 aMainLayout->addWidget(aBtnBox);
197 void SUPERVGUI_Service::initialise() {
198 CORBA::Object_ptr obj = naming->Resolve("/Kernel/ModulCatalog");
199 SALOME_ModuleCatalog::ModuleCatalog_var *aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
200 *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
201 if (CORBA::is_nil(*aModuleCatalog)) {
202 setCaption("Error in Connexion to omniNames with '/Kernel/ModulCatalog'");
206 QAD_ResourceMgr* aResMgr = QAD_Desktop::createResourceManager();
208 SALOME_ModuleCatalog::ListOfComponents_var lComponents = (*aModuleCatalog)->GetComponentList();
209 long nbComp = lComponents->length();
210 for (int i=0; i<nbComp; i++) {
211 SALOME_ModuleCatalog::Acomponent_ptr C = (*aModuleCatalog)->GetComponent((char *)lComponents[i]);
212 QListViewItem* myComponentItem = new QListViewItem(components, (char*)lComponents[i]);
213 myComponentItem->setSelectable(false);
214 QString aIconName = C->component_icone();
215 if (!aIconName.isEmpty()) {
216 QString resDir = aResMgr->findFile(aIconName, C->componentname()) ;
218 resDir = QAD_Tools::addSlash(resDir);
219 QPixmap aIcone(resDir + aIconName);
220 QIconSet aIconSet(aIcone);
221 myComponentItem->setPixmap(0, aIconSet.pixmap(QIconSet::Small, QIconSet::Normal));
223 myComponentItem->setPixmap(0, ComponentIcon);
226 myComponentItem->setPixmap(0, ComponentIcon);
228 SALOME_ModuleCatalog::ListOfInterfaces* lInterfaces = C->GetInterfaceList();
229 long nbInterf = lInterfaces->length();
230 for (int j=0; j<nbInterf; j++) {
231 SALOME_ModuleCatalog::DefinitionInterface* Interface = C->GetInterface((char*)(*lInterfaces)[j]);
232 QListViewItem* myInterfaceItem = new QListViewItem(myComponentItem, (char*)Interface->interfacename);
233 myInterfaceItem->setSelectable(false);
234 myInterfaceItem->setPixmap(0, InterfaceIcon);
236 long nbServices = Interface->interfaceservicelist.length();
237 for (int k=0; k<nbServices; k++) {
238 SALOME_ModuleCatalog::Service* Service = &(Interface->interfaceservicelist[k]);
239 QListViewItem* myServiceItem = new QListViewItem(myInterfaceItem, (char*)Service->ServiceName);
240 myServiceItem->setSelectable(true);
241 components->ensureItemVisible(myServiceItem);
243 long nbPortsOut = Service->ServiceoutParameter.length();
244 for (int m=0; m<nbPortsOut; m++) {
245 SALOME_ModuleCatalog::ServicesParameter* PortOut = &(Service->ServiceoutParameter[m]);
246 QListViewItem* myPortOutItem = new QListViewItem(myServiceItem, (char*)PortOut->Parametername, (char*)PortOut->Parametertype, "Out");
247 myPortOutItem->setSelectable(false);
250 long nbPortsIn = Service->ServiceinParameter.length();
251 for (int l=0; l<nbPortsIn; l++) {
252 SALOME_ModuleCatalog::ServicesParameter* PortIn = &(Service->ServiceinParameter[l]);
253 QListViewItem* myPortInItem = new QListViewItem(myServiceItem, (char*)PortIn->Parametername, (char*)PortIn->Parametertype, "In");
254 myPortInItem->setSelectable(false);
263 SUPERVGUI_Service::~SUPERVGUI_Service() {
264 Trace("SUPERVGUI_Service::~SUPERVGUI_Service")
267 void SUPERVGUI_Service::addComputeNode() {
268 SUPERVGUI_Main* aMain = Supervision.getMain();
270 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
272 CORBA::Object_ptr obj = naming->Resolve("/Kernel/ModulCatalog");
273 SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
274 *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
275 if (CORBA::is_nil(*aModuleCatalog)) {
276 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
280 QListViewItemIterator i(components);
281 for (; i.current(); ++i) {
283 if (item->isSelected()) {
284 const char* service = item->text(0).latin1();
285 const char* interface = item->parent()->text(0).latin1();
286 const char* component = item->parent()->parent()->text(0).latin1();
287 SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(component);
288 if (myComponent==NULL) {
289 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
291 const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
294 SUPERV_CNode node = aMain->getDataflow()->CNode(*myService);
295 if (CORBA::is_nil(node)) {
296 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
299 node->Coords(myX, myY);
302 aMain->addComputeNode(SUPERV::CNode::_narrow(node));
307 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NONODE_TOADD"));
313 void SUPERVGUI_Service::addFactoryNode() {
314 SUPERVGUI_Main* aMain = Supervision.getMain();
316 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
318 CORBA::Object_ptr obj = naming->Resolve("/Kernel/ModulCatalog");
319 SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
320 *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
321 if (CORBA::is_nil(*aModuleCatalog)) {
322 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
326 QListViewItemIterator i(components);
327 for (; i.current(); ++i) {
329 if (item->isSelected()) {
330 const char* service = item->text(0).latin1();
331 const char* interface = item->parent()->text(0).latin1();
332 const char* component = item->parent()->parent()->text(0).latin1();
333 SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(component);
334 if (myComponent==NULL) {
335 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
337 const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
340 SUPERV_FNode node = aMain->getDataflow()->FNode(component, interface, *myService);
341 if (CORBA::is_nil(node)) {
342 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
345 node->Coords(myX, myY);
348 aMain->addComputeNode(SUPERV::CNode::_narrow(node));
353 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NONODE_TOADD"));
360 void SUPERVGUI_Service::addInlineNode() {
361 SUPERVGUI_Main* aMain = Supervision.getMain();
363 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
365 int aSel = myTypeCombo->currentItem();
367 case 0: // Computation
368 if (myScriptPane->isDefined()) {
369 SUPERV_INode aNode = aMain->getDataflow()->INode(myScriptPane->getFuncName().latin1(),
370 (myScriptPane->getFunction()).in());
371 if (CORBA::is_nil(aNode)) {
372 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
375 aNode->Coords(myX, myY);
378 aMain->addComputeNode(SUPERV::CNode::_narrow(aNode));
383 if (myScriptPane->isDefined()) {
384 SUPERV_INode aEndNode;
385 SUPERV_SNode aStartNode = aMain->getDataflow()->SNode(myScriptPane->getFuncName().latin1(),
386 (myScriptPane->getFunction()).in(),
388 if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
389 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
392 aStartNode->Coords(myX, myY);
393 aEndNode->Coords(myX + LABEL_WIDTH*2, myY);
396 aMain->addControlNode(SUPERV::CNode::_narrow(aStartNode), SUPERV::CNode::_narrow(aEndNode), true);
402 SUPERV_INode aEndNode;
403 SUPERV_LNode aStartNode = aMain->getDataflow()->LNode(myInitPane->getFuncName().latin1(), (myInitPane->getFunction()).in(),
404 myMorePane->getFuncName().latin1(), (myMorePane->getFunction()).in(),
405 myNextPane->getFuncName().latin1(), (myNextPane->getFunction()).in(),
407 if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
408 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
411 aStartNode->Coords(myX, myY);
412 aEndNode->Coords(myX + LABEL_WIDTH*2, myY);
415 aMain->addControlNode(SUPERV::CNode::_narrow(aStartNode), SUPERV::CNode::_narrow(aEndNode), true);
421 SUPERV_GNode aGotoNode;
422 if (myScriptPane->isDefined())
423 aGotoNode = aMain->getDataflow()->GNode(myScriptPane->getFuncName().latin1(),
424 (myScriptPane->getFunction()).in(), "");
426 aGotoNode = aMain->getDataflow()->GNode("GoTo", (myScriptPane->getFunction()).in(), "");
427 if (CORBA::is_nil(aGotoNode)) {
428 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
431 aGotoNode->Coords(myX, myY);
434 aMain->addGOTONode(SUPERV::GNode::_narrow(aGotoNode));
443 void SUPERVGUI_Service::typeNodeSelected(int theRow) {
445 myStackWidget->raiseWidget(myLoopId);
447 myStackWidget->raiseWidget(myOtherId);
452 void SUPERVGUI_Service::choose() {
453 Trace("SUPERVGUI_Service::choose")
459 void SUPERVGUI_Service::showEvent(QShowEvent* theEvent) {
460 SUPERVGUI_Main* aMain = Supervision.getMain();
461 if (aMain && (!aMain->isArrayShown())) {
462 aMain->getGraph()->viewportToContents(0, 0, myX, myY);
464 QDialog::showEvent(theEvent);
468 void SUPERVGUI_Service::tabChanged(QWidget* theWidget) {
469 myIsInline = (myTabPane->currentPageIndex() == 1);
474 //*****************************************************
475 // Pane for Python script editing
476 //*****************************************************
477 SUPERVGUI_PythonEditPane::SUPERVGUI_PythonEditPane(QWidget* theParent)
481 QGridLayout* aEditLayout = new QGridLayout(this, 2, 4);
484 QPushButton* aLoadBtn = new QPushButton(tr("BUT_LOAD"), this);
485 connect(aLoadBtn, SIGNAL(clicked()), this, SLOT(loadFile()));
487 aEditLayout->addWidget(aLoadBtn, 0, 2);
489 myNextBtn = new QPushButton(tr("BUT_NEXT"), this);
490 myNextBtn->setEnabled(false);
491 connect(myNextBtn, SIGNAL(clicked()), this, SLOT(readFunction()));
493 aEditLayout->addWidget(myNextBtn, 0, 3);
496 myText = new QTextEdit(this);
497 myText->setWordWrap(QTextEdit::FixedColumnWidth);
498 myText->setWrapColumnOrWidth(80);
500 aEditLayout->addMultiCellWidget(myText, 1, 1, 0, 3);
505 * Load existing Python script
507 void SUPERVGUI_PythonEditPane::loadFile() {
508 QString aFileName = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
511 tr("TIT_LOADSCRIPT"),
513 if (aFileName.isEmpty()) return;
515 myFile = new QFile(aFileName);
516 if (!myFile->open(IO_ReadOnly)) {
517 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"),
518 tr("MSG_CANT_LOADSCRIPT"));
521 myIStream = new QTextStream(myFile);
523 if (myIStream->atEnd()) {
524 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"),
525 tr("MSG_EMTY_FILE"));
526 myNextBtn->setEnabled(false);
533 myNextBtn->setEnabled(true);
539 * Finds and Reads a function from current position in the opened file
541 void SUPERVGUI_PythonEditPane::readFunction() {
542 if (!myIStream) return;
544 while (!myIStream->atEnd()) {
545 QString aLine = myIStream->readLine();
546 if (aLine.isNull()) return;
548 // find first function
549 int aDefPos = aLine.find("def");
550 if (aDefPos == 0) { // only not a class members
553 /*int aStart = aLine.find(" ", aDefPos);
554 int aEnd = aLine.find("(", aStart);
555 QString aName = aLine.mid(aStart, (aEnd-aStart));
556 aName = aName.stripWhiteSpace();
557 myNameEdt->setText(aName);*/
561 aEnd = aLine.find(")", aStart);
562 QString aParams = aLine.mid(aStart, (aEnd-aStart));
563 aParams = aParams.stripWhiteSpace();
564 myParamEdt->setText(aParams);*/
566 myText->append(aLine);
568 QString aBodyLine = myIStream->readLine();
569 while ((!aBodyLine.isNull()) && aBodyLine[0].isSpace()) {
570 myText->append(aBodyLine);
571 aBodyLine = myIStream->readLine();
576 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"),
577 tr("MSG_NOMORE_FUNCTIONS"));
580 myNextBtn->setEnabled(false);
586 QString SUPERVGUI_PythonEditPane::getFuncName() {
588 for (int i=0; i < myText->paragraphs(); i++) {
589 QString aLine = myText->text(i);
590 int aDefPos = aLine.find("def");
592 int aStart = aLine.find(" ", aDefPos);
593 int aEnd = aLine.find("(", aStart);
594 QString aName = aLine.mid(aStart, (aEnd-aStart));
595 return aName.stripWhiteSpace();
602 SUPERV_Strings SUPERVGUI_PythonEditPane::getFunction() {
603 SUPERV_Strings aStrings = new SUPERV::ListOfStrings();
604 aStrings->length(myText->paragraphs());
605 for (int i=0; i < myText->paragraphs(); i++) {
606 aStrings[i] = CORBA::string_dup(myText->text(i).latin1());
608 return aStrings._retn();
612 void SUPERVGUI_PythonEditPane::setFunction(SUPERV_Strings theStr) {
613 int aLen = theStr->length();
614 for (int i=0; i < aLen; i++)
615 myText->append(QString(theStr[i]));
623 SUPERVGUI_EditPythonDlg::SUPERVGUI_EditPythonDlg(bool isLoop)
624 :QDialog(QAD_Application::getDesktop(), 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
626 setSizeGripEnabled( true );
627 setCaption(tr("TIT_FUNC_PYTHON"));
629 QVBoxLayout* aMainLayout = new QVBoxLayout(this, 7, 4);
631 QTabWidget* aLoopTabPane = new QTabWidget(this);
632 myInitPane = new SUPERVGUI_PythonEditPane(this);
633 aLoopTabPane->addTab(myInitPane, "Init");
635 myMorePane = new SUPERVGUI_PythonEditPane(this);
636 aLoopTabPane->addTab(myMorePane, "More");
638 myNextPane = new SUPERVGUI_PythonEditPane(this);
639 aLoopTabPane->addTab(myNextPane, "Next");
641 aMainLayout->addWidget(aLoopTabPane);
643 myEditPane = new SUPERVGUI_PythonEditPane(this);
644 aMainLayout->addWidget(myEditPane);
646 QGroupBox* aBtnBox = new QGroupBox( this );
647 aBtnBox->setColumnLayout( 0, Qt::Vertical );
648 aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
649 QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
650 aBtnLayout->setAlignment( Qt::AlignTop );
651 aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
653 QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
654 connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
655 aBtnLayout->addWidget( aOKBtn );
657 aBtnLayout->addStretch();
659 QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
660 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
661 aBtnLayout->addWidget( aCancelBtn );
663 aMainLayout->addWidget(aBtnBox);