Back to index

salome-gui  6.5.0
VTKViewer_MarkerWidget.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "VTKViewer_MarkerWidget.h"
00021 #include "VTKViewer_MarkerUtils.h"
00022 
00023 #include <QtxComboBox.h>
00024 
00025 #include <SUIT_ResourceMgr.h>
00026 #include <SUIT_Session.h>
00027 
00028 #include <vtkImageData.h>
00029 
00030 #include <QButtonGroup>
00031 #include <QGridLayout>
00032 #include <QHBoxLayout>
00033 #include <QLabel>
00034 #include <QPushButton>
00035 #include <QRadioButton>
00036 #include <QStackedWidget>
00037 
00038 #define MARGIN  9
00039 #define SPACING 6
00040 
00049 VTKViewer_MarkerWidget::VTKViewer_MarkerWidget( QWidget* theParent )
00050 : QWidget( theParent )
00051 {
00052   QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
00053   QRadioButton* aCustomTypeRB   = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
00054   myTypeGroup = new QButtonGroup( this );
00055   myTypeGroup->addButton( aStandardTypeRB, 0 );
00056   myTypeGroup->addButton( aCustomTypeRB,   1 );
00057 
00058   QHBoxLayout* aRadioLayout = new QHBoxLayout;
00059   aRadioLayout->setMargin( 0 );
00060   aRadioLayout->setSpacing( SPACING );
00061   aRadioLayout->addWidget( aStandardTypeRB );
00062   aRadioLayout->addWidget( aCustomTypeRB );
00063 
00064   // ---
00065 
00066   myWGStack = new QStackedWidget( this );
00067   myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
00068 
00069   // ---
00070 
00071   QWidget* aStdWidget = new QWidget( myWGStack );
00072 
00073   QLabel* aTypeLab  = new QLabel( tr( "TYPE" ),  aStdWidget );
00074   QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
00075 
00076   myStdTypeCombo  = new QtxComboBox( aStdWidget );
00077   myStdScaleCombo = new QtxComboBox( aStdWidget );
00078 
00079   QGridLayout* aStdLayout = new QGridLayout;
00080   aStdLayout->setMargin( MARGIN );
00081   aStdLayout->setSpacing( SPACING );
00082   aStdLayout->addWidget( aTypeLab,        0, 0 );
00083   aStdLayout->addWidget( myStdTypeCombo,  0, 1 );
00084   aStdLayout->addWidget( aScaleLab,       1, 0 );
00085   aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
00086   aStdWidget->setLayout( aStdLayout );
00087 
00088   // ---
00089 
00090   QWidget* aCustomWidget = new QWidget( myWGStack );
00091 
00092   QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
00093   myCustomTypeCombo = new QtxComboBox( aCustomWidget );
00094   QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
00095 
00096   QGridLayout* aCustomLayout = new QGridLayout;
00097   aCustomLayout->setMargin( MARGIN );
00098   aCustomLayout->setSpacing( SPACING );
00099   aCustomLayout->addWidget( aCustomLab,        0, 0 );
00100   aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
00101   aCustomLayout->addWidget( aBrowseBtn,        0, 2 );
00102   aCustomLayout->setRowStretch( 1, 5 );
00103   aCustomWidget->setLayout( aCustomLayout );
00104 
00105   // ---
00106   
00107   myWGStack->insertWidget( 0, aStdWidget );
00108   myWGStack->insertWidget( 1, aCustomWidget );
00109 
00110   // ---
00111 
00112   QVBoxLayout* aTopLayout = new QVBoxLayout;
00113   aTopLayout->setMargin( MARGIN );
00114   aTopLayout->setSpacing( SPACING );
00115   aTopLayout->addLayout( aRadioLayout );
00116   aTopLayout->addWidget( myWGStack );
00117   setLayout( aTopLayout );
00118 
00119   // ---
00120 
00121   connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
00122   connect( myStdTypeCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onStdMarkerChanged( int ) ) );
00123   connect( aBrowseBtn,  SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
00124 
00125   // ---
00126 
00127   aStandardTypeRB->setChecked( true );
00128   init();
00129 }
00130 
00134 VTKViewer_MarkerWidget::~VTKViewer_MarkerWidget()
00135 {
00136 }
00137 
00138 void VTKViewer_MarkerWidget::setCustomMarkerMap( VTK::MarkerMap theMarkerMap )
00139 {
00140   myCustomMarkerMap = theMarkerMap;
00141 
00142   VTK::MarkerMap::const_iterator it = theMarkerMap.begin(), itEnd = theMarkerMap.end();
00143   for( ; it != itEnd; it++ )
00144   {
00145     int anId = it->first;
00146     VTK::MarkerData aMarkerData = it->second;
00147     QPixmap aPixmap = markerFromData( aMarkerData );
00148     if( !aPixmap.isNull() )
00149     {
00150       myCustomTypeCombo->addItem( aPixmap, QString::number( anId ) );
00151       myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, anId );
00152     }
00153   }
00154 }
00155 
00156 VTK::MarkerMap VTKViewer_MarkerWidget::getCustomMarkerMap()
00157 {
00158   return myCustomMarkerMap;
00159 }
00160 
00161 void VTKViewer_MarkerWidget::setStandardMarker( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
00162 {
00163   if ( ( theMarkerType > VTK::MT_NONE && theMarkerType < VTK::MT_USER ) ||
00164        myExtraMarkerList.contains( theMarkerType ) ) {
00165     myTypeGroup->button( 0 )->setChecked( true );
00166     myWGStack->setCurrentIndex( 0 );
00167     myStdTypeCombo->setCurrentId( theMarkerType );
00168     int aMarkerScale = std::max( (int)VTK::MS_10, std::min( (int)VTK::MS_70, (int)theMarkerScale ) );
00169     myStdScaleCombo->setCurrentId( aMarkerScale );
00170   }
00171 }
00172 
00173 void VTKViewer_MarkerWidget::setCustomMarker( int theId )
00174 {
00175   if ( theId > 0 ) {
00176     myTypeGroup->button( 1 )->setChecked( true );
00177     myWGStack->setCurrentIndex( 1 );
00178     addTexture( theId );
00179     myCustomTypeCombo->setCurrentId( theId );
00180   }
00181 }
00182 
00183 VTK::MarkerType VTKViewer_MarkerWidget::getMarkerType() const
00184 {
00185   return myWGStack->currentIndex() == 0 ? (VTK::MarkerType)myStdTypeCombo->currentId() : VTK::MT_USER;
00186 }
00187 
00188 VTK::MarkerScale VTKViewer_MarkerWidget::getStandardMarkerScale() const
00189 {
00190   return myWGStack->currentIndex() == 0 ? (VTK::MarkerScale)myStdScaleCombo->currentId() : VTK::MS_NONE;
00191 }
00192 
00193 int VTKViewer_MarkerWidget::getCustomMarkerID() const
00194 {
00195   return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
00196 }
00197 
00198 void VTKViewer_MarkerWidget::addExtraStdMarker( VTK::MarkerType theMarkerType, const QPixmap& thePixmap )
00199 {
00200   if( myExtraMarkerList.isEmpty() )
00201     myStdTypeCombo->insertSeparator( myStdTypeCombo->count() );
00202   myStdTypeCombo->addItem( thePixmap, QString() );
00203   myStdTypeCombo->setId( myStdTypeCombo->count()-1, theMarkerType );
00204 
00205   myExtraMarkerList.append( theMarkerType );
00206 }
00207 
00208 void VTKViewer_MarkerWidget::init()
00209 {
00210   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00211 
00212   for ( int i = VTK::MT_POINT; i < VTK::MT_USER; i++ ) {
00213     QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
00214     QPixmap pixmap = resMgr->loadPixmap( "VTKViewer", tr( qPrintable( icoFile ) ) );
00215     myStdTypeCombo->addItem( pixmap, QString() );
00216     myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
00217   }
00218 
00219   for ( int i = VTK::MS_10; i <= VTK::MS_70; i++ ) {
00220     myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
00221     myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
00222   }
00223 }
00224 
00225 void VTKViewer_MarkerWidget::addTexture( int id, bool select )
00226 {
00227   if ( id > 0 && myCustomTypeCombo->index( id ) == -1 &&
00228        myCustomMarkerMap.find( id ) != myCustomMarkerMap.end() ) {
00229     VTK::MarkerData aMarkerData = myCustomMarkerMap[ id ];
00230     QPixmap pixmap = markerFromData( aMarkerData );
00231     if( !pixmap.isNull() ) {
00232       myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
00233       myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
00234       if ( select ) myCustomTypeCombo->setCurrentId( id );
00235     }
00236   }
00237 }
00238 
00239 QPixmap VTKViewer_MarkerWidget::markerFromData( const VTK::MarkerData& theMarkerData )
00240 {
00241   const VTK::MarkerTexture& aMarkerTexture = theMarkerData.second;
00242   vtkSmartPointer<vtkImageData> anImageData = VTK::MakeVTKImage( aMarkerTexture, false );
00243 
00244   QImage anImage = VTK::ConvertToQImage( anImageData.GetPointer() );
00245   if( anImage.isNull() )
00246     return QPixmap();
00247 
00248   return QPixmap::fromImage( anImage );
00249 }
00250 
00251 void VTKViewer_MarkerWidget::onStdMarkerChanged( int index )
00252 {
00253   VTK::MarkerType aMarkerType = (VTK::MarkerType)myStdTypeCombo->id( index );
00254   bool anIsExtraMarker = myExtraMarkerList.contains( aMarkerType );
00255   myStdScaleCombo->setEnabled( !anIsExtraMarker );
00256 }
00257 
00258 void VTKViewer_MarkerWidget::onBrowse()
00259 {
00260   QStringList filters;
00261   filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
00262   QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
00263   if ( !aFileName.isEmpty() ) {
00264     VTK::MarkerTexture aMarkerTexture;
00265     if ( VTK::LoadTextureData( aFileName, VTK::MS_NONE, aMarkerTexture ) ) {
00266       int anId = VTK::GetUniqueId( myCustomMarkerMap );
00267       VTK::MarkerData& aMarkerData = myCustomMarkerMap[ anId ];
00268       aMarkerData.first = aFileName.toStdString();
00269       aMarkerData.second = aMarkerTexture;
00270       addTexture( anId, true );
00271     }
00272   }
00273 }