Back to index

salome-geom  6.5.0
GEOMToolsGUI_TransparencyDlg.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 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : GEOMToolsGUI_TransparencyDlg.cxx
00025 // Author : Lucien PIGNOLONI
00026 //
00027 #include "GEOMToolsGUI_TransparencyDlg.h"
00028 #include <GEOMBase.h>
00029 #include <GEOM_AISShape.hxx>
00030 #include <GeometryGUI.h>
00031 #include <GEOM_Constants.h>
00032 
00033 #include <SALOME_ListIO.hxx>
00034 #include <SALOME_ListIteratorOfListIO.hxx>
00035 
00036 #include <SVTK_ViewModel.h>
00037 #include <SVTK_ViewWindow.h>
00038 #include <SVTK_View.h>
00039 
00040 #include <OCCViewer_ViewModel.h>
00041 
00042 #include <SUIT_ViewManager.h>
00043 #include <SUIT_Desktop.h>
00044 #include <SUIT_MessageBox.h>
00045 #include <SUIT_ResourceMgr.h>
00046 #include <SUIT_Session.h>
00047 #include <SUIT_OverrideCursor.h>
00048 
00049 #include <SalomeApp_Application.h>
00050 #include <LightApp_Application.h>
00051 #include <LightApp_SelectionMgr.h>
00052 #include <SalomeApp_Study.h>
00053 
00054 #include <QLabel>
00055 #include <QPushButton>
00056 #include <QSlider>
00057 #include <QGridLayout>
00058 #include <QVBoxLayout>
00059 #include <QHBoxLayout>
00060 #include <QGroupBox>
00061 #include <QKeyEvent>
00062 
00063 #include <AIS_InteractiveContext.hxx>
00064 
00065 
00066 //=================================================================================
00067 // class    : GEOMBase_TransparencyDlg()
00068 // purpose  : Constructs a GEOMBase_SUBSHAPE which is a child of 'parent', with the
00069 //            name 'name' and widget flags set to 'f'.
00070 //
00071 //          : WARNING : this dialog is modal !
00072 //
00073 //=================================================================================
00074 GEOMToolsGUI_TransparencyDlg::GEOMToolsGUI_TransparencyDlg( QWidget* parent )
00075   :QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint )
00076 {
00077   setModal( true );
00078   
00079   resize(152, 107); 
00080   setWindowTitle(tr("GEOM_TRANSPARENCY_TITLE"));
00081   setSizeGripEnabled(true);
00082   QVBoxLayout* lay = new QVBoxLayout(this); 
00083   lay->setSpacing(6);
00084   lay->setMargin(11);
00085   
00086   /*************************************************************************/
00087   QGroupBox* GroupButtons = new QGroupBox( this );
00088   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
00089   GroupButtonsLayout->setAlignment( Qt::AlignTop );
00090   GroupButtonsLayout->setSpacing( 6 );
00091   GroupButtonsLayout->setMargin( 11 );
00092   
00093   /*************************************************************************/
00094   QGroupBox* GroupC1 = new QGroupBox( this );
00095   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1 );
00096   GroupC1Layout->setAlignment( Qt::AlignTop );
00097   GroupC1Layout->setSpacing( 6 );
00098   GroupC1Layout->setMargin( 11 );
00099   
00100   QLabel* TextLabelTransparent = new QLabel( tr( "GEOM_TRANSPARENCY_TRANSPARENT" ), GroupC1 );
00101   TextLabelTransparent->setAlignment( Qt::AlignLeft );
00102   GroupC1Layout->addWidget( TextLabelTransparent, 0, 2 );
00103   
00104   myValueLab = new QLabel( GroupC1 );
00105   myValueLab->setAlignment( Qt::AlignCenter );
00106   myValueLab->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
00107   QFont fnt = myValueLab->font(); fnt.setBold( true ); myValueLab->setFont( fnt );
00108   GroupC1Layout->addWidget( myValueLab, 0, 1 );
00109 
00110   QLabel* TextLabelOpaque = new QLabel( tr( "GEOM_TRANSPARENCY_OPAQUE" ), GroupC1 );
00111   TextLabelOpaque->setAlignment( Qt::AlignRight );
00112   GroupC1Layout->addWidget( TextLabelOpaque, 0, 0 );
00113   //GroupC1Layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
00114   
00115   mySlider = new QSlider( Qt::Horizontal, GroupC1 );
00116   mySlider->setFocusPolicy( Qt::NoFocus );
00117   mySlider->setMinimumSize( 300, 0 );
00118   mySlider->setTickPosition( QSlider::TicksAbove );
00119   mySlider->setTickInterval( 10 );
00120   mySlider->setMinimum( 0 );
00121   mySlider->setMaximum( 100 );
00122   mySlider->setSingleStep( 1 );
00123   mySlider->setPageStep( 10 );
00124   //mySlider->setValue( 5 );
00125   mySlider->setTracking(false);
00126 
00127   mySlider->setTickPosition( QSlider::TicksAbove );
00128   GroupC1Layout->addWidget( mySlider, 1, 0, 1, 3 );
00129 
00130   /*************************************************************************/
00131   QPushButton* buttonOk = new QPushButton( tr( "GEOM_BUT_OK" ), GroupButtons );
00132   buttonOk->setAutoDefault( true );
00133   buttonOk->setDefault( true );
00134   
00135   QPushButton* buttonHelp = new QPushButton( tr( "GEOM_BUT_HELP" ), GroupButtons );
00136   buttonHelp->setAutoDefault( true );
00137   buttonHelp->setDefault( true );
00138   
00139   GroupButtonsLayout->addWidget( buttonOk );
00140   GroupButtonsLayout->addSpacing( 10 );
00141   GroupButtonsLayout->addStretch();
00142   GroupButtonsLayout->addWidget( buttonHelp );
00143   
00144   /*************************************************************************/
00145   lay->addWidget(GroupC1);
00146   lay->addWidget(GroupButtons);
00147     
00148   /* First call valueChanged() method for initialisation               */
00149   /* The default value of transparency will change with the selection  */
00150   myFirstInit = true;
00151   //  mySlider->setMaxValue( 10 );
00152   //  mySlider->setValue( 5 ) ;
00153   SetTransparency();
00154   
00155   myHelpFileName = "transparency_page.html";
00156 
00157   // signals and slots connections : after ValueHasChanged()
00158   connect(buttonOk,   SIGNAL(clicked()), this, SLOT(ClickOnOk()));
00159   connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
00160   connect(mySlider,   SIGNAL(valueChanged(int)), this, SLOT(SetTransparency()));
00161   connect(mySlider,   SIGNAL(sliderMoved(int)),  this, SLOT(SliderHasMoved(int)));
00162 }
00163 
00164 
00165 //=================================================================================
00166 // function : ~GEOMBase_TransparencyDlg()
00167 // purpose  :
00168 //=================================================================================
00169 GEOMToolsGUI_TransparencyDlg::~GEOMToolsGUI_TransparencyDlg()
00170 {
00171     // no need to delete child widgets, Qt does it all for us
00172 }
00173 
00174 
00175 //=======================================================================
00176 // function : ClickOnOk()
00177 // purpose  :
00178 //=======================================================================
00179 void GEOMToolsGUI_TransparencyDlg::ClickOnOk()
00180 {
00181   accept();
00182   return;
00183 }
00184 
00185 
00186 //=======================================================================
00187 // function : ClickOnClose()
00188 // purpose  :
00189 //=======================================================================
00190 void GEOMToolsGUI_TransparencyDlg::ClickOnClose()
00191 {
00192   accept();
00193   return;
00194 }
00195 
00196 //=================================================================================
00197 // function : ClickOnHelp()
00198 // purpose  :
00199 //=================================================================================
00200 void GEOMToolsGUI_TransparencyDlg::ClickOnHelp()
00201 {
00202   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
00203   if (app) {
00204     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
00205     app->onHelpContextModule(aGeomGUI ? app->moduleName(aGeomGUI->moduleName()) : QString(""), myHelpFileName);
00206   }
00207   else {
00208                 QString platform;
00209 #ifdef WIN32
00210                 platform = "winapplication";
00211 #else
00212                 platform = "application";
00213 #endif
00214     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
00215                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
00216                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
00217                              QObject::tr("BUT_OK"));
00218   }
00219 }
00220 
00221 //=================================================================================
00222 // function : ValueHasChanged()
00223 // purpose  : Called when value of slider change
00224 //=================================================================================
00225 void GEOMToolsGUI_TransparencyDlg::ValueHasChanged()
00226 {
00227   myValueLab->setText( QString("%1%").arg( mySlider->value() ) );
00228 }
00229 
00230 //=================================================================================
00231 // function : SliderHasMoved()
00232 // purpose  : Called when value of slider change
00233 //=================================================================================
00234 void GEOMToolsGUI_TransparencyDlg::SliderHasMoved(int value)
00235 {
00236   myValueLab->setText( QString("%1%").arg( value ) );
00237 }
00238 
00239 //=================================================================================
00240 // function : SetTransparency()
00241 // purpose  : Called when value of slider change
00242 //          : or the first time as initilisation
00243 //=================================================================================
00244 void GEOMToolsGUI_TransparencyDlg::SetTransparency()
00245 {
00246   float newValue = mySlider->value() / 100.;
00247 
00248   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
00249   if ( !app )
00250     return;
00251   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
00252   if ( !aSelMgr )
00253     return;
00254   
00255   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
00256   
00257   if(!aStudy)
00258     return;
00259   
00260   SALOME_ListIO selected;
00261   aSelMgr->selectedObjects( selected );
00262   if ( selected.IsEmpty() )
00263     return;
00264 
00265   Handle(SALOME_InteractiveObject) FirstIOS =  selected.First();
00266   if ( FirstIOS.IsNull() )
00267     return;
00268         
00269   SUIT_ViewWindow* window = app->desktop()->activeWindow();
00270   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
00271   bool isVTK = ( window && window->getViewManager()->getType() == SVTK_Viewer::Type() );
00272   int aMgrId = window->getViewManager()->getGlobalId();
00273   if ( isVTK ) {
00274     SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
00275     if ( !vtkVW )
00276       return;
00277     SVTK_View* aView = vtkVW->getView();
00278     if ( myFirstInit ) {        
00279       myFirstInit = false;
00280       int transp = int ((aView->GetTransparency(FirstIOS))*100.0 + 0.5);
00281       mySlider->setValue(transp);
00282       ValueHasChanged();
00283       return;
00284     }
00285 
00286     SUIT_OverrideCursor();
00287     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
00288       aView->SetTransparency( It.Value(), newValue );
00289       aStudy->setObjectProperty( aMgrId , It.Value()->getEntry(), TRANSPARENCY_PROP , newValue );
00290     }
00291     GeometryGUI::Modified();
00292     aView->Repaint();
00293   } // if ( isVTK )
00294         
00295   else if ( isOCC ) {
00296     GEOMBase* gb = new GEOMBase();
00297     Handle(GEOM_AISShape) aisShape;
00298     if ( myFirstInit ) {
00299       mySlider->setValue(mySlider->maximum());
00300       myFirstInit = false;
00301       aisShape = gb->ConvertIOinGEOMAISShape( FirstIOS, true );
00302       if( aisShape.IsNull() )
00303         return;
00304       int transp = int( aisShape->Transparency() * 100.0 + 0.5);
00305       mySlider->setValue(transp);
00306       ValueHasChanged();
00307       return;
00308     }
00309     
00310     SUIT_OverrideCursor();
00311     OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
00312     if ( !vm )
00313       return;
00314     Handle(AIS_InteractiveContext) ic = vm->getAISContext();
00315     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
00316       aisShape = gb->ConvertIOinGEOMAISShape( It.Value(), true );
00317       if ( !aisShape.IsNull() ) {
00318         ic->SetTransparency( aisShape, newValue, false );
00319         ic->Redisplay( aisShape, Standard_False, Standard_True );
00320        aStudy->setObjectProperty( aMgrId , It.Value()->getEntry(), TRANSPARENCY_PROP , newValue );
00321       }
00322     } // for...
00323     ic->UpdateCurrentViewer();
00324     GeometryGUI::Modified();
00325   } // if ( isOCC )
00326 
00327   ValueHasChanged();
00328 }
00329 
00330 //=================================================================================
00331 // function : keyPressEvent()
00332 // purpose  :
00333 //=================================================================================
00334 void GEOMToolsGUI_TransparencyDlg::keyPressEvent( QKeyEvent* e )
00335 {
00336   QDialog::keyPressEvent( e );
00337   if ( e->isAccepted() )
00338     return;
00339 
00340   if ( e->key() == Qt::Key_F1 )
00341     {
00342       e->accept();
00343       ClickOnHelp();
00344     }
00345 }