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