1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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_Constants.h>
27 #include <GEOM_Displayer.h>
29 #include <Basics_OCCTVersion.hxx>
31 #include <QtxComboBox.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ViewWindow.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_ViewManager.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <SalomeApp_Application.h>
39 #include <SalomeApp_Study.h>
40 #include <SALOME_ListIO.hxx>
42 #include <QButtonGroup>
43 #include <QGridLayout>
44 #include <QHBoxLayout>
47 #include <QPushButton>
48 #include <QRadioButton>
49 #include <QStackedWidget>
54 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
57 setWindowTitle( tr( "SET_MARKER_TLT" ) );
58 setSizeGripEnabled( true );
63 QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
64 QRadioButton* aCustomTypeRB = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
65 myTypeGroup = new QButtonGroup( this );
66 myTypeGroup->addButton( aStandardTypeRB, 0 );
67 myTypeGroup->addButton( aCustomTypeRB, 1 );
69 QHBoxLayout* aRadioLayout = new QHBoxLayout;
70 aRadioLayout->setMargin( 0 );
71 aRadioLayout->setSpacing( SPACING );
72 aRadioLayout->addWidget( aStandardTypeRB );
73 aRadioLayout->addWidget( aCustomTypeRB );
77 myWGStack = new QStackedWidget( this );
78 myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
82 QWidget* aStdWidget = new QWidget( myWGStack );
84 QLabel* aTypeLab = new QLabel( tr( "TYPE" ), aStdWidget );
85 QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
87 myStdTypeCombo = new QtxComboBox( aStdWidget );
88 myStdScaleCombo = new QtxComboBox( aStdWidget );
90 QGridLayout* aStdLayout = new QGridLayout;
91 aStdLayout->setMargin( MARGIN );
92 aStdLayout->setSpacing( SPACING );
93 aStdLayout->addWidget( aTypeLab, 0, 0 );
94 aStdLayout->addWidget( myStdTypeCombo, 0, 1 );
95 aStdLayout->addWidget( aScaleLab, 1, 0 );
96 aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
97 aStdWidget->setLayout( aStdLayout );
101 QWidget* aCustomWidget = new QWidget( myWGStack );
103 QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
104 myCustomTypeCombo = new QtxComboBox( aCustomWidget );
105 QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
107 QGridLayout* aCustomLayout = new QGridLayout;
108 aCustomLayout->setMargin( MARGIN );
109 aCustomLayout->setSpacing( SPACING );
110 aCustomLayout->addWidget( aCustomLab, 0, 0 );
111 aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
112 aCustomLayout->addWidget( aBrowseBtn, 0, 2 );
113 aCustomLayout->setRowStretch( 1, 5 );
114 aCustomWidget->setLayout( aCustomLayout );
118 myWGStack->insertWidget( 0, aStdWidget );
119 myWGStack->insertWidget( 1, aCustomWidget );
123 QPushButton* aOkBtn = new QPushButton( tr( "OK_BTN" ), this );
124 aOkBtn->setAutoDefault( true ); aOkBtn->setDefault( true );
125 QPushButton* aCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
126 aCancelBtn->setAutoDefault( true );
127 QPushButton* aHelpBtn = new QPushButton( tr( "HELP_BTN" ), this );
128 aHelpBtn->setAutoDefault( true );
130 QHBoxLayout* aBtnLayout = new QHBoxLayout;
131 aBtnLayout->setMargin( 0 );
132 aBtnLayout->setSpacing( SPACING );
133 aBtnLayout->addWidget( aOkBtn );
134 aBtnLayout->addSpacing( 10 );
135 aBtnLayout->addStretch();
136 aBtnLayout->addWidget( aCancelBtn );
137 aBtnLayout->addWidget( aHelpBtn );
141 QVBoxLayout* aTopLayout = new QVBoxLayout;
142 aTopLayout->setMargin( MARGIN );
143 aTopLayout->setSpacing( SPACING );
144 aTopLayout->addLayout( aRadioLayout );
145 aTopLayout->addWidget( myWGStack );
146 aTopLayout->addLayout( aBtnLayout );
147 setLayout( aTopLayout );
151 connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
152 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( browse() ) );
153 connect( aOkBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
154 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
155 connect( aHelpBtn, SIGNAL( clicked() ), this, SLOT( help() ) );
159 aStandardTypeRB->setChecked( true );
163 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
165 myOperation->UnRegister();
168 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
170 if ( type > GEOM::MT_NONE && type < GEOM::MT_USER ) {
171 myTypeGroup->button( 0 )->setChecked( true );
172 myWGStack->setCurrentIndex( 0 );
173 myStdTypeCombo->setCurrentId( (int)type );
175 int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
177 int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
179 myStdScaleCombo->setCurrentId( asize );
183 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
186 myTypeGroup->button( 1 )->setChecked( true );
187 myWGStack->setCurrentIndex( 1 );
189 myCustomTypeCombo->setCurrentId( id );
193 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
195 return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId().toInt() : GEOM::MT_USER;
198 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
200 return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId().toInt() : GEOM::MS_NONE;
203 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
205 return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId().toInt() : 0;
208 void GEOMToolsGUI_MarkerDlg::accept()
211 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
213 SUIT_ViewWindow* window = getStudy()->application()->desktop()->activeWindow();
214 if (window && window->getViewManager()) {
215 int mgrId = window->getViewManager()->getGlobalId();
217 SALOME_ListIO selected;
218 selMgr->selectedObjects( selected );
219 if ( !selected.IsEmpty() ) {
220 _PTR(Study) study = getStudy()->studyDS();
221 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
222 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
223 GEOM::GEOM_Object_var anObject =
224 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
225 if ( !anObject->_is_nil() ) {
226 if ( myWGStack->currentIndex() == 0 ) {
227 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
228 QString aMarker = "%1%2%3";
229 aMarker = aMarker.arg(getMarkerType());
230 aMarker = aMarker.arg(GEOM::subSectionSeparator());
231 aMarker = aMarker.arg(getStandardMarkerScale());
232 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),GEOM::propertyName( GEOM::PointMarker ), aMarker);
234 else if ( getCustomMarkerID() > 0 ) {
235 anObject->SetMarkerTexture( getCustomMarkerID() );
236 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),GEOM::propertyName( GEOM::PointMarker ), QString::number(getCustomMarkerID()));
240 GEOM_Displayer displayer( getStudy() );
241 displayer.Redisplay( selected, true );
242 selMgr->setSelectedObjects( selected );
250 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
252 if ( e->key() == Qt::Key_F1 )
254 QDialog::keyPressEvent( e );
257 void GEOMToolsGUI_MarkerDlg::init()
259 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
263 for ( int i = GEOM::MT_POINT; i < GEOM::MT_USER; i++ ) {
264 QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
265 QPixmap pixmap = resMgr->loadPixmap( "GEOM", tr( qPrintable( icoFile ) ) );
266 myStdTypeCombo->addItem( pixmap, QString() );
267 myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
272 for ( int i = GEOM::MS_10; i <= GEOM::MS_70; i++ ) {
273 myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
274 myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
279 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
280 myOperation = engine->GetIInsertOperations( getStudy()->id() );
281 GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
282 for ( int i = 0; i < ids->length(); i++ )
283 addTexture( ids[i] );
287 GEOM::marker_type aType = (GEOM::marker_type)-1;
288 GEOM::marker_size aSize = (GEOM::marker_size)-1;
291 LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
293 SALOME_ListIO selected;
294 selMgr->selectedObjects( selected );
295 if ( !selected.IsEmpty() ) {
296 _PTR(Study) study = getStudy()->studyDS();
297 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
298 _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
299 GEOM::GEOM_Object_var anObject =
300 GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
301 if ( !anObject->_is_nil() ) {
302 GEOM::marker_type mtype = anObject->GetMarkerType();
305 else if ( aType != mtype ) {
306 aType = (GEOM::marker_type)-1;
309 if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
310 GEOM::marker_size msize = anObject->GetMarkerSize();
313 else if ( aSize != msize )
316 else if ( mtype == GEOM::MT_USER ) {
317 int mtexture = anObject->GetMarkerTexture();
320 else if ( aTexture != mtexture )
328 if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
329 setStandardMarker( aType, aSize );
330 else if ( aType == GEOM::MT_USER )
331 setCustomMarker( aTexture );
333 setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
334 (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
337 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
339 if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
342 Handle(TColStd_HArray1OfByte) texture = GeometryGUI::getTexture(getStudy(), id, tWidth, tHeight);
344 if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
345 QImage image( tWidth, tHeight, QImage::Format_Mono );
346 image.setColor( 0, qRgba( 0, 0, 0, 0 ) );
347 image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
348 int bytesperline = tWidth/8;
349 for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
350 uchar val = (uchar)texture->Value( j );
351 for ( int k = 0; k < 8; k++ ) {
352 int row = ( j - texture->Lower() ) / bytesperline;
353 int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
354 image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
357 QPixmap pixmap = QPixmap::fromImage( image );
358 if ( !pixmap.isNull() ) {
359 myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
360 myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
361 if ( select ) myCustomTypeCombo->setCurrentId( id );
367 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
369 return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
372 void GEOMToolsGUI_MarkerDlg::browse()
375 filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
376 QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
377 if ( !aFileName.isEmpty() ) {
378 addTexture( myOperation->LoadTexture( aFileName.toLatin1().constData() ), true );
382 void GEOMToolsGUI_MarkerDlg::help()
385 SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
386 app->onHelpContextModule( "GEOM", "point_marker_page.html" );