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