Salome HOME
NRI : Correction 1.1a version.
[modules/visu.git] / src / VISUGUI / VisuGUI_VectorsDlg.cxx
1 using namespace std;
2 //  File      : VisuGUI_VectorsDlg.cxx
3 //  Created   : Wed Aug 01 10:23:06 2001
4 //  Author    : Laurent CORNABE & Hubert ROLLAND 
5 //  Project   : SALOME
6 //  Module    : VISUGUI
7 //  Copyright : PRINCIPIA
8 //  $Header$
9
10 #include "VisuGUI_VectorsDlg.h"
11 #include <qlayout.h>
12 #include <qcolordialog.h>
13 #include "QAD_Application.h"
14 #include "QAD_Desktop.h"
15
16 /*!
17   Constructor
18 */
19 VisuGUI_VectorsDlg::VisuGUI_VectorsDlg()
20     : QDialog( QAD_Application::getDesktop(), "VisuGUI_VectorsDlg", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
21 {
22   setCaption( tr( "Vector Field Representation" ) );
23   setSizeGripEnabled( TRUE );
24
25   QVBoxLayout* TopLayout = new QVBoxLayout( this ); 
26   TopLayout->setSpacing( 6 );
27   TopLayout->setMargin( 11 );
28
29   TopGroup = new QButtonGroup( this, "TopGroup" );
30   TopGroup->setColumnLayout(0, Qt::Vertical );
31   TopGroup->layout()->setSpacing( 0 );
32   TopGroup->layout()->setMargin( 0 );
33   QGridLayout* TopGroupLayout = new QGridLayout( TopGroup->layout() );
34   TopGroupLayout->setAlignment( Qt::AlignTop );
35   TopGroupLayout->setSpacing( 6 );
36   TopGroupLayout->setMargin( 11 );
37
38   // Scale factor
39   ScaleLabel = new QLabel( tr( "Scale factor:" ), TopGroup, "ScaleLabel" );
40
41   ScalFact = new QAD_SpinBoxDbl( TopGroup, 1e-20, 1.0E+38, 0.1, 5);
42   ScalFact->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
43   ScalFact->setValue( 0.1 );
44
45   TopGroupLayout->addWidget( ScaleLabel, 0, 0 );
46   TopGroupLayout->addWidget( ScalFact, 0, 1 );
47
48   // Line width
49   LineWidLabel = new QLabel( tr( "Line width:" ), TopGroup, "LineWidLabel" );
50
51   LinWid = new QSpinBox( 1, 10, 1, TopGroup, "LinWid" );
52   LinWid->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
53   LinWid->setValue( 1 );
54
55   TopGroupLayout->addWidget( LineWidLabel, 1, 0 );
56   TopGroupLayout->addWidget( LinWid, 1, 1 );
57
58   // Color
59   UseMagn = new QCheckBox( tr( "Magnitude coloring" ), TopGroup, "UseMagn" );
60   UseMagn->setText( tr( "Magnitude coloring" ) );
61   SelColor = new QPushButton( tr( "Select Color" ), TopGroup, "SelColor" );
62
63   /*  ColorLab = new QLabel( TopGroup, "ColorLab" );
64   ColorLab->setFixedSize( SelColor->sizeHint().height(), SelColor->sizeHint().height() );
65   ColorLab->setFrameStyle( QLabel::Plain | QLabel::Box );
66   */  
67   TopGroupLayout->addWidget( UseMagn,  2, 0 );
68   //TopGroupLayout->addWidget( ColorLab, 2, 1 );
69   TopGroupLayout->addWidget( SelColor, 2, 1 );
70
71   // Gliphs
72   UseGlyph = new QCheckBox( tr( "Use glyphs" ), TopGroup, "UseGlyph" );
73   
74   TypeGlyph = new QButtonGroup( tr( "Glyph type" ), TopGroup, "TypeGlyph" );
75   TypeGlyph->setColumnLayout(0, Qt::Vertical );
76   TypeGlyph->layout()->setSpacing( 0 );
77   TypeGlyph->layout()->setMargin( 0 );
78   QGridLayout* TypeGlyphLayout = new QGridLayout( TypeGlyph->layout() );
79   TypeGlyphLayout->setAlignment( Qt::AlignTop );
80   TypeGlyphLayout->setSpacing( 6 );
81   TypeGlyphLayout->setMargin( 11 );
82
83   RBArrows = new QRadioButton( tr( "Arrows" ), TypeGlyph, "RBArrows" );
84   TypeGlyphLayout->addWidget( RBArrows, 0, 0 );
85   RBCones2 = new QRadioButton( tr( "Cones (2)" ), TypeGlyph, "RBCones2" );
86   TypeGlyphLayout->addWidget( RBCones2, 1, 0 );
87   RBCones6 = new QRadioButton( tr( "Cones (6)" ), TypeGlyph, "RBCones6" );
88   TypeGlyphLayout->addWidget( RBCones6, 2, 0 );
89
90   PosGlyph = new QButtonGroup( tr( "Glyph position" ), TopGroup, "PosGlyph" );
91   PosGlyph->setColumnLayout(0, Qt::Vertical );
92   PosGlyph->layout()->setSpacing( 0 );
93   PosGlyph->layout()->setMargin( 0 );
94   QGridLayout* PosGlyphLayout = new QGridLayout( PosGlyph->layout() );
95   PosGlyphLayout->setAlignment( Qt::AlignTop );
96   PosGlyphLayout->setSpacing( 6 );
97   PosGlyphLayout->setMargin( 11 );
98   
99   RBTail = new QRadioButton( tr( "Tail" ), PosGlyph, "RBTail" );
100   PosGlyphLayout->addWidget( RBTail, 0, 0 );
101   RBCent = new QRadioButton( tr( "Center" ), PosGlyph, "RBCent" );
102   PosGlyphLayout->addWidget( RBCent, 1, 0 );
103   RBHead = new QRadioButton( tr( "Head" ), PosGlyph, "RBHead" );
104   PosGlyphLayout->addWidget( RBHead, 2, 0 );
105
106   TopGroupLayout->addMultiCellWidget( UseGlyph, 3, 3, 0, 2 );
107   TopGroupLayout->addWidget( TypeGlyph, 4, 0 );
108   TopGroupLayout->addMultiCellWidget( PosGlyph, 4, 4, 1, 2 );
109
110   // Common buttons ===========================================================
111   GroupButtons = new QGroupBox( this, "GroupButtons" );
112   GroupButtons->setColumnLayout(0, Qt::Vertical );
113   GroupButtons->layout()->setSpacing( 0 );
114   GroupButtons->layout()->setMargin( 0 );
115   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
116   GroupButtonsLayout->setAlignment( Qt::AlignTop );
117   GroupButtonsLayout->setSpacing( 6 );
118   GroupButtonsLayout->setMargin( 11 );
119
120   buttonOk = new QPushButton( tr( "&OK" ), GroupButtons, "buttonOk" );
121   buttonOk->setAutoDefault( TRUE );
122   buttonOk->setDefault( TRUE );
123   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
124   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
125   buttonCancel = new QPushButton( tr( "&Cancel" ) , GroupButtons, "buttonCancel" );
126   buttonCancel->setAutoDefault( TRUE );
127   GroupButtonsLayout->addWidget( buttonCancel, 0, 2 );
128
129   // top layout
130   TopLayout->addWidget( TopGroup );
131   TopLayout->addWidget( GroupButtons );
132   
133   // signals and slots connections
134   connect( UseGlyph,     SIGNAL( clicked() ), this, SLOT( enableGlyphType() ) );
135   connect( SelColor,     SIGNAL( clicked() ), this, SLOT( setVColor() ) );
136   connect( UseMagn,      SIGNAL( clicked() ), this, SLOT( enableSetColor() ) );
137   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
138   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
139   
140   // default values
141   UseMagn->setChecked( TRUE );
142   UseGlyph->setChecked( TRUE );
143   RBArrows->setChecked( TRUE );
144   RBTail->setChecked( TRUE );
145   setColor( QColor( 255, 0, 0 ) );
146   enableGlyphType();
147   enableSetColor();
148 }
149
150 /*!
151   Destructor
152 */
153 VisuGUI_VectorsDlg::~VisuGUI_VectorsDlg()
154 {
155 }
156
157
158 void VisuGUI_VectorsDlg::initFromPrsObject(VISU::Vectors_i* thePrs) {
159   setScaleFactor(thePrs->GetScale());
160   setLineWidth(thePrs->GetLineWidth());
161   setUseMagnColor(thePrs->isColored());
162   setColor(QColor(thePrs->GetColor()[0]*255,
163                   thePrs->GetColor()[1]*255,
164                   thePrs->GetColor()[2]*255));
165   
166   if (thePrs->GetGlyphType() != VISU::Vectors::NONE) {
167     setUseGlyphs(true);
168     setGlyphType(thePrs->GetGlyphType());
169     setGlyphPos(thePrs->GetGlyphPos());
170   } else 
171     setUseGlyphs(false);
172
173   enableSetColor();
174 }
175
176
177 void VisuGUI_VectorsDlg::storeToPrsObject(VISU::Vectors_i* thePrs) {
178   thePrs->SetScale(getScaleFactor());
179   thePrs->SetLineWidth(getLineWidth());
180   thePrs->setColored(getUseMagnColor());
181   
182   float aColor[3];
183   aColor[0] = myColor.red()/255.;
184   aColor[1] = myColor.green()/255.;
185   aColor[2] = myColor.blue()/255.;
186
187   thePrs->SetColor(aColor);
188   
189   if (getUseGlyphs()) {
190     thePrs->SetGlyphPos(getGlyphPos());
191     thePrs->SetGlyphType(getGlyphType());
192   } else 
193     thePrs->SetGlyphType(VISU::Vectors::NONE);
194 }
195
196
197
198 /*!
199   Called when "Use glyphs" check box clicked
200 */
201 void VisuGUI_VectorsDlg::enableGlyphType()
202 {
203   TypeGlyph->setEnabled( UseGlyph->isChecked() );
204   PosGlyph->setEnabled( UseGlyph->isChecked() );
205 }
206
207 /*!
208   Called when "Magnitude Coloring" check box clicked
209 */
210 void VisuGUI_VectorsDlg::enableSetColor()
211 {
212   SelColor->setEnabled(!UseMagn->isChecked() );
213   //ColorLab->setEnabled( UseMagn->isEnabled() && !UseMagn->isChecked() );
214 }
215
216 /*!
217   Called when "Select Color" buttonx clicked
218 */
219 void VisuGUI_VectorsDlg::setVColor()
220 {
221   QColor cnew = QColorDialog::getColor( myColor, this );
222   if ( cnew.isValid() )
223     setColor( cnew );
224 }
225
226 /*!
227   Sets Scale factor
228 */
229 void VisuGUI_VectorsDlg::setScaleFactor( double sf )
230 {
231   ScalFact->setValue( sf );
232 }
233
234 /*!
235   Gets Scale factor
236 */
237 double VisuGUI_VectorsDlg::getScaleFactor()
238 {
239   return ScalFact->value();
240 }
241
242 /*!
243   Sets Line width
244 */
245 void VisuGUI_VectorsDlg::setLineWidth( int lw )
246 {
247   LinWid->setValue( lw );
248 }
249
250 /*!
251   Gets Line width
252 */
253 int VisuGUI_VectorsDlg::getLineWidth() 
254 {
255   return LinWid->value();
256 }
257
258 /*!
259   Sets "Use Magnitude Coloring" flag
260 */
261 void VisuGUI_VectorsDlg::setUseMagnColor( bool on )
262 {
263   UseMagn->setChecked( on );
264   enableSetColor();
265 }
266
267 /*!
268   Gets "Use Magnitude Coloring" flag state
269 */
270 bool VisuGUI_VectorsDlg::getUseMagnColor()
271 {
272   return UseMagn->isChecked();
273 }
274
275 /*!
276   Sets "Use Glyphs" flag
277 */
278 void VisuGUI_VectorsDlg::setUseGlyphs( bool on ) 
279 {
280   UseGlyph->setChecked( on );
281   enableGlyphType();
282 }
283
284 /*!
285   Gets "Use Glyphs" flag state
286 */
287 bool VisuGUI_VectorsDlg::getUseGlyphs()
288 {
289   return UseGlyph->isChecked();
290 }
291
292 /*!
293   Sets color
294 */
295 void VisuGUI_VectorsDlg::setColor( QColor color )
296 {
297   myColor = color;
298   SelColor->setPaletteBackgroundColor(myColor);
299 /*  QPalette pal = ColorLab->palette();
300   QColorGroup ca = pal.active();
301   ca.setColor( QColorGroup::Background, myColor );
302   pal.setActive( ca );
303   ColorLab->setPalette( pal );*/
304 }
305
306 /*!
307   Sets glyphs type : 0 - arrows, 1 - cones2, 2 - cones6
308 */
309 void VisuGUI_VectorsDlg::setGlyphType(VISU::Vectors::GlyphType type )
310 {
311   if ( type ==  VISU::Vectors::CONE2) 
312     RBCones2->setChecked( true );
313   else if ( type == VISU::Vectors::CONE6)
314     RBCones6->setChecked( true );
315   else
316     RBArrows->setChecked( true );
317 }
318
319 /*!
320   Gets glyphs type : 0 - arrows, 1 - cones2, 2 - cones6
321 */
322 VISU::Vectors::GlyphType VisuGUI_VectorsDlg::getGlyphType()
323 {
324   VISU::Vectors::GlyphType type;
325   if ( RBCones2->isChecked() )
326     type = VISU::Vectors::CONE2;
327   else if ( RBCones6->isChecked() )
328     type = VISU::Vectors::CONE6;
329   else
330     type = VISU::Vectors::ARROW;
331   return type;
332 }
333
334 /*!
335   Sets glyph position : -1 - tail, 0 - center, 1 - head
336 */
337 void VisuGUI_VectorsDlg::setGlyphPos(VISU::Vectors::GlyphPos pos)
338 {
339   if ( pos == VISU::Vectors::TAIL) 
340     RBTail->setChecked(true);
341   else if ( pos == VISU::Vectors::HEAD)
342     RBHead->setChecked(true);
343   else
344     RBCent->setChecked(true);
345 }
346
347 /*!
348   Gets glyph position : -1 - tail, 0 - center, 1 - head
349 */
350 VISU::Vectors::GlyphPos VisuGUI_VectorsDlg::getGlyphPos()
351 {
352   VISU::Vectors::GlyphPos pos;
353   if ( RBTail->isChecked() )
354     pos = VISU::Vectors::TAIL;
355   else if ( RBHead->isChecked() )
356     pos = VISU::Vectors::HEAD;
357   else
358     pos = VISU::Vectors::CENTER;
359   return pos;
360 }
361
362 /*!
363   Enbled/disables magnitude coloring
364 */
365 void VisuGUI_VectorsDlg::enableMagnColor( bool enable )
366 {
367   UseMagn->setEnabled( enable );
368   enableSetColor();
369 }
370
371
372