]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_InputPane.cxx
Salome HOME
Bug IPAL21228 SIGSEGV on exit from Salome with active GaussPointsDlg
[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   if ( myModule->getApp() ) {
238     LightApp_SelectionMgr* aSelectionMgr = VISU::GetSelectionMgr(myModule);
239     
240     if (myFieldFilter)
241       {
242         aSelectionMgr->removeFilter(myFieldFilter);
243         delete myFieldFilter;
244       }
245
246     // Restore initial selection
247     if(isRestoreInitialSelection)
248       aSelectionMgr->setSelectedObjects(mySavedSelection);
249   }
250 }
251
252 void VisuGUI_InputPane::SetRestoreInitialSelection(bool on){
253   isRestoreInitialSelection = on;
254 }
255
256 bool VisuGUI_InputPane::GetRestoreInitialSelection(){
257   return isRestoreInitialSelection;
258 }
259
260 //---------------------------------------------------------------
261 /*!
262   Event filter
263 */
264 bool VisuGUI_InputPane::eventFilter (QObject* object, QEvent* event)
265 {
266   if (event->type() == QEvent::KeyPress) {
267     QKeyEvent* aKeyEvent = (QKeyEvent*)event;
268     if (object == mySelectedGroups && aKeyEvent->key() == Qt::Key_Delete)
269       onRemove();
270   }
271   return QObject::eventFilter(object, event);
272 }
273
274
275 //---------------------------------------------------------------
276 /*!
277   Called when the checkbox is toggled
278 */
279 void VisuGUI_InputPane::onTypeChanged( )
280 {
281   bool toEnable = myUseGroupsGroupBox->isChecked();
282   if (!toEnable)
283     {
284       myAllGroups->clearSelection();
285       mySelectedGroups->clearSelection();
286     }
287   myAllGroups->setEnabled( toEnable );
288   mySelectedGroups->setEnabled( toEnable );
289   myAddButton->setEnabled( toEnable );
290   myRemoveButton->setEnabled( toEnable );
291 }
292
293
294 //---------------------------------------------------------------
295 /*!
296   Called when add button is clicked, adds item to choosen groups
297 */
298 void VisuGUI_InputPane::onAdd()
299 {
300   QStringList aList;
301   
302   //for (int i = 0; i < myAllGroups->count(); i++)
303   //  if (myAllGroups->isSelected(i))
304   //    aList.append(myAllGroups->text(i));
305   QList<QListWidgetItem*> aItemList = myAllGroups->selectedItems();
306   QList<QListWidgetItem*>::Iterator it = aItemList.begin();
307   for ( ; it != aItemList.end(); it++ )
308     aList.append( (*it)->text() );  
309   
310   for (int i = 0; i < mySelectedGroups->count(); i++)
311     aList.removeAll(mySelectedGroups->item(i)->text());
312     
313   mySelectedGroups->insertItems( mySelectedGroups->count(), aList);
314 }
315
316
317 //---------------------------------------------------------------
318 /*!
319   Called when remove button is clicked, remove selected items from choosen
320 */
321 void VisuGUI_InputPane::onRemove()
322 {
323   QList<QListWidgetItem*> aList = mySelectedGroups->selectedItems();
324   
325   //aList.setAutoDelete(false);
326   //for (int i = 0; i < mySelectedGroups->count(); i++)
327   //  if (mySelectedGroups->isSelected(i))
328   //    aList.append(mySelectedGroups->item(i));
329   
330   for (int i = 0; i < aList.count(); i++)
331     delete aList.at(i);
332 }
333
334
335 //---------------------------------------------------------------
336 /*!
337   Called when an item of listbox is double-clicked
338 */
339 void VisuGUI_InputPane::onListDoubleClicked( QListWidgetItem* theItem )
340 {
341   QListWidget* aListWidget = theItem->listWidget();
342
343   if (aListWidget == myAllGroups)
344     {
345       QList<QListWidgetItem*> aList = mySelectedGroups->findItems( theItem->text(), Qt::MatchExactly );
346       if ( aList.isEmpty() )
347         mySelectedGroups->insertItem( mySelectedGroups->count(), theItem->text()  );
348     }
349   else if (aListWidget == mySelectedGroups)
350     delete theItem;
351 }
352
353
354 //---------------------------------------------------------------
355 bool VisuGUI_InputPane::check()
356 {
357   return myTimeStamps->count() != 0;
358 }
359
360
361 //---------------------------------------------------------------
362 void VisuGUI_InputPane::clear()
363 {
364   myMedFile->clear();
365   myMeshName->clear();
366   myEntityName->clear();
367   myFieldName->clear();
368   myTimeStamps->clear();
369 }
370
371
372 //---------------------------------------------------------------
373 void VisuGUI_InputPane::onSelectionChanged()
374 {
375   //clear();
376
377   SALOME_ListIO aListIO;
378   VISU::GetSelectionMgr( myModule )->selectedObjects(aListIO);
379
380   if (aListIO.Extent() != 1)
381     return;
382
383   const Handle(SALOME_InteractiveObject)& anIO = aListIO.First();
384
385   _PTR(Study) aCStudy = VISU::GetCStudy(VISU::GetAppStudy(myModule));
386   _PTR(SObject) aSObject = aCStudy->FindObjectID(anIO->getEntry());
387   if (!aSObject)
388     return;
389
390   VISU::VISUType aType = VISU::Storable::SObject2Type(aSObject);
391   if (aType == VISU::TFIELD)
392   {
393     _PTR(SObject) aMedObject = aSObject->GetFather()->GetFather()->GetFather();
394     if( !aMedObject )
395       return;
396
397     myTimeStamps->clear();
398
399     QString anEntityName, aTimeStampName;
400
401     _PTR(StudyBuilder) aBuilder = aCStudy->NewBuilder();
402     _PTR(ChildIterator) aIter = aCStudy->NewChildIterator(aSObject);
403     for( ; aIter->More(); aIter->Next() )
404     {
405       _PTR(SObject) aChildObj = aIter->Value();
406       if( !aChildObj )
407         return;
408
409       if( anEntityName.isNull() )
410       {
411         _PTR(SObject) aRefObj;
412         if( aChildObj->ReferencedObject( aRefObj ) )
413           anEntityName = aRefObj->GetName().c_str();
414       }
415
416       VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aChildObj);
417       if( aRestoringMap["myComment"] == "TIMESTAMP" )
418       {
419         aTimeStampName = aChildObj->GetName().c_str();
420         myTimeStamps->addItem( aTimeStampName );
421       }
422     }
423
424     myResult = VISU::FindResult( VISU::GetSObject( aSObject ).in() );
425
426     VISU::Storable::TRestoringMap aRestoringMap = VISU::Storable::GetStorableMap(aSObject);
427     myEntity = aRestoringMap["myEntityId"].toInt();
428
429     QString aMedFile = aMedObject->GetName().c_str();
430     QString aMeshName = aRestoringMap["myMeshName"];
431     QString aFieldName = aRestoringMap["myName"];
432
433     myMedFile->setText( aMedFile );
434     myMeshName->setText( aMeshName );
435     myEntityName->setText( anEntityName );
436     myFieldName->setText( aFieldName );
437     myTimeStamps->setCurrentIndex( 0 );
438
439     if( myReInit->isChecked() && myPrs )
440     {
441       QApplication::setOverrideCursor(Qt::WaitCursor);
442
443       myPrs->SetResultObject( myResult );
444       myPrs->SetMeshName( aMeshName.toLatin1().data() );
445       myPrs->SetEntity( VISU::Entity( myEntity ) );
446       myPrs->SetFieldName( aFieldName.toLatin1().data() );
447       myPrs->SetTimeStampNumber( myPrs->GetTimeStampNumberByIndex( 0 ) );
448       myPrs->Apply( true );
449
450       myDialog->initFromPrsObject( myPrs, false );
451
452       QApplication::restoreOverrideCursor();
453     }
454   }
455 }
456
457
458 //---------------------------------------------------------------
459 void VisuGUI_InputPane::changeTimeStamp( int theTimeStamp )
460 {
461   QApplication::setOverrideCursor(Qt::WaitCursor);
462
463   myPrs->SetTimeStampNumber( myPrs->GetTimeStampNumberByIndex( theTimeStamp ) );
464   myPrs->Apply( true );
465   myDialog->initFromPrsObject( myPrs, false );
466
467   QApplication::restoreOverrideCursor();
468 }
469
470
471 //---------------------------------------------------------------
472 void VisuGUI_InputPane::onReInitialize( bool on )
473 {
474   if( on )
475     onSelectionChanged();
476 }
477
478
479 //---------------------------------------------------------------
480 void VisuGUI_InputPane::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs )
481 {
482   if( myPrs == thePrs )
483     return;
484
485   myPrs = thePrs;
486
487   clear();
488
489   CORBA::Long aTimeStampNumber = thePrs->GetTimeStampNumber();
490   VISU::ColoredPrs3dHolder::TimeStampsRange_var aTimeStampsRange = thePrs->GetTimeStampsRange();
491   CORBA::Long aLength = aTimeStampsRange->length();
492
493   for( int index = 0; index < aLength; index++ )
494   {
495     VISU::ColoredPrs3dHolder::TimeStampInfo anInfo = aTimeStampsRange[ index ];
496     QString aTime = anInfo.myTime.in();
497     myTimeStamps->addItem( aTime );
498   }
499
500   myResult = thePrs->GetResultObject();
501   VISU::Result_i* aResult = dynamic_cast<VISU::Result_i*>(GetServant(myResult).in());
502   myMedFile->setText( aResult->GetName().c_str() );
503
504   myEntity = (int)thePrs->GetEntity();
505
506   QString anEntityName;
507   switch( myEntity )
508   {
509     case VISU::NODE_ENTITY: 
510       anEntityName = "onNodes"; 
511       break;
512     case VISU::EDGE_ENTITY: 
513       anEntityName = "onEdges"; 
514       break;
515     case VISU::FACE_ENTITY: 
516       anEntityName = "onFaces"; 
517       break;
518     case VISU::CELL_ENTITY: 
519       anEntityName = "onCells"; 
520       break;
521     default: break;
522   }
523   myEntityName->setText( anEntityName );
524
525   myMeshName->setText( thePrs->GetMeshName() );
526   myFieldName->setText( thePrs->GetFieldName() );
527   myTimeStamps->setCurrentIndex( thePrs->GetTimeStampIndexByNumber( aTimeStampNumber ) );
528
529   myFieldFilter->setPrs3dEntry( thePrs->GetHolderEntry().c_str() );
530   bool anIsTimeStampFixed = myPrs->IsTimeStampFixed();
531   myDataSourceGroupBox->setEnabled(!anIsTimeStampFixed);
532   if(!anIsTimeStampFixed)
533     VISU::GetSelectionMgr( myModule )->installFilter( myFieldFilter );
534   
535   // type of presentation and groups
536   VISU::Result_i::PInput anInput = aResult->GetInput();
537   const VISU::TMeshMap& aMeshMap = anInput->GetMeshMap();
538   std::string aMeshName = thePrs->GetCMeshName();
539   VISU::TMeshMap::const_iterator aMeshIter = aMeshMap.find(aMeshName);
540   if(aMeshIter != aMeshMap.end()){
541     const VISU::PMesh& aMesh = aMeshIter->second;
542     const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
543     VISU::TGroupMap::const_iterator aGroupIter = aGroupMap.begin();
544     for(; aGroupIter != aGroupMap.end(); aGroupIter++){
545       const std::string& aGroupName = aGroupIter->first;
546       myAllGroups->insertItem( myAllGroups->count(), aGroupName.c_str());
547     }
548   }
549
550   if(myAllGroups->count() < 1){
551     myAllGroups->insertItem( myAllGroups->count(), tr("NO_GROUPS") );
552     myUseGroupsGroupBox->setEnabled(false);
553   }else{
554     const VISU::ColoredPrs3d_i::TGroupNames& aGroupNames = thePrs->GetGroupNames();
555     VISU::ColoredPrs3d_i::TGroupNames::const_iterator anIter = aGroupNames.begin();
556     for(; anIter != aGroupNames.end(); anIter++){
557       const std::string aGroupName = *anIter;
558       mySelectedGroups->insertItem(mySelectedGroups->count(), aGroupName.c_str());
559     }
560     myUseGroupsGroupBox->setEnabled(true);
561   }
562   myUseGroupsGroupBox->setChecked(mySelectedGroups->count() > 0);
563 }
564
565
566 //---------------------------------------------------------------
567 int VisuGUI_InputPane::storeToPrsObject( VISU::ColoredPrs3d_i* thePrs )
568 {
569   if(myUseGroupsGroupBox->isChecked()){
570     thePrs->RemoveAllGeom();
571     for(int i = 0; i < mySelectedGroups->count(); i++)
572       thePrs->AddMeshOnGroup(mySelectedGroups->item(i)->text().toLatin1().data() );
573   }else
574     thePrs->SetSourceGeometry();
575   
576   thePrs->SetResultObject( myResult );
577
578   thePrs->SetMeshName( myMeshName->text().toLatin1().data() );
579   thePrs->SetEntity( VISU::Entity( myEntity ) );
580   thePrs->SetFieldName( myFieldName->text().toLatin1().data() );
581   thePrs->SetTimeStampNumber( thePrs->GetTimeStampNumberByIndex( myTimeStamps->currentIndex() ) );
582   return ( int )thePrs->Apply( false );
583 }
584
585
586 //---------------------------------------------------------------