Back to index

salome-smesh  6.5.0
StdMeshersGUI_ObjectReferenceParamWdg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : StdMeshersGUI_ObjectReferenceParamWdg.cxx
00024 // Author : Open CASCADE S.A.S.
00025 // SMESH includes
00026 //
00027 #include "StdMeshersGUI_ObjectReferenceParamWdg.h"
00028 
00029 #include <SMESHGUI.h>
00030 #include <SMESHGUI_VTKUtils.h>
00031 #include <SMESH_TypeFilter.hxx>
00032 
00033 // SALOME GUI includes
00034 #include <SUIT_ResourceMgr.h>
00035 #include <LightApp_SelectionMgr.h>
00036 #include <SVTK_ViewWindow.h>
00037 #include <SALOME_ListIO.hxx>
00038 #include <SALOME_ListIteratorOfListIO.hxx>
00039 
00040 // SALOME KERNEL incldues
00041 #include <SALOMEDSClient_SObject.hxx>
00042 #include <SALOMEDSClient_Study.hxx>
00043 
00044 // Qt includes
00045 #include <QPushButton>
00046 #include <QLineEdit>
00047 #include <QHBoxLayout>
00048 
00049 #define SPACING 6
00050 
00051 //================================================================================
00056 //================================================================================
00057 
00058 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
00059 ( SUIT_SelectionFilter* f, QWidget* parent, bool multiSelection, bool stretch )
00060   : QWidget( parent ), myMultiSelection( multiSelection )
00061 {
00062   myFilter = f;
00063   myStretchActivated = stretch;
00064   init();
00065 }
00066 
00067 //================================================================================
00072 //================================================================================
00073 
00074 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
00075 ( MeshObjectType objType, QWidget* parent, bool multiSelection )
00076   : QWidget( parent ), myMultiSelection( multiSelection )
00077 {
00078   myFilter = new SMESH_TypeFilter( objType );
00079   init();
00080 }
00081 //================================================================================
00085 //================================================================================
00086 
00087 StdMeshersGUI_ObjectReferenceParamWdg::~StdMeshersGUI_ObjectReferenceParamWdg()
00088 {
00089   if ( myFilter )
00090   {
00091     mySelectionMgr->removeFilter( myFilter );
00092     delete myFilter;
00093   }
00094 }
00095 
00096 
00097 //================================================================================
00101 //================================================================================
00102 
00103 void StdMeshersGUI_ObjectReferenceParamWdg::init()
00104 {
00105   QHBoxLayout* aHBox = new QHBoxLayout(this);
00106 
00107   aHBox->setMargin(0);
00108   aHBox->setSpacing(SPACING);
00109 
00110   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
00111   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
00112   mySelectionActivated = false;
00113   myParamValue = "";
00114   myEmptyText = "";
00115   myEmptyStyleSheet ="";
00116 
00117   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
00118   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
00119 
00120   mySelButton = new QPushButton(this);
00121   mySelButton->setIcon(iconSlct);
00122   mySelButton->setCheckable( true );
00123 
00124   myObjNameLineEdit = new QLineEdit(this);
00125   myObjNameLineEdit->setReadOnly(true);
00126   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
00127 
00128   aHBox->addWidget( mySelButton );
00129   aHBox->addWidget( myObjNameLineEdit );
00130   if (myStretchActivated){
00131     aHBox->addStretch();
00132   }
00133 
00134   connect( mySelButton, SIGNAL(clicked()), SLOT(activateSelection()));
00135 }
00136 
00137 //================================================================================
00141 //================================================================================
00142 
00143 void StdMeshersGUI_ObjectReferenceParamWdg::activateSelection()
00144 {
00145   if ( !mySelectionActivated && mySelectionMgr )
00146   {
00147     mySelectionActivated = true;
00148     mySelectionMgr->clearFilters();
00149     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00150       aViewWindow->SetSelectionMode(ActorSelection);
00151     if ( myFilter )
00152       mySelectionMgr->installFilter( myFilter );
00153     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
00154   }
00155   emit selectionActivated();
00156   onSelectionDone();
00157 
00158   mySelButton->setChecked( mySelectionActivated );
00159 }
00160 
00161 //================================================================================
00165 //================================================================================
00166 
00167 void StdMeshersGUI_ObjectReferenceParamWdg::deactivateSelection()
00168 {
00169   mySelectionActivated = false;
00170   disconnect(mySelectionMgr, 0, this, 0 );
00171   mySelectionMgr->removeFilter( myFilter );
00172 
00173   mySelButton->setChecked( mySelectionActivated );
00174 }
00175 
00176 //================================================================================
00181 //================================================================================
00182 
00183 void StdMeshersGUI_ObjectReferenceParamWdg::AvoidSimultaneousSelection
00184                                    ( StdMeshersGUI_ObjectReferenceParamWdg* other)
00185 {
00186   connect(other, SIGNAL(selectionActivated()), this, SLOT(deactivateSelection()));
00187   connect(this, SIGNAL(selectionActivated()), other, SLOT(deactivateSelection()));
00188 }
00189 
00190 //================================================================================
00195 //================================================================================
00196 
00197 void StdMeshersGUI_ObjectReferenceParamWdg::SetObject(CORBA::Object_ptr obj)
00198 {
00199   myObjects.clear();
00200   myObjNameLineEdit->setText( myEmptyText );
00201   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
00202   myParamValue = "";
00203 
00204   _PTR(SObject) sobj;
00205   if ( !CORBA::is_nil( obj ))
00206     sobj = SMESH::FindSObject (obj);
00207   if ( sobj ) {
00208     std::string name = sobj->GetName();
00209     myObjNameLineEdit->setText( QString( name.c_str() ).trimmed() );
00210     myObjNameLineEdit->setStyleSheet("");
00211     myObjects.push_back( CORBA::Object::_duplicate( obj ));
00212     myParamValue = sobj->GetID().c_str();
00213     emit contentModified();
00214   }
00215 }
00216 
00217 //================================================================================
00222 //================================================================================
00223 
00224 void StdMeshersGUI_ObjectReferenceParamWdg::SetObjects(SMESH::string_array_var& objects)
00225 {
00226   myObjects.clear();
00227   myObjNameLineEdit->setText( myEmptyText );
00228   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
00229   myParamValue = "";
00230   bool selChanged = false;
00231   
00232   for ( unsigned i = 0; i < objects->length(); ++i )
00233   {
00234     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
00235     _PTR(SObject) aSObj = aStudy->FindObjectID(objects[i].in());
00236     CORBA::Object_var anObj = SMESH::SObjectToObject(aSObj,aStudy);
00237     if ( !CORBA::is_nil( anObj )) {
00238       std::string name = aSObj->GetName();
00239       QString text = myObjNameLineEdit->text();
00240       if ( text != myEmptyText )
00241         text += " ";
00242       else
00243         text = "";
00244       text += QString( name.c_str() ).trimmed();
00245       myObjNameLineEdit->setText( text );
00246       myObjNameLineEdit->setStyleSheet("");
00247       myObjects.push_back( anObj );
00248       myParamValue += " ";
00249       myParamValue += objects[i];
00250       selChanged = true;
00251     }
00252   }
00253   if (selChanged)
00254     emit contentModified();
00255 }
00256 
00257 //================================================================================
00261 //================================================================================
00262 
00263 void StdMeshersGUI_ObjectReferenceParamWdg::onSelectionDone()
00264 {
00265   if ( mySelectionActivated ) {
00266     CORBA::Object_var obj;
00267     SALOME_ListIO aList;
00268     mySelectionMgr->selectedObjects(aList);
00269     if (aList.Extent() == 1)
00270     {
00271       obj = SMESH::IObjectToObject( aList.First() );
00272       SetObject( obj.in() );
00273     }
00274     else if (myMultiSelection)
00275     {
00276       SMESH::string_array_var objIds = new SMESH::string_array;
00277       objIds->length( aList.Extent());
00278       SALOME_ListIteratorOfListIO io( aList );
00279       int i = 0;
00280       for ( ; io.More(); io.Next(), ++i )
00281       {
00282         Handle(SALOME_InteractiveObject) anIO = io.Value();
00283         if ( anIO->hasEntry() )
00284           objIds[i] = anIO->getEntry();
00285         else
00286           i--;
00287       }
00288       objIds->length(i);
00289       SetObjects( objIds );
00290     }
00291   }
00292 }
00293 
00294 void StdMeshersGUI_ObjectReferenceParamWdg::SetDefaultText(QString defaultText, QString styleSheet)
00295 {
00296   myEmptyText = defaultText;
00297   myEmptyStyleSheet = styleSheet;
00298   myObjNameLineEdit->setText( myEmptyText );
00299   myObjNameLineEdit->setStyleSheet( myEmptyStyleSheet);
00300 }