1 // Copyright (C) 2007-2011 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
19 // File : GEOMToolsGUI_MarkerDlg.cxx
20 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22 #include "GEOMToolsGUI_MarkerDlg.h"
24 #include <GeometryGUI.h>
25 #include <GEOM_Displayer.h>
27 #include <Basics_OCCTVersion.hxx>
29 #include <QtxComboBox.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_ViewWindow.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ViewManager.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_Study.h>
38 #include <SALOME_ListIO.hxx>
39 #include <SALOME_ListIteratorOfListIO.hxx>
41 #include <QButtonGroup>
42 #include <QGridLayout>
43 #include <QHBoxLayout>
46 #include <QPushButton>
47 #include <QRadioButton>
48 #include <QStackedWidget>
53 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
56 setWindowTitle( tr( "SET_MARKER_TLT" ) );
57 setSizeGripEnabled( true );
62 QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
63 QRadioButton* aCustomTypeRB = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
64 myTypeGroup = new QButtonGroup( this );
65 myTypeGroup->addButton( aStandardTypeRB, 0 );
66 myTypeGroup->addButton( aCustomTypeRB, 1 );
68 QHBoxLayout* aRadioLayout = new QHBoxLayout;
69 aRadioLayout->setMargin( 0 );
70 aRadioLayout->setSpacing( SPACING );
71 aRadioLayout->addWidget( aStandardTypeRB );
72 aRadioLayout->addWidget( aCustomTypeRB );
76 myWGStack = new QStackedWidget( this );
77 myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
81 QWidget* aStdWidget = new QWidget( myWGStack );
83 QLabel* aTypeLab = new QLabel( tr( "TYPE" ), aStdWidget );
84 QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
86 myStdTypeCombo = new QtxComboBox( aStdWidget );
87 myStdScaleCombo = new QtxComboBox( aStdWidget );
89 QGridLayout* aStdLayout = new QGridLayout;
90 aStdLayout->setMargin( MARGIN );
91 aStdLayout->setSpacing( SPACING );
92 aStdLayout->addWidget( aTypeLab, 0, 0 );
93 aStdLayout->addWidget( myStdTypeCombo, 0, 1 );
94 aStdLayout->addWidget( aScaleLab, 1, 0 );
95 aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
96 aStdWidget->setLayout( aStdLayout );
100 QWidget* aCustomWidget = new QWidget( myWGStack );
102 QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
103 myCustomTypeCombo = new QtxComboBox( aCustomWidget );
104 QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
106 QGridLayout* aCustomLayout = new QGridLayout;
107 aCustomLayout->setMargin( MARGIN );
108 aCustomLayout->setSpacing( SPACING );
109 aCustomLayout->addWidget( aCustomLab, 0, 0 );
110 aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
111 aCustomLayout->addWidget( aBrowseBtn, 0, 2 );
112 aCustomLayout->setRowStretch( 1, 5 );
113 aCustomWidget->setLayout( aCustomLayout );
117 myWGStack->insertWidget( 0, aStdWidget );
118 myWGStack->insertWidget( 1, aCustomWidget );
122 QPushButton* aOkBtn = new QPushButton( tr( "OK_BTN" ), this );
123 aOkBtn->setAutoDefault( true ); aOkBtn->setDefault( true );
124 QPushButton* aCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
125 aCancelBtn->setAutoDefault( true );
126 QPushButton* aHelpBtn = new QPushButton( tr( "HELP_BTN" ), this );
127 aHelpBtn->setAutoDefault( true );
129 QHBoxLayout* aBtnLayout = new QHBoxLayout;
130 aBtnLayout->setMargin( 0 );
131 aBtnLayout->setSpacing( SPACING );
132 aBtnLayout->addWidget( aOkBtn );
133 aBtnLayout->addSpacing( 10 );
134 aBtnLayout->addStretch();
135 aBtnLayout->addWidget( aCancelBtn );
136 aBtnLayout->addWidget( aHelpBtn );
140 QVBoxLayout* aTopLayout = new QVBoxLayout;
141 aTopLayout->setMargin( MARGIN );
142 aTopLayout->setSpacing( SPACING );
143 aTopLayout->addLayout( aRadioLayout );
144 aTopLayout->addWidget( myWGStack );
145 aTopLayout->addLayout( aBtnLayout );
146 setLayout( aTopLayout );
150 connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
151 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( browse() ) );
152 connect( aOkBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
153 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
154 connect( aHelpBtn, SIGNAL( clicked() ), this, SLOT( help() ) );
158 aStandardTypeRB->setChecked( true );
162 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
164 myOperation->UnRegister();
167 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
169 if ( type > GEOM::MT_NONE && type < GEOM::MT_USER ) {
170 myTypeGroup->button( 0 )->setChecked( true );
171 myWGStack->setCurrentIndex( 0 );
172 myStdTypeCombo->setCurrentIndex( (int)type - 1 );
174 int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
176 int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
178 myStdScaleCombo->setCurrentIndex( asize-1 );
182 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
185 myTypeGroup->button( 1 )->setChecked( true );
186 myWGStack->setCurrentIndex( 1 );
188 myCustomTypeCombo->setCurrentId( id );
192 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
194 return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId() : GEOM::MT_USER;
197 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
199 return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId() : GEOM::MS_NONE;
202 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
204 return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
207 void GEOMToolsGUI_MarkerDlg::accept()
210 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
212 SUIT_ViewWindow* window = getStudy()->application()->desktop()->activeWindow();
213 if (window && window->getViewManager()) {
214 int mgrId = window->getViewManager()->getGlobalId();
216 SALOME_ListIO selected;
217 selMgr->selectedObjects( selected );
218 if ( !selected.IsEmpty() ) {
219 _PTR(Study) study = getStudy()->studyDS();
220 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
221 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
222 GEOM::GEOM_Object_var anObject =
223 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
224 if ( !anObject->_is_nil() ) {
225 if ( myWGStack->currentIndex() == 0 ) {
226 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
227 QString aMarker = "%1%2%3";
228 aMarker = aMarker.arg(getMarkerType());
229 aMarker = aMarker.arg(DIGIT_SEPARATOR);
230 aMarker = aMarker.arg(getStandardMarkerScale());
231 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),MARKER_TYPE_PROP, aMarker);
233 else if ( getCustomMarkerID() > 0 ) {
234 anObject->SetMarkerTexture( getCustomMarkerID() );
235 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),MARKER_TYPE_PROP, QString::number(getCustomMarkerID()));
239 GEOM_Displayer displayer( getStudy() );
240 displayer.Redisplay( selected, true );
241 selMgr->setSelectedObjects( selected );
249 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
251 if ( e->key() == Qt::Key_F1 )
253 QDialog::keyPressEvent( e );
256 void GEOMToolsGUI_MarkerDlg::init()
258 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
262 for ( int i = GEOM::MT_POINT; i < GEOM::MT_USER; i++ ) {
263 QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
264 QPixmap pixmap = resMgr->loadPixmap( "GEOM", tr( qPrintable( icoFile ) ) );
265 myStdTypeCombo->addItem( pixmap, QString() );
266 myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
271 for ( int i = GEOM::MS_10; i <= GEOM::MS_70; i++ ) {
272 myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
273 myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
278 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
279 myOperation = engine->GetIInsertOperations( getStudy()->id() );
280 GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
281 for ( int i = 0; i < ids->length(); i++ )
282 addTexture( ids[i] );
286 GEOM::marker_type aType = (GEOM::marker_type)-1;
287 GEOM::marker_size aSize = (GEOM::marker_size)-1;
290 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
292 SALOME_ListIO selected;
293 selMgr->selectedObjects( selected );
294 if ( !selected.IsEmpty() ) {
295 _PTR(Study) study = getStudy()->studyDS();
296 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
297 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
298 GEOM::GEOM_Object_var anObject =
299 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
300 if ( !anObject->_is_nil() ) {
301 GEOM::marker_type mtype = anObject->GetMarkerType();
304 else if ( aType != mtype ) {
305 aType = (GEOM::marker_type)-1;
308 if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
309 GEOM::marker_size msize = anObject->GetMarkerSize();
312 else if ( aSize != msize )
315 else if ( mtype == GEOM::MT_USER ) {
316 int mtexture = anObject->GetMarkerTexture();
319 else if ( aTexture != mtexture )
327 if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
328 setStandardMarker( aType, aSize );
329 else if ( aType == GEOM::MT_USER )
330 setCustomMarker( aTexture );
332 setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
333 (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
336 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
338 if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
341 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
342 Handle(TColStd_HArray1OfByte) texture = GeometryGUI::getTexture(getStudy(), id, tWidth, tHeight);
344 Handle(Graphic3d_HArray1OfBytes) texture = GeometryGUI::getTexture(getStudy(), id, tWidth, tHeight);
347 if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
348 QImage image( tWidth, tHeight, QImage::Format_Mono );
349 image.setColor( 0, qRgba( 0, 0, 0, 0 ) );
350 image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
351 int bytesperline = tWidth/8;
352 for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
353 uchar val = (uchar)texture->Value( j );
354 for ( int k = 0; k < 8; k++ ) {
355 int row = ( j - texture->Lower() ) / bytesperline;
356 int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
357 image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
360 QPixmap pixmap = QPixmap::fromImage( image );
361 if ( !pixmap.isNull() ) {
362 myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
363 myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
364 if ( select ) myCustomTypeCombo->setCurrentId( id );
370 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
372 return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
375 void GEOMToolsGUI_MarkerDlg::browse()
378 filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
379 QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
380 if ( !aFileName.isEmpty() ) {
381 addTexture( myOperation->LoadTexture( aFileName.toLatin1().constData() ), true );
385 void GEOMToolsGUI_MarkerDlg::help()
388 SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
389 app->onHelpContextModule( "GEOM", "point_marker_page.html" );