Salome HOME
Porting to OCCT6.5.1
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_MarkerDlg.cxx
1 // Copyright (C) 2007-2011  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 // File   : GEOMToolsGUI_MarkerDlg.cxx
20 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
21
22 #include "GEOMToolsGUI_MarkerDlg.h"
23
24 #include <GeometryGUI.h>
25 #include <GEOM_Displayer.h>
26
27 #include <Basics_OCCTVersion.hxx>
28
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>
40
41 #include <QButtonGroup>
42 #include <QGridLayout>
43 #include <QHBoxLayout>
44 #include <QKeyEvent>
45 #include <QLabel>
46 #include <QPushButton>
47 #include <QRadioButton>
48 #include <QStackedWidget>
49
50 #define MARGIN  9
51 #define SPACING 6
52
53 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
54   : QDialog ( parent )
55 {
56   setWindowTitle( tr( "SET_MARKER_TLT" ) );
57   setSizeGripEnabled( true );
58   setModal( true );
59
60   // ---
61
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 );
67
68   QHBoxLayout* aRadioLayout = new QHBoxLayout;
69   aRadioLayout->setMargin( 0 );
70   aRadioLayout->setSpacing( SPACING );
71   aRadioLayout->addWidget( aStandardTypeRB );
72   aRadioLayout->addWidget( aCustomTypeRB );
73
74   // ---
75
76   myWGStack = new QStackedWidget( this );
77   myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
78
79   // ---
80
81   QWidget* aStdWidget = new QWidget( myWGStack );
82
83   QLabel* aTypeLab  = new QLabel( tr( "TYPE" ),  aStdWidget );
84   QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
85
86   myStdTypeCombo  = new QtxComboBox( aStdWidget );
87   myStdScaleCombo = new QtxComboBox( aStdWidget );
88
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 );
97
98   // ---
99
100   QWidget* aCustomWidget = new QWidget( myWGStack );
101
102   QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
103   myCustomTypeCombo = new QtxComboBox( aCustomWidget );
104   QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
105
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 );
114
115   // ---
116   
117   myWGStack->insertWidget( 0, aStdWidget );
118   myWGStack->insertWidget( 1, aCustomWidget );
119
120   // ---
121   
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 );
128
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 );
137
138   // ---
139
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 );
147
148   // ---
149
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() ) );
155
156   // ---
157
158   aStandardTypeRB->setChecked( true );
159   init();
160 }
161
162 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
163 {
164   myOperation->UnRegister();
165 }
166
167 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
168 {
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 );
173 #ifdef WNT
174     int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
175 #else
176     int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
177 #endif
178     myStdScaleCombo->setCurrentIndex( asize-1 );
179   }
180 }
181
182 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
183 {
184   if ( id > 0 ) {
185     myTypeGroup->button( 1 )->setChecked( true );
186     myWGStack->setCurrentIndex( 1 );
187     addTexture( id );
188     myCustomTypeCombo->setCurrentId( id );
189   }
190 }
191
192 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
193 {
194   return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId() : GEOM::MT_USER;
195 }
196
197 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
198 {
199   return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId() : GEOM::MS_NONE;
200 }
201
202 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
203 {
204   return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId() : 0;
205 }
206
207 void GEOMToolsGUI_MarkerDlg::accept()
208 {
209   if ( getStudy() ) {
210     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
211     
212     SUIT_ViewWindow* window =  getStudy()->application()->desktop()->activeWindow();
213     if (window && window->getViewManager()) {
214       int mgrId = window->getViewManager()->getGlobalId();
215       if ( selMgr ) {
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);
232               }
233               else if ( getCustomMarkerID() > 0 ) {
234                 anObject->SetMarkerTexture( getCustomMarkerID() );
235                 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),MARKER_TYPE_PROP, QString::number(getCustomMarkerID()));
236               }
237             }
238           }
239           GEOM_Displayer displayer( getStudy() );
240           displayer.Redisplay( selected, true );
241           selMgr->setSelectedObjects( selected );
242         }
243       }
244     }
245   }
246   QDialog::accept();
247 }
248
249 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
250 {
251   if ( e->key() == Qt::Key_F1 )
252     help();
253   QDialog::keyPressEvent( e );
254 }
255
256 void GEOMToolsGUI_MarkerDlg::init()
257 {
258   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
259
260   // ---
261
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 );
267   }
268
269   // ---
270
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 );
274   }
275
276   // ---
277   
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] );
283
284   // ---
285
286   GEOM::marker_type aType    = (GEOM::marker_type)-1;
287   GEOM::marker_size aSize    = (GEOM::marker_size)-1;
288   int               aTexture = 0;
289   if ( getStudy() ) {
290     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
291     if ( selMgr ) {
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();
302             if ( aType == -1 )
303               aType = mtype;
304             else if ( aType != mtype ) {
305               aType = (GEOM::marker_type)-1;
306               break;
307             }
308             if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
309               GEOM::marker_size msize = anObject->GetMarkerSize();
310               if ( aSize == -1 )
311                 aSize = msize;
312               else if ( aSize != msize )
313                 break;
314             }
315             else if ( mtype == GEOM::MT_USER ) {
316               int mtexture = anObject->GetMarkerTexture();
317               if ( aTexture == 0 )
318                 aTexture = mtexture;
319               else if ( aTexture != mtexture )
320                 break;
321             }
322           }
323         }
324       }
325     }
326   }
327   if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
328     setStandardMarker( aType, aSize );
329   else if ( aType == GEOM::MT_USER )
330     setCustomMarker( aTexture );
331   else
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)));
334 }
335
336 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
337 {
338   if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
339     int tWidth, tHeight;
340
341 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
342     Handle(TColStd_HArray1OfByte) texture = GeometryGUI::getTexture(getStudy(), id, tWidth, tHeight);
343 #else
344     Handle(Graphic3d_HArray1OfBytes) texture = GeometryGUI::getTexture(getStudy(), id, tWidth, tHeight);
345 #endif
346
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 );
358         }
359       }
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 );
365       }
366     }
367   }
368 }
369
370 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
371 {
372   return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
373 }
374
375 void GEOMToolsGUI_MarkerDlg::browse()
376 {
377   QStringList filters;
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 );
382   }
383 }
384
385 void GEOMToolsGUI_MarkerDlg::help()
386 {
387   if ( getStudy() ) {
388     SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
389     app->onHelpContextModule( "GEOM", "point_marker_page.html" );
390   }
391 }