Salome HOME
Merge branch merge_1_2_d
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Preferences_ColorDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_Preferences_ColorDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESHGUI_Preferences_ColorDlg.h"
31 #include "SMESHGUI.h"
32
33 #include "utilities.h"
34
35 #include <qbuttongroup.h>
36 #include <qcheckbox.h>
37 #include <qcombobox.h>
38 #include <qframe.h>
39 #include <qlabel.h>
40 #include <qpixmap.h>
41 #include <qpushbutton.h>
42 #include <qlayout.h>
43 #include <qcolordialog.h>
44 #include <qspinbox.h> 
45 #include <qcolor.h>
46
47 /* 
48  *  Constructs a SMESHGUI_Preferences_ColorDlg which is a child of 'parent', with the 
49  *  name 'name' and widget flags set to 'f' 
50  *
51  *  The dialog will by default be modeless, unless you set 'modal' to
52  *  TRUE to construct a modal dialog.
53  */
54 SMESHGUI_Preferences_ColorDlg::SMESHGUI_Preferences_ColorDlg( QWidget* parent,  const char* name  )
55     : QDialog( parent, name, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
56 {
57     if ( !name )
58         setName( "SMESHGUI_Preferences_ColorDlg" );
59     setCaption( tr( "Preferences - Set Color" ) );
60     setSizeGripEnabled( TRUE );
61     QGridLayout* SMESHGUI_Preferences_ColorDlgLayout = new QGridLayout( this ); 
62     SMESHGUI_Preferences_ColorDlgLayout->setSpacing( 6 );
63     SMESHGUI_Preferences_ColorDlgLayout->setMargin( 11 );
64
65     ButtonGroup1 = new QButtonGroup( tr( "Elements" ), this, "ButtonGroup1" );
66     ButtonGroup1->setColumnLayout(0, Qt::Vertical );
67     ButtonGroup1->layout()->setSpacing( 0 );
68     ButtonGroup1->layout()->setMargin( 0 );
69     QGridLayout* ButtonGroup1Layout = new QGridLayout( ButtonGroup1->layout() );
70     ButtonGroup1Layout->setAlignment( Qt::AlignTop );
71     ButtonGroup1Layout->setSpacing( 6 );
72     ButtonGroup1Layout->setMargin( 11 );
73
74     TextLabel_Fill = new QLabel( tr( "Fill" ), ButtonGroup1, "TextLabel_2D_Fill" );
75     ButtonGroup1Layout->addWidget( TextLabel_Fill, 0, 0 );
76
77     btnFillColor = new QPushButton( ButtonGroup1, "btnFillColor" );
78     btnFillColor->setFixedSize( QSize( 25, 25 ) );
79     ButtonGroup1Layout->addWidget( btnFillColor, 0, 1 );
80
81     TextLabel_Outine = new QLabel( tr( "Outline" ), ButtonGroup1, "TextLabel_2D_Outine" );
82     ButtonGroup1Layout->addWidget( TextLabel_Outine, 0, 2 );
83
84     btnOutlineColor = new QPushButton( ButtonGroup1, "btnOutlineColor" );
85     btnOutlineColor->setFixedSize( QSize( 25, 25 ) );
86     ButtonGroup1Layout->addWidget( btnOutlineColor, 0, 3 );
87
88     TextLabel_Width = new QLabel( tr( "Width" ), ButtonGroup1, "TextLabel_2D_Width" );
89     ButtonGroup1Layout->addWidget( TextLabel_Width, 0, 4 );
90
91     SpinBox_Width = new QSpinBox( 0, 5, 1, ButtonGroup1, "SpinBox_Width" );
92     SpinBox_Width->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
93     SpinBox_Width->setWrapping( FALSE );
94     SpinBox_Width->setButtonSymbols( QSpinBox::PlusMinus );
95     ButtonGroup1Layout->addWidget( SpinBox_Width, 0, 5 );
96
97     TextLabel_BackFace = new QLabel( tr( "Back Face" ), ButtonGroup1, "TextLabel_BackFace" );
98     ButtonGroup1Layout->addWidget( TextLabel_BackFace, 1, 0 );
99
100     btnBackFaceColor = new QPushButton( ButtonGroup1, "btnBackFaceColor" );
101     btnBackFaceColor->setFixedSize( QSize( 25, 25 ) );
102     ButtonGroup1Layout->addWidget( btnBackFaceColor, 1, 1 );
103
104     TextLabel_ShrinkCoeff = new QLabel( tr( "Shrink coef." ), ButtonGroup1, "TextLabel_ShrinkCoeff" );
105     ButtonGroup1Layout->addWidget( TextLabel_ShrinkCoeff, 2, 0 );
106
107     SpinBox_Shrink = new QSpinBox( 20, 100, 1, ButtonGroup1, "SpinBox_Shrink" );
108     SpinBox_Shrink->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
109     SpinBox_Shrink->setButtonSymbols( QSpinBox::PlusMinus );
110     ButtonGroup1Layout->addMultiCellWidget( SpinBox_Shrink, 2, 2, 1, 5);
111
112     SMESHGUI_Preferences_ColorDlgLayout->addWidget( ButtonGroup1, 0, 0 );
113
114     ButtonGroup2 = new QButtonGroup( tr( "Nodes" ), this, "ButtonGroup2" );
115     ButtonGroup2->setColumnLayout(0, Qt::Vertical );
116     ButtonGroup2->layout()->setSpacing( 0 );
117     ButtonGroup2->layout()->setMargin( 0 );
118     QGridLayout* ButtonGroup2Layout = new QGridLayout( ButtonGroup2->layout() );
119     ButtonGroup2Layout->setAlignment( Qt::AlignTop );
120     ButtonGroup2Layout->setSpacing( 6 );
121     ButtonGroup2Layout->setMargin( 11 );
122
123     TextLabel_Nodes_Color = new QLabel( tr( "Color" ), ButtonGroup2, "TextLabel_Nodes_Color" );
124     ButtonGroup2Layout->addWidget( TextLabel_Nodes_Color, 0, 0 );
125
126     btnNodeColor = new QPushButton( ButtonGroup2, "btnNodeColor" );
127     btnNodeColor->setFixedSize( QSize( 25, 25 ) );
128     ButtonGroup2Layout->addWidget( btnNodeColor, 0, 1 );
129
130     TextLabel_Nodes_Size = new QLabel( tr( "Size" ), ButtonGroup2, "TextLabel_Nodes_Size" );
131     ButtonGroup2Layout->addWidget( TextLabel_Nodes_Size, 0, 2 );
132
133     SpinBox_Nodes_Size = new QSpinBox( 0, 5, 1, ButtonGroup2, "SpinBox_Nodes_Size" );
134     SpinBox_Nodes_Size->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
135     SpinBox_Nodes_Size->setWrapping( FALSE );
136     SpinBox_Nodes_Size->setButtonSymbols( QSpinBox::PlusMinus );
137     ButtonGroup2Layout->addWidget( SpinBox_Nodes_Size, 0, 3 );
138
139     SMESHGUI_Preferences_ColorDlgLayout->addWidget( ButtonGroup2, 1, 0 );
140
141     GroupButtons = new QButtonGroup( this, "GroupButtons" );
142     GroupButtons->setColumnLayout(0, Qt::Vertical );
143     GroupButtons->layout()->setSpacing( 0 );
144     GroupButtons->layout()->setMargin( 0 );
145     QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
146     GroupButtonsLayout->setAlignment( Qt::AlignTop );
147     GroupButtonsLayout->setSpacing( 6 );
148     GroupButtonsLayout->setMargin( 11 );
149
150     buttonOk = new QPushButton( tr( "&OK" ), GroupButtons, "buttonOk" );
151     buttonOk->setAutoDefault( TRUE );
152     buttonOk->setDefault( TRUE );
153     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
154
155     GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
156
157     buttonCancel = new QPushButton( tr( "&Cancel" ), GroupButtons, "buttonCancel" );
158     buttonCancel->setAutoDefault( TRUE );
159     GroupButtonsLayout->addWidget( buttonCancel, 0, 2 );
160
161     SMESHGUI_Preferences_ColorDlgLayout->addWidget( GroupButtons, 2, 0 );
162
163     Init();
164 }
165
166 /*  
167  *  Destroys the object and frees any allocated resources
168  */
169 SMESHGUI_Preferences_ColorDlg::~SMESHGUI_Preferences_ColorDlg()
170 {
171     // no need to delete child widgets, Qt does it all for us
172 }
173
174 //=================================================================================
175 // function : Init()
176 // purpose  :
177 //=================================================================================
178 void SMESHGUI_Preferences_ColorDlg::Init()
179 {  
180   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
181   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
182   
183   /* signals and slots connections */
184   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
185   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
186
187   connect( btnFillColor, SIGNAL( clicked() ), this, SLOT( SelectFillColor() ) ) ;
188   connect( btnOutlineColor, SIGNAL( clicked() ), this, SLOT( SelectOutlineColor() ) ) ;
189   connect( btnNodeColor, SIGNAL( clicked() ), this, SLOT( SelectNodeColor() ) ) ;
190   connect( btnBackFaceColor, SIGNAL( clicked() ), this, SLOT( SelectBackFaceColor() ) ) ;
191
192   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
193   /* to close dialog if study change */
194   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
195  
196   /* Move widget on the botton right corner of main widget */
197   int x, y ;
198   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
199   this->move( x, y ) ;
200 }
201
202 //=================================================================================
203 // function : SelectFillColor()
204 // purpose  :
205 //=================================================================================
206 void SMESHGUI_Preferences_ColorDlg::SelectFillColor()
207 {
208   QColor color = btnFillColor->palette().active().button();
209   color = QColorDialog::getColor( color );
210   if ( color.isValid() ) {
211     QPalette pal = btnFillColor->palette();
212     pal.setColor(QColorGroup::Button, color);
213     btnFillColor->setPalette(pal);
214   }
215 }
216
217 //=================================================================================
218 // function : SelectBackFaceColor()
219 // purpose  :
220 //=================================================================================
221 void SMESHGUI_Preferences_ColorDlg::SelectBackFaceColor()
222 {
223   QColor color = btnBackFaceColor->palette().active().button();
224   color = QColorDialog::getColor( color );
225   if ( color.isValid() ) {
226     QPalette pal = btnBackFaceColor->palette();
227     pal.setColor(QColorGroup::Button, color);
228     btnBackFaceColor->setPalette(pal);
229   }
230 }
231
232 //=================================================================================
233 // function : SelectOutlineColor()
234 // purpose  :
235 //=================================================================================
236 void SMESHGUI_Preferences_ColorDlg::SelectOutlineColor()
237 {
238   QColor color = btnOutlineColor->palette().active().button();
239   color = QColorDialog::getColor( color );
240   if ( color.isValid() ) {
241     QPalette pal = btnOutlineColor->palette();
242     pal.setColor(QColorGroup::Button, color);
243     btnOutlineColor->setPalette(pal);
244   }
245 }
246
247 //=================================================================================
248 // function : SelectNodeColor()
249 // purpose  :
250 //=================================================================================
251 void SMESHGUI_Preferences_ColorDlg::SelectNodeColor()
252 {
253   QColor color = btnNodeColor->palette().active().button();
254   color = QColorDialog::getColor( color );
255   if ( color.isValid() ) {
256     QPalette pal = btnNodeColor->palette();
257     pal.setColor(QColorGroup::Button, color);
258     btnNodeColor->setPalette(pal);
259   }
260 }
261
262 //=================================================================================
263 // function : ClickOnOk()
264 // purpose  :
265 //=================================================================================
266 void SMESHGUI_Preferences_ColorDlg::ClickOnOk()
267 {
268   mySMESHGUI->ResetState();
269   accept();
270 }
271
272 //=================================================================================
273 // function : ClickOnCancel()
274 // purpose  :
275 //=================================================================================
276 void SMESHGUI_Preferences_ColorDlg::ClickOnCancel()
277 {
278   mySMESHGUI->ResetState();
279   reject() ;
280 }
281
282 //=================================================================================
283 // function : DeactivateActiveDialog()
284 // purpose  :
285 //=================================================================================
286 void SMESHGUI_Preferences_ColorDlg::DeactivateActiveDialog()
287 {
288   return ;
289 }
290
291
292
293 //=================================================================================
294 // function : closeEvent()
295 // purpose  :
296 //=================================================================================
297 void SMESHGUI_Preferences_ColorDlg::closeEvent( QCloseEvent* e )
298 {
299   this->ClickOnCancel() ; /* same than click on cancel button */
300 }
301
302 //=================================================================================
303 // function : ActivateThisDialog()
304 // purpose  :
305 //=================================================================================
306 void SMESHGUI_Preferences_ColorDlg::ActivateThisDialog()
307 {
308   /* Emit a signal to deactivate any active dialog */
309   mySMESHGUI->EmitSignalDeactivateDialog() ;
310
311   return ;
312 }
313
314 //=================================================================================
315 // function : SetColor()
316 // purpose  :
317 //=================================================================================
318 void SMESHGUI_Preferences_ColorDlg::SetColor(int type, QColor color)
319 {
320   switch (type) {
321   case 1 : // fill 
322     {
323       QPalette pal = btnFillColor->palette();
324       pal.setColor(QColorGroup::Button, color);
325       btnFillColor->setPalette(pal);
326       break;
327     }
328   case 2 : // outline
329     {
330       QPalette pal = btnOutlineColor->palette();
331       pal.setColor(QColorGroup::Button, color);
332       btnOutlineColor->setPalette(pal);
333       break;
334     }
335   case 3 :  // node
336     {
337       QPalette pal = btnNodeColor->palette();
338       pal.setColor(QColorGroup::Button, color);
339       btnNodeColor->setPalette(pal);
340       break;
341     }
342   case 4 : // back face
343     {
344       QPalette pal = btnBackFaceColor->palette();
345       pal.setColor(QColorGroup::Button, color);
346       btnBackFaceColor->setPalette(pal);
347       break;
348     }
349   }
350 }
351
352 //=================================================================================
353 // function : GetColor()
354 // purpose  :
355 //=================================================================================
356 QColor SMESHGUI_Preferences_ColorDlg::GetColor(int type)
357 {
358   QColor color;
359   switch (type) {
360   case 1 : color = btnFillColor->palette().active().button();     break; // fill
361   case 2 : color = btnOutlineColor->palette().active().button();  break; // outline
362   case 3 : color = btnNodeColor->palette().active().button();     break; // node
363   case 4 : color = btnBackFaceColor->palette().active().button(); break; // back face
364   }
365   return color;
366 }
367
368 //=================================================================================
369 // function : SetIntValue()
370 // purpose  : 
371 //=================================================================================
372 void SMESHGUI_Preferences_ColorDlg::SetIntValue(int type, int value)
373 {
374   switch (type) {
375   case 1 : SpinBox_Width->setValue(value);      break; // width
376   case 2 : SpinBox_Nodes_Size->setValue(value); break; // nodes size = value; break;
377   case 3 : SpinBox_Shrink->setValue(value);     break; // shrink coeff
378   }
379 }
380
381 //=================================================================================
382 // function : GetIntValue()
383 // purpose  : 
384 //=================================================================================
385 int SMESHGUI_Preferences_ColorDlg::GetIntValue(int type)
386 {
387   int res = 0;
388   switch (type) {
389   case 1 : res = SpinBox_Width->value();      break; // width
390   case 2 : res = SpinBox_Nodes_Size->value(); break; // nodes size
391   case 3 : res = SpinBox_Shrink->value();     break; // shrink coeff
392   }
393   return res;
394 }