Salome HOME
merge V5_1_4
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Preferences_ColorDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_Preferences_ColorDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_Preferences_ColorDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_SpinBox.h"
32 #include "SMESHGUI_Utils.h"
33
34 // SALOME GUI includes
35 #include <SUIT_Desktop.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <SUIT_Session.h>
39 #include <QtxColorButton.h>
40 #include <VTKViewer_MarkerWidget.h>
41 #include <LightApp_Application.h>
42 #include <SalomeApp_IntSpinBox.h>
43
44 // Qt includes
45 #include <QGroupBox>
46 #include <QLabel>
47 #include <QPushButton>
48 #include <QVBoxLayout>
49 #include <QHBoxLayout>
50 #include <QGridLayout>
51 #include <QCheckBox>
52 #include <QKeyEvent>
53
54 #define SPACING 6
55 #define MARGIN  11
56
57 //=================================================================================
58 // function : SMESHGUI_Preferences_ColorDlg()
59 // purpose  : Constructs a SMESHGUI_Preferences_ColorDlg which is a child
60 //            of 'parent', with the name 'name' and widget flags set to 'f'
61 //            The dialog will by default be modeless, unless you
62 //            set'modal' to true to construct a modal dialog.
63 //=================================================================================
64 SMESHGUI_Preferences_ColorDlg::SMESHGUI_Preferences_ColorDlg( SMESHGUI* theModule )
65   : QDialog( SMESH::GetDesktop( theModule ) ),
66     mySMESHGUI( theModule )
67 {
68   setModal( true );
69   setWindowTitle( tr( "Preferences - Set Color" ) );
70   setSizeGripEnabled( true );
71
72   // -------------------------------
73   QVBoxLayout* topLayout = new QVBoxLayout( this );
74   topLayout->setSpacing( SPACING );
75   topLayout->setMargin( MARGIN );
76
77   // -------------------------------
78   QGroupBox* ButtonGroup1 = new QGroupBox( tr( "Elements" ), this );
79   QGridLayout* ButtonGroup1Layout = new QGridLayout( ButtonGroup1 );
80   ButtonGroup1Layout->setSpacing( SPACING );
81   ButtonGroup1Layout->setMargin( MARGIN );
82
83   QLabel* TextLabel_Fill = new QLabel( tr( "Fill" ), ButtonGroup1 );
84   btnFillColor = new QtxColorButton( ButtonGroup1 );
85
86   QLabel* TextLabel_BackFace = new QLabel( tr( "Back Face" ), ButtonGroup1 );
87   btnBackFaceColor = new QtxColorButton( ButtonGroup1 );
88
89   QLabel* TextLabel_Outine = new QLabel( tr( "Outline" ), ButtonGroup1 );
90   btnOutlineColor = new QtxColorButton( ButtonGroup1 );
91
92   QLabel* TextLabel_0DElements_Color = new QLabel( tr( "0D elements" ), ButtonGroup1 );
93   btn0DElementsColor = new QtxColorButton( ButtonGroup1 );
94
95   QLabel* TextLabel_0DElements_Size = new QLabel( tr( "Size of 0D elements" ), ButtonGroup1 );
96   SpinBox_0DElements_Size = new SalomeApp_IntSpinBox( ButtonGroup1 );
97   SpinBox_0DElements_Size->setAcceptNames( false ); // No Notebook variables allowed
98   SpinBox_0DElements_Size->setRange( 1, 10 );
99   SpinBox_0DElements_Size->setSingleStep( 1 );
100   SpinBox_0DElements_Size->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
101   SpinBox_0DElements_Size->setButtonSymbols( QSpinBox::PlusMinus );
102
103   QLabel* TextLabel_Width = new QLabel( tr( "Width" ), ButtonGroup1 );
104   SpinBox_Width = new SalomeApp_IntSpinBox( ButtonGroup1 );
105   SpinBox_Width->setAcceptNames( false ); // No Notebook variables allowed
106   SpinBox_Width->setRange( 0, 5 );
107   SpinBox_Width->setSingleStep( 1 );
108   SpinBox_Width->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
109   SpinBox_Width->setButtonSymbols( QSpinBox::PlusMinus );
110
111   QLabel* TextLabel_ShrinkCoeff = new QLabel( tr( "Shrink coef." ), ButtonGroup1 );
112   SpinBox_Shrink = new SalomeApp_IntSpinBox( ButtonGroup1 );
113   SpinBox_Shrink->setAcceptNames( false ); // No Notebook variables allowed
114   SpinBox_Shrink->setRange( 20, 100 );
115   SpinBox_Shrink->setSingleStep( 1 );
116   SpinBox_Shrink->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
117   SpinBox_Shrink->setButtonSymbols( QSpinBox::PlusMinus );
118
119   ButtonGroup1Layout->addWidget( TextLabel_Fill,             0, 0 );
120   ButtonGroup1Layout->addWidget( btnFillColor,               0, 1 );
121   ButtonGroup1Layout->addWidget( TextLabel_BackFace,         0, 2 );
122   ButtonGroup1Layout->addWidget( btnBackFaceColor,           0, 3 );
123   ButtonGroup1Layout->addWidget( TextLabel_Outine,           1, 0 );
124   ButtonGroup1Layout->addWidget( btnOutlineColor,            1, 1 );
125   ButtonGroup1Layout->addWidget( TextLabel_0DElements_Color, 1, 2 );
126   ButtonGroup1Layout->addWidget( btn0DElementsColor,         1, 3 );
127   ButtonGroup1Layout->addWidget( TextLabel_0DElements_Size,  2, 0 );
128   ButtonGroup1Layout->addWidget( SpinBox_0DElements_Size,    2, 1 );
129   ButtonGroup1Layout->addWidget( TextLabel_Width,            3, 0 );
130   ButtonGroup1Layout->addWidget( SpinBox_Width,              3, 1 );
131   ButtonGroup1Layout->addWidget( TextLabel_ShrinkCoeff,      3, 2 );
132   ButtonGroup1Layout->addWidget( SpinBox_Shrink,             3, 3 );
133
134   // -------------------------------
135   QGroupBox* ButtonGroup2 = new QGroupBox( tr( "Nodes" ), this );
136   QGridLayout* ButtonGroup2Layout = new QGridLayout( ButtonGroup2 );
137   ButtonGroup2Layout->setSpacing( SPACING );
138   ButtonGroup2Layout->setMargin( MARGIN );
139
140   QLabel* TextLabel_Nodes_Color = new QLabel( tr( "Color" ), ButtonGroup2 );
141   btnNodeColor = new QtxColorButton( ButtonGroup2 );
142
143   QGroupBox* MarkerGroup = new QGroupBox( tr( "Marker" ), ButtonGroup2 );
144   QVBoxLayout* MarkerGroupLayout = new QVBoxLayout( MarkerGroup );
145   MarkerGroupLayout->setSpacing( 0 );
146   MarkerGroupLayout->setMargin( 0 );
147
148   MarkerWidget = new VTKViewer_MarkerWidget( MarkerGroup );
149
150   MarkerGroupLayout->addWidget( MarkerWidget );
151
152   ButtonGroup2Layout->addWidget( TextLabel_Nodes_Color, 0, 0 );
153   ButtonGroup2Layout->addWidget( btnNodeColor,          0, 1 );
154   ButtonGroup2Layout->addWidget( MarkerGroup,           1, 0, 1, 3 );
155   ButtonGroup2Layout->setColumnStretch( 2, 1 );
156
157   // -------------------------------
158   QGroupBox* ButtonGroup3 = new QGroupBox( tr( "Orientation of faces" ), this );
159   QGridLayout* ButtonGroup3Layout = new QGridLayout( ButtonGroup3 );
160   ButtonGroup3Layout->setSpacing( SPACING );
161   ButtonGroup3Layout->setMargin( MARGIN );
162
163   QLabel* TextLabel_Orientation_Color = new QLabel( tr( "Color" ), ButtonGroup3 );
164   btnOrientationColor = new QtxColorButton( ButtonGroup3 );
165
166   QLabel* TextLabel_Orientation_Scale = new QLabel( tr( "Scale" ), ButtonGroup3 );
167   SpinBox_Orientation_Scale = new SMESHGUI_SpinBox( ButtonGroup3 );
168   SpinBox_Orientation_Scale->setAcceptNames( false ); // No Notebook variables allowed
169   SpinBox_Orientation_Scale->RangeStepAndValidator( .05, .5, .05, "parametric_precision" );
170   SpinBox_Orientation_Scale->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
171   SpinBox_Orientation_Scale->setButtonSymbols( QSpinBox::PlusMinus );
172
173   CheckBox_Orientation_3DVectors = new QCheckBox( tr( "3D vectors" ), ButtonGroup3 );
174
175   ButtonGroup3Layout->addWidget( TextLabel_Orientation_Color,    0, 0 );
176   ButtonGroup3Layout->addWidget( btnOrientationColor,            0, 1 );
177   ButtonGroup3Layout->addWidget( TextLabel_Orientation_Scale,    0, 2 );
178   ButtonGroup3Layout->addWidget( SpinBox_Orientation_Scale,      0, 3 );
179   ButtonGroup3Layout->addWidget( CheckBox_Orientation_3DVectors, 1, 0, 1, 4 );
180
181   // -------------------------------
182   QGroupBox* GroupButtons = new QGroupBox( this );
183   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
184   GroupButtonsLayout->setSpacing( SPACING );
185   GroupButtonsLayout->setMargin( MARGIN );
186
187   QPushButton* buttonOk = new QPushButton( tr( "&OK" ), GroupButtons );
188   buttonOk->setAutoDefault( true );
189   buttonOk->setDefault( true );
190
191   QPushButton* buttonCancel = new QPushButton( tr( "&Cancel" ), GroupButtons );
192   buttonCancel->setAutoDefault( true );
193
194   QPushButton* buttonHelp = new QPushButton( tr( "&Help" ), GroupButtons );
195   buttonHelp->setAutoDefault( true );
196
197   GroupButtonsLayout->addWidget( buttonOk );
198   GroupButtonsLayout->addSpacing( 10 );
199   GroupButtonsLayout->addStretch();
200   GroupButtonsLayout->addWidget( buttonCancel );
201   GroupButtonsLayout->addWidget( buttonHelp );
202
203   // -------------------------------
204   topLayout->addWidget( ButtonGroup1 );
205   topLayout->addWidget( ButtonGroup2 );
206   topLayout->addWidget( ButtonGroup3 );
207   topLayout->addWidget( GroupButtons );
208
209   // -------------------------------
210   mySMESHGUI->SetActiveDialogBox( this );
211
212   myHelpFileName = "colors_size_page.html";
213
214   /* signals and slots connections */
215   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
216   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
217   connect( buttonHelp,   SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
218
219   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ),
220            this,       SLOT( DeactivateActiveDialog() ) );
221   /* to close dialog if study change */
222   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ),
223            this,       SLOT( ClickOnCancel() ) );
224 }
225
226 //=================================================================================
227 // function : ~SMESHGUI_Preferences_ColorDlg()
228 // purpose  : Destructor
229 //=================================================================================
230 SMESHGUI_Preferences_ColorDlg::~SMESHGUI_Preferences_ColorDlg()
231 {
232 }
233
234 //=================================================================================
235 // function : ClickOnOk()
236 // purpose  :
237 //=================================================================================
238 void SMESHGUI_Preferences_ColorDlg::ClickOnOk()
239 {
240   mySMESHGUI->ResetState();
241   accept();
242 }
243
244 //=================================================================================
245 // function : ClickOnCancel()
246 // purpose  :
247 //=================================================================================
248 void SMESHGUI_Preferences_ColorDlg::ClickOnCancel()
249 {
250   mySMESHGUI->ResetState();
251   reject();
252 }
253
254 //=================================================================================
255 // function : ClickOnHelp()
256 // purpose  :
257 //=================================================================================
258 void SMESHGUI_Preferences_ColorDlg::ClickOnHelp()
259 {
260   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
261   if (app) 
262     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
263   else {
264     QString platform;
265 #ifdef WIN32
266     platform = "winapplication";
267 #else
268     platform = "application";
269 #endif
270     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
271                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
272                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
273                                                                  platform)).
274                              arg(myHelpFileName));
275   }
276 }
277
278 //=================================================================================
279 // function : DeactivateActiveDialog()
280 // purpose  :
281 //=================================================================================
282 void SMESHGUI_Preferences_ColorDlg::DeactivateActiveDialog()
283 {
284 }
285
286 //=================================================================================
287 // function : closeEvent()
288 // purpose  :
289 //=================================================================================
290 void SMESHGUI_Preferences_ColorDlg::closeEvent( QCloseEvent* )
291 {
292   ClickOnCancel(); /* same than click on cancel button */
293 }
294
295 //=================================================================================
296 // function : ActivateThisDialog()
297 // purpose  :
298 //=================================================================================
299 void SMESHGUI_Preferences_ColorDlg::ActivateThisDialog()
300 {
301   /* Emit a signal to deactivate any active dialog */
302   mySMESHGUI->EmitSignalDeactivateDialog();
303 }
304
305 //=================================================================================
306 // function : SetColor()
307 // purpose  :
308 //=================================================================================
309 void SMESHGUI_Preferences_ColorDlg::SetColor( int type, const QColor& color )
310 {
311   switch ( type ) {
312   case 1 : btnFillColor->setColor( color );        break; // fill
313   case 2 : btnOutlineColor->setColor( color );     break; // outline
314   case 3 : btnNodeColor->setColor( color );        break; // node
315   case 4 : btnBackFaceColor->setColor( color );    break; // back face
316   case 5 : btn0DElementsColor->setColor( color );  break; // 0d elements
317   case 6 : btnOrientationColor->setColor( color ); break; // orientation of faces
318   default: break;
319   }
320 }
321
322 //=================================================================================
323 // function : GetColor()
324 // purpose  :
325 //=================================================================================
326 QColor SMESHGUI_Preferences_ColorDlg::GetColor( int type )
327 {
328   QColor color;
329   switch ( type ) {
330   case 1 : color = btnFillColor->color();        break; // fill
331   case 2 : color = btnOutlineColor->color();     break; // outline
332   case 3 : color = btnNodeColor->color();        break; // node
333   case 4 : color = btnBackFaceColor->color();    break; // back face
334   case 5 : color = btn0DElementsColor->color();  break; // 0d elements
335   case 6 : color = btnOrientationColor->color(); break; // orientation of faces
336   default: break;
337   }
338   return color;
339 }
340
341 //=================================================================================
342 // function : SetIntValue()
343 // purpose  :
344 //=================================================================================
345 void SMESHGUI_Preferences_ColorDlg::SetIntValue( int type, int value )
346 {
347   switch ( type ) {
348   case 1 : SpinBox_Width->setValue( value );           break; // width
349   case 2 : SpinBox_Shrink->setValue( value );          break; // shrink coeff
350   case 3 : SpinBox_0DElements_Size->setValue( value ); break; // 0d elements
351   default: break;
352   }
353 }
354
355 //=================================================================================
356 // function : GetIntValue()
357 // purpose  :
358 //=================================================================================
359 int SMESHGUI_Preferences_ColorDlg::GetIntValue( int type )
360 {
361   int res = 0;
362   switch ( type ) {
363   case 1 : res = SpinBox_Width->value();           break; // width
364   case 2 : res = SpinBox_Shrink->value();          break; // shrink coeff
365   case 3 : res = SpinBox_0DElements_Size->value(); break; // 0d elements
366   default: break;
367   }
368   return res;
369 }
370
371 //=================================================================================
372 // function : SetDoubleValue()
373 // purpose  :
374 //=================================================================================
375 void SMESHGUI_Preferences_ColorDlg::SetDoubleValue( int type, double value )
376 {
377   switch ( type ) {
378   case 1 : SpinBox_Orientation_Scale->setValue( value ); break; // orientation scale
379   default: break;
380   }
381 }
382
383 //=================================================================================
384 // function : GetDoubleValue()
385 // purpose  :
386 //=================================================================================
387 double SMESHGUI_Preferences_ColorDlg::GetDoubleValue( int type )
388 {
389   double res = 0;
390   switch ( type ) {
391   case 1 : res = SpinBox_Orientation_Scale->value(); break; // orientation scale
392   default: break;
393   }
394   return res;
395 }
396
397 //=================================================================================
398 // function : SetBooleanValue()
399 // purpose  :
400 //=================================================================================
401 void SMESHGUI_Preferences_ColorDlg::SetBooleanValue( int type, bool value )
402 {
403   switch ( type ) {
404   case 1 : CheckBox_Orientation_3DVectors->setChecked( value ); break; // 3D vectors
405   default: break;
406   }
407 }
408
409 //=================================================================================
410 // function : GetBooleanValue()
411 // purpose  :
412 //=================================================================================
413 bool SMESHGUI_Preferences_ColorDlg::GetBooleanValue( int type )
414 {
415   bool res = false;
416   switch ( type ) {
417   case 1 : res = CheckBox_Orientation_3DVectors->isChecked(); break; // 3D vectors
418   default: break;
419   }
420   return res;
421 }
422
423 //=================================================================================
424 // function : setCustomMarkerMap()
425 // purpose  :
426 //=================================================================================
427 void SMESHGUI_Preferences_ColorDlg::setCustomMarkerMap( VTK::MarkerMap theMarkerMap )
428 {
429   MarkerWidget->setCustomMarkerMap( theMarkerMap );
430 }
431
432 //=================================================================================
433 // function : getCustomMarkerMap()
434 // purpose  :
435 //=================================================================================
436 VTK::MarkerMap SMESHGUI_Preferences_ColorDlg::getCustomMarkerMap()
437 {
438   return MarkerWidget->getCustomMarkerMap();
439 }
440
441 //=================================================================================
442 // function : setStandardMarker()
443 // purpose  :
444 //=================================================================================
445 void SMESHGUI_Preferences_ColorDlg::setStandardMarker( VTK::MarkerType theMarkerType,
446                                                        VTK::MarkerScale theMarkerScale )
447 {
448   MarkerWidget->setStandardMarker( theMarkerType, theMarkerScale );
449 }
450
451 //=================================================================================
452 // function : setCustomMarker()
453 // purpose  :
454 //=================================================================================
455 void SMESHGUI_Preferences_ColorDlg::setCustomMarker( int theId )
456 {
457   MarkerWidget->setCustomMarker( theId );
458 }
459
460 //=================================================================================
461 // function : getMarkerType()
462 // purpose  :
463 //=================================================================================
464 VTK::MarkerType SMESHGUI_Preferences_ColorDlg::getMarkerType() const
465 {
466   return MarkerWidget->getMarkerType();
467 }
468
469 //=================================================================================
470 // function : getStandardMarkerScale()
471 // purpose  :
472 //=================================================================================
473 VTK::MarkerScale SMESHGUI_Preferences_ColorDlg::getStandardMarkerScale() const
474 {
475   return MarkerWidget->getStandardMarkerScale();
476 }
477
478 //=================================================================================
479 // function : getCustomMarkerID()
480 // purpose  :
481 //=================================================================================
482 int SMESHGUI_Preferences_ColorDlg::getCustomMarkerID() const
483 {
484   return MarkerWidget->getCustomMarkerID();
485 }
486
487 //=================================================================================
488 // function : keyPressEvent()
489 // purpose  :
490 //=================================================================================
491 void SMESHGUI_Preferences_ColorDlg::keyPressEvent( QKeyEvent* e )
492 {
493   QDialog::keyPressEvent( e );
494   if ( e->isAccepted() )
495     return;
496
497   if ( e->key() == Qt::Key_F1 ) {
498     e->accept();
499     ClickOnHelp();
500   }
501 }