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