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