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())); //!!
113 QPushButton* aComputeBtn = new QPushButton(tr("TIT_ADDFNODE"), aCorbaPane);
114 connect(aComputeBtn, SIGNAL(clicked()), this, SLOT(addFactoryNode()));
116 aBaseLayout->addWidget(aComputeBtn);
117 aBaseLayout->addWidget(aComputeCBtn); //!!
119 aBaseLayoutV->insertLayout(-1, aBaseLayout);
120 myTabPane->addTab(aCorbaPane, tr("MODULES_PANE"));
123 // Create Tab for Python services
124 QWidget* aPythonPane = new QWidget(myTabPane);
125 QVBoxLayout* aPythonLayout = new QVBoxLayout(aPythonPane, 0, 4);
126 aPythonLayout->setMargin(5);
127 aPythonLayout->setSpacing(10);
130 QHGroupBox* aAddBox2 = new QHGroupBox(tr("TIT_ADDNODE"), aPythonPane);
131 aAddBox2->setInsideSpacing(20);
133 QLabel* aTypeLbl = new QLabel(tr("LBL_NODETYPE"), aAddBox2);
135 myTypeCombo = new QComboBox(aAddBox2);
136 myTypeCombo->insertItem("Computation");
137 myTypeCombo->insertItem("Switch");
138 myTypeCombo->insertItem("Loop");
139 myTypeCombo->insertItem("GoTo");
140 //myTypeCombo->insertItem("Label");
141 connect(myTypeCombo, SIGNAL(activated(int)), this, SLOT(typeNodeSelected(int)));
143 aPythonLayout->addWidget(aAddBox2);
146 myStackWidget = new QWidgetStack(aPythonPane);
149 myScriptPane = new SUPERVGUI_PythonEditPane(myStackWidget);
150 myOtherId = myStackWidget->addWidget(myScriptPane);
153 QTabWidget* aLoopTabPane = new QTabWidget(myStackWidget);
154 myInitPane = new SUPERVGUI_PythonEditPane(myStackWidget);
155 aLoopTabPane->addTab(myInitPane, "Init");
157 myMorePane = new SUPERVGUI_PythonEditPane(myStackWidget);
158 aLoopTabPane->addTab(myMorePane, "More");
160 myNextPane = new SUPERVGUI_PythonEditPane(myStackWidget);
161 aLoopTabPane->addTab(myNextPane, "Next");
162 myLoopId = myStackWidget->addWidget(aLoopTabPane);
164 myStackWidget->raiseWidget(myOtherId);
166 aPythonLayout->addWidget(myStackWidget, 1);
169 QPushButton* aCreateBtn = new QPushButton(tr("TIT_ADDNODE"), aPythonPane);
170 connect(aCreateBtn, SIGNAL(clicked()), this, SLOT(addInlineNode()));
171 aPythonLayout->addWidget(aCreateBtn);
173 myTabPane->addTab(aPythonPane, tr("INLINE_PANE"));
175 aMainLayout->addWidget(myTabPane);
178 QHBox* aBtnBox = new QHBox(this);
179 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnBox->layout());
180 aBtnLayout->addStretch();
182 QPushButton* aCloseBtn = new QPushButton(tr("BUT_CLOSE"), aBtnBox);
183 connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(reject()));
185 aMainLayout->addWidget(aBtnBox);
195 void SUPERVGUI_Service::initialise() {
196 CORBA::Object_ptr obj = naming->Resolve("/Kernel/ModulCatalog");
197 SALOME_ModuleCatalog::ModuleCatalog_var *aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
198 *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
199 if (CORBA::is_nil(*aModuleCatalog)) {
200 setCaption("Error in Connexion to omniNames with '/Kernel/ModulCatalog'");
204 QAD_ResourceMgr* aResMgr = QAD_Desktop::createResourceManager();
206 SALOME_ModuleCatalog::ListOfComponents_var lComponents = (*aModuleCatalog)->GetComponentList();
207 long nbComp = lComponents->length();
208 for (int i=0; i<nbComp; i++) {
209 SALOME_ModuleCatalog::Acomponent_ptr C = (*aModuleCatalog)->GetComponent((char *)lComponents[i]);
210 QListViewItem* myComponentItem = new QListViewItem(components, (char*)lComponents[i]);
211 myComponentItem->setSelectable(false);
212 QString aIconName = C->component_icone();
213 if (!aIconName.isEmpty()) {
214 QString resDir = aResMgr->findFile(aIconName, C->componentname()) ;
216 resDir = QAD_Tools::addSlash(resDir);
217 QPixmap aIcone(resDir + aIconName);
218 QIconSet aIconSet(aIcone);
219 myComponentItem->setPixmap(0, aIconSet.pixmap(QIconSet::Small, QIconSet::Normal));
221 myComponentItem->setPixmap(0, ComponentIcon);
224 myComponentItem->setPixmap(0, ComponentIcon);
226 SALOME_ModuleCatalog::ListOfInterfaces* lInterfaces = C->GetInterfaceList();
227 long nbInterf = lInterfaces->length();
228 for (int j=0; j<nbInterf; j++) {
229 SALOME_ModuleCatalog::DefinitionInterface* Interface = C->GetInterface((char*)(*lInterfaces)[j]);
230 QListViewItem* myInterfaceItem = new QListViewItem(myComponentItem, (char*)Interface->interfacename);
231 myInterfaceItem->setSelectable(false);
232 myInterfaceItem->setPixmap(0, InterfaceIcon);
234 long nbServices = Interface->interfaceservicelist.length();
235 for (int k=0; k<nbServices; k++) {
236 SALOME_ModuleCatalog::Service* Service = &(Interface->interfaceservicelist[k]);
237 QListViewItem* myServiceItem = new QListViewItem(myInterfaceItem, (char*)Service->ServiceName);
238 myServiceItem->setSelectable(true);
239 components->ensureItemVisible(myServiceItem);
241 long nbPortsOut = Service->ServiceoutParameter.length();
242 for (int m=0; m<nbPortsOut; m++) {
243 SALOME_ModuleCatalog::ServicesParameter* PortOut = &(Service->ServiceoutParameter[m]);
244 QListViewItem* myPortOutItem = new QListViewItem(myServiceItem, (char*)PortOut->Parametername, (char*)PortOut->Parametertype, "Out");
245 myPortOutItem->setSelectable(false);
248 long nbPortsIn = Service->ServiceinParameter.length();
249 for (int l=0; l<nbPortsIn; l++) {
250 SALOME_ModuleCatalog::ServicesParameter* PortIn = &(Service->ServiceinParameter[l]);
251 QListViewItem* myPortInItem = new QListViewItem(myServiceItem, (char*)PortIn->Parametername, (char*)PortIn->Parametertype, "In");
252 myPortInItem->setSelectable(false);
261 SUPERVGUI_Service::~SUPERVGUI_Service() {
262 Trace("SUPERVGUI_Service::~SUPERVGUI_Service")
265 void SUPERVGUI_Service::addComputeNode() {
266 SUPERVGUI_Main* aMain = Supervision.getMain();
268 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
270 CORBA::Object_ptr obj = naming->Resolve("/Kernel/ModulCatalog");
271 SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
272 *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
273 if (CORBA::is_nil(*aModuleCatalog)) {
274 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
278 QListViewItemIterator i(components);
279 for (; i.current(); ++i) {
281 if (item->isSelected()) {
282 const char* service = item->text(0).latin1();
283 const char* interface = item->parent()->text(0).latin1();
284 const char* component = item->parent()->parent()->text(0).latin1();
285 SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(component);
286 if (myComponent==NULL) {
287 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
289 const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
292 SUPERV_CNode node = aMain->getDataflow()->CNode(*myService);
293 if (CORBA::is_nil(node)) {
294 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
297 node->Coords(myX, myY);
300 aMain->addComputeNode(SUPERV::CNode::_narrow(node));
305 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NONODE_TOADD"));
311 void SUPERVGUI_Service::addFactoryNode() {
312 SUPERVGUI_Main* aMain = Supervision.getMain();
314 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
316 CORBA::Object_ptr obj = naming->Resolve("/Kernel/ModulCatalog");
317 SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
318 *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
319 if (CORBA::is_nil(*aModuleCatalog)) {
320 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
324 QListViewItemIterator i(components);
325 for (; i.current(); ++i) {
327 if (item->isSelected()) {
328 const char* service = item->text(0).latin1();
329 const char* interface = item->parent()->text(0).latin1();
330 const char* component = item->parent()->parent()->text(0).latin1();
331 SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(component);
332 if (myComponent==NULL) {
333 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
335 const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
338 SUPERV_FNode node = aMain->getDataflow()->FNode(component, interface, *myService);
339 if (CORBA::is_nil(node)) {
340 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
343 node->Coords(myX, myY);
346 aMain->addComputeNode(SUPERV::CNode::_narrow(node));
351 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NONODE_TOADD"));
358 void SUPERVGUI_Service::addInlineNode() {
359 SUPERVGUI_Main* aMain = Supervision.getMain();
361 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
363 int aSel = myTypeCombo->currentItem();
365 case 0: // Computation
366 if (myScriptPane->isDefined()) {
367 SUPERV_INode aNode = aMain->getDataflow()->INode(myScriptPane->getFuncName().latin1(),
368 (myScriptPane->getFunction()).in());
369 if (CORBA::is_nil(aNode)) {
370 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
373 aNode->Coords(myX, myY);
376 aMain->addComputeNode(SUPERV::CNode::_narrow(aNode));
381 if (myScriptPane->isDefined()) {
382 SUPERV_INode aEndNode;
383 SUPERV_SNode aStartNode = aMain->getDataflow()->SNode(myScriptPane->getFuncName().latin1(),
384 (myScriptPane->getFunction()).in(),
386 if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
387 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
390 aStartNode->Coords(myX, myY);
391 aEndNode->Coords(myX + LABEL_WIDTH*2, myY);
394 aMain->addControlNode(SUPERV::CNode::_narrow(aStartNode), SUPERV::CNode::_narrow(aEndNode));
400 SUPERV_INode aEndNode;
401 SUPERV_LNode aStartNode = aMain->getDataflow()->LNode(myInitPane->getFuncName().latin1(), (myInitPane->getFunction()).in(),
402 myMorePane->getFuncName().latin1(), (myMorePane->getFunction()).in(),
403 myNextPane->getFuncName().latin1(), (myNextPane->getFunction()).in(),
405 if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
406 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
409 aStartNode->Coords(myX, myY);
410 aEndNode->Coords(myX + LABEL_WIDTH*2, myY);
413 aMain->addControlNode(SUPERV::CNode::_narrow(aStartNode), SUPERV::CNode::_narrow(aEndNode));
419 SUPERV_GNode aGotoNode;
420 if (myScriptPane->isDefined())
421 aGotoNode = aMain->getDataflow()->GNode(myScriptPane->getFuncName().latin1(),
422 (myScriptPane->getFunction()).in(), "");
424 aGotoNode = aMain->getDataflow()->GNode("GoTo", (myScriptPane->getFunction()).in(), "");
425 if (CORBA::is_nil(aGotoNode)) {
426 QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
429 aGotoNode->Coords(myX, myY);
432 aMain->addGOTONode(SUPERV::GNode::_narrow(aGotoNode));
441 void SUPERVGUI_Service::typeNodeSelected(int theRow) {
443 myStackWidget->raiseWidget(myLoopId);
445 myStackWidget->raiseWidget(myOtherId);
450 void SUPERVGUI_Service::choose() {
451 Trace("SUPERVGUI_Service::choose")
457 void SUPERVGUI_Service::showEvent(QShowEvent* theEvent) {
458 SUPERVGUI_Main* aMain = Supervision.getMain();
459 if (aMain && (!aMain->isArrayShown())) {
460 aMain->getGraph()->viewportToContents(0, 0, myX, myY);
462 QDialog::showEvent(theEvent);
466 void SUPERVGUI_Service::tabChanged(QWidget* theWidget) {
467 myIsInline = (myTabPane->currentPageIndex() == 1);
472 //*****************************************************
473 // Pane for Python script editing
474 //*****************************************************
475 SUPERVGUI_PythonEditPane::SUPERVGUI_PythonEditPane(QWidget* theParent)
479 QGridLayout* aEditLayout = new QGridLayout(this, 2, 4);
482 QPushButton* aLoadBtn = new QPushButton(tr("BUT_LOAD"), this);
483 connect(aLoadBtn, SIGNAL(clicked()), this, SLOT(loadFile()));
485 aEditLayout->addWidget(aLoadBtn, 0, 2);
487 myNextBtn = new QPushButton(tr("BUT_NEXT"), this);
488 myNextBtn->setEnabled(false);
489 connect(myNextBtn, SIGNAL(clicked()), this, SLOT(readFunction()));
491 aEditLayout->addWidget(myNextBtn, 0, 3);
494 myText = new QTextEdit(this);
495 myText->setWordWrap(QTextEdit::FixedColumnWidth);
496 myText->setWrapColumnOrWidth(80);
498 aEditLayout->addMultiCellWidget(myText, 1, 1, 0, 3);
503 * Load existing Python script
505 void SUPERVGUI_PythonEditPane::loadFile() {
506 QString aFileName = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
509 tr("TIT_LOADSCRIPT"),
511 if (aFileName.isEmpty()) return;
513 myFile = new QFile(aFileName);
514 if (!myFile->open(IO_ReadOnly)) {
515 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"),
516 tr("MSG_CANT_LOADSCRIPT"));
519 myIStream = new QTextStream(myFile);
521 if (myIStream->atEnd()) {
522 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"),
523 tr("MSG_EMTY_FILE"));
524 myNextBtn->setEnabled(false);
531 myNextBtn->setEnabled(true);
537 * Finds and Reads a function from current position in the opened file
539 void SUPERVGUI_PythonEditPane::readFunction() {
540 if (!myIStream) return;
542 while (!myIStream->atEnd()) {
543 QString aLine = myIStream->readLine();
544 if (aLine.isNull()) return;
546 // find first function
547 int aDefPos = aLine.find("def");
548 if (aDefPos == 0) { // only not a class members
551 /*int aStart = aLine.find(" ", aDefPos);
552 int aEnd = aLine.find("(", aStart);
553 QString aName = aLine.mid(aStart, (aEnd-aStart));
554 aName = aName.stripWhiteSpace();
555 myNameEdt->setText(aName);*/
559 aEnd = aLine.find(")", aStart);
560 QString aParams = aLine.mid(aStart, (aEnd-aStart));
561 aParams = aParams.stripWhiteSpace();
562 myParamEdt->setText(aParams);*/
564 myText->append(aLine);
566 QString aBodyLine = myIStream->readLine();
567 while ((!aBodyLine.isNull()) && aBodyLine[0].isSpace()) {
568 myText->append(aBodyLine);
569 aBodyLine = myIStream->readLine();
574 QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"),
575 tr("MSG_NOMORE_FUNCTIONS"));
578 myNextBtn->setEnabled(false);
584 QString SUPERVGUI_PythonEditPane::getFuncName() {
586 for (int i=0; i < myText->paragraphs(); i++) {
587 QString aLine = myText->text(i);
588 int aDefPos = aLine.find("def");
590 int aStart = aLine.find(" ", aDefPos);
591 int aEnd = aLine.find("(", aStart);
592 QString aName = aLine.mid(aStart, (aEnd-aStart));
593 return aName.stripWhiteSpace();
600 SUPERV_Strings SUPERVGUI_PythonEditPane::getFunction() {
601 SUPERV_Strings aStrings = new SUPERV::ListOfStrings();
602 aStrings->length(myText->paragraphs());
603 for (int i=0; i < myText->paragraphs(); i++) {
604 aStrings[i] = CORBA::string_dup(myText->text(i).latin1());
606 return aStrings._retn();
610 void SUPERVGUI_PythonEditPane::setFunction(SUPERV_Strings theStr) {
611 int aLen = theStr->length();
612 for (int i=0; i < aLen; i++)
613 myText->append(QString(theStr[i]));
621 SUPERVGUI_EditPythonDlg::SUPERVGUI_EditPythonDlg(bool isLoop)
622 :QDialog(QAD_Application::getDesktop(), 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
624 setSizeGripEnabled( true );
625 setCaption(tr("TIT_FUNC_PYTHON"));
627 QVBoxLayout* aMainLayout = new QVBoxLayout(this, 7, 4);
629 QTabWidget* aLoopTabPane = new QTabWidget(this);
630 myInitPane = new SUPERVGUI_PythonEditPane(this);
631 aLoopTabPane->addTab(myInitPane, "Init");
633 myMorePane = new SUPERVGUI_PythonEditPane(this);
634 aLoopTabPane->addTab(myMorePane, "More");
636 myNextPane = new SUPERVGUI_PythonEditPane(this);
637 aLoopTabPane->addTab(myNextPane, "Next");
639 aMainLayout->addWidget(aLoopTabPane);
641 myEditPane = new SUPERVGUI_PythonEditPane(this);
642 aMainLayout->addWidget(myEditPane);
644 QGroupBox* aBtnBox = new QGroupBox( this );
645 aBtnBox->setColumnLayout( 0, Qt::Vertical );
646 aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
647 QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
648 aBtnLayout->setAlignment( Qt::AlignTop );
649 aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
651 QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
652 connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
653 aBtnLayout->addWidget( aOKBtn );
655 aBtnLayout->addStretch();
657 QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
658 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
659 aBtnLayout->addWidget( aCancelBtn );
661 aMainLayout->addWidget(aBtnBox);