Salome HOME
f512cffddeab435bf495b0e0c26101fa1c7ca3de
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Preferences_ColorDlg.cxx
1 // Copyright (C) 2007-2011  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( "Display settings (color, line width, shrink size, ...)" ) );
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( "Surface color" ), ButtonGroup1 );
84
85   toolSurfColor = new QtxBiColorTool(ButtonGroup1);
86   toolSurfColor->setText( tr( "Back surface color" ));
87
88   QLabel* TextLabel_Outline = new QLabel( tr( "Outline color" ), ButtonGroup1 );
89   btnOutlineColor = new QtxColorButton( ButtonGroup1 );
90
91   QLabel* TextLabel_Wireframe = new QLabel( tr( "Wireframe color" ), ButtonGroup1 );
92   btnWireframeColor = new QtxColorButton( ButtonGroup1 );
93
94   QLabel* TextLabel_0DElements_Color = new QLabel( tr( "0D elements" ), ButtonGroup1 );
95   btn0DElementsColor = new QtxColorButton( ButtonGroup1 );
96
97   QLabel* TextLabel_0DElements_Size = new QLabel( tr( "Size of 0D elements" ), ButtonGroup1 );
98   SpinBox_0DElements_Size = new SalomeApp_IntSpinBox( ButtonGroup1 );
99   SpinBox_0DElements_Size->setAcceptNames( false ); // No Notebook variables allowed
100   SpinBox_0DElements_Size->setRange( 1, 10 );
101   SpinBox_0DElements_Size->setSingleStep( 1 );
102   SpinBox_0DElements_Size->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
103   SpinBox_0DElements_Size->setButtonSymbols( QSpinBox::PlusMinus );
104
105   QLabel* TextLabel_Width = new QLabel( tr( "Line width" ), ButtonGroup1 );
106   SpinBox_Width = new SalomeApp_IntSpinBox( ButtonGroup1 );
107   SpinBox_Width->setAcceptNames( false ); // No Notebook variables allowed
108   SpinBox_Width->setRange( 1, 5 );
109   SpinBox_Width->setSingleStep( 1 );
110   SpinBox_Width->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
111   SpinBox_Width->setButtonSymbols( QSpinBox::PlusMinus );
112
113   QLabel* TextLabel_ShrinkCoeff = new QLabel( tr( "Shrink coef." ), ButtonGroup1 );
114   SpinBox_Shrink = new SalomeApp_IntSpinBox( ButtonGroup1 );
115   SpinBox_Shrink->setAcceptNames( false ); // No Notebook variables allowed
116   SpinBox_Shrink->setRange( 20, 100 );
117   SpinBox_Shrink->setSingleStep( 1 );
118   SpinBox_Shrink->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
119   SpinBox_Shrink->setButtonSymbols( QSpinBox::PlusMinus );
120
121   ButtonGroup1Layout->addWidget( TextLabel_Fill,             0, 0 );
122   ButtonGroup1Layout->addWidget( toolSurfColor,              0, 1, 1, 3 );
123
124   ButtonGroup1Layout->addWidget( TextLabel_Outline,          1, 0 );
125   ButtonGroup1Layout->addWidget( btnOutlineColor,            1, 1 );
126   ButtonGroup1Layout->addWidget( TextLabel_Wireframe,        1, 2 );
127   ButtonGroup1Layout->addWidget( btnWireframeColor,         1, 3 );
128
129   ButtonGroup1Layout->addWidget( TextLabel_0DElements_Color, 2, 0 );
130   ButtonGroup1Layout->addWidget( btn0DElementsColor,         2, 1 );
131
132   ButtonGroup1Layout->addWidget( TextLabel_0DElements_Size,  2, 2 );
133   ButtonGroup1Layout->addWidget( SpinBox_0DElements_Size,    2, 3 );
134
135   ButtonGroup1Layout->addWidget( TextLabel_Width,            3, 0 );
136   ButtonGroup1Layout->addWidget( SpinBox_Width,              3, 1 );
137   ButtonGroup1Layout->addWidget( TextLabel_ShrinkCoeff,      3, 2 );
138   ButtonGroup1Layout->addWidget( SpinBox_Shrink,             3, 3 );
139
140   // -------------------------------
141   QGroupBox* ButtonGroup2 = new QGroupBox( tr( "Nodes" ), this );
142   QGridLayout* ButtonGroup2Layout = new QGridLayout( ButtonGroup2 );
143   ButtonGroup2Layout->setSpacing( SPACING );
144   ButtonGroup2Layout->setMargin( MARGIN );
145
146   QLabel* TextLabel_Nodes_Color = new QLabel( tr( "Color" ), ButtonGroup2 );
147   btnNodeColor = new QtxColorButton( ButtonGroup2 );
148
149   QGroupBox* MarkerGroup = new QGroupBox( tr( "Marker" ), ButtonGroup2 );
150   QVBoxLayout* MarkerGroupLayout = new QVBoxLayout( MarkerGroup );
151   MarkerGroupLayout->setSpacing( 0 );
152   MarkerGroupLayout->setMargin( 0 );
153
154   MarkerWidget = new VTKViewer_MarkerWidget( MarkerGroup );
155
156   MarkerGroupLayout->addWidget( MarkerWidget );
157
158   ButtonGroup2Layout->addWidget( TextLabel_Nodes_Color, 0, 0 );
159   ButtonGroup2Layout->addWidget( btnNodeColor,          0, 1 );
160   ButtonGroup2Layout->addWidget( MarkerGroup,           1, 0, 1, 3 );
161   ButtonGroup2Layout->setColumnStretch( 2, 1 );
162
163   // -------------------------------
164   QGroupBox* ButtonGroup3 = new QGroupBox( tr( "Orientation of faces" ), this );
165   QGridLayout* ButtonGroup3Layout = new QGridLayout( ButtonGroup3 );
166   ButtonGroup3Layout->setSpacing( SPACING );
167   ButtonGroup3Layout->setMargin( MARGIN );
168
169   QLabel* TextLabel_Orientation_Color = new QLabel( tr( "Color" ), ButtonGroup3 );
170   btnOrientationColor = new QtxColorButton( ButtonGroup3 );
171
172   QLabel* TextLabel_Orientation_Scale = new QLabel( tr( "Scale" ), ButtonGroup3 );
173   SpinBox_Orientation_Scale = new SMESHGUI_SpinBox( ButtonGroup3 );
174   SpinBox_Orientation_Scale->setAcceptNames( false ); // No Notebook variables allowed
175   SpinBox_Orientation_Scale->RangeStepAndValidator( .05, .5, .05, "parametric_precision" );
176   SpinBox_Orientation_Scale->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
177   SpinBox_Orientation_Scale->setButtonSymbols( QSpinBox::PlusMinus );
178
179   CheckBox_Orientation_3DVectors = new QCheckBox( tr( "3D vectors" ), ButtonGroup3 );
180
181   ButtonGroup3Layout->addWidget( TextLabel_Orientation_Color,    0, 0 );
182   ButtonGroup3Layout->addWidget( btnOrientationColor,            0, 1 );
183   ButtonGroup3Layout->addWidget( TextLabel_Orientation_Scale,    0, 2 );
184   ButtonGroup3Layout->addWidget( SpinBox_Orientation_Scale,      0, 3 );
185   ButtonGroup3Layout->addWidget( CheckBox_Orientation_3DVectors, 1, 0, 1, 4 );
186
187   // -------------------------------
188   QGroupBox* ButtonGroup4 = new QGroupBox( tr( "Selection" ), this );
189   QGridLayout* ButtonGroup4Layout = new QGridLayout( ButtonGroup4 );
190   ButtonGroup3Layout->setSpacing( SPACING );
191   ButtonGroup3Layout->setMargin( MARGIN );
192   
193   QLabel* TextLabel_Selection_Color = new QLabel( tr( "Selection color" ), ButtonGroup4 );
194   btnSelectionColor = new QtxColorButton( ButtonGroup4 );
195   
196   QLabel* TextLabel_Preselection_Color = new QLabel( tr( "Pre-selection color" ), ButtonGroup4 );
197   btnPreselectionColor = new QtxColorButton( ButtonGroup4 );
198   
199   ButtonGroup4Layout->addWidget( TextLabel_Selection_Color,      0, 0 );
200   ButtonGroup4Layout->addWidget( btnSelectionColor,              0, 1 );
201   ButtonGroup4Layout->addWidget( TextLabel_Preselection_Color,   0, 2 );
202   ButtonGroup4Layout->addWidget( btnPreselectionColor,           0, 3 );
203
204   // -------------------------------
205   QGroupBox* GroupButtons = new QGroupBox( this );
206   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
207   GroupButtonsLayout->setSpacing( SPACING );
208   GroupButtonsLayout->setMargin( MARGIN );
209
210   QPushButton* buttonOk = new QPushButton( tr( "&OK" ), GroupButtons );
211   buttonOk->setAutoDefault( true );
212   buttonOk->setDefault( true );
213
214   QPushButton* buttonCancel = new QPushButton( tr( "&Cancel" ), GroupButtons );
215   buttonCancel->setAutoDefault( true );
216
217   QPushButton* buttonHelp = new QPushButton( tr( "&Help" ), GroupButtons );
218   buttonHelp->setAutoDefault( true );
219
220   GroupButtonsLayout->addWidget( buttonOk );
221   GroupButtonsLayout->addSpacing( 10 );
222   GroupButtonsLayout->addStretch();
223   GroupButtonsLayout->addWidget( buttonCancel );
224   GroupButtonsLayout->addWidget( buttonHelp );
225
226   // -------------------------------
227   topLayout->addWidget( ButtonGroup1 );
228   topLayout->addWidget( ButtonGroup2 );
229   topLayout->addWidget( ButtonGroup3 );
230   //  rnv: Selection and preselection colors are defined only in the Preferences 
231   //  topLayout->addWidget( ButtonGroup4 );
232   ButtonGroup4->hide();
233   topLayout->addWidget( GroupButtons );
234
235   // -------------------------------
236   mySMESHGUI->SetActiveDialogBox( this );
237
238   myHelpFileName = "colors_size_page.html";
239
240   /* signals and slots connections */
241   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
242   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
243   connect( buttonHelp,   SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
244
245   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ),
246            this,       SLOT( DeactivateActiveDialog() ) );
247   /* to close dialog if study change */
248   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ),
249            this,       SLOT( ClickOnCancel() ) );
250 }
251
252 //=================================================================================
253 // function : ~SMESHGUI_Preferences_ColorDlg()
254 // purpose  : Destructor
255 //=================================================================================
256 SMESHGUI_Preferences_ColorDlg::~SMESHGUI_Preferences_ColorDlg()
257 {
258 }
259
260 //=================================================================================
261 // function : ClickOnOk()
262 // purpose  :
263 //=================================================================================
264 void SMESHGUI_Preferences_ColorDlg::ClickOnOk()
265 {
266   mySMESHGUI->ResetState();
267   accept();
268 }
269
270 //=================================================================================
271 // function : ClickOnCancel()
272 // purpose  :
273 //=================================================================================
274 void SMESHGUI_Preferences_ColorDlg::ClickOnCancel()
275 {
276   mySMESHGUI->ResetState();
277   reject();
278 }
279
280 //=================================================================================
281 // function : ClickOnHelp()
282 // purpose  :
283 //=================================================================================
284 void SMESHGUI_Preferences_ColorDlg::ClickOnHelp()
285 {
286   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
287   if (app) 
288     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
289   else {
290     QString platform;
291 #ifdef WIN32
292     platform = "winapplication";
293 #else
294     platform = "application";
295 #endif
296     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
297                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
298                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
299                                                                  platform)).
300                              arg(myHelpFileName));
301   }
302 }
303
304 //=================================================================================
305 // function : DeactivateActiveDialog()
306 // purpose  :
307 //=================================================================================
308 void SMESHGUI_Preferences_ColorDlg::DeactivateActiveDialog()
309 {
310 }
311
312 //=================================================================================
313 // function : closeEvent()
314 // purpose  :
315 //=================================================================================
316 void SMESHGUI_Preferences_ColorDlg::closeEvent( QCloseEvent* )
317 {
318   ClickOnCancel(); /* same than click on cancel button */
319 }
320
321 //=================================================================================
322 // function : ActivateThisDialog()
323 // purpose  :
324 //=================================================================================
325 void SMESHGUI_Preferences_ColorDlg::ActivateThisDialog()
326 {
327   /* Emit a signal to deactivate any active dialog */
328   mySMESHGUI->EmitSignalDeactivateDialog();
329 }
330
331 //=================================================================================
332 // function : SetColor()
333 // purpose  :
334 //=================================================================================
335 void SMESHGUI_Preferences_ColorDlg::SetColor( int type, const QColor& color )
336 {
337   switch ( type ) {
338   case 1 : toolSurfColor->setMainColor( color );     break; // fill
339   case 2 : btnWireframeColor->setColor( color );     break; // wireframe
340   case 3 : btnNodeColor->setColor( color );          break; // node
341   case 4 : btnOutlineColor->setColor( color );       break; // outline
342   case 5 : btn0DElementsColor->setColor( color );    break; // 0d elements
343   case 6 : btnOrientationColor->setColor( color );   break; // orientation of faces
344   case 7 : btnSelectionColor->setColor( color );     break; // selection color
345   case 8 : btnPreselectionColor->setColor( color );  break; // pre-selection color
346   default: break;
347   }
348 }
349
350 //=================================================================================
351 // function : GetColor()
352 // purpose  :
353 //=================================================================================
354 QColor SMESHGUI_Preferences_ColorDlg::GetColor( int type )
355 {
356   QColor color;
357   switch ( type ) {
358   case 1 : color = toolSurfColor->mainColor();    break; // fill
359   case 2 : color = btnWireframeColor->color();    break; // outline
360   case 3 : color = btnNodeColor->color();         break; // node
361   case 4 : color = btnOutlineColor->color();      break; // node
362   case 5 : color = btn0DElementsColor->color();   break; // 0d elements
363   case 6 : color = btnOrientationColor->color();  break; // orientation of faces
364   case 7 : color = btnSelectionColor->color();    break; // selection color
365   case 8 : color = btnPreselectionColor->color(); break; // pre-selection color
366
367   default: break;
368   }
369   return color;
370 }
371
372 //=================================================================================
373 // function : SetIntValue()
374 // purpose  :
375 //=================================================================================
376 void SMESHGUI_Preferences_ColorDlg::SetIntValue( int type, int value )
377 {
378   switch ( type ) {
379   case 1 : SpinBox_Width->setValue( value );           break; // width
380   case 2 : SpinBox_Shrink->setValue( value );          break; // shrink coeff
381   case 3 : SpinBox_0DElements_Size->setValue( value ); break; // 0d elements
382   default: break;
383   }
384 }
385
386 //=================================================================================
387 // function : GetIntValue()
388 // purpose  :
389 //=================================================================================
390 int SMESHGUI_Preferences_ColorDlg::GetIntValue( int type )
391 {
392   int res = 0;
393   switch ( type ) {
394   case 1 : res = SpinBox_Width->value();           break; // width
395   case 2 : res = SpinBox_Shrink->value();          break; // shrink coeff
396   case 3 : res = SpinBox_0DElements_Size->value(); break; // 0d elements
397   default: break;
398   }
399   return res;
400 }
401
402 //=================================================================================
403 // function : SetDoubleValue()
404 // purpose  :
405 //=================================================================================
406 void SMESHGUI_Preferences_ColorDlg::SetDoubleValue( int type, double value )
407 {
408   switch ( type ) {
409   case 1 : SpinBox_Orientation_Scale->setValue( value ); break; // orientation scale
410   default: break;
411   }
412 }
413
414 //=================================================================================
415 // function : GetDoubleValue()
416 // purpose  :
417 //=================================================================================
418 double SMESHGUI_Preferences_ColorDlg::GetDoubleValue( int type )
419 {
420   double res = 0;
421   switch ( type ) {
422   case 1 : res = SpinBox_Orientation_Scale->value(); break; // orientation scale
423   default: break;
424   }
425   return res;
426 }
427
428 //=================================================================================
429 // function : SetBooleanValue()
430 // purpose  :
431 //=================================================================================
432 void SMESHGUI_Preferences_ColorDlg::SetBooleanValue( int type, bool value )
433 {
434   switch ( type ) {
435   case 1 : CheckBox_Orientation_3DVectors->setChecked( value ); break; // 3D vectors
436   default: break;
437   }
438 }
439
440 //=================================================================================
441 // function : GetBooleanValue()
442 // purpose  :
443 //=================================================================================
444 bool SMESHGUI_Preferences_ColorDlg::GetBooleanValue( int type )
445 {
446   bool res = false;
447   switch ( type ) {
448   case 1 : res = CheckBox_Orientation_3DVectors->isChecked(); break; // 3D vectors
449   default: break;
450   }
451   return res;
452 }
453
454 //=================================================================================
455 // function : setCustomMarkerMap()
456 // purpose  :
457 //=================================================================================
458 void SMESHGUI_Preferences_ColorDlg::setCustomMarkerMap( VTK::MarkerMap theMarkerMap )
459 {
460   MarkerWidget->setCustomMarkerMap( theMarkerMap );
461 }
462
463 //=================================================================================
464 // function : getCustomMarkerMap()
465 // purpose  :
466 //=================================================================================
467 VTK::MarkerMap SMESHGUI_Preferences_ColorDlg::getCustomMarkerMap()
468 {
469   return MarkerWidget->getCustomMarkerMap();
470 }
471
472 //=================================================================================
473 // function : setStandardMarker()
474 // purpose  :
475 //=================================================================================
476 void SMESHGUI_Preferences_ColorDlg::setStandardMarker( VTK::MarkerType theMarkerType,
477                                                        VTK::MarkerScale theMarkerScale )
478 {
479   MarkerWidget->setStandardMarker( theMarkerType, theMarkerScale );
480 }
481
482 //=================================================================================
483 // function : setCustomMarker()
484 // purpose  :
485 //=================================================================================
486 void SMESHGUI_Preferences_ColorDlg::setCustomMarker( int theId )
487 {
488   MarkerWidget->setCustomMarker( theId );
489 }
490
491 //=================================================================================
492 // function : getMarkerType()
493 // purpose  :
494 //=================================================================================
495 VTK::MarkerType SMESHGUI_Preferences_ColorDlg::getMarkerType() const
496 {
497   return MarkerWidget->getMarkerType();
498 }
499
500 //=================================================================================
501 // function : getStandardMarkerScale()
502 // purpose  :
503 //=================================================================================
504 VTK::MarkerScale SMESHGUI_Preferences_ColorDlg::getStandardMarkerScale() const
505 {
506   return MarkerWidget->getStandardMarkerScale();
507 }
508
509 //=================================================================================
510 // function : getCustomMarkerID()
511 // purpose  :
512 //=================================================================================
513 int SMESHGUI_Preferences_ColorDlg::getCustomMarkerID() const
514 {
515   return MarkerWidget->getCustomMarkerID();
516 }
517
518 //=================================================================================
519 // function : SetDeltaBrightness(int)
520 // purpose  :
521 //=================================================================================
522 void SMESHGUI_Preferences_ColorDlg::SetDeltaBrightness(int delta) 
523 {
524   toolSurfColor->setDelta(delta);
525 }
526 //=================================================================================
527 // function : GetDeltaBrightness()
528 // purpose  :
529 //=================================================================================
530 int SMESHGUI_Preferences_ColorDlg::GetDeltaBrightness() 
531 {
532   return toolSurfColor->delta();
533 }
534
535 //=================================================================================
536 // function : keyPressEvent()
537 // purpose  :
538 //=================================================================================
539 void SMESHGUI_Preferences_ColorDlg::keyPressEvent( QKeyEvent* e )
540 {
541   QDialog::keyPressEvent( e );
542   if ( e->isAccepted() )
543     return;
544
545   if ( e->key() == Qt::Key_F1 ) {
546     e->accept();
547     ClickOnHelp();
548   }
549 }