Salome HOME
Update copyright information
[modules/visu.git] / src / VISUGUI / VisuGUI_VectorsDlg.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  VISU VISUGUI : GUI of VISU component
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_Tools.h"
33 #include "VisuGUI_InputPane.h"
34
35 #include "VISU_ColoredPrs3dFactory.hh"
36 #include "VISU_Vectors_i.hh"
37 #include "LightApp_Application.h"
38 #include "SalomeApp_Module.h"
39 #include <SalomeApp_IntSpinBox.h>
40 #include <SalomeApp_DoubleSpinBox.h>
41
42 #include "SUIT_Desktop.h"
43 #include "SUIT_MessageBox.h"
44
45 #include <QtxColorButton.h>
46
47 #include <QLayout>
48 #include <QColorDialog>
49 #include <QTabWidget>
50 #include <QButtonGroup>
51 #include <QGroupBox>
52 #include <QRadioButton>
53 #include <QCheckBox>
54 #include <QLabel>
55 #include <QPushButton>
56 #include <QKeyEvent>
57
58 #include <limits>
59
60 using namespace std;
61
62 /*!
63   Constructor
64 */
65 VisuGUI_VectorsDlg::VisuGUI_VectorsDlg (SalomeApp_Module* theModule)
66   : VisuGUI_ScalarBarBaseDlg(theModule)
67 {
68   setWindowTitle(tr("DLG_TITLE"));
69   setSizeGripEnabled(TRUE);
70
71   QVBoxLayout* TopLayout = new QVBoxLayout( this ); 
72   TopLayout->setSpacing( 6 );
73   TopLayout->setMargin( 11 );
74
75   myTabBox = new QTabWidget(this);
76
77   QWidget* aBox = new QWidget(this);
78   QVBoxLayout* aVBLay = new QVBoxLayout( aBox );
79   aVBLay->setMargin( 11 );
80
81   TopGroup = new QGroupBox( aBox );
82   aVBLay->addWidget( TopGroup );
83   //TopGroup->setColumnLayout(0, Qt::Vertical );
84   //TopGroup->layout()->setSpacing( 0 );
85   //TopGroup->layout()->setMargin( 5 );
86   QGridLayout* TopGroupLayout = new QGridLayout( TopGroup );
87   TopGroupLayout->setAlignment( Qt::AlignTop );
88   TopGroupLayout->setSpacing( 6 );
89   TopGroupLayout->setMargin( 11 );
90
91   // Scale factor
92   ScaleLabel = new QLabel (tr("LBL_SCALE_FACTOR"), TopGroup );
93
94   ScalFact = new SalomeApp_DoubleSpinBox( TopGroup );
95   VISU::initSpinBox( ScalFact, 0., 1.0E+38, .1, "visual_data_precision" );
96   ScalFact->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
97   ScalFact->setValue( 0.1 );
98
99   TopGroupLayout->addWidget( ScaleLabel, 0, 0 );
100   TopGroupLayout->addWidget( ScalFact, 0, 1 );
101
102   // Line width
103   LineWidLabel = new QLabel (tr("LBL_LINE_WIDTH"), TopGroup );
104
105   LinWid = new SalomeApp_IntSpinBox( TopGroup );
106   LinWid->setAcceptNames( false );
107   LinWid->setMinimum( 1 );
108   LinWid->setMaximum( 10 );
109   LinWid->setSingleStep( 1 );
110   
111   LinWid->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
112   LinWid->setValue( 1 );
113
114   TopGroupLayout->addWidget( LineWidLabel, 1, 0 );
115   TopGroupLayout->addWidget( LinWid, 1, 1 );
116
117   // Color
118   UseMagn = new QCheckBox (tr("MAGNITUDE_COLORING_CHK"), TopGroup);
119   //UseMagn->setText(tr("MAGNITUDE_COLORING_CHK"));
120   SelColor = new QtxColorButton (TopGroup);
121   SelColor->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
122   SelColor->setText( tr("SEL_COLOR_BTN") );
123
124   /*  ColorLab = new QLabel( TopGroup, "ColorLab" );
125   ColorLab->setFixedSize( SelColor->sizeHint().height(), SelColor->sizeHint().height() );
126   ColorLab->setFrameStyle( QLabel::Plain | QLabel::Box );
127   */
128   TopGroupLayout->addWidget( UseMagn,  2, 0 );
129   //TopGroupLayout->addWidget( ColorLab, 2, 1 );
130   TopGroupLayout->addWidget( SelColor, 2, 1 );
131
132   // Gliphs
133   UseGlyph = new QCheckBox (tr("USE_GLYPHS_CHK"), TopGroup);
134   
135   TypeGlyph = new QButtonGroup ( TopGroup);
136   TypeGB = new QGroupBox( tr("GLYPH_TYPE_GRP"), TopGroup );
137   //TypeGlyph->setColumnLayout(0, Qt::Vertical );
138   //TypeGlyph->layout()->setSpacing( 0 );
139   //TypeGlyph->layout()->setMargin( 0 );
140   QGridLayout* TypeGlyphLayout = new QGridLayout( TypeGB );
141   TypeGlyphLayout->setAlignment( Qt::AlignTop );
142   TypeGlyphLayout->setSpacing( 6 );
143   TypeGlyphLayout->setMargin( 11 );
144
145   RBArrows = new QRadioButton (tr("ARROWS_BTN"), TypeGB );
146   TypeGlyphLayout->addWidget( RBArrows, 0, 0 );
147   RBCones2 = new QRadioButton (tr("CONES2_BTN"), TypeGB );
148   TypeGlyphLayout->addWidget( RBCones2, 1, 0 );
149   RBCones6 = new QRadioButton (tr("CONES6_BTN"), TypeGB );
150   TypeGlyphLayout->addWidget( RBCones6, 2, 0 );
151
152   TypeGlyph->addButton( RBArrows );
153   TypeGlyph->addButton( RBCones2 );
154   TypeGlyph->addButton( RBCones6 );
155
156   PosGlyph = new QButtonGroup ( TopGroup );
157   PosGB = new QGroupBox( tr("GLYPH_POSITION_GRP"), TopGroup );
158   //PosGlyph->setColumnLayout(0, Qt::Vertical );
159   //PosGlyph->layout()->setSpacing( 0 );
160   //PosGlyph->layout()->setMargin( 0 );
161   QGridLayout* PosGlyphLayout = new QGridLayout( PosGB );
162   PosGlyphLayout->setAlignment( Qt::AlignTop );
163   PosGlyphLayout->setSpacing( 6 );
164   PosGlyphLayout->setMargin( 11 );
165   
166   RBTail = new QRadioButton (tr("TAIL_BTN"  ), PosGB );
167   PosGlyphLayout->addWidget( RBTail, 0, 0 );
168   RBCent = new QRadioButton (tr("CENTER_BTN"), PosGB );
169   PosGlyphLayout->addWidget( RBCent, 1, 0 );
170   RBHead = new QRadioButton (tr("HEAD_BTN"  ), PosGB );
171   PosGlyphLayout->addWidget( RBHead, 2, 0 );
172
173   PosGlyph->addButton( RBTail );
174   PosGlyph->addButton( RBCent );
175   PosGlyph->addButton( RBHead );
176
177   TopGroupLayout->addWidget( UseGlyph, 3, 0, 1, 3 );
178   TopGroupLayout->addWidget( TypeGB, 4, 0 );
179   TopGroupLayout->addWidget( PosGB, 4, 1, 1, 2 );
180
181   // Common buttons ===========================================================
182   GroupButtons = new QGroupBox( this );
183   //GroupButtons->setColumnLayout(0, Qt::Vertical );
184   //GroupButtons->layout()->setSpacing( 0 );
185   //GroupButtons->layout()->setMargin( 0 );
186   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons );
187   GroupButtonsLayout->setAlignment( Qt::AlignTop );
188   GroupButtonsLayout->setSpacing( 6 );
189   GroupButtonsLayout->setMargin( 11 );
190
191   buttonOk = new QPushButton( tr( "BUT_OK" ), GroupButtons );
192   buttonOk->setAutoDefault( TRUE );
193   buttonOk->setDefault( TRUE );
194   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
195   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
196   buttonCancel = new QPushButton( tr( "BUT_CANCEL" ) , GroupButtons );
197   buttonCancel->setAutoDefault( TRUE );
198   GroupButtonsLayout->addWidget( buttonCancel, 0, 2 );
199   buttonHelp = new QPushButton( tr( "BUT_HELP" ) , GroupButtons );
200   buttonHelp->setAutoDefault( TRUE );
201   GroupButtonsLayout->addWidget( buttonHelp, 0, 3 );
202
203   // top layout
204   myTabBox->addTab(aBox, "Vectors");
205   myInputPane = new VisuGUI_InputPane(VISU::TVECTORS, theModule, this);
206   myTabBox->addTab(GetScalarPane(), "Scalar Bar");
207   myTabBox->addTab(myInputPane, "Input");
208
209   TopLayout->addWidget( myTabBox );
210   TopLayout->addWidget( GroupButtons );
211   
212   // signals and slots connections
213   connect( UseGlyph,     SIGNAL( clicked() ), this, SLOT( enableGlyphType() ) );
214   //connect( SelColor,     SIGNAL( clicked() ), this, SLOT( setVColor() ) );
215   connect( UseMagn,      SIGNAL( clicked() ), this, SLOT( enableSetColor() ) );
216   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
217   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
218   connect( buttonHelp,   SIGNAL( clicked() ), this, SLOT( onHelp() ) );
219   
220   // default values
221   UseMagn->setChecked( TRUE );
222   UseGlyph->setChecked( TRUE );
223   RBArrows->setChecked( TRUE );
224   RBTail->setChecked( TRUE );
225   setColor( QColor( 255, 0, 0 ) );
226   enableGlyphType();
227   enableSetColor();
228 }
229
230 VisuGUI_VectorsDlg::~VisuGUI_VectorsDlg()
231 {}
232
233 void VisuGUI_VectorsDlg::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs,
234                                             bool theInit )
235 {
236   if ( theInit )
237     myPrsCopy = VISU::TSameAsFactory<VISU::TVECTORS>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
238
239   VisuGUI_ScalarBarBaseDlg::initFromPrsObject(myPrsCopy, theInit);
240
241   setScaleFactor(myPrsCopy->GetScale());
242   setLineWidth((int)myPrsCopy->GetLineWidth());
243   setUseMagnColor(myPrsCopy->IsColored());
244   SALOMEDS::Color anOldColor = myPrsCopy->GetColor();
245   QColor aColor = QColor(int(255*anOldColor.R),int(255*anOldColor.G),int(255*anOldColor.B));
246   setColor(aColor);
247   
248   if (myPrsCopy->GetGlyphType() != VISU::Vectors::NONE) {
249     setUseGlyphs(true);
250     setGlyphType(myPrsCopy->GetGlyphType());
251     setGlyphPos(myPrsCopy->GetGlyphPos());
252   } else 
253     setUseGlyphs(false);
254
255   enableSetColor();
256
257   if( !theInit )
258     return;
259
260   myInputPane->initFromPrsObject( myPrsCopy );
261   myTabBox->setCurrentIndex( 0 );
262 }
263
264
265 int VisuGUI_VectorsDlg::storeToPrsObject(VISU::ColoredPrs3d_i* thePrs)
266 {
267   if(!myInputPane->check() || !GetScalarPane()->check())
268     return 0;
269   
270   int anIsOk = myInputPane->storeToPrsObject( myPrsCopy );
271   anIsOk &= GetScalarPane()->storeToPrsObject( myPrsCopy );
272   
273   myPrsCopy->SetScale(getScaleFactor());
274   myPrsCopy->SetLineWidth(getLineWidth());
275   myPrsCopy->ShowColored(getUseMagnColor());
276   if(!myPrsCopy->IsColored()){
277     QColor aSelectedColor = SelColor->color();
278     SALOMEDS::Color aColor;
279     aColor.R = aSelectedColor.red()/255.;
280     aColor.G = aSelectedColor.green()/255.;
281     aColor.B = aSelectedColor.blue()/255.;
282     myPrsCopy->SetColor(aColor);
283   }
284   if (getUseGlyphs()) {
285     myPrsCopy->SetGlyphPos(getGlyphPos());
286     myPrsCopy->SetGlyphType(getGlyphType());
287   } else 
288     myPrsCopy->SetGlyphType(VISU::Vectors::NONE);
289
290   VISU::TSameAsFactory<VISU::TVECTORS>().Copy(myPrsCopy, thePrs);
291
292   return anIsOk;
293 }
294
295
296
297 /*!
298   Called when "Use glyphs" check box clicked
299 */
300 void VisuGUI_VectorsDlg::enableGlyphType()
301 {
302   TypeGB->setEnabled( UseGlyph->isChecked() );
303   PosGB->setEnabled( UseGlyph->isChecked() );
304 }
305
306 /*!
307   Called when "Magnitude Coloring" check box clicked
308 */
309 void VisuGUI_VectorsDlg::enableSetColor()
310 {
311   SelColor->setEnabled(!UseMagn->isChecked() );
312   //ColorLab->setEnabled( UseMagn->isEnabled() && !UseMagn->isChecked() );
313 }
314
315 /*!
316   Called when "Select Color" buttonx clicked
317 */
318 /*void VisuGUI_VectorsDlg::setVColor()
319 {
320   QColor cnew = QColorDialog::getColor( myColor, this );
321   if ( cnew.isValid() )
322     setColor( cnew );
323 }*/
324
325 /*!
326   Sets Scale factor
327 */
328 void VisuGUI_VectorsDlg::setScaleFactor(double theFactor)
329 {
330   double step = 0.1;
331   if (fabs(theFactor) > std::numeric_limits<double>::epsilon()) {
332     int degree = int(log10(fabs(theFactor))) - 1;
333     if (fabs(theFactor) < 1) {
334       // as logarithm value is negative in this case
335       // and it is truncated to the bigger integer
336       degree -= 1;
337     }
338     step = pow(10., double(degree));
339   }
340
341   ScalFact->setSingleStep(step);
342   ScalFact->setValue(theFactor);
343 }
344
345 /*!
346   Gets Scale factor
347 */
348 double VisuGUI_VectorsDlg::getScaleFactor()
349 {
350   return ScalFact->value();
351 }
352
353 /*!
354   Sets Line width
355 */
356 void VisuGUI_VectorsDlg::setLineWidth( int lw )
357 {
358   LinWid->setValue( lw );
359 }
360
361 /*!
362   Gets Line width
363 */
364 int VisuGUI_VectorsDlg::getLineWidth() 
365 {
366   return LinWid->value();
367 }
368
369 /*!
370   Sets "Use Magnitude Coloring" flag
371 */
372 void VisuGUI_VectorsDlg::setUseMagnColor( bool on )
373 {
374   UseMagn->setChecked( on );
375   enableSetColor();
376 }
377
378 /*!
379   Gets "Use Magnitude Coloring" flag state
380 */
381 bool VisuGUI_VectorsDlg::getUseMagnColor()
382 {
383   return UseMagn->isChecked();
384 }
385
386 /*!
387   Sets "Use Glyphs" flag
388 */
389 void VisuGUI_VectorsDlg::setUseGlyphs( bool on ) 
390 {
391   UseGlyph->setChecked( on );
392   enableGlyphType();
393 }
394
395 /*!
396   Gets "Use Glyphs" flag state
397 */
398 bool VisuGUI_VectorsDlg::getUseGlyphs()
399 {
400   return UseGlyph->isChecked();
401 }
402
403 /*!
404   Sets color
405 */
406 void VisuGUI_VectorsDlg::setColor( QColor color )
407 {
408   //myColor = color;
409   //QPalette aPal = SelColor->palette();
410   //aPal.setColor( SelColor->backgroundRole(), myColor );
411   //SelColor->setPalette( aPal );
412   SelColor->setColor( color );
413 }
414
415 /*!
416   Sets glyphs type : 0 - arrows, 1 - cones2, 2 - cones6
417 */
418 void VisuGUI_VectorsDlg::setGlyphType(VISU::Vectors::GlyphType type )
419 {
420   if ( type ==  VISU::Vectors::CONE2) 
421     RBCones2->setChecked( true );
422   else if ( type == VISU::Vectors::CONE6)
423     RBCones6->setChecked( true );
424   else
425     RBArrows->setChecked( true );
426 }
427
428 /*!
429   Gets glyphs type : 0 - arrows, 1 - cones2, 2 - cones6
430 */
431 VISU::Vectors::GlyphType VisuGUI_VectorsDlg::getGlyphType()
432 {
433   VISU::Vectors::GlyphType type;
434   if ( RBCones2->isChecked() )
435     type = VISU::Vectors::CONE2;
436   else if ( RBCones6->isChecked() )
437     type = VISU::Vectors::CONE6;
438   else
439     type = VISU::Vectors::ARROW;
440   return type;
441 }
442
443 /*!
444   Sets glyph position : -1 - tail, 0 - center, 1 - head
445 */
446 void VisuGUI_VectorsDlg::setGlyphPos(VISU::Vectors::GlyphPos pos)
447 {
448   if ( pos == VISU::Vectors::TAIL) 
449     RBTail->setChecked(true);
450   else if ( pos == VISU::Vectors::HEAD)
451     RBHead->setChecked(true);
452   else
453     RBCent->setChecked(true);
454 }
455
456 /*!
457   Gets glyph position : -1 - tail, 0 - center, 1 - head
458 */
459 VISU::Vectors::GlyphPos VisuGUI_VectorsDlg::getGlyphPos()
460 {
461   VISU::Vectors::GlyphPos pos;
462   if ( RBTail->isChecked() )
463     pos = VISU::Vectors::TAIL;
464   else if ( RBHead->isChecked() )
465     pos = VISU::Vectors::HEAD;
466   else
467     pos = VISU::Vectors::CENTER;
468   return pos;
469 }
470
471 /*!
472   Enbled/disables magnitude coloring
473 */
474 void VisuGUI_VectorsDlg::enableMagnColor( bool enable )
475 {
476   UseMagn->setEnabled( enable );
477   enableSetColor();
478 }
479
480 QString VisuGUI_VectorsDlg::GetContextHelpFilePath()
481 {
482   return "vectors_page.html";
483 }