Salome HOME
Unicode support: correct handling of unicode on GUI level
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_MarkerDlg.cxx
1 // Copyright (C) 2007-2016  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 ( !anObject->_is_nil() ) {
224               if ( myWGStack->currentIndex() == 0 ) {
225                 anObject->SetMarkerStd( getMarkerType(), getStandardMarkerScale() );
226                 QString aMarker = "%1%2%3";
227                 aMarker = aMarker.arg(getMarkerType());
228                 aMarker = aMarker.arg(GEOM::subSectionSeparator());
229                 aMarker = aMarker.arg(getStandardMarkerScale());
230                 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),GEOM::propertyName( GEOM::PointMarker ), aMarker);
231               }
232               else if ( getCustomMarkerID() > 0 ) {
233                 anObject->SetMarkerTexture( getCustomMarkerID() );
234                 getStudy()->setObjectProperty(mgrId ,it.Value()->getEntry(),GEOM::propertyName( GEOM::PointMarker ), QString::number(getCustomMarkerID()));
235               }
236             }
237           }
238           GEOM_Displayer displayer;
239           displayer.Redisplay( selected, true );
240           selMgr->setSelectedObjects( selected );
241         }
242       }
243     }
244   }
245   QDialog::accept();
246 }
247
248 void GEOMToolsGUI_MarkerDlg::keyPressEvent( QKeyEvent* e )
249 {
250   if ( e->key() == Qt::Key_F1 )
251     help();
252   QDialog::keyPressEvent( e );
253 }
254
255 void GEOMToolsGUI_MarkerDlg::init()
256 {
257   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
258
259   // ---
260
261   for ( int i = GEOM::MT_POINT; i < GEOM::MT_USER; i++ ) {
262     QString icoFile = QString( "ICON_VERTEX_MARKER_%1" ).arg( i );
263     QPixmap pixmap = resMgr->loadPixmap( "GEOM", tr( qPrintable( icoFile ) ) );
264     myStdTypeCombo->addItem( pixmap, QString() );
265     myStdTypeCombo->setId( myStdTypeCombo->count()-1, i );
266   }
267
268   // ---
269
270   for ( int i = GEOM::MS_10; i <= GEOM::MS_70; i++ ) {
271     myStdScaleCombo->addItem( QString::number( (i-1)*0.5 + 1.0 ) );
272     myStdScaleCombo->setId( myStdScaleCombo->count()-1, i );
273   }
274
275   // ---
276   
277   GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
278   myOperation = engine->GetIInsertOperations();
279   GEOM::ListOfLong_var ids = myOperation->GetAllTextures();
280   for ( int i = 0; i < ids->length(); i++ )
281     addTexture( ids[i] );
282
283   // ---
284
285   GEOM::marker_type aType    = (GEOM::marker_type)-1;
286   GEOM::marker_size aSize    = (GEOM::marker_size)-1;
287   int               aTexture = 0;
288   if ( getStudy() ) {
289     LightApp_SelectionMgr* selMgr = qobject_cast<SalomeApp_Application*>( getStudy()->application() )->selectionMgr();
290     if ( selMgr ) {
291       SALOME_ListIO selected;
292       selMgr->selectedObjects( selected );
293       if ( !selected.IsEmpty() ) {
294         _PTR(Study) study = getStudy()->studyDS();
295         for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
296           _PTR(SObject) aSObject( study->FindObjectID( it.Value()->getEntry() ) );
297           GEOM::GEOM_Object_var anObject =
298             GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( aSObject ) );
299           if ( !anObject->_is_nil() ) {
300             GEOM::marker_type mtype = anObject->GetMarkerType();
301             if ( aType == -1 )
302               aType = mtype;
303             else if ( aType != mtype ) {
304               aType = (GEOM::marker_type)-1;
305               break;
306             }
307             if ( mtype > GEOM::MT_NONE && mtype < GEOM::MT_USER ) {
308               GEOM::marker_size msize = anObject->GetMarkerSize();
309               if ( aSize == -1 )
310                 aSize = msize;
311               else if ( aSize != msize )
312                 break;
313             }
314             else if ( mtype == GEOM::MT_USER ) {
315               int mtexture = anObject->GetMarkerTexture();
316               if ( aTexture == 0 )
317                 aTexture = mtexture;
318               else if ( aTexture != mtexture )
319                 break;
320             }
321           }
322         }
323       }
324     }
325   }
326   if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER )
327     setStandardMarker( aType, aSize );
328   else if ( aType == GEOM::MT_USER )
329     setCustomMarker( aTexture );
330   else
331     setStandardMarker((GEOM::marker_type)(resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS) + 1),
332                       (GEOM::marker_size)(resMgr->integerValue("Geometry", "marker_scale", 1)));
333 }
334
335 void GEOMToolsGUI_MarkerDlg::addTexture( int id, bool select ) const
336 {
337   if ( id > 0 && myCustomTypeCombo->index( id ) == -1 ) {
338     int tWidth, tHeight;
339
340     Handle(TColStd_HArray1OfByte) texture = GeometryGUI::getTexture(id, tWidth, tHeight);
341
342     if ( !texture.IsNull() && texture->Length() == tWidth*tHeight/8 ) {
343       QImage image( tWidth, tHeight, QImage::Format_Mono );
344       image.setColor( 0, qRgba( 0, 0, 0, 0   ) );
345       image.setColor( 1, qRgba( 0, 0, 0, 255 ) );
346       int bytesperline = tWidth/8;
347       for ( int j = texture->Lower(); j <= texture->Upper(); j++ ) {
348         uchar val = (uchar)texture->Value( j );
349         for ( int k = 0; k < 8; k++ ) {
350           int row = ( j - texture->Lower() ) / bytesperline;
351           int col = ( ( j - texture->Lower() ) % bytesperline ) * 8 + k;
352           image.setPixel( row, col, ( val & (1<<(8-k-1)) ) ? 1 : 0 );
353         }
354       }
355       QPixmap pixmap = QPixmap::fromImage( image );
356       if ( !pixmap.isNull() ) {
357         myCustomTypeCombo->addItem( pixmap, QString::number( id ) );
358         myCustomTypeCombo->setId( myCustomTypeCombo->count()-1, id );
359         if ( select ) myCustomTypeCombo->setCurrentId( id );
360       }
361     }
362   }
363 }
364
365 SalomeApp_Study* GEOMToolsGUI_MarkerDlg::getStudy() const
366 {
367   return qobject_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
368 }
369
370 void GEOMToolsGUI_MarkerDlg::browse()
371 {
372   QStringList filters;
373   filters << tr( "Texture files (*.dat)" ) << tr( "All files (*)" );
374   QString aFileName = SUIT_Session::session()->activeApplication()->getFileName( true, QString(), filters.join( ";;" ), tr( "LOAD_TEXTURE_TLT" ), this );
375   if ( !aFileName.isEmpty() ) {
376     addTexture( myOperation->LoadTexture( aFileName.toUtf8().constData() ), true );
377   }
378 }
379
380 void GEOMToolsGUI_MarkerDlg::help()
381 {
382   if ( getStudy() ) {
383     SalomeApp_Application* app = qobject_cast<SalomeApp_Application*>( getStudy()->application() );
384     app->onHelpContextModule( "GEOM", "point_marker_page.html" );
385   }
386 }