1 // Copyright (C) 2007-2009 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : GEOMToolsGUI_MarkerDlg.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 #include "GEOMToolsGUI_MarkerDlg.h"
28 #include <GeometryGUI.h>
29 #include <GEOM_Displayer.h>
31 #include <QtxComboBox.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37 #include <SALOME_ListIO.hxx>
38 #include <SALOME_ListIteratorOfListIO.hxx>
40 #include <QButtonGroup>
41 #include <QGridLayout>
42 #include <QHBoxLayout>
45 #include <QPushButton>
46 #include <QRadioButton>
47 #include <QStackedWidget>
52 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
55 setWindowTitle( tr( "SET_MARKER_TLT" ) );
56 setSizeGripEnabled( true );
61 QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
62 QRadioButton* aCustomTypeRB = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
63 myTypeGroup = new QButtonGroup( this );
64 myTypeGroup->addButton( aStandardTypeRB, 0 );
65 myTypeGroup->addButton( aCustomTypeRB, 1 );
67 QHBoxLayout* aRadioLayout = new QHBoxLayout;
68 aRadioLayout->setMargin( 0 );
69 aRadioLayout->setSpacing( SPACING );
70 aRadioLayout->addWidget( aStandardTypeRB );
71 aRadioLayout->addWidget( aCustomTypeRB );
75 myWGStack = new QStackedWidget( this );
76 myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
80 QWidget* aStdWidget = new QWidget( myWGStack );
82 QLabel* aTypeLab = new QLabel( tr( "TYPE" ), aStdWidget );
83 QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
85 myStdTypeCombo = new QtxComboBox( aStdWidget );
86 myStdScaleCombo = new QtxComboBox( aStdWidget );
88 QGridLayout* aStdLayout = new QGridLayout;
89 aStdLayout->setMargin( MARGIN );
90 aStdLayout->setSpacing( SPACING );
91 aStdLayout->addWidget( aTypeLab, 0, 0 );
92 aStdLayout->addWidget( myStdTypeCombo, 0, 1 );
93 aStdLayout->addWidget( aScaleLab, 1, 0 );
94 aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
95 aStdWidget->setLayout( aStdLayout );
99 QWidget* aCustomWidget = new QWidget( myWGStack );
101 QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
102 myCustomTypeCombo = new QtxComboBox( aCustomWidget );
103 QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
105 QGridLayout* aCustomLayout = new QGridLayout;
106 aCustomLayout->setMargin( MARGIN );
107 aCustomLayout->setSpacing( SPACING );
108 aCustomLayout->addWidget( aCustomLab, 0, 0 );
109 aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
110 aCustomLayout->addWidget( aBrowseBtn, 0, 2 );
111 aCustomLayout->setRowStretch( 1, 5 );
112 aCustomWidget->setLayout( aCustomLayout );
116 myWGStack->insertWidget( 0, aStdWidget );
117 myWGStack->insertWidget( 1, aCustomWidget );
121 QPushButton* aOkBtn = new QPushButton( tr( "OK_BTN" ), this );
122 aOkBtn->setAutoDefault( true ); aOkBtn->setDefault( true );
123 QPushButton* aCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
124 aCancelBtn->setAutoDefault( true );
125 QPushButton* aHelpBtn = new QPushButton( tr( "HELP_BTN" ), this );
126 aHelpBtn->setAutoDefault( true );
128 QHBoxLayout* aBtnLayout = new QHBoxLayout;
129 aBtnLayout->setMargin( 0 );
130 aBtnLayout->setSpacing( SPACING );
131 aBtnLayout->addWidget( aOkBtn );
132 aBtnLayout->addSpacing( 10 );
133 aBtnLayout->addStretch();
134 aBtnLayout->addWidget( aCancelBtn );
135 aBtnLayout->addWidget( aHelpBtn );
139 QVBoxLayout* aTopLayout = new QVBoxLayout;
140 aTopLayout->setMargin( MARGIN );
141 aTopLayout->setSpacing( SPACING );
142 aTopLayout->addLayout( aRadioLayout );
143 aTopLayout->addWidget( myWGStack );
144 aTopLayout->addLayout( aBtnLayout );
145 setLayout( aTopLayout );
149 connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
150 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( browse() ) );
151 connect( aOkBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
152 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
153 connect( aHelpBtn, SIGNAL( clicked() ), this, SLOT( help() ) );
157 aStandardTypeRB->setChecked( true );
161 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
163 myOperation->Destroy();
166 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
168 if ( type > GEOM::MT_NONE && type < GEOM::MT_USER ) {
169 myTypeGroup->button( 0 )->setChecked( true );
170 myWGStack->setCurrentIndex( 0 );
171 myStdTypeCombo->setCurrentIndex( (int)type - 1 );
173 int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
175 int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
177 myStdScaleCombo->setCurrentIndex( asize-1 );
181 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
184 myTypeGroup->button( 1 )->setChecked( true );
185 myWGStack->setCurrentIndex( 1 );
187 myCustomTypeCombo->setCurrentId( id );
191 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
193 return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId() : GEOM::MT_USER;
196 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
198 return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId() : GEOM::MS_NONE;
201 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
203 return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
206 void GEOMToolsGUI_MarkerDlg::accept()
209 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
211 SALOME_ListIO selected;
212 selMgr->selectedObjects( selected );
213 if ( !selected.IsEmpty() ) {
214 _PTR(Study) study = getStudy()->studyDS();
215 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
216 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
217 GEOM::GEOM_Object_var anObject =
218 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
219 if ( !anObject->_is_nil() ) {
220 if ( myWGStack->currentIndex() == 0 )
221 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
222 else if ( getCustomMarkerID() > 0 )
223 anObject->SetMarkerTexture( getCustomMarkerID() );
226 GEOM_Displayer displayer( getStudy() );
227 displayer.Redisplay( selected, true );
234 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
236 if ( e->key() == Qt::Key_F1 )
238 QDialog::keyPressEvent( e );
241 void GEOMToolsGUI_MarkerDlg::init()
243 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
247 for ( int i = GEOM::MT_POINT; i < GEOM::MT_USER; i++ ) {
248 QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
249 QPixmap pixmap = resMgr->loadPixmap( "GEOM", tr( qPrintable( icoFile ) ) );
250 myStdTypeCombo->addItem( pixmap, QString() );
251 myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
256 for ( int i = GEOM::MS_10; i <= GEOM::MS_70; i++ ) {
257 myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
258 myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
263 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
264 myOperation = engine->GetIInsertOperations( getStudy()->id() );
265 GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
266 for ( int i = 0; i < ids->length(); i++ )
267 addTexture( ids[i] );
271 GEOM::marker_type aType = (GEOM::marker_type)-1;
272 GEOM::marker_size aSize = (GEOM::marker_size)-1;
275 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
277 SALOME_ListIO selected;
278 selMgr->selectedObjects( selected );
279 if ( !selected.IsEmpty() ) {
280 _PTR(Study) study = getStudy()->studyDS();
281 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
282 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
283 GEOM::GEOM_Object_var anObject =
284 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
285 if ( !anObject->_is_nil() ) {
286 GEOM::marker_type mtype = anObject->GetMarkerType();
289 else if ( aType != mtype ) {
290 aType = (GEOM::marker_type)-1;
293 if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
294 GEOM::marker_size msize = anObject->GetMarkerSize();
297 else if ( aSize != msize )
300 else if ( mtype == GEOM::MT_USER ) {
301 int mtexture = anObject->GetMarkerTexture();
304 else if ( aTexture != mtexture )
312 if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
313 setStandardMarker( aType, aSize );
314 else if ( aType == GEOM::MT_USER )
315 setCustomMarker( aTexture );
317 setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
318 (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
321 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
323 if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
325 Handle(Graphic3d_HArray1OfBytes) texture = GeometryGUI::getTexture( getStudy(), id, tWidth, tHeight );
326 if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
327 QImage image( tWidth, tHeight, QImage::Format_Mono );
328 image.setColor( 0, qRgba( 0, 0, 0, 0 ) );
329 image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
330 int bytesperline = tWidth/8;
331 for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
332 uchar val = (uchar)texture->Value( j );
333 for ( int k = 0; k < 8; k++ ) {
334 int row = ( j - texture->Lower() ) / bytesperline;
335 int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
336 image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
339 QPixmap pixmap = QPixmap::fromImage( image );
340 if ( !pixmap.isNull() ) {
341 myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
342 myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
343 if ( select ) myCustomTypeCombo->setCurrentId( id );
349 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
351 return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
354 void GEOMToolsGUI_MarkerDlg::browse()
357 filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
358 QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
359 if ( !aFileName.isEmpty() ) {
360 addTexture( myOperation->LoadTexture( aFileName.toLatin1().constData() ), true );
364 void GEOMToolsGUI_MarkerDlg::help()
367 SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
368 app->onHelpContextModule( "GEOM", "point_marker_page.html" );