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 <SUIT_ViewWindow.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_ViewManager.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 SUIT_ViewWindow* window = getStudy()->application()->desktop()->activeWindow();
212 if (window && window->getViewManager()) {
213 int mgrId = window->getViewManager()->getGlobalId();
215 SALOME_ListIO selected;
216 selMgr->selectedObjects( selected );
217 if ( !selected.IsEmpty() ) {
218 _PTR(Study) study = getStudy()->studyDS();
219 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
220 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
221 GEOM::GEOM_Object_var anObject =
222 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
223 if ( !anObject->_is_nil() ) {
224 if ( myWGStack->currentIndex() == 0 ) {
225 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
226 QString aMarker = "%1%2%3";
227 aMarker = aMarker.arg(getMarkerType());
228 aMarker = aMarker.arg(DIGIT_SEPARATOR);
229 aMarker = aMarker.arg(getStandardMarkerScale());
230 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),MARKER_TYPE_PROP, aMarker);
232 else if ( getCustomMarkerID() > 0 ) {
233 anObject->SetMarkerTexture( getCustomMarkerID() );
234 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),MARKER_TYPE_PROP, QString::number(getCustomMarkerID()));
238 GEOM_Displayer displayer( getStudy() );
239 displayer.Redisplay( selected, true );
240 selMgr->setSelectedObjects( selected );
248 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
250 if ( e->key() == Qt::Key_F1 )
252 QDialog::keyPressEvent( e );
255 void GEOMToolsGUI_MarkerDlg::init()
257 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
261 for ( int i = GEOM::MT_POINT; i < GEOM::MT_USER; i++ ) {
262 QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
263 QPixmap pixmap = resMgr->loadPixmap( "GEOM", tr( qPrintable( icoFile ) ) );
264 myStdTypeCombo->addItem( pixmap, QString() );
265 myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
270 for ( int i = GEOM::MS_10; i <= GEOM::MS_70; i++ ) {
271 myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
272 myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
277 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
278 myOperation = engine->GetIInsertOperations( getStudy()->id() );
279 GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
280 for ( int i = 0; i < ids->length(); i++ )
281 addTexture( ids[i] );
285 GEOM::marker_type aType = (GEOM::marker_type)-1;
286 GEOM::marker_size aSize = (GEOM::marker_size)-1;
289 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
291 SALOME_ListIO selected;
292 selMgr->selectedObjects( selected );
293 if ( !selected.IsEmpty() ) {
294 _PTR(Study) study = getStudy()->studyDS();
295 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
296 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
297 GEOM::GEOM_Object_var anObject =
298 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
299 if ( !anObject->_is_nil() ) {
300 GEOM::marker_type mtype = anObject->GetMarkerType();
303 else if ( aType != mtype ) {
304 aType = (GEOM::marker_type)-1;
307 if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
308 GEOM::marker_size msize = anObject->GetMarkerSize();
311 else if ( aSize != msize )
314 else if ( mtype == GEOM::MT_USER ) {
315 int mtexture = anObject->GetMarkerTexture();
318 else if ( aTexture != mtexture )
326 if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
327 setStandardMarker( aType, aSize );
328 else if ( aType == GEOM::MT_USER )
329 setCustomMarker( aTexture );
331 setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
332 (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
335 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
337 if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
339 Handle(Graphic3d_HArray1OfBytes) texture = GeometryGUI::getTexture( getStudy(), id, tWidth, tHeight );
340 if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
341 QImage image( tWidth, tHeight, QImage::Format_Mono );
342 image.setColor( 0, qRgba( 0, 0, 0, 0 ) );
343 image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
344 int bytesperline = tWidth/8;
345 for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
346 uchar val = (uchar)texture->Value( j );
347 for ( int k = 0; k < 8; k++ ) {
348 int row = ( j - texture->Lower() ) / bytesperline;
349 int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
350 image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
353 QPixmap pixmap = QPixmap::fromImage( image );
354 if ( !pixmap.isNull() ) {
355 myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
356 myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
357 if ( select ) myCustomTypeCombo->setCurrentId( id );
363 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
365 return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
368 void GEOMToolsGUI_MarkerDlg::browse()
371 filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
372 QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
373 if ( !aFileName.isEmpty() ) {
374 addTexture( myOperation->LoadTexture( aFileName.toLatin1().constData() ), true );
378 void GEOMToolsGUI_MarkerDlg::help()
381 SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
382 app->onHelpContextModule( "GEOM", "point_marker_page.html" );