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