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