Salome HOME
SALOME::GenericObj : Destroy() -> UnRegister()
[modules/visu.git] / src / VISUGUI / VisuGUI_InputPane.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  VISU VISUGUI : GUI of VISU component
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>
54 #include <QComboBox>
55 #include <QToolButton>
56 #include <QLabel>
57 #include <QLayout>
58 #include <QLineEdit>
59 #include <QKeyEvent>
60 #include <QListWidget>
61
62
63 //---------------------------------------------------------------
64 class VisuGUI_ListWidget: public QListWidget
65 {
66  public:
67   VisuGUI_ListWidget( QWidget * parent = 0 ):
68     QListWidget(parent ) 
69   {};
70
71   virtual QSize sizeHint() const 
72   { 
73     return minimumSizeHint(); }
74   ;
75 };
76
77
78 //---------------------------------------------------------------
79 VisuGUI_InputPane::VisuGUI_InputPane( VISU::VISUType theType,
80                                       SalomeApp_Module* theModule,
81                                       VisuGUI_Prs3dDlg* theDialog ) :
82   QGroupBox( theDialog ),
83   myModule( theModule ),
84   myDialog( theDialog ),
85   myPrs( NULL ),
86   isRestoreInitialSelection(true)
87 {
88   //setFrameStyle( QFrame::NoFrame );
89
90   LightApp_SelectionMgr* aSelectionMgr = VISU::GetSelectionMgr(theModule);
91
92   connect( aSelectionMgr, SIGNAL( selectionChanged() ), SLOT( onSelectionChanged() ) );
93
94   QGridLayout* aMainLayout = new QGridLayout( this );
95   aMainLayout->setMargin( 11 );
96   aMainLayout->setSpacing( 6 );
97
98   // Definition of data source
99   myDataSourceGroupBox = new QGroupBox( this );
100   aMainLayout->addWidget( myDataSourceGroupBox, 0, 0 );
101   myDataSourceGroupBox->setTitle( tr( "PRS_DATA_SOUIRCE" ) );
102   {
103     QGroupBox* aGroupBox = myDataSourceGroupBox;
104     //aGroupBox->setColumnLayout(0, Qt::Vertical );
105     //aGroupBox->layout()->setSpacing( 6 );
106     //aGroupBox->layout()->setMargin( 11 );
107
108     QGridLayout* aGroupBoxLayout = new QGridLayout( aGroupBox );
109     aGroupBoxLayout->setAlignment( Qt::AlignTop );
110     {
111       QLabel* aLabel = new QLabel( tr( "MED_FILE" ), aGroupBox );
112       aGroupBoxLayout->addWidget( aLabel, 0, 0 );
113
114       myMedFile = new QLineEdit( aGroupBox );
115       myMedFile->setReadOnly( true );
116       myMedFile->setEnabled( false );
117       QPalette aPal = myMedFile->palette();
118       aPal.setColor( myMedFile->backgroundRole(), Qt::black );
119       myMedFile->setPalette( aPal );
120       //myMedFile->setPaletteForegroundColor( Qt::black );
121       aGroupBoxLayout->addWidget( myMedFile, 0, 1 );
122     }
123     {
124       QLabel* aLabel = new QLabel( tr( "MESH" ), aGroupBox );
125       aGroupBoxLayout->addWidget( aLabel, 1, 0 );
126       
127       myMeshName = new QLineEdit( aGroupBox );
128       myMeshName->setReadOnly( true );
129       myMeshName->setEnabled( false );
130       QPalette aPal = myMeshName->palette();
131       aPal.setColor( myMeshName->backgroundRole(), Qt::black );
132       myMeshName->setPalette( aPal );
133       //myMeshName->setPaletteForegroundColor( Qt::black );
134       aGroupBoxLayout->addWidget( myMeshName, 1, 1 );
135     }
136     {
137       QLabel* aLabel = new QLabel( tr( "ENTITY" ), aGroupBox );
138       aGroupBoxLayout->addWidget( aLabel, 2, 0 );
139       
140       myEntityName = new QLineEdit( aGroupBox );
141       myEntityName->setReadOnly( true );
142       myEntityName->setEnabled( false );
143       QPalette aPal = myEntityName->palette();
144       aPal.setColor( myEntityName->backgroundRole(), Qt::black );
145       myEntityName->setPalette( aPal );
146       //myEntityName->setPaletteForegroundColor( Qt::black );
147       aGroupBoxLayout->addWidget( myEntityName, 2, 1 );
148     }
149     {
150       QLabel* aLabel = new QLabel( tr( "FIELD" ), aGroupBox );
151       aGroupBoxLayout->addWidget( aLabel, 3, 0 );
152       
153       myFieldName = new QLineEdit( aGroupBox );
154       myFieldName->setReadOnly( true );
155       aGroupBoxLayout->addWidget( myFieldName, 3, 1 );
156     }
157     {
158       QLabel* aLabel = new QLabel( tr( "TIME_STAMP" ), aGroupBox );
159       aGroupBoxLayout->addWidget( aLabel, 4, 0 );
160       
161       myTimeStamps = new QComboBox( aGroupBox );
162       aGroupBoxLayout->addWidget( myTimeStamps, 4, 1 );
163     }
164
165     myReInit = new QCheckBox( tr( "REINITIALIZE" ), aGroupBox );
166     myReInit->setChecked( true );
167     aGroupBoxLayout->addWidget( myReInit, 5, 1 );
168   }
169
170   // Definition of used groups
171   myUseGroupsGroupBox = new QGroupBox( this );
172   aMainLayout->addWidget( myUseGroupsGroupBox, 1, 0 );
173   myUseGroupsGroupBox->setTitle( tr( "PRS_ON_GROUPS" ) );
174   myUseGroupsGroupBox->setCheckable( TRUE );
175   {
176     QGroupBox* aGroupBox = myUseGroupsGroupBox;
177     //aGroupBox->setColumnLayout(0, Qt::Vertical );
178     //aGroupBox->layout()->setSpacing( 6 );
179     //aGroupBox->layout()->setMargin( 11 );
180
181     QGridLayout* aGroupBoxLayout = new QGridLayout( aGroupBox );
182     aGroupBoxLayout->setAlignment( Qt::AlignTop );
183     {
184       myAllGroups = new VisuGUI_ListWidget(aGroupBox);
185       myAllGroups->setSelectionMode(QAbstractItemView::ExtendedSelection);
186       aGroupBoxLayout->addWidget( myAllGroups, 0, 0, 4, 1 );
187     }
188     {
189       mySelectedGroups = new VisuGUI_ListWidget(aGroupBox );
190       mySelectedGroups->setSelectionMode(QAbstractItemView::ExtendedSelection);
191       mySelectedGroups->installEventFilter(this);
192       aGroupBoxLayout->addWidget( mySelectedGroups, 0, 2, 4, 1 );
193     }
194     {
195       myAddButton = new QToolButton(aGroupBox);
196       myAddButton->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr("ICON_ADD")) );
197       aGroupBoxLayout->addWidget( myAddButton, 1, 1 );
198     }
199     {
200       myRemoveButton = new QToolButton(aGroupBox);
201       myRemoveButton->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr("ICON_REMOVE")) );
202       aGroupBoxLayout->addWidget( myRemoveButton, 2, 1 );
203     }
204     {
205       QSpacerItem* aSpacer = new QSpacerItem( 20, 51, QSizePolicy::Minimum, QSizePolicy::Expanding );
206       aGroupBoxLayout->addItem( aSpacer, 0, 1 );
207     }
208     {
209       QSpacerItem* aSpacer = new QSpacerItem( 20, 61, QSizePolicy::Minimum, QSizePolicy::Expanding );
210       aGroupBoxLayout->addItem( aSpacer, 3, 1 );
211     }
212   }
213
214   connect( myTimeStamps,   SIGNAL( activated( int ) ), this, SLOT( changeTimeStamp( int ) ) );
215   connect( myReInit, SIGNAL( toggled( bool ) ), SLOT( onReInitialize( bool ) ) );
216
217   connect( myUseGroupsGroupBox, SIGNAL( toggled( bool ) ), this, SLOT( onTypeChanged() ) );
218   connect( myAllGroups,  SIGNAL( itemDoubleClicked( QListWidgetItem* ) ), this, SLOT( onListDoubleClicked( QListWidgetItem* ) ) );
219   connect( mySelectedGroups,  SIGNAL( itemDoubleClicked( QListWidgetItem* ) ), this, SLOT( onListDoubleClicked( QListWidgetItem* ) ) );
220   connect( myAddButton, SIGNAL(clicked()), this, SLOT(onAdd()));
221   connect( myRemoveButton, SIGNAL(clicked()), this, SLOT(onRemove()));
222
223   // Save current selection (to be restored in destructor)
224   aSelectionMgr->selectedObjects(mySavedSelection);
225
226   onTypeChanged();
227   onSelectionChanged();
228
229   myFieldFilter = new VisuGUI_FieldFilter( theType );
230
231   hide();
232 }
233
234
235 //---------------------------------------------------------------
236 VisuGUI_InputPane::~VisuGUI_InputPane()
237 {
238   if ( myModule->getApp() ) {
239     LightApp_SelectionMgr* aSelectionMgr = VISU::GetSelectionMgr(myModule);
240     
241     if (myFieldFilter)
242       {
243         aSelectionMgr->removeFilter(myFieldFilter);
244         delete myFieldFilter;
245       }
246
247     // Restore initial selection
248     if(isRestoreInitialSelection)
249       aSelectionMgr->setSelectedObjects(mySavedSelection);
250   }
251 }
252
253 void VisuGUI_InputPane::SetRestoreInitialSelection(bool on){
254   isRestoreInitialSelection = on;
255 }
256
257 bool VisuGUI_InputPane::GetRestoreInitialSelection(){
258   return isRestoreInitialSelection;
259 }
260
261 //---------------------------------------------------------------
262 /*!
263   Event filter
264 */
265 bool VisuGUI_InputPane::eventFilter (QObject* object, QEvent* event)
266 {
267   if (event->type() == QEvent::KeyPress) {
268     QKeyEvent* aKeyEvent = (QKeyEvent*)event;
269     if (object == mySelectedGroups && aKeyEvent->key() == Qt::Key_Delete)
270       onRemove();
271   }
272   return QObject::eventFilter(object, event);
273 }
274
275
276 //---------------------------------------------------------------
277 /*!
278   Called when the checkbox is toggled
279 */
280 void VisuGUI_InputPane::onTypeChanged( )
281 {
282   bool toEnable = myUseGroupsGroupBox->isChecked();
283   if (!toEnable)
284     {
285       myAllGroups->clearSelection();
286       mySelectedGroups->clearSelection();
287     }
288   myAllGroups->setEnabled( toEnable );
289   mySelectedGroups->setEnabled( toEnable );
290   myAddButton->setEnabled( toEnable );
291   myRemoveButton->setEnabled( toEnable );
292 }
293
294
295 //---------------------------------------------------------------
296 /*!
297   Called when add button is clicked, adds item to choosen groups
298 */
299 void VisuGUI_InputPane::onAdd()
300 {
301   QStringList aList;
302   
303   //for (int i = 0; i < myAllGroups->count(); i++)
304   //  if (myAllGroups->isSelected(i))
305   //    aList.append(myAllGroups->text(i));
306   QList<QListWidgetItem*> aItemList = myAllGroups->selectedItems();
307   QList<QListWidgetItem*>::Iterator it = aItemList.begin();
308   for ( ; it != aItemList.end(); it++ )
309     aList.append( (*it)->text() );  
310   
311   for (int i = 0; i < mySelectedGroups->count(); i++)
312     aList.removeAll(mySelectedGroups->item(i)->text());
313     
314   mySelectedGroups->insertItems( mySelectedGroups->count(), aList);
315 }
316
317
318 //---------------------------------------------------------------
319 /*!
320   Called when remove button is clicked, remove selected items from choosen
321 */
322 void VisuGUI_InputPane::onRemove()
323 {
324   QList<QListWidgetItem*> aList = mySelectedGroups->selectedItems();
325   
326   //aList.setAutoDelete(false);
327   //for (int i = 0; i < mySelectedGroups->count(); i++)
328   //  if (mySelectedGroups->isSelected(i))
329   //    aList.append(mySelectedGroups->item(i));
330   
331   for (int i = 0; i < aList.count(); i++)
332     delete aList.at(i);
333 }
334
335
336 //---------------------------------------------------------------
337 /*!
338   Called when an item of listbox is double-clicked
339 */
340 void VisuGUI_InputPane::onListDoubleClicked( QListWidgetItem* theItem )
341 {
342   QListWidget* aListWidget = theItem->listWidget();
343
344   if (aListWidget == myAllGroups)
345     {
346       QList<QListWidgetItem*> aList = mySelectedGroups->findItems( theItem->text(), Qt::MatchExactly );
347       if ( aList.isEmpty() )
348         mySelectedGroups->insertItem( mySelectedGroups->count(), theItem->text()  );
349     }
350   else if (aListWidget == mySelectedGroups)
351     delete theItem;
352 }
353
354
355 //---------------------------------------------------------------
356 bool VisuGUI_InputPane::check()
357 {
358   return myTimeStamps->count() != 0;
359 }
360
361
362 //---------------------------------------------------------------
363 void VisuGUI_InputPane::clear()
364 {
365   myMedFile->clear();
366   myMeshName->clear();
367   myEntityName->clear();
368   myFieldName->clear();
369   myTimeStamps->clear();
370 }
371
372
373 //---------------------------------------------------------------
374 void VisuGUI_InputPane::onSelectionChanged()
375 {
376   //clear();
377
378   SALOME_ListIO aListIO;
379   VISU::GetSelectionMgr( myModule )->selectedObjects(aListIO);
380
381   if (aListIO.Extent() != 1)
382     return;
383
384   const Handle(SALOME_InteractiveObject)& anIO = aListIO.First();
385
386   _PTR(Study) aCStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
387   _PTR(SObject) aSObject = aCStudy->FindObjectID(anIO->getEntry());
388   if (!aSObject)
389     return;
390
391   VISU::VISUType aType = VISU::Storable::SObject2Type(aSObject);
392   if (aType == VISU::TFIELD)
393   {
394     _PTR(SObject) aMedObject = aSObject->GetFather()->GetFather()->GetFather();
395     if( !aMedObject )
396       return;
397
398     myTimeStamps->clear();
399
400     QString anEntityName, aTimeStampName;
401
402     _PTR(StudyBuilder) aBuilder = aCStudy->NewBuilder();
403     _PTR(ChildIterator) aIter = aCStudy->NewChildIterator(aSObject);
404     for( ; aIter->More(); aIter->Next() )
405     {
406       _PTR(SObject) aChildObj = aIter->Value();
407       if( !aChildObj )
408         return;
409
410       if( anEntityName.isNull() )
411       {
412         _PTR(SObject) aRefObj;
413         if( aChildObj->ReferencedObject( aRefObj ) )
414           anEntityName = aRefObj->GetName().c_str();
415       }
416
417       VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aChildObj);
418       if( aRestoringMap["myComment"] == "TIMESTAMP" )
419       {
420         aTimeStampName = aChildObj->GetName().c_str();
421         myTimeStamps->addItem( aTimeStampName );
422       }
423     }
424
425     myResult = VISU::FindResult( VISU::GetSObject( aSObject ).in() );
426
427     VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aSObject);
428     myEntity = aRestoringMap["myEntityId"].toInt();
429
430     QString aMedFile = aMedObject->GetName().c_str();
431     QString aMeshName = aRestoringMap["myMeshName"];
432     QString aFieldName = aRestoringMap["myName"];
433
434     myMedFile->setText( aMedFile );
435     myMeshName->setText( aMeshName );
436     myEntityName->setText( anEntityName );
437     myFieldName->setText( aFieldName );
438     myTimeStamps->setCurrentIndex( 0 );
439
440     if( myReInit->isChecked() && myPrs )
441     {
442       QApplication::setOverrideCursor(Qt::WaitCursor);
443
444       myPrs->SetResultObject( myResult );
445       myPrs->SetMeshName( aMeshName.toLatin1().data() );
446       myPrs->SetEntity( VISU::Entity( myEntity ) );
447       myPrs->SetFieldName( aFieldName.toLatin1().data() );
448       myPrs->SetTimeStampNumber( myPrs->GetTimeStampNumberByIndex( 0 ) );
449       myPrs->Apply( true );
450
451       myDialog->initFromPrsObject( myPrs, false );
452
453       QApplication::restoreOverrideCursor();
454     }
455   }
456 }
457
458
459 //---------------------------------------------------------------
460 void VisuGUI_InputPane::changeTimeStamp( int theTimeStamp )
461 {
462   QApplication::setOverrideCursor(Qt::WaitCursor);
463
464   myPrs->SetTimeStampNumber( myPrs->GetTimeStampNumberByIndex( theTimeStamp ) );
465   myPrs->Apply( true );
466   myDialog->initFromPrsObject( myPrs, false );
467
468   QApplication::restoreOverrideCursor();
469 }
470
471
472 //---------------------------------------------------------------
473 void VisuGUI_InputPane::onReInitialize( bool on )
474 {
475   if( on )
476     onSelectionChanged();
477 }
478
479
480 //---------------------------------------------------------------
481 void VisuGUI_InputPane::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs )
482 {
483   if( myPrs == thePrs )
484     return;
485
486   myPrs = thePrs;
487
488   clear();
489
490   CORBA::Long aTimeStampNumber = thePrs->GetTimeStampNumber();
491   VISU::ColoredPrs3dHolder::TimeStampsRange_var aTimeStampsRange = thePrs->GetTimeStampsRange();
492   CORBA::Long aLength = aTimeStampsRange->length();
493
494   for( int index = 0; index < aLength; index++ )
495   {
496     VISU::ColoredPrs3dHolder::TimeStampInfo anInfo = aTimeStampsRange[ index ];
497     QString aTime = anInfo.myTime.in();
498     myTimeStamps->addItem( aTime );
499   }
500
501   myResult = thePrs->GetResultObject();
502   VISU::Result_i* aResult = dynamic_cast<VISU::Result_i*>(GetServant(myResult).in());
503   myMedFile->setText( aResult->GetName().c_str() );
504
505   myEntity = (int)thePrs->GetEntity();
506
507   QString anEntityName;
508   switch( myEntity )
509   {
510     case VISU::NODE_ENTITY: 
511       anEntityName = "onNodes"; 
512       break;
513     case VISU::EDGE_ENTITY: 
514       anEntityName = "onEdges"; 
515       break;
516     case VISU::FACE_ENTITY: 
517       anEntityName = "onFaces"; 
518       break;
519     case VISU::CELL_ENTITY: 
520       anEntityName = "onCells"; 
521       break;
522     default: break;
523   }
524   myEntityName->setText( anEntityName );
525
526   myMeshName->setText( thePrs->GetMeshName() );
527   myFieldName->setText( thePrs->GetFieldName() );
528   myTimeStamps->setCurrentIndex( thePrs->GetTimeStampIndexByNumber( aTimeStampNumber ) );
529
530   myFieldFilter->setPrs3dEntry( thePrs->GetHolderEntry().c_str() );
531   bool anIsTimeStampFixed = myPrs->IsTimeStampFixed();
532   myDataSourceGroupBox->setEnabled(!anIsTimeStampFixed);
533   if(!anIsTimeStampFixed)
534     VISU::GetSelectionMgr( myModule )->installFilter( myFieldFilter );
535   
536   // type of presentation and groups
537   VISU::Result_i::PInput anInput = aResult->GetInput();
538   const VISU::TMeshMap& aMeshMap = anInput->GetMeshMap();
539   std::string aMeshName = thePrs->GetCMeshName();
540   VISU::TMeshMap::const_iterator aMeshIter = aMeshMap.find(aMeshName);
541   if(aMeshIter != aMeshMap.end()){
542     const VISU::PMesh& aMesh = aMeshIter->second;
543     const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
544     VISU::TGroupMap::const_iterator aGroupIter = aGroupMap.begin();
545     for(; aGroupIter != aGroupMap.end(); aGroupIter++){
546       const std::string& aGroupName = aGroupIter->first;
547       myAllGroups->insertItem( myAllGroups->count(), aGroupName.c_str());
548     }
549   }
550
551   if(myAllGroups->count() < 1){
552     myAllGroups->insertItem( myAllGroups->count(), tr("NO_GROUPS") );
553     myUseGroupsGroupBox->setEnabled(false);
554   }else{
555     const VISU::ColoredPrs3d_i::TGroupNames& aGroupNames = thePrs->GetGroupNames();
556     VISU::ColoredPrs3d_i::TGroupNames::const_iterator anIter = aGroupNames.begin();
557     for(; anIter != aGroupNames.end(); anIter++){
558       const std::string aGroupName = *anIter;
559       mySelectedGroups->insertItem(mySelectedGroups->count(), aGroupName.c_str());
560     }
561     myUseGroupsGroupBox->setEnabled(true);
562   }
563   myUseGroupsGroupBox->setChecked(mySelectedGroups->count() > 0);
564 }
565
566
567 //---------------------------------------------------------------
568 int VisuGUI_InputPane::storeToPrsObject( VISU::ColoredPrs3d_i* thePrs )
569 {
570   if(myUseGroupsGroupBox->isChecked()){
571     thePrs->RemoveAllGeom();
572     for(int i = 0; i < mySelectedGroups->count(); i++)
573       thePrs->AddMeshOnGroup(mySelectedGroups->item(i)->text().toLatin1().data() );
574   }else
575     thePrs->SetSourceGeometry();
576   
577   thePrs->SetResultObject( myResult );
578
579   thePrs->SetMeshName( myMeshName->text().toLatin1().data() );
580   thePrs->SetEntity( VISU::Entity( myEntity ) );
581   thePrs->SetFieldName( myFieldName->text().toLatin1().data() );
582   thePrs->SetTimeStampNumber( thePrs->GetTimeStampNumberByIndex( myTimeStamps->currentIndex() ) );
583   return ( int )thePrs->Apply( false );
584 }
585
586
587 //---------------------------------------------------------------