Salome HOME
bos #19960: [CEA 19958] Show/Hide SHAPERSTUDY objects
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_MarkerDlg.cxx
1 // Copyright (C) 2007-2020  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 <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
40 #include <QButtonGroup>
41 #include <QGridLayout>
42 #include <QHBoxLayout>
43 #include <QKeyEvent>
44 #include <QLabel>
45 #include <QPushButton>
46 #include <QRadioButton>
47 #include <QStackedWidget>
48
49 #define MARGIN  9
50 #define SPACING 6
51
52 GEOMToolsGUI_MarkerDlg::GEOMToolsGUI_MarkerDlg( QWidget* parent )
53   : QDialog ( parent )
54 {
55   setWindowTitle( tr( "SET_MARKER_TLT" ) );
56   setSizeGripEnabled( true );
57   setModal( true );
58
59   // ---
60
61   QRadioButton* aStandardTypeRB = new QRadioButton( tr( "STANDARD_MARKER" ), this );
62   QRadioButton* aCustomTypeRB   = new QRadioButton( tr( "CUSTOM_MARKER" ), this );
63   myTypeGroup = new QButtonGroup( this );
64   myTypeGroup->addButton( aStandardTypeRB, 0 );
65   myTypeGroup->addButton( aCustomTypeRB,   1 );
66
67   QHBoxLayout* aRadioLayout = new QHBoxLayout;
68   aRadioLayout->setMargin( 0 );
69   aRadioLayout->setSpacing( SPACING );
70   aRadioLayout->addWidget( aStandardTypeRB );
71   aRadioLayout->addWidget( aCustomTypeRB );
72
73   // ---
74
75   myWGStack = new QStackedWidget( this );
76   myWGStack->setFrameStyle( QFrame::Box | QFrame::Sunken );
77
78   // ---
79
80   QWidget* aStdWidget = new QWidget( myWGStack );
81
82   QLabel* aTypeLab  = new QLabel( tr( "TYPE" ),  aStdWidget );
83   QLabel* aScaleLab = new QLabel( tr( "SCALE" ), aStdWidget );
84
85   myStdTypeCombo  = new QtxComboBox( aStdWidget );
86   myStdScaleCombo = new QtxComboBox( aStdWidget );
87
88   QGridLayout* aStdLayout = new QGridLayout;
89   aStdLayout->setMargin( MARGIN );
90   aStdLayout->setSpacing( SPACING );
91   aStdLayout->addWidget( aTypeLab,        0, 0 );
92   aStdLayout->addWidget( myStdTypeCombo,  0, 1 );
93   aStdLayout->addWidget( aScaleLab,       1, 0 );
94   aStdLayout->addWidget( myStdScaleCombo, 1, 1 );
95   aStdWidget->setLayout( aStdLayout );
96
97   // ---
98
99   QWidget* aCustomWidget = new QWidget( myWGStack );
100
101   QLabel* aCustomLab = new QLabel( tr( "CUSTOM" ), aCustomWidget );
102   myCustomTypeCombo = new QtxComboBox( aCustomWidget );
103   QPushButton* aBrowseBtn = new QPushButton( tr( "BROWSE" ), aCustomWidget );
104
105   QGridLayout* aCustomLayout = new QGridLayout;
106   aCustomLayout->setMargin( MARGIN );
107   aCustomLayout->setSpacing( SPACING );
108   aCustomLayout->addWidget( aCustomLab,        0, 0 );
109   aCustomLayout->addWidget( myCustomTypeCombo, 0, 1 );
110   aCustomLayout->addWidget( aBrowseBtn,       0, 2 );
111   aCustomLayout->setRowStretch( 1, 5 );
112   aCustomWidget->setLayout( aCustomLayout );
113
114   // ---
115   
116   myWGStack->insertWidget( 0, aStdWidget );
117   myWGStack->insertWidget( 1, aCustomWidget );
118
119   // ---
120   
121   QPushButton* aOkBtn     = new QPushButton( tr( "OK_BTN" ),     this );
122   aOkBtn->setAutoDefault( true ); aOkBtn->setDefault( true ); 
123   QPushButton* aCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
124   aCancelBtn->setAutoDefault( true );
125   QPushButton* aHelpBtn   = new QPushButton( tr( "HELP_BTN" ), this );
126   aHelpBtn->setAutoDefault( true );
127
128   QHBoxLayout* aBtnLayout = new QHBoxLayout;
129   aBtnLayout->setMargin( 0 );
130   aBtnLayout->setSpacing( SPACING );
131   aBtnLayout->addWidget( aOkBtn );
132   aBtnLayout->addSpacing( 10 );
133   aBtnLayout->addStretch();
134   aBtnLayout->addWidget( aCancelBtn );
135   aBtnLayout->addWidget( aHelpBtn );
136
137   // ---
138
139   QVBoxLayout* aTopLayout = new QVBoxLayout;
140   aTopLayout->setMargin( MARGIN );
141   aTopLayout->setSpacing( SPACING );
142   aTopLayout->addLayout( aRadioLayout );
143   aTopLayout->addWidget( myWGStack );
144   aTopLayout->addLayout( aBtnLayout );
145   setLayout( aTopLayout );
146
147   // ---
148
149   connect( myTypeGroup, SIGNAL( buttonClicked( int ) ), myWGStack, SLOT( setCurrentIndex( int ) ) );
150   connect( aBrowseBtn,  SIGNAL( clicked() ), this, SLOT( browse() ) );
151   connect( aOkBtn,      SIGNAL( clicked() ), this, SLOT( accept() ) );
152   connect( aCancelBtn,  SIGNAL( clicked() ), this, SLOT( reject() ) );
153   connect( aHelpBtn,    SIGNAL( clicked() ), this, SLOT( help() ) );
154
155   // ---
156
157   aStandardTypeRB->setChecked( true );
158   init();
159 }
160
161 GEOMToolsGUI_MarkerDlg::~GEOMToolsGUI_MarkerDlg()
162 {
163   myOperation->UnRegister();
164 }
165
166 void GEOMToolsGUI_MarkerDlg::setStandardMarker( GEOM::marker_type type, GEOM::marker_size size )
167 {
168   if ( type > GEOM::MT_NONE && type < GEOM::MT_USER ) {
169     myTypeGroup->button( 0 )->setChecked( true );
170     myWGStack->setCurrentIndex( 0 );
171     myStdTypeCombo->setCurrentId( (int)type );
172 #ifdef WIN32
173     int asize = max( (int)GEOM::MS_10, min( (int)GEOM::MS_70, (int)size ) );
174 #else
175     int asize = std::max( (int)GEOM::MS_10, std::min( (int)GEOM::MS_70, (int)size ) );
176 #endif
177     myStdScaleCombo->setCurrentId( asize );
178   }
179 }
180
181 void GEOMToolsGUI_MarkerDlg::setCustomMarker( int id )
182 {
183   if ( id > 0 ) {
184     myTypeGroup->button( 1 )->setChecked( true );
185     myWGStack->setCurrentIndex( 1 );
186     addTexture( id );
187     myCustomTypeCombo->setCurrentId( id );
188   }
189 }
190
191 GEOM::marker_type GEOMToolsGUI_MarkerDlg::getMarkerType() const
192 {
193   return myWGStack->currentIndex() == 0 ? (GEOM::marker_type)myStdTypeCombo->currentId().toInt() : GEOM::MT_USER;
194 }
195
196 GEOM::marker_size GEOMToolsGUI_MarkerDlg::getStandardMarkerScale() const
197 {
198   return myWGStack->currentIndex() == 0 ? (GEOM::marker_size)myStdScaleCombo->currentId().toInt() : GEOM::MS_NONE;
199 }
200
201 int GEOMToolsGUI_MarkerDlg::getCustomMarkerID() const
202 {
203   return myWGStack->currentIndex() == 1 ? myCustomTypeCombo->currentId().toInt() : 0;
204 }
205
206 void GEOMToolsGUI_MarkerDlg::accept()
207 {
208   if ( getStudy() ) {
209     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
210     
211     SUIT_ViewWindow* window =  getStudy()->application()->desktop()->activeWindow();
212     if (window && window->getViewManager()) {
213       int mgrId = window->getViewManager()->getGlobalId();
214       if ( selMgr ) {
215         SALOME_ListIO selected;
216         selMgr->selectedObjects( selected );
217         if ( !selected.IsEmpty() ) {
218           _PTR(Study) study = getStudy()->studyDS();
219           for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
220             _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
221             GEOM::GEOM_Object_var anObject =
222               GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
223             if ( myWGStack->currentIndex() == 0 ) {
224               if ( !anObject->_is_nil() && GeometryGUI::IsInGeomComponent( aSObject )) {
225                 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
226               }
227               QString aMarker = "%1%2%3";
228               aMarker = aMarker.arg(getMarkerType());
229               aMarker = aMarker.arg(GEOM::subSectionSeparator());
230               aMarker = aMarker.arg(getStandardMarkerScale());
231               getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),GEOM::propertyName( GEOM::PointMarker ), aMarker);
232             }
233             else if ( getCustomMarkerID() > 0 ) {
234               if ( !anObject->_is_nil() && GeometryGUI::IsInGeomComponent( aSObject )) {
235                 anObject->SetMarkerTexture( getCustomMarkerID() );
236               }
237               getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),GEOM::propertyName( GEOM::PointMarker ), QString::number(getCustomMarkerID()));
238             }
239           }
240           GEOM_Displayer displayer;
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();
281   GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
282   for ( int i = 0; i < (int)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() && GeometryGUI::IsInGeomComponent( aSObject )) {
302             GEOM::marker_type mtype = anObject->GetMarkerType();
303
304             if ( aType == -1 )
305               aType = mtype;
306             else if ( aType != mtype ) {
307               aType = (GEOM::marker_type)-1;
308               break;
309             }
310             if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
311               GEOM::marker_size msize = anObject->GetMarkerSize();
312               if ( aSize == -1 )
313                 aSize = msize;
314               else if ( aSize != msize )
315                 break;
316             }
317             else if ( mtype == GEOM::MT_USER ) {
318               int mtexture = anObject->GetMarkerTexture();
319               if ( aTexture == 0 )
320                 aTexture = mtexture;
321               else if ( aTexture != mtexture )
322                 break;
323             }
324           }
325           else {
326             // try study object properties
327             QStringList aMarkerProp;
328             SUIT_ViewWindow* window = getStudy()->application()->desktop()->activeWindow();
329             if (window && window->getViewManager()) {
330               int mgrId = window->getViewManager()->getGlobalId();
331               PropMap aPropMap = getStudy()->getObjectProperties(mgrId, it.Value()->getEntry());
332               aMarkerProp = aPropMap.value(GEOM::propertyName(GEOM::PointMarker)).toString().split( GEOM::subSectionSeparator());
333             }
334             if ( aMarkerProp.size() == 2 ) {
335               // standard marker string contains "TypeOfMarker:ScaleOfMarker"
336               GEOM::marker_type mtype = (GEOM::marker_type)aMarkerProp[0].toInt();
337               GEOM::marker_size msize = (GEOM::marker_size)aMarkerProp[1].toInt();
338
339               if ( aType == -1 )
340                 aType = mtype;
341               else if ( aType != mtype ) {
342                 aType = (GEOM::marker_type)-1;
343                 break;
344               }
345               if ( aSize == -1 )
346                 aSize = msize;
347               else if ( aSize != msize )
348                 break;
349             }
350             else if ( aMarkerProp.size() == 1 ) {
351               // custom marker string contains "IdOfTexture"
352               int mtexture = aMarkerProp[0].toInt();
353
354               if ( aType == -1 )
355                 aType = GEOM::MT_USER;
356               else if ( aType !=  GEOM::MT_USER) {
357                 aType = (GEOM::marker_type)-1;
358                 break;
359               }
360               if ( aTexture == 0 )
361                 aTexture = mtexture;
362               else if ( aTexture != mtexture )
363                 break;
364             }
365           }
366         }
367       }
368     }
369   }
370   if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
371     setStandardMarker( aType, aSize );
372   else if ( aType == GEOM::MT_USER )
373     setCustomMarker( aTexture );
374   else
375     setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
376                       (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
377 }
378
379 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
380 {
381   if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
382     int tWidth, tHeight;
383
384     Handle(TColStd_HArray1OfByte) texture = GeometryGUI::getTexture(id, tWidth, tHeight);
385
386     if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
387       QImage image( tWidth, tHeight, QImage::Format_Mono );
388       image.setColor( 0, qRgba( 0, 0, 0, 0   ) );
389       image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
390       int bytesperline = tWidth/8;
391       for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
392         uchar val = (uchar)texture->Value( j );
393         for ( int k = 0; k < 8; k++ ) {
394           int row = ( j - texture->Lower() ) / bytesperline;
395           int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
396           image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
397         }
398       }
399       QPixmap pixmap = QPixmap::fromImage( image );
400       if ( !pixmap.isNull() ) {
401         myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
402         myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
403         if ( select ) myCustomTypeCombo->setCurrentId( id );
404       }
405     }
406   }
407 }
408
409 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
410 {
411   return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
412 }
413
414 void GEOMToolsGUI_MarkerDlg::browse()
415 {
416   QStringList filters;
417   filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
418   QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
419   if ( !aFileName.isEmpty() ) {
420     addTexture( myOperation->LoadTexture( aFileName.toUtf8().constData() ), true );
421   }
422 }
423
424 void GEOMToolsGUI_MarkerDlg::help()
425 {
426   if ( getStudy() ) {
427     SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
428     app->onHelpContextModule( "GEOM", "point_marker_page.html" );
429   }
430 }