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