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