Salome HOME
NRI : Merge from V1_2.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_BrowseNodeDlg.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SUPERVGUI_BrowseNodeDlg.cxx
8 //  Author : Vitaly SMETANNIKOV
9 //  Module : SUPERV
10
11 using namespace std;
12 #include "SUPERVGUI_BrowseNodeDlg.h"
13 #include "SUPERVGUI_Node.h"
14 #include "SUPERVGUI.h"
15 #include <qlayout.h>
16 #include <qlabel.h>
17 #include <qlineedit.h>
18 #include <qpushbutton.h>
19 #include <qhbox.h>
20 #include <qgroupbox.h>
21
22 /**
23  * Constructor
24  */
25 SUPERVGUI_PortField::SUPERVGUI_PortField( QWidget* theParent, SUPERV_Port thePort ) {
26   myPort = thePort;
27   myIsEditable = myPort->IsInput() && ( !myPort->IsLinked() );
28
29   QString aLabelTxt(myPort->Name());
30
31   SUPERV_CNode aNode = myPort->Node();
32   SALOME_ModuleCatalog::Service* aService = aNode->Service();
33   SALOME_ModuleCatalog::ListOfServicesParameter aList;
34   aList = (myPort->IsInput())? aService->ServiceinParameter : aService->ServiceoutParameter;
35   for (int i = 0; i < aList.length(); i++) {
36     SALOME_ModuleCatalog::ServicesParameter* aParam = &(aList[i]);
37     if (aLabelTxt == aParam->Parametername) {
38       aLabelTxt += QString(" (") + QString(aParam->Parametertype) + QString(")");
39       break;
40     }
41   }
42  
43   aLabelTxt += QString(":");
44   myLabel = new QLabel(aLabelTxt, theParent );
45
46   myValue = new QLineEdit( theParent );
47   myLabel->setBuddy( myValue );
48   if (!myIsEditable) {
49     myValue->setReadOnly( !myIsEditable );
50     QPalette aPalette = myValue->palette();
51     aPalette.setInactive(aPalette.disabled());
52     aPalette.setActive(aPalette.disabled());
53     myValue->setPalette(aPalette);
54   } 
55   myValue->installEventFilter( this );
56 }
57
58 /**
59  * Sets value from text edit control to engine of port
60  */
61 bool SUPERVGUI_PortField::setNewValue() {
62   if ( !myIsEditable ) return false;
63
64   QString aTxt = myValue->text();
65   if ( aTxt.isNull() || aTxt.isEmpty() ) return false;
66   
67   if ( aTxt.find( "Unknown" ) < 0 ) {
68     return myPort->Input( ( *Supervision.getEngine() )->StringValue( aTxt ) );
69   }
70   return false;
71 }
72  
73 /** 
74  * Event filter
75  */ 
76 bool SUPERVGUI_PortField::eventFilter( QObject* o, QEvent* e )
77 {
78   if ( o == myValue ) {
79     if ( e->type() == QEvent::FocusIn ) {
80       emit activated();
81     }
82   }
83   return QObject::eventFilter( o, e );
84 }
85
86
87 /**
88  * Constructor
89  */
90 SUPERVGUI_BrowseNodeDlg::SUPERVGUI_BrowseNodeDlg( SUPERVGUI_Node* theNode )
91   : QDialog( theNode, 0, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
92 {
93   myActiveField = 0;
94   setSizeGripEnabled( true );
95   myNode = theNode;
96   myPortsList.setAutoDelete( true );
97
98   SUPERV_CNode aEngine = myNode->getEngine();
99   mySelection = SALOME_Selection::Selection( Supervision.getActiveStudy()->getSelection() );
100 //  mySelection->ClearIObjects();
101
102   setName( "SUPERVGUI_BrowseNodeDlg" );
103   setCaption( tr( "TIT_BROWSENODE" ) + aEngine->Name() );
104
105   QGridLayout* aBaseLayout = new QGridLayout( this );
106   aBaseLayout->setMargin( 11 ); aBaseLayout->setSpacing( 6 );
107
108   QGroupBox* aInBox = new QGroupBox( 2, Qt::Horizontal, "Input", this );
109   aInBox->setMargin( 11 );
110   aBaseLayout->addWidget( aInBox, 0, 0 );
111
112   QGroupBox* aOutBox = new QGroupBox( 2, Qt::Horizontal, "Output", this);
113   aOutBox->setMargin( 11 );
114   aBaseLayout->addWidget( aOutBox, 0, 1 );
115
116   myIsEditable = false;
117   int                aPIcount = 0;
118   int                aPOcount = 0;
119   SUPERV_Ports       ports = aEngine->Ports();
120   int                n     = ports->length();
121
122   for ( int i = 0; i < n; i++ ) {
123     if ( ports[i]->IsInput() ) {
124       if ( !ports[i]->IsGate() ) { // not a gate
125         SUPERVGUI_PortField* aField = new SUPERVGUI_PortField( aInBox, ports[i] );
126         if ( aField->isEditable() ) myIsEditable = true;
127         myPortsList.append( aField );
128         if ( !myActiveField )
129           myActiveField = aField;
130         connect( aField, SIGNAL( activated() ), this, SLOT( onFieldActivated() ) );
131       }
132       aPIcount++;
133     } 
134     else {
135       if ( !ports[i]->IsGate() ) { // not a gate
136         myPortsList.append( new SUPERVGUI_PortField( aOutBox, ports[i] ) );
137       }
138       aPOcount++;
139     }
140   }
141   
142   QGroupBox* aBtnBox = new QGroupBox( this );
143   aBtnBox->setColumnLayout( 0, Qt::Vertical );
144   aBtnBox->layout()->setSpacing( 0 ); 
145   aBtnBox->layout()->setMargin( 0 );
146   
147   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
148   aBtnLayout->setAlignment( Qt::AlignTop );
149   aBtnLayout->setSpacing( 6 ); 
150   aBtnLayout->setMargin( 11 );
151
152   if ( myIsEditable ) {
153     QLabel* aInfoLab = new QLabel( tr( "ENTER_OR_SELECT_LBL" ), this );
154     aBaseLayout->addMultiCellWidget( aInfoLab, 1, 1, 0, 1 );
155
156     QPushButton* aOkBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
157     connect( aOkBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
158     aBtnLayout->addWidget( aOkBtn );
159     aBaseLayout->addMultiCellWidget( aBtnBox, 2, 2, 0, 1 );
160   } else
161     aBaseLayout->addMultiCellWidget( aBtnBox, 1, 1, 0, 1 );
162
163   aBtnLayout->addStretch();
164   QPushButton* aCancelBtn = new QPushButton( myIsEditable ? tr( "BUT_CANCEL" ) : tr( "BUT_CLOSE" ), aBtnBox );
165   aBtnLayout->addWidget( aCancelBtn );
166   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
167   
168   if ( !myIsEditable )
169     aBtnLayout->addStretch();
170
171   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
172   myNode->getMain()->lockedGraph(true);
173 }
174
175 /**
176  * Destructor
177  */
178 SUPERVGUI_BrowseNodeDlg::~SUPERVGUI_BrowseNodeDlg() {
179 }
180
181 /**
182  * Set current values of editable ports
183  */
184 void SUPERVGUI_BrowseNodeDlg::setValues() {
185   SUPERVGUI_PortField* aField;
186   for ( aField = myPortsList.first(); aField; aField = myPortsList.next() ) {
187     aField->updateGUI();
188   }
189 }
190
191 /** 
192  * Set inputed values of editable ports and then closes and destroys dialog
193  */
194 void SUPERVGUI_BrowseNodeDlg::accept() {
195   if ( myIsEditable ) {
196     SUPERVGUI_PortField* aField;
197     for ( aField = myPortsList.first(); aField; aField = myPortsList.next() ) {
198       aField->setNewValue();
199     }
200     myNode->sync();
201   }
202   myNode->getMain()->lockedGraph(false);
203   QDialog::accept();
204   close();
205 }
206
207
208 /** 
209  * Closes and destroys dialog
210  */
211 void SUPERVGUI_BrowseNodeDlg::reject() {
212   myNode->getMain()->lockedGraph(false);
213   QDialog::reject();
214   close();
215 }
216
217 /**
218  * Update current values on show event
219  */
220 void SUPERVGUI_BrowseNodeDlg::showEvent( QShowEvent* theEvent ) {
221   setValues();
222   QDialog::showEvent( theEvent );
223 }
224
225 /**
226  * Slot, called when field is focused
227  */
228 void SUPERVGUI_BrowseNodeDlg::onFieldActivated()
229 {
230   SUPERVGUI_PortField* aField = (SUPERVGUI_PortField*)sender();
231   myActiveField = (aField->isEditable())? aField : 0;
232 }
233
234
235
236 /**
237  * Slot, called when selection is changed
238  */
239 void SUPERVGUI_BrowseNodeDlg::onSelectionChanged()
240 {
241   if ( myActiveField ) {
242     if( mySelection->IObjectCount() == 1 ) {
243       Handle( SALOME_InteractiveObject ) anIO = mySelection->firstIObject();
244       if ( anIO->hasEntry() ) {
245         SALOMEDS::SObject_var aObj = Supervision.getActiveStudy()->
246         getStudyDocument()->FindObjectID( anIO->getEntry() );
247           
248         SALOMEDS::GenericAttribute_var anAttr;
249         SALOMEDS::AttributeIOR_var     anIOR;
250         Standard_CString               ior = "";
251           
252         if (aObj->FindAttribute( anAttr, "AttributeIOR" ) ) {
253           anIOR = SALOMEDS::AttributeIOR::_narrow( anAttr );
254           ior = anIOR->Value();
255         }
256         else {
257           ior = aObj->GetStudy()->ConvertObjectToIOR( aObj );
258         }
259         myActiveField->setData( ior );
260       }
261     }
262   }
263 }
264
265 /**
266  * Constructor
267  */
268 SUPERVGUI_GetValueDlg::SUPERVGUI_GetValueDlg( SUPERVGUI_PortIn* thePort )
269   : QDialog( thePort, 0, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
270 {
271   myOKBtn = 0;
272   setSizeGripEnabled( true );
273   myPort = thePort;
274   myPortESNode = 0;
275
276   mySelection = SALOME_Selection::Selection( Supervision.getActiveStudy()->getSelection() );
277 //  mySelection->ClearIObjects();
278
279   setName( "SUPERVGUI_GetValueDlg" );
280   setCaption( tr( "TIT_SETVALUE_PORT" ) );
281   
282   QGridLayout* aBaseLayout = new QGridLayout( this );
283   aBaseLayout->setMargin( 11 ); aBaseLayout->setSpacing( 6 );
284   
285   QGroupBox* aBox = new QGroupBox( this );
286   aBox->setColumnLayout( 0, Qt::Vertical );
287   aBox->layout()->setSpacing( 0 ); aBox->layout()->setMargin( 0 );
288   QGridLayout* aBoxLayout = new QGridLayout( aBox->layout() );
289   aBoxLayout->setAlignment( Qt::AlignTop );
290   aBoxLayout->setSpacing( 6 ); aBoxLayout->setMargin( 11 );
291   aBaseLayout->addWidget( aBox, 0, 0 );
292   aBox->setMinimumWidth( 200 );
293   
294   QLabel* aInfoLab = new QLabel( tr( "ENTER_OR_SELECT_LBL" ), aBox );
295   QFont fnt = aInfoLab->font(); fnt.setBold( true ); aInfoLab->setFont( fnt );
296   aBoxLayout->addMultiCellWidget( aInfoLab, 0, 0, 0, 1 );
297   myField = new SUPERVGUI_PortField( aBox, myPort->getPort() );
298   bool myIsEditable = myField->isEditable();
299   aBoxLayout->addWidget( myField->myLabel, 1, 0 ); 
300   aBoxLayout->addWidget( myField->myValue, 1, 1 );
301   
302   QGroupBox* aBtnBox = new QGroupBox( this );
303   aBtnBox->setColumnLayout( 0, Qt::Vertical );
304   aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
305   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
306   aBtnLayout->setAlignment( Qt::AlignTop );
307   aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
308
309   aBaseLayout->addWidget( aBtnBox, 1, 0 );
310
311   if ( myIsEditable ) {
312     myOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
313     connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
314     aBtnLayout->addWidget( myOKBtn );
315   }
316
317   aBtnLayout->addStretch();
318   myCancelBtn = new QPushButton( myIsEditable ? tr( "BUT_CANCEL" ) : tr( "BUT_CLOSE" ), aBtnBox );
319   aBtnLayout->addWidget( myCancelBtn );
320   connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
321   
322   if ( !myIsEditable )
323     aBtnLayout->addStretch();
324
325   myField->updateGUI();
326   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
327   myPort->getMain()->lockedGraph(true);
328 }
329
330 /**
331  * Constructor
332  */
333 SUPERVGUI_GetValueDlg::SUPERVGUI_GetValueDlg( SUPERVGUI_PortInESNode* thePort )
334   : QDialog( thePort, 0, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
335 {
336   myOKBtn = 0;
337   setSizeGripEnabled( true );
338   myPort = 0;
339   myPortESNode = thePort;
340
341   mySelection = SALOME_Selection::Selection( Supervision.getActiveStudy()->getSelection() );
342 //  mySelection->ClearIObjects();
343
344   setName( "SUPERVGUI_GetValueDlg" );
345   setCaption( tr( "TIT_SETVALUE_PORT" ) );
346   
347   QGridLayout* aBaseLayout = new QGridLayout( this );
348   aBaseLayout->setMargin( 11 ); aBaseLayout->setSpacing( 6 );
349   
350   QGroupBox* aBox = new QGroupBox( this );
351   aBox->setColumnLayout( 0, Qt::Vertical );
352   aBox->layout()->setSpacing( 0 ); aBox->layout()->setMargin( 0 );
353   QGridLayout* aBoxLayout = new QGridLayout( aBox->layout() );
354   aBoxLayout->setAlignment( Qt::AlignTop );
355   aBoxLayout->setSpacing( 6 ); aBoxLayout->setMargin( 11 );
356   aBaseLayout->addWidget( aBox, 0, 0 );
357   aBox->setMinimumWidth( 200 );
358   
359   QLabel* aInfoLab = new QLabel( tr( "ENTER_OR_SELECT_LBL" ), aBox );
360   QFont fnt = aInfoLab->font(); fnt.setBold( true ); aInfoLab->setFont( fnt );
361   aBoxLayout->addMultiCellWidget( aInfoLab, 0, 0, 0, 1 );
362   myField = new SUPERVGUI_PortField( aBox, myPortESNode->getPort() );
363   bool myIsEditable = myField->isEditable();
364   aBoxLayout->addWidget( myField->myLabel, 1, 0 ); 
365   aBoxLayout->addWidget( myField->myValue, 1, 1 );
366   
367   QGroupBox* aBtnBox = new QGroupBox( this );
368   aBtnBox->setColumnLayout( 0, Qt::Vertical );
369   aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
370   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
371   aBtnLayout->setAlignment( Qt::AlignTop );
372   aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
373
374   aBaseLayout->addWidget( aBtnBox, 1, 0 );
375
376   if ( myIsEditable ) {
377     myOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
378     connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
379     aBtnLayout->addWidget( myOKBtn );
380   }
381
382   aBtnLayout->addStretch();
383   myCancelBtn = new QPushButton( myIsEditable ? tr( "BUT_CANCEL" ) : tr( "BUT_CLOSE" ), aBtnBox );
384   aBtnLayout->addWidget( myCancelBtn );
385   connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
386   
387   if ( !myIsEditable )
388     aBtnLayout->addStretch();
389
390   myField->updateGUI();
391   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
392   myPortESNode->getMain()->lockedGraph(true);
393 }
394
395 /**
396  * Destructor
397  */
398 SUPERVGUI_GetValueDlg::~SUPERVGUI_GetValueDlg() {
399 }
400
401 /** 
402  * Set entered value into port and then closes and destroys dialog
403  */
404 void SUPERVGUI_GetValueDlg::accept() {
405   if ( myField->setNewValue() ) {
406     
407     if (myPort != 0)
408       myPort->sync();
409     else
410       myPortESNode->sync();
411   }
412   else {
413     if ( QMessageBox::warning( QAD_Application::getDesktop(), 
414                                tr( "ERROR" ), tr( "MSG_CANT_SETVAL" ),
415                                QMessageBox::Retry, QMessageBox::Abort) == QMessageBox::Retry )
416       return;
417   }
418
419   if (myPort != 0)
420     myPort->getMain()->lockedGraph(false);
421   else
422     myPortESNode->getMain()->lockedGraph(false);
423   QDialog::accept();
424   close();
425 }
426
427 /** 
428  * Closes and destroys dialog
429  */
430 void SUPERVGUI_GetValueDlg::reject() {
431   if (myPort != 0)
432     myPort->getMain()->lockedGraph(false);
433   else
434     myPortESNode->getMain()->lockedGraph(false);
435   QDialog::reject();
436   close();
437 }
438
439 /**
440  * Slot, called when selection is changed
441  */
442 void SUPERVGUI_GetValueDlg::onSelectionChanged()
443 {
444   if( mySelection->IObjectCount() == 1 ) {
445     Handle( SALOME_InteractiveObject ) anIO = mySelection->firstIObject();
446     if ( anIO->hasEntry() ) {
447       SALOMEDS::SObject_var aObj = Supervision.getActiveStudy()->
448       getStudyDocument()->FindObjectID( anIO->getEntry() );
449         
450       SALOMEDS::GenericAttribute_var anAttr;
451       SALOMEDS::AttributeIOR_var     anIOR;
452       Standard_CString               ior = "";
453         
454       if (aObj->FindAttribute( anAttr, "AttributeIOR" ) ) {
455         anIOR = SALOMEDS::AttributeIOR::_narrow( anAttr );
456         ior = anIOR->Value();
457       }
458       else {
459         ior = aObj->GetStudy()->ConvertObjectToIOR( aObj );
460       }
461       myField->setData( ior );
462     }
463   }
464 }
465