]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMToolsGUI/GEOMToolsGUI_MarkerDlg.cxx
Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_MarkerDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : GEOMToolsGUI_MarkerDlg.cxx
21 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22 //
23 #include "GEOMToolsGUI_MarkerDlg.h"
24
25 #include <GeometryGUI.h>
26 #include <GEOM_Displayer.h>
27
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>
36
37 #include <QButtonGroup>
38 #include <QGridLayout>
39 #include <QHBoxLayout>
40 #include <QKeyEvent>
41 #include <QLabel>
42 #include <QPushButton>
43 #include <QRadioButton>
44 #include <QStackedWidget>
45
46 #define MARGIN  9
47 #define SPACING 6
48
49 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
50   : QDialog ( parent )
51 {
52   setWindowTitle( tr( "SET_MARKER_TLT" ) );
53   setSizeGripEnabled( true );
54   setModal( true );
55
56   // ---
57
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 );
63
64   QHBoxLayout* aRadioLayout = new QHBoxLayout;
65   aRadioLayout->setMargin( 0 );
66   aRadioLayout->setSpacing( SPACING );
67   aRadioLayout->addWidget( aStandardTypeRB );
68   aRadioLayout->addWidget( aCustomTypeRB );
69
70   // ---
71
72   myWGStack = new QStackedWidget( this );
73   myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
74
75   // ---
76
77   QWidget* aStdWidget = new QWidget( myWGStack );
78
79   QLabel* aTypeLab  = new QLabel( tr( "TYPE" ),  aStdWidget );
80   QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
81
82   myStdTypeCombo  = new QtxComboBox( aStdWidget );
83   myStdScaleCombo = new QtxComboBox( aStdWidget );
84
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 );
93
94   // ---
95
96   QWidget* aCustomWidget = new QWidget( myWGStack );
97
98   QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
99   myCustomTypeCombo = new QtxComboBox( aCustomWidget );
100   QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
101
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 );
110
111   // ---
112   
113   myWGStack->insertWidget( 0, aStdWidget );
114   myWGStack->insertWidget( 1, aCustomWidget );
115
116   // ---
117   
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 );
124
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 );
133
134   // ---
135
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 );
143
144   // ---
145
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() ) );
151
152   // ---
153
154   aStandardTypeRB->setChecked( true );
155   init();
156 }
157
158 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
159 {
160   myOperation->Destroy();
161 }
162
163 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
164 {
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 );
169 #ifdef WNT
170     int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
171 #else
172     int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
173 #endif
174     myStdScaleCombo->setCurrentIndex( asize-1 );
175   }
176 }
177
178 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
179 {
180   if ( id > 0 ) {
181     myTypeGroup->button( 1 )->setChecked( true );
182     myWGStack->setCurrentIndex( 1 );
183     addTexture( id );
184     myCustomTypeCombo->setCurrentId( id );
185   }
186 }
187
188 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
189 {
190   return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId() : GEOM::MT_USER;
191 }
192
193 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
194 {
195   return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId() : GEOM::MS_NONE;
196 }
197
198 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
199 {
200   return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
201 }
202
203 void GEOMToolsGUI_MarkerDlg::accept()
204 {
205   if ( getStudy() ) {
206     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
207     if ( selMgr ) {
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() );
221           }
222         }
223         GEOM_Displayer displayer( getStudy() );
224         displayer.Redisplay( selected, true );
225         selMgr->setSelectedObjects( selected );
226       }
227     }
228   }
229   QDialog::accept();
230 }
231
232 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
233 {
234   if ( e->key() == Qt::Key_F1 )
235     help();
236   QDialog::keyPressEvent( e );
237 }
238
239 void GEOMToolsGUI_MarkerDlg::init()
240 {
241   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
242
243   // ---
244
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 );
250   }
251
252   // ---
253
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 );
257   }
258
259   // ---
260   
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] );
266
267   // ---
268
269   GEOM::marker_type aType    = (GEOM::marker_type)-1;
270   GEOM::marker_size aSize    = (GEOM::marker_size)-1;
271   int               aTexture = 0;
272   if ( getStudy() ) {
273     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
274     if ( selMgr ) {
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();
285             if ( aType == -1 )
286               aType = mtype;
287             else if ( aType != mtype ) {
288               aType = (GEOM::marker_type)-1;
289               break;
290             }
291             if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
292               GEOM::marker_size msize = anObject->GetMarkerSize();
293               if ( aSize == -1 )
294                 aSize = msize;
295               else if ( aSize != msize )
296                 break;
297             }
298             else if ( mtype == GEOM::MT_USER ) {
299               int mtexture = anObject->GetMarkerTexture();
300               if ( aTexture == 0 )
301                 aTexture = mtexture;
302               else if ( aTexture != mtexture )
303                 break;
304             }
305           }
306         }
307       }
308     }
309   }
310   if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
311     setStandardMarker( aType, aSize );
312   else if ( aType == GEOM::MT_USER )
313     setCustomMarker( aTexture );
314   else
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)));
317 }
318
319 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
320 {
321   if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
322     int tWidth, tHeight;
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 );
335         }
336       }
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 );
342       }
343     }
344   }
345 }
346
347 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
348 {
349   return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
350 }
351
352 void GEOMToolsGUI_MarkerDlg::browse()
353 {
354   QStringList filters;
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 );
359   }
360 }
361
362 void GEOMToolsGUI_MarkerDlg::help()
363 {
364   if ( getStudy() ) {
365     SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
366     app->onHelpContextModule( "GEOM", "point_marker_page.html" );
367   }
368 }