Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISUGUI / VisuGUI_InputPane.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : VisuGUI_InputPane.cxx
25 //  Author : Oleg UVAROV
26 //  Module : VISU
27
28 #include "VisuGUI_InputPane.h"
29
30 #include "VisuGUI_Prs3dDlg.h"
31 #include "VisuGUI_FieldFilter.h"
32 #include "VisuGUI_Tools.h"
33
34 #include "VISU_ColoredPrs3d_i.hh"
35 #include "VISU_ScalarMap_i.hh"
36 #include "VISU_Result_i.hh"
37 #include "VISU_Convertor.hxx"
38
39 #include "SUIT_ResourceMgr.h"
40
41 #include "SalomeApp_Module.h"
42
43 #include "LightApp_Application.h"
44 #include "LightApp_SelectionMgr.h"
45
46 #include "SALOME_ListIO.hxx"
47
48 #include "SALOMEDSClient_AttributeString.hxx"
49 #include "SALOMEDSClient_AttributeName.hxx"
50
51 #include "QtxListBox.h"
52
53 #include <qcheckbox.h>
54 #include <qcombobox.h>
55 #include <qtoolbutton.h>
56 #include <qlabel.h>
57 #include <qlayout.h>
58 #include <qlineedit.h>
59
60
61 //---------------------------------------------------------------
62 class VisuGUI_ListBox: public QtxListBox
63 {
64  public:
65   VisuGUI_ListBox( QWidget * parent = 0, const char * name = 0, WFlags f = 0 ):
66     QtxListBox(parent, name, f) 
67   {};
68
69   virtual QSize sizeHint() const 
70   { 
71     return minimumSizeHint(); }
72   ;
73 };
74
75
76 //---------------------------------------------------------------
77 VisuGUI_InputPane::VisuGUI_InputPane( VISU::VISUType theType,
78                                       SalomeApp_Module* theModule,
79                                       VisuGUI_Prs3dDlg* theDialog ) :
80   QGroupBox( theDialog ),
81   myModule( theModule ),
82   myDialog( theDialog ),
83   myPrs( NULL )
84 {
85   setFrameStyle( QFrame::NoFrame );
86
87   LightApp_SelectionMgr* aSelectionMgr = VISU::GetSelectionMgr(theModule);
88
89   connect( aSelectionMgr, SIGNAL( selectionChanged() ), SLOT( onSelectionChanged() ) );
90
91   QGridLayout* aMainLayout = new QGridLayout( this, 1, 1, 11, 6 ); 
92
93   // Definition of data source
94   myDataSourceGroupBox = new QGroupBox( this );
95   aMainLayout->addWidget( myDataSourceGroupBox, 0, 0 );
96   myDataSourceGroupBox->setTitle( tr( "PRS_DATA_SOUIRCE" ) );
97   {
98     QGroupBox* aGroupBox = myDataSourceGroupBox;
99     aGroupBox->setColumnLayout(0, Qt::Vertical );
100     aGroupBox->layout()->setSpacing( 6 );
101     aGroupBox->layout()->setMargin( 11 );
102
103     QGridLayout* aGroupBoxLayout = new QGridLayout( aGroupBox->layout() );
104     aGroupBoxLayout->setAlignment( Qt::AlignTop );
105     {
106       QLabel* aLabel = new QLabel( tr( "MED_FILE" ), aGroupBox );
107       aGroupBoxLayout->addWidget( aLabel, 0, 0 );
108
109       myMedFile = new QLineEdit( aGroupBox );
110       myMedFile->setReadOnly( true );
111       myMedFile->setEnabled( false );
112       myMedFile->setPaletteForegroundColor( Qt::black );
113       aGroupBoxLayout->addWidget( myMedFile, 0, 1 );
114     }
115     {
116       QLabel* aLabel = new QLabel( tr( "MESH" ), aGroupBox );
117       aGroupBoxLayout->addWidget( aLabel, 1, 0 );
118       
119       myMeshName = new QLineEdit( aGroupBox );
120       myMeshName->setReadOnly( true );
121       myMeshName->setEnabled( false );
122       myMeshName->setPaletteForegroundColor( Qt::black );
123       aGroupBoxLayout->addWidget( myMeshName, 1, 1 );
124     }
125     {
126       QLabel* aLabel = new QLabel( tr( "ENTITY" ), aGroupBox );
127       aGroupBoxLayout->addWidget( aLabel, 2, 0 );
128       
129       myEntityName = new QLineEdit( aGroupBox );
130       myEntityName->setReadOnly( true );
131       myEntityName->setEnabled( false );
132       myEntityName->setPaletteForegroundColor( Qt::black );
133       aGroupBoxLayout->addWidget( myEntityName, 2, 1 );
134     }
135     {
136       QLabel* aLabel = new QLabel( tr( "FIELD" ), aGroupBox );
137       aGroupBoxLayout->addWidget( aLabel, 3, 0 );
138       
139       myFieldName = new QLineEdit( aGroupBox );
140       myFieldName->setReadOnly( true );
141       aGroupBoxLayout->addWidget( myFieldName, 3, 1 );
142     }
143     {
144       QLabel* aLabel = new QLabel( tr( "TIME_STAMP" ), aGroupBox );
145       aGroupBoxLayout->addWidget( aLabel, 4, 0 );
146       
147       myTimeStamps = new QComboBox( aGroupBox );
148       aGroupBoxLayout->addWidget( myTimeStamps, 4, 1 );
149     }
150
151     myReInit = new QCheckBox( tr( "REINITIALIZE" ), aGroupBox );
152     myReInit->setChecked( true );
153     aGroupBoxLayout->addWidget( myReInit, 5, 1 );
154   }
155
156   // Definition of used groups
157   myUseGroupsGroupBox = new QGroupBox( this );
158   aMainLayout->addWidget( myUseGroupsGroupBox, 1, 0 );
159   myUseGroupsGroupBox->setTitle( tr( "PRS_ON_GROUPS" ) );
160   myUseGroupsGroupBox->setCheckable( TRUE );
161   {
162     QGroupBox* aGroupBox = myUseGroupsGroupBox;
163     aGroupBox->setColumnLayout(0, Qt::Vertical );
164     aGroupBox->layout()->setSpacing( 6 );
165     aGroupBox->layout()->setMargin( 11 );
166
167     QGridLayout* aGroupBoxLayout = new QGridLayout( aGroupBox->layout() );
168     aGroupBoxLayout->setAlignment( Qt::AlignTop );
169     {
170       myAllGroups = new VisuGUI_ListBox(aGroupBox, "myAllGroups");
171       myAllGroups->setSelectionMode(QListBox::Extended);
172       aGroupBoxLayout->addMultiCellWidget( myAllGroups, 0, 3, 0, 0 );
173     }
174     {
175       mySelectedGroups = new VisuGUI_ListBox(aGroupBox, "mySelectedGroups");
176       mySelectedGroups->setSelectionMode(QListBox::Extended);
177       mySelectedGroups->installEventFilter(this);
178       aGroupBoxLayout->addMultiCellWidget( mySelectedGroups, 0, 3, 2, 2 );
179     }
180     {
181       myAddButton = new QToolButton(aGroupBox);
182       myAddButton->setPixmap( VISU::GetResourceMgr()->loadPixmap("VISU", tr("ICON_ADD")) );
183       aGroupBoxLayout->addWidget( myAddButton, 1, 1 );
184     }
185     {
186       myRemoveButton = new QToolButton(aGroupBox);
187       myRemoveButton->setPixmap( VISU::GetResourceMgr()->loadPixmap("VISU", tr("ICON_REMOVE")) );
188       aGroupBoxLayout->addWidget( myRemoveButton, 2, 1 );
189     }
190     {
191       QSpacerItem* aSpacer = new QSpacerItem( 20, 51, QSizePolicy::Minimum, QSizePolicy::Expanding );
192       aGroupBoxLayout->addItem( aSpacer, 0, 1 );
193     }
194     {
195       QSpacerItem* aSpacer = new QSpacerItem( 20, 61, QSizePolicy::Minimum, QSizePolicy::Expanding );
196       aGroupBoxLayout->addItem( aSpacer, 3, 1 );
197     }
198   }
199
200   connect( myTimeStamps,   SIGNAL( activated( int ) ), this, SLOT( changeTimeStamp( int ) ) );
201   connect( myReInit, SIGNAL( toggled( bool ) ), SLOT( onReInitialize( bool ) ) );
202
203   connect( myUseGroupsGroupBox, SIGNAL( toggled( bool ) ), this, SLOT( onTypeChanged() ) );
204   connect( myAllGroups,  SIGNAL( doubleClicked( QListBoxItem* ) ), this, SLOT( onListDoubleClicked( QListBoxItem* ) ) );
205   connect( mySelectedGroups,  SIGNAL( doubleClicked( QListBoxItem* ) ), this, SLOT( onListDoubleClicked( QListBoxItem* ) ) );
206   connect( myAddButton, SIGNAL(clicked()), this, SLOT(onAdd()));
207   connect( myRemoveButton, SIGNAL(clicked()), this, SLOT(onRemove()));
208
209   // Save current selection (to be restored in destructor)
210   aSelectionMgr->selectedObjects(mySavedSelection);
211
212   onTypeChanged();
213   onSelectionChanged();
214
215   myFieldFilter = new VisuGUI_FieldFilter( theType );
216
217   hide();
218 }
219
220
221 //---------------------------------------------------------------
222 VisuGUI_InputPane::~VisuGUI_InputPane()
223 {
224   LightApp_SelectionMgr* aSelectionMgr = VISU::GetSelectionMgr(myModule);
225
226   if (myFieldFilter)
227   {
228     aSelectionMgr->removeFilter(myFieldFilter);
229     delete myFieldFilter;
230   }
231
232   // Restore initial selection
233   aSelectionMgr->setSelectedObjects(mySavedSelection);
234 }
235
236
237 //---------------------------------------------------------------
238 /*!
239   Event filter
240 */
241 bool VisuGUI_InputPane::eventFilter (QObject* object, QEvent* event)
242 {
243   if (event->type() == QEvent::KeyPress) {
244     QKeyEvent* aKeyEvent = (QKeyEvent*)event;
245     if (object == mySelectedGroups && aKeyEvent->key() == Key_Delete)
246       onRemove();
247   }
248   return QObject::eventFilter(object, event);
249 }
250
251
252 //---------------------------------------------------------------
253 /*!
254   Called when the checkbox is toggled
255 */
256 void VisuGUI_InputPane::onTypeChanged( )
257 {
258   bool toEnable = myUseGroupsGroupBox->isChecked();
259   if (!toEnable)
260     {
261       myAllGroups->clearSelection();
262       mySelectedGroups->clearSelection();
263     }
264   myAllGroups->setEnabled( toEnable );
265   mySelectedGroups->setEnabled( toEnable );
266   myAddButton->setEnabled( toEnable );
267   myRemoveButton->setEnabled( toEnable );
268 }
269
270
271 //---------------------------------------------------------------
272 /*!
273   Called when add button is clicked, adds item to choosen groups
274 */
275 void VisuGUI_InputPane::onAdd()
276 {
277   QStringList aList;
278   
279   for (int i = 0; i < myAllGroups->count(); i++)
280     if (myAllGroups->isSelected(i))
281       aList.append(myAllGroups->text(i));
282
283   for (int i = 0; i < mySelectedGroups->count(); i++)
284     aList.remove(mySelectedGroups->text(i));
285     
286   mySelectedGroups->insertStringList(aList);
287 }
288
289
290 //---------------------------------------------------------------
291 /*!
292   Called when remove button is clicked, remove selected items from choosen
293 */
294 void VisuGUI_InputPane::onRemove()
295 {
296   QPtrList<QListBoxItem> aList;
297   aList.setAutoDelete(false);
298   for (int i = 0; i < mySelectedGroups->count(); i++)
299     if (mySelectedGroups->isSelected(i))
300       aList.append(mySelectedGroups->item(i));
301   
302   for (int i = 0; i < aList.count(); i++)
303     delete aList.at(i);
304 }
305
306
307 //---------------------------------------------------------------
308 /*!
309   Called when an item of listbox is double-clicked
310 */
311 void VisuGUI_InputPane::onListDoubleClicked( QListBoxItem* theItem )
312 {
313   QListBox* aListBox = theItem->listBox();
314
315   if (aListBox == myAllGroups)
316     {
317       if (!mySelectedGroups->findItem( theItem->text(), Qt::ExactMatch ))
318         mySelectedGroups->insertItem(theItem->text());
319     }
320   else if (aListBox == mySelectedGroups)
321     delete theItem;
322 }
323
324
325 //---------------------------------------------------------------
326 bool VisuGUI_InputPane::check()
327 {
328   return myTimeStamps->count() != 0;
329 }
330
331
332 //---------------------------------------------------------------
333 void VisuGUI_InputPane::clear()
334 {
335   myMedFile->clear();
336   myMeshName->clear();
337   myEntityName->clear();
338   myFieldName->clear();
339   myTimeStamps->clear();
340 }
341
342
343 //---------------------------------------------------------------
344 void VisuGUI_InputPane::onSelectionChanged()
345 {
346   //clear();
347
348   SALOME_ListIO aListIO;
349   VISU::GetSelectionMgr( myModule )->selectedObjects(aListIO);
350
351   if (aListIO.Extent() != 1)
352     return;
353
354   const Handle(SALOME_InteractiveObject)& anIO = aListIO.First();
355
356   _PTR(Study) aCStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
357   _PTR(SObject) aSObject = aCStudy->FindObjectID(anIO->getEntry());
358   if (!aSObject)
359     return;
360
361   VISU::VISUType aType = VISU::Storable::SObject2Type(aSObject);
362   if (aType == VISU::TFIELD)
363   {
364     _PTR(SObject) aMedObject = aSObject->GetFather()->GetFather()->GetFather();
365     if( !aMedObject )
366       return;
367
368     myTimeStamps->clear();
369
370     QString anEntityName, aTimeStampName;
371
372     _PTR(StudyBuilder) aBuilder = aCStudy->NewBuilder();
373     _PTR(ChildIterator) aIter = aCStudy->NewChildIterator(aSObject);
374     for( ; aIter->More(); aIter->Next() )
375     {
376       _PTR(SObject) aChildObj = aIter->Value();
377       if( !aChildObj )
378         return;
379
380       if( anEntityName.isNull() )
381       {
382         _PTR(SObject) aRefObj;
383         if( aChildObj->ReferencedObject( aRefObj ) )
384           anEntityName = aRefObj->GetName().c_str();
385       }
386
387       VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aChildObj);
388       if( aRestoringMap["myComment"] == "TIMESTAMP" )
389       {
390         aTimeStampName = aChildObj->GetName().c_str();
391         myTimeStamps->insertItem( aTimeStampName );
392       }
393     }
394
395     myResult = VISU::FindResult( VISU::GetSObject( aSObject ).in() );
396
397     VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aSObject);
398     myEntity = aRestoringMap["myEntityId"].toInt();
399
400     QString aMedFile = aMedObject->GetName().c_str();
401     QString aMeshName = aRestoringMap["myMeshName"];
402     QString aFieldName = aRestoringMap["myName"];
403
404     myMedFile->setText( aMedFile );
405     myMeshName->setText( aMeshName );
406     myEntityName->setText( anEntityName );
407     myFieldName->setText( aFieldName );
408     myTimeStamps->setCurrentItem( 0 );
409
410     if( myReInit->isChecked() && myPrs )
411     {
412       QApplication::setOverrideCursor(Qt::waitCursor);
413
414       myPrs->SetResultObject( myResult );
415       myPrs->SetMeshName( aMeshName.latin1() );
416       myPrs->SetEntity( VISU::Entity( myEntity ) );
417       myPrs->SetFieldName( aFieldName.latin1() );
418       myPrs->SetTimeStampNumber( myPrs->GetTimeStampNumberByIndex( 0 ) );
419       myPrs->Apply( true );
420
421       myDialog->initFromPrsObject( myPrs, false );
422
423       QApplication::restoreOverrideCursor();
424     }
425   }
426 }
427
428
429 //---------------------------------------------------------------
430 void VisuGUI_InputPane::changeTimeStamp( int theTimeStamp )
431 {
432   QApplication::setOverrideCursor(Qt::waitCursor);
433
434   myPrs->SetTimeStampNumber( myPrs->GetTimeStampNumberByIndex( theTimeStamp ) );
435   myPrs->Apply( true );
436   myDialog->initFromPrsObject( myPrs, false );
437
438   QApplication::restoreOverrideCursor();
439 }
440
441
442 //---------------------------------------------------------------
443 void VisuGUI_InputPane::onReInitialize( bool on )
444 {
445   if( on )
446     onSelectionChanged();
447 }
448
449
450 //---------------------------------------------------------------
451 void VisuGUI_InputPane::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs )
452 {
453   if( myPrs == thePrs )
454     return;
455
456   myPrs = thePrs;
457
458   clear();
459
460   CORBA::Long aTimeStampNumber = thePrs->GetTimeStampNumber();
461   VISU::ColoredPrs3dHolder::TimeStampsRange_var aTimeStampsRange = thePrs->GetTimeStampsRange();
462   CORBA::Long aLength = aTimeStampsRange->length();
463
464   for( int index = 0; index < aLength; index++ )
465   {
466     VISU::ColoredPrs3dHolder::TimeStampInfo anInfo = aTimeStampsRange[ index ];
467     QString aTime = anInfo.myTime.in();
468     myTimeStamps->insertItem( aTime );
469   }
470
471   myResult = thePrs->GetResultObject();
472   VISU::Result_i* aResult = dynamic_cast<VISU::Result_i*>(GetServant(myResult).in());
473   myMedFile->setText( aResult->GetName().c_str() );
474
475   myEntity = (int)thePrs->GetEntity();
476
477   QString anEntityName;
478   switch( myEntity )
479   {
480     case VISU::NODE_ENTITY: 
481       anEntityName = "onNodes"; 
482       break;
483     case VISU::EDGE_ENTITY: 
484       anEntityName = "onEdges"; 
485       break;
486     case VISU::FACE_ENTITY: 
487       anEntityName = "onFaces"; 
488       break;
489     case VISU::CELL_ENTITY: 
490       anEntityName = "onCells"; 
491       break;
492     default: break;
493   }
494   myEntityName->setText( anEntityName );
495
496   myMeshName->setText( thePrs->GetMeshName() );
497   myFieldName->setText( thePrs->GetFieldName() );
498   myTimeStamps->setCurrentItem( thePrs->GetTimeStampIndexByNumber( aTimeStampNumber ) );
499
500   myFieldFilter->setPrs3dEntry( thePrs->GetHolderEntry() );
501   bool anIsTimeStampFixed = myPrs->IsTimeStampFixed();
502   myDataSourceGroupBox->setEnabled(!anIsTimeStampFixed);
503   if(!anIsTimeStampFixed)
504     VISU::GetSelectionMgr( myModule )->installFilter( myFieldFilter );
505   
506   // type of presentation and groups
507   VISU::Result_i::PInput anInput = aResult->GetInput();
508   const VISU::TMeshMap& aMeshMap = anInput->GetMeshMap();
509   std::string aMeshName = thePrs->GetCMeshName();
510   VISU::TMeshMap::const_iterator aMeshIter = aMeshMap.find(aMeshName);
511   if(aMeshIter != aMeshMap.end()){
512     const VISU::PMesh& aMesh = aMeshIter->second;
513     const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
514     VISU::TGroupMap::const_iterator aGroupIter = aGroupMap.begin();
515     for(; aGroupIter != aGroupMap.end(); aGroupIter++){
516       const std::string& aGroupName = aGroupIter->first;
517       myAllGroups->insertItem(QString(aGroupName));
518     }
519   }
520
521   if(myAllGroups->count() < 1){
522     myAllGroups->insertItem( tr("NO_GROUPS") );
523     myUseGroupsGroupBox->setEnabled(false);
524   }else{
525     const VISU::ColoredPrs3d_i::TGroupNames& aGroupNames = thePrs->GetGroupNames();
526     VISU::ColoredPrs3d_i::TGroupNames::const_iterator anIter = aGroupNames.begin();
527     for(; anIter != aGroupNames.end(); anIter++){
528       const std::string aGroupName = *anIter;
529       mySelectedGroups->insertItem(QString(aGroupName));
530     }      
531   }
532   myUseGroupsGroupBox->setChecked(mySelectedGroups->count() > 0);
533   myUseGroupsGroupBox->setEnabled(anIsTimeStampFixed);
534 }
535
536
537 //---------------------------------------------------------------
538 int VisuGUI_InputPane::storeToPrsObject( VISU::ColoredPrs3d_i* thePrs )
539 {
540   
541   if(myUseGroupsGroupBox->isChecked()){
542     thePrs->RemoveAllGeom();
543     for(int i = 0; i < mySelectedGroups->count(); i++)
544       thePrs->AddMeshOnGroup(mySelectedGroups->text(i) );
545   }else
546     thePrs->SetSourceGeometry();
547   
548   thePrs->SetResultObject( myResult );
549
550   thePrs->SetMeshName( myMeshName->text().latin1() );
551   thePrs->SetEntity( VISU::Entity( myEntity ) );
552   thePrs->SetFieldName( myFieldName->text().latin1() );
553   thePrs->SetTimeStampNumber( thePrs->GetTimeStampNumberByIndex( myTimeStamps->currentItem() ) );
554   return ( int )thePrs->Apply( false );
555 }
556
557
558 //---------------------------------------------------------------