Back to index

salome-geom  6.5.0
GEOMToolsGUI_MarkerDlg.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 // File   : GEOMToolsGUI_MarkerDlg.cxx
00021 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00022 
00023 #include "GEOMToolsGUI_MarkerDlg.h"
00024 
00025 #include <GeometryGUI.h>
00026 #include <GEOM_Constants.h>
00027 #include <GEOM_Displayer.h>
00028 
00029 #include <Basics_OCCTVersion.hxx>
00030 
00031 #include <QtxComboBox.h>
00032 #include <SUIT_ResourceMgr.h>
00033 #include <SUIT_Session.h>
00034 #include <SUIT_ViewWindow.h>
00035 #include <SUIT_Desktop.h>
00036 #include <SUIT_ViewManager.h>
00037 #include <LightApp_SelectionMgr.h>
00038 #include <SalomeApp_Application.h>
00039 #include <SalomeApp_Study.h>
00040 #include <SALOME_ListIO.hxx>
00041 #include <SALOME_ListIteratorOfListIO.hxx>
00042 
00043 #include <QButtonGroup>
00044 #include <QGridLayout>
00045 #include <QHBoxLayout>
00046 #include <QKeyEvent>
00047 #include <QLabel>
00048 #include <QPushButton>
00049 #include <QRadioButton>
00050 #include <QStackedWidget>
00051 
00052 #define MARGIN  9
00053 #define SPACING 6
00054 
00055 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
00056   : QDialog ( parent )
00057 {
00058   setWindowTitle( tr( "SET_MARKER_TLT" ) );
00059   setSizeGripEnabled( true );
00060   setModal( true );
00061 
00062   // ---
00063 
00064   QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
00065   QRadioButton* aCustomTypeRB   = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
00066   myTypeGroup = new QButtonGroup( this );
00067   myTypeGroup->addButton( aStandardTypeRB, 0 );
00068   myTypeGroup->addButton( aCustomTypeRB,   1 );
00069 
00070   QHBoxLayout* aRadioLayout = new QHBoxLayout;
00071   aRadioLayout->setMargin( 0 );
00072   aRadioLayout->setSpacing( SPACING );
00073   aRadioLayout->addWidget( aStandardTypeRB );
00074   aRadioLayout->addWidget( aCustomTypeRB );
00075 
00076   // ---
00077 
00078   myWGStack = new QStackedWidget( this );
00079   myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
00080 
00081   // ---
00082 
00083   QWidget* aStdWidget = new QWidget( myWGStack );
00084 
00085   QLabel* aTypeLab  = new QLabel( tr( "TYPE" ),  aStdWidget );
00086   QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
00087 
00088   myStdTypeCombo  = new QtxComboBox( aStdWidget );
00089   myStdScaleCombo = new QtxComboBox( aStdWidget );
00090 
00091   QGridLayout* aStdLayout = new QGridLayout;
00092   aStdLayout->setMargin( MARGIN );
00093   aStdLayout->setSpacing( SPACING );
00094   aStdLayout->addWidget( aTypeLab,        0, 0 );
00095   aStdLayout->addWidget( myStdTypeCombo,  0, 1 );
00096   aStdLayout->addWidget( aScaleLab,       1, 0 );
00097   aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
00098   aStdWidget->setLayout( aStdLayout );
00099 
00100   // ---
00101 
00102   QWidget* aCustomWidget = new QWidget( myWGStack );
00103 
00104   QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
00105   myCustomTypeCombo = new QtxComboBox( aCustomWidget );
00106   QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
00107 
00108   QGridLayout* aCustomLayout = new QGridLayout;
00109   aCustomLayout->setMargin( MARGIN );
00110   aCustomLayout->setSpacing( SPACING );
00111   aCustomLayout->addWidget( aCustomLab,        0, 0 );
00112   aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
00113   aCustomLayout->addWidget( aBrowseBtn,       0, 2 );
00114   aCustomLayout->setRowStretch( 1, 5 );
00115   aCustomWidget->setLayout( aCustomLayout );
00116 
00117   // ---
00118   
00119   myWGStack->insertWidget( 0, aStdWidget );
00120   myWGStack->insertWidget( 1, aCustomWidget );
00121 
00122   // ---
00123   
00124   QPushButton* aOkBtn     = new QPushButton( tr( "OK_BTN" ),     this );
00125   aOkBtn->setAutoDefault( true ); aOkBtn->setDefault( true ); 
00126   QPushButton* aCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
00127   aCancelBtn->setAutoDefault( true );
00128   QPushButton* aHelpBtn   = new QPushButton( tr( "HELP_BTN" ), this );
00129   aHelpBtn->setAutoDefault( true );
00130 
00131   QHBoxLayout* aBtnLayout = new QHBoxLayout;
00132   aBtnLayout->setMargin( 0 );
00133   aBtnLayout->setSpacing( SPACING );
00134   aBtnLayout->addWidget( aOkBtn );
00135   aBtnLayout->addSpacing( 10 );
00136   aBtnLayout->addStretch();
00137   aBtnLayout->addWidget( aCancelBtn );
00138   aBtnLayout->addWidget( aHelpBtn );
00139 
00140   // ---
00141 
00142   QVBoxLayout* aTopLayout = new QVBoxLayout;
00143   aTopLayout->setMargin( MARGIN );
00144   aTopLayout->setSpacing( SPACING );
00145   aTopLayout->addLayout( aRadioLayout );
00146   aTopLayout->addWidget( myWGStack );
00147   aTopLayout->addLayout( aBtnLayout );
00148   setLayout( aTopLayout );
00149 
00150   // ---
00151 
00152   connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
00153   connect( aBrowseBtn,  SIGNAL( clicked() ), this, SLOT( browse() ) );
00154   connect( aOkBtn,      SIGNAL( clicked() ), this, SLOT( accept() ) );
00155   connect( aCancelBtn,  SIGNAL( clicked() ), this, SLOT( reject() ) );
00156   connect( aHelpBtn,    SIGNAL( clicked() ), this, SLOT( help() ) );
00157 
00158   // ---
00159 
00160   aStandardTypeRB->setChecked( true );
00161   init();
00162 }
00163 
00164 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
00165 {
00166   myOperation->UnRegister();
00167 }
00168 
00169 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
00170 {
00171   if ( type > GEOM::MT_NONE && type < GEOM::MT_USER ) {
00172     myTypeGroup->button( 0 )->setChecked( true );
00173     myWGStack->setCurrentIndex( 0 );
00174     myStdTypeCombo->setCurrentIndex( (int)type - 1 );
00175 #ifdef WNT
00176     int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
00177 #else
00178     int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
00179 #endif
00180     myStdScaleCombo->setCurrentIndex( asize-1 );
00181   }
00182 }
00183 
00184 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
00185 {
00186   if ( id > 0 ) {
00187     myTypeGroup->button( 1 )->setChecked( true );
00188     myWGStack->setCurrentIndex( 1 );
00189     addTexture( id );
00190     myCustomTypeCombo->setCurrentId( id );
00191   }
00192 }
00193 
00194 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
00195 {
00196   return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId() : GEOM::MT_USER;
00197 }
00198 
00199 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
00200 {
00201   return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId() : GEOM::MS_NONE;
00202 }
00203 
00204 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
00205 {
00206   return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
00207 }
00208 
00209 void GEOMToolsGUI_MarkerDlg::accept()
00210 {
00211   if ( getStudy() ) {
00212     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
00213     
00214     SUIT_ViewWindow* window =  getStudy()->application()->desktop()->activeWindow();
00215     if (window && window->getViewManager()) {
00216       int mgrId = window->getViewManager()->getGlobalId();
00217       if ( selMgr ) {
00218         SALOME_ListIO selected;
00219         selMgr->selectedObjects( selected );
00220         if ( !selected.IsEmpty() ) {
00221           _PTR(Study) study = getStudy()->studyDS();
00222           for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
00223             _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
00224             GEOM::GEOM_Object_var anObject =
00225               GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
00226             if ( !anObject->_is_nil() ) {
00227               if ( myWGStack->currentIndex() == 0 ) {
00228                 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
00229                 QString aMarker = "%1%2%3";
00230                 aMarker = aMarker.arg(getMarkerType());
00231                 aMarker = aMarker.arg(DIGIT_SEPARATOR);
00232                 aMarker = aMarker.arg(getStandardMarkerScale());
00233                 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),MARKER_TYPE_PROP, aMarker);
00234               }
00235               else if ( getCustomMarkerID() > 0 ) {
00236                 anObject->SetMarkerTexture( getCustomMarkerID() );
00237                 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),MARKER_TYPE_PROP, QString::number(getCustomMarkerID()));
00238               }
00239             }
00240           }
00241           GEOM_Displayer displayer( getStudy() );
00242           displayer.Redisplay( selected, true );
00243           selMgr->setSelectedObjects( selected );
00244         }
00245       }
00246     }
00247   }
00248   QDialog::accept();
00249 }
00250 
00251 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
00252 {
00253   if ( e->key() == Qt::Key_F1 )
00254     help();
00255   QDialog::keyPressEvent( e );
00256 }
00257 
00258 void GEOMToolsGUI_MarkerDlg::init()
00259 {
00260   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00261 
00262   // ---
00263 
00264   for ( int i = GEOM::MT_POINT; i < GEOM::MT_USER; i++ ) {
00265     QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
00266     QPixmap pixmap = resMgr->loadPixmap( "GEOM", tr( qPrintable( icoFile ) ) );
00267     myStdTypeCombo->addItem( pixmap, QString() );
00268     myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
00269   }
00270 
00271   // ---
00272 
00273   for ( int i = GEOM::MS_10; i <= GEOM::MS_70; i++ ) {
00274     myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
00275     myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
00276   }
00277 
00278   // ---
00279   
00280   GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
00281   myOperation = engine->GetIInsertOperations( getStudy()->id() );
00282   GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
00283   for ( int i = 0; i < ids->length(); i++ )
00284     addTexture( ids[i] );
00285 
00286   // ---
00287 
00288   GEOM::marker_type aType    = (GEOM::marker_type)-1;
00289   GEOM::marker_size aSize    = (GEOM::marker_size)-1;
00290   int               aTexture = 0;
00291   if ( getStudy() ) {
00292     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
00293     if ( selMgr ) {
00294       SALOME_ListIO selected;
00295       selMgr->selectedObjects( selected );
00296       if ( !selected.IsEmpty() ) {
00297         _PTR(Study) study = getStudy()->studyDS();
00298         for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
00299           _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
00300           GEOM::GEOM_Object_var anObject =
00301             GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
00302           if ( !anObject->_is_nil() ) {
00303             GEOM::marker_type mtype = anObject->GetMarkerType();
00304             if ( aType == -1 )
00305               aType = mtype;
00306             else if ( aType != mtype ) {
00307               aType = (GEOM::marker_type)-1;
00308               break;
00309             }
00310             if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
00311               GEOM::marker_size msize = anObject->GetMarkerSize();
00312               if ( aSize == -1 )
00313                 aSize = msize;
00314               else if ( aSize != msize )
00315                 break;
00316             }
00317             else if ( mtype == GEOM::MT_USER ) {
00318               int mtexture = anObject->GetMarkerTexture();
00319               if ( aTexture == 0 )
00320                 aTexture = mtexture;
00321               else if ( aTexture != mtexture )
00322                 break;
00323             }
00324           }
00325         }
00326       }
00327     }
00328   }
00329   if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
00330     setStandardMarker( aType, aSize );
00331   else if ( aType == GEOM::MT_USER )
00332     setCustomMarker( aTexture );
00333   else
00334     setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
00335                       (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
00336 }
00337 
00338 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
00339 {
00340   if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
00341     int tWidth, tHeight;
00342 
00343 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
00344     Handle(TColStd_HArray1OfByte) texture = GeometryGUI::getTexture(getStudy(), id, tWidth, tHeight);
00345 #else
00346     Handle(Graphic3d_HArray1OfBytes) texture = GeometryGUI::getTexture(getStudy(), id, tWidth, tHeight);
00347 #endif
00348 
00349     if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
00350       QImage image( tWidth, tHeight, QImage::Format_Mono );
00351       image.setColor( 0, qRgba( 0, 0, 0, 0   ) );
00352       image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
00353       int bytesperline = tWidth/8;
00354       for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
00355         uchar val = (uchar)texture->Value( j );
00356         for ( int k = 0; k < 8; k++ ) {
00357           int row = ( j - texture->Lower() ) / bytesperline;
00358           int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
00359           image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
00360         }
00361       }
00362       QPixmap pixmap = QPixmap::fromImage( image );
00363       if ( !pixmap.isNull() ) {
00364         myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
00365         myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
00366         if ( select ) myCustomTypeCombo->setCurrentId( id );
00367       }
00368     }
00369   }
00370 }
00371 
00372 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
00373 {
00374   return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00375 }
00376 
00377 void GEOMToolsGUI_MarkerDlg::browse()
00378 {
00379   QStringList filters;
00380   filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
00381   QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
00382   if ( !aFileName.isEmpty() ) {
00383     addTexture( myOperation->LoadTexture( aFileName.toLatin1().constData() ), true );
00384   }
00385 }
00386 
00387 void GEOMToolsGUI_MarkerDlg::help()
00388 {
00389   if ( getStudy() ) {
00390     SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
00391     app->onHelpContextModule( "GEOM", "point_marker_page.html" );
00392   }
00393 }