1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : GEOMToolsGUI_MarkerDlg.cxx
21 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
23 #include "GEOMToolsGUI_MarkerDlg.h"
25 #include <GeometryGUI.h>
26 #include <GEOM_Displayer.h>
28 #include <QtxComboBox.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SUIT_Session.h>
31 #include <LightApp_SelectionMgr.h>
32 #include <SalomeApp_Application.h>
33 #include <SalomeApp_Study.h>
34 #include <SALOME_ListIO.hxx>
35 #include <SALOME_ListIteratorOfListIO.hxx>
37 #include <QButtonGroup>
38 #include <QGridLayout>
39 #include <QHBoxLayout>
42 #include <QPushButton>
43 #include <QRadioButton>
44 #include <QStackedWidget>
49 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
52 setWindowTitle( tr( "SET_MARKER_TLT" ) );
53 setSizeGripEnabled( true );
58 QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
59 QRadioButton* aCustomTypeRB = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
60 myTypeGroup = new QButtonGroup( this );
61 myTypeGroup->addButton( aStandardTypeRB, 0 );
62 myTypeGroup->addButton( aCustomTypeRB, 1 );
64 QHBoxLayout* aRadioLayout = new QHBoxLayout;
65 aRadioLayout->setMargin( 0 );
66 aRadioLayout->setSpacing( SPACING );
67 aRadioLayout->addWidget( aStandardTypeRB );
68 aRadioLayout->addWidget( aCustomTypeRB );
72 myWGStack = new QStackedWidget( this );
73 myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
77 QWidget* aStdWidget = new QWidget( myWGStack );
79 QLabel* aTypeLab = new QLabel( tr( "TYPE" ), aStdWidget );
80 QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
82 myStdTypeCombo = new QtxComboBox( aStdWidget );
83 myStdScaleCombo = new QtxComboBox( aStdWidget );
85 QGridLayout* aStdLayout = new QGridLayout;
86 aStdLayout->setMargin( MARGIN );
87 aStdLayout->setSpacing( SPACING );
88 aStdLayout->addWidget( aTypeLab, 0, 0 );
89 aStdLayout->addWidget( myStdTypeCombo, 0, 1 );
90 aStdLayout->addWidget( aScaleLab, 1, 0 );
91 aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
92 aStdWidget->setLayout( aStdLayout );
96 QWidget* aCustomWidget = new QWidget( myWGStack );
98 QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
99 myCustomTypeCombo = new QtxComboBox( aCustomWidget );
100 QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
102 QGridLayout* aCustomLayout = new QGridLayout;
103 aCustomLayout->setMargin( MARGIN );
104 aCustomLayout->setSpacing( SPACING );
105 aCustomLayout->addWidget( aCustomLab, 0, 0 );
106 aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
107 aCustomLayout->addWidget( aBrowseBtn, 0, 2 );
108 aCustomLayout->setRowStretch( 1, 5 );
109 aCustomWidget->setLayout( aCustomLayout );
113 myWGStack->insertWidget( 0, aStdWidget );
114 myWGStack->insertWidget( 1, aCustomWidget );
118 QPushButton* aOkBtn = new QPushButton( tr( "OK_BTN" ), this );
119 aOkBtn->setAutoDefault( true ); aOkBtn->setDefault( true );
120 QPushButton* aCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
121 aCancelBtn->setAutoDefault( true );
122 QPushButton* aHelpBtn = new QPushButton( tr( "HELP_BTN" ), this );
123 aHelpBtn->setAutoDefault( true );
125 QHBoxLayout* aBtnLayout = new QHBoxLayout;
126 aBtnLayout->setMargin( 0 );
127 aBtnLayout->setSpacing( SPACING );
128 aBtnLayout->addWidget( aOkBtn );
129 aBtnLayout->addSpacing( 10 );
130 aBtnLayout->addStretch();
131 aBtnLayout->addWidget( aCancelBtn );
132 aBtnLayout->addWidget( aHelpBtn );
136 QVBoxLayout* aTopLayout = new QVBoxLayout;
137 aTopLayout->setMargin( MARGIN );
138 aTopLayout->setSpacing( SPACING );
139 aTopLayout->addLayout( aRadioLayout );
140 aTopLayout->addWidget( myWGStack );
141 aTopLayout->addLayout( aBtnLayout );
142 setLayout( aTopLayout );
146 connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
147 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( browse() ) );
148 connect( aOkBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
149 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
150 connect( aHelpBtn, SIGNAL( clicked() ), this, SLOT( help() ) );
154 aStandardTypeRB->setChecked( true );
158 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
160 myOperation->Destroy();
163 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
165 if ( type > GEOM::MT_NONE && type < GEOM::MT_USER ) {
166 myTypeGroup->button( 0 )->setChecked( true );
167 myWGStack->setCurrentIndex( 0 );
168 myStdTypeCombo->setCurrentIndex( (int)type - 1 );
170 int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
172 int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
174 myStdScaleCombo->setCurrentIndex( asize-1 );
178 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
181 myTypeGroup->button( 1 )->setChecked( true );
182 myWGStack->setCurrentIndex( 1 );
184 myCustomTypeCombo->setCurrentId( id );
188 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
190 return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId() : GEOM::MT_USER;
193 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
195 return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId() : GEOM::MS_NONE;
198 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
200 return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
203 void GEOMToolsGUI_MarkerDlg::accept()
206 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
208 SALOME_ListIO selected;
209 selMgr->selectedObjects( selected );
210 if ( !selected.IsEmpty() ) {
211 _PTR(Study) study = getStudy()->studyDS();
212 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
213 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
214 GEOM::GEOM_Object_var anObject =
215 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
216 if ( !anObject->_is_nil() ) {
217 if ( myWGStack->currentIndex() == 0 )
218 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
219 else if ( getCustomMarkerID() > 0 )
220 anObject->SetMarkerTexture( getCustomMarkerID() );
223 GEOM_Displayer displayer( getStudy() );
224 displayer.Redisplay( selected, true );
225 selMgr->setSelectedObjects( selected );
232 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
234 if ( e->key() == Qt::Key_F1 )
236 QDialog::keyPressEvent( e );
239 void GEOMToolsGUI_MarkerDlg::init()
241 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
245 for ( int i = GEOM::MT_POINT; i < GEOM::MT_USER; i++ ) {
246 QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
247 QPixmap pixmap = resMgr->loadPixmap( "GEOM", tr( qPrintable( icoFile ) ) );
248 myStdTypeCombo->addItem( pixmap, QString() );
249 myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
254 for ( int i = GEOM::MS_10; i <= GEOM::MS_70; i++ ) {
255 myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
256 myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
261 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
262 myOperation = engine->GetIInsertOperations( getStudy()->id() );
263 GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
264 for ( int i = 0; i < ids->length(); i++ )
265 addTexture( ids[i] );
269 GEOM::marker_type aType = (GEOM::marker_type)-1;
270 GEOM::marker_size aSize = (GEOM::marker_size)-1;
273 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
275 SALOME_ListIO selected;
276 selMgr->selectedObjects( selected );
277 if ( !selected.IsEmpty() ) {
278 _PTR(Study) study = getStudy()->studyDS();
279 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
280 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
281 GEOM::GEOM_Object_var anObject =
282 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
283 if ( !anObject->_is_nil() ) {
284 GEOM::marker_type mtype = anObject->GetMarkerType();
287 else if ( aType != mtype ) {
288 aType = (GEOM::marker_type)-1;
291 if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
292 GEOM::marker_size msize = anObject->GetMarkerSize();
295 else if ( aSize != msize )
298 else if ( mtype == GEOM::MT_USER ) {
299 int mtexture = anObject->GetMarkerTexture();
302 else if ( aTexture != mtexture )
310 if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
311 setStandardMarker( aType, aSize );
312 else if ( aType == GEOM::MT_USER )
313 setCustomMarker( aTexture );
315 setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
316 (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
319 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
321 if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
323 Handle(Graphic3d_HArray1OfBytes) texture = GeometryGUI::getTexture( getStudy(), id, tWidth, tHeight );
324 if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
325 QImage image( tWidth, tHeight, QImage::Format_Mono );
326 image.setColor( 0, qRgba( 0, 0, 0, 0 ) );
327 image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
328 int bytesperline = tWidth/8;
329 for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
330 uchar val = (uchar)texture->Value( j );
331 for ( int k = 0; k < 8; k++ ) {
332 int row = ( j - texture->Lower() ) / bytesperline;
333 int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
334 image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
337 QPixmap pixmap = QPixmap::fromImage( image );
338 if ( !pixmap.isNull() ) {
339 myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
340 myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
341 if ( select ) myCustomTypeCombo->setCurrentId( id );
347 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
349 return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
352 void GEOMToolsGUI_MarkerDlg::browse()
355 filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
356 QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
357 if ( !aFileName.isEmpty() ) {
358 addTexture( myOperation->LoadTexture( aFileName.toLatin1().constData() ), true );
362 void GEOMToolsGUI_MarkerDlg::help()
365 SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
366 app->onHelpContextModule( "GEOM", "point_marker_page.html" );