Salome HOME
7ab8e93ac76648d371f85763be459ce3bafb3a2d
[modules/visu.git] / src / VISUGUI / VisuGUI_IsoSurfacesDlg.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_IsoSurfacesDlg.cxx
24 //  Author : Laurent CORNABE & Hubert ROLLAND
25 //  Module : VISU
26 //  $Header$
27
28 #include "VisuGUI_IsoSurfacesDlg.h"
29
30 #include "VisuGUI.h"
31 #include "VisuGUI_Tools.h"
32 #include "VisuGUI_InputPane.h"
33
34 #include <VISU_ColoredPrs3dFactory.hh>
35
36 #include <LightApp_Application.h>
37
38 #include <QtxColorButton.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_Session.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_ResourceMgr.h>
43
44 #include <limits.h>
45
46 #include <QLayout>
47 #include <QValidator>
48 #include <QLabel>
49 #include <QGroupBox>
50 #include <QSpinBox>
51 #include <QPushButton>
52 #include <QCheckBox>
53 #include <QLineEdit>
54 #include <QTabWidget>
55 #include <QKeyEvent>
56 #include <QColorDialog>
57 #include <QButtonGroup>
58 #include <QRadioButton>
59
60 using namespace std;
61
62 VisuGUI_IsoSurfPane::VisuGUI_IsoSurfPane (QWidget* parent,
63                                           VisuGUI_ScalarBarPane* theScalarPane)
64   : QWidget(parent),
65     myScalarPane(theScalarPane)
66 {
67   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
68   QFrame* TopGroup = new QFrame( this );
69   aMainLayout->addWidget( TopGroup );
70   
71   TopGroup->setFrameStyle(QFrame::Box | QFrame::Sunken);
72   TopGroup->setLineWidth(1);
73
74   QGridLayout* TopGroupLayout = new QGridLayout(TopGroup);
75   TopGroupLayout->setAlignment( Qt::AlignTop );
76   TopGroupLayout->setSpacing( 6 );
77   TopGroupLayout->setMargin( 11 );
78
79   QGroupBox* aRangeBox = new QGroupBox( tr( "RANGE" ), this );
80   QRadioButton* aUseScalarBarRange = new QRadioButton( tr( "USE_SCALAR_BAR_RANGE" ), aRangeBox );
81   QRadioButton* aUseCustomRange = new QRadioButton( tr( "USE_CUSTOM_RANGE" ), aRangeBox );
82
83   myRangeGrp = new QButtonGroup( aRangeBox );
84   myRangeGrp->addButton( aUseScalarBarRange, ScalarBarRange );
85   myRangeGrp->addButton( aUseCustomRange, CustomRange );
86   aUseScalarBarRange->setChecked( true );
87
88   connect( myRangeGrp, SIGNAL( buttonClicked( int ) ), this, SLOT( onRangeButtonClicked( int ) ) );
89
90   QLabel* LabelMin = new QLabel( tr( "MIN_VALUE" ), aRangeBox );
91   MinIso = new QLineEdit( aRangeBox );
92   MinIso->setValidator( new QDoubleValidator( aRangeBox ) );
93   MinIso->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
94   MinIso->setMinimumSize( 70, 0 );
95   LabelMin->setBuddy( MinIso );
96
97   QLabel* LabelMax = new QLabel( tr( "MAX_VALUE" ), aRangeBox );
98   MaxIso = new QLineEdit( aRangeBox );
99   MaxIso->setValidator( new QDoubleValidator( aRangeBox ) );
100   MaxIso->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
101   MaxIso->setMinimumSize( 70, 0 );
102   LabelMax->setBuddy( MaxIso );
103
104   QPushButton* aUpdateBtn = new QPushButton( "Update scalar bar range with these values", aRangeBox );
105   aUpdateBtn->setEnabled( false );
106   connect( aUpdateBtn, SIGNAL( clicked() ), this, SLOT(onCBUpdate() ) );
107   connect( aUseCustomRange, SIGNAL( toggled( bool ) ), aUpdateBtn, SLOT( setEnabled( bool ) ) );
108
109   QGridLayout* aRangeLayout = new QGridLayout( aRangeBox );
110   aRangeLayout->setSpacing( 6 );
111   aRangeLayout->setMargin( 11 );
112   aRangeLayout->addWidget( aUseScalarBarRange, 0, 0 );
113   aRangeLayout->addWidget( aUseCustomRange, 0, 1 );
114   aRangeLayout->addWidget( LabelMin, 1, 0 );
115   aRangeLayout->addWidget( MinIso, 1, 1 );
116   aRangeLayout->addWidget( LabelMax, 2, 0 );
117   aRangeLayout->addWidget( MaxIso, 2, 1 );
118   aRangeLayout->addWidget( aUpdateBtn, 3, 0, 1, 2 );
119
120   TopGroupLayout->addWidget( aRangeBox, 0, 0, 1, 2 );
121
122   QLabel* LabelNbr = new QLabel (tr("NB_SURFACES"), TopGroup);
123   TopGroupLayout->addWidget( LabelNbr, 1, 0 );
124   NbrIso = new QSpinBox( TopGroup );
125   NbrIso->setMaximum( 100 );
126   NbrIso->setMinimum( 1 );
127   NbrIso->setSingleStep( 1 );
128   NbrIso->setValue( 1 );
129   TopGroupLayout->addWidget( NbrIso, 1, 1 );
130
131   myUseMagnitude = new QCheckBox(tr("MAGNITUDE_COLORING_CHK"), TopGroup);
132   myUseMagnitude->setChecked(true);
133   TopGroupLayout->addWidget( myUseMagnitude, 2, 0 );
134
135   mySelColor = new QtxColorButton( TopGroup );
136   mySelColor->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
137   TopGroupLayout->addWidget( mySelColor, 2, 1 );
138   //connect( mySelColor, SIGNAL( clicked() ), this, SLOT( setColor() ) );
139   connect( myUseMagnitude, SIGNAL( toggled(bool) ), mySelColor, SLOT( setDisabled(bool) ) );
140
141   myUseLabels = new QCheckBox(tr("SHOW_VALUES_CHK"), TopGroup);
142   myUseLabels->setChecked(false);
143   TopGroupLayout->addWidget( myUseLabels, 3, 0 );
144   myNbLabels = new QSpinBox( TopGroup );
145   myNbLabels->setMinimum(1);
146   myNbLabels->setMaximum(100);
147   myNbLabels->setSingleStep(1);
148   myNbLabels->setEnabled(false);
149   TopGroupLayout->addWidget( myNbLabels, 3, 1 );
150   connect( myUseLabels, SIGNAL( toggled(bool) ), myNbLabels, SLOT( setEnabled(bool) ) );
151
152   mySelColor->setEnabled( !myUseMagnitude->isChecked() );
153 }
154
155 void VisuGUI_IsoSurfPane::initFromPrsObject (VISU::IsoSurfaces_i* thePrs)
156 {
157   NbrIso->setValue(thePrs->GetNbSurfaces());
158   MinIso->setText(QString::number(thePrs->GetSubMin()));
159   MaxIso->setText(QString::number(thePrs->GetSubMax()));
160
161   int anId = thePrs->IsSubRangeFixed() ? ScalarBarRange : CustomRange;
162   bool anIsSubRangeFixed = thePrs->IsSubRangeFixed();
163   myRangeGrp->button( anId )->setChecked( true );
164   onRangeButtonClicked( anId );
165
166   myUseMagnitude->setChecked(thePrs->IsColored());
167   SALOMEDS::Color anOldColor = thePrs->GetColor();
168   QColor aColor = QColor(int(255*anOldColor.R),int(255*anOldColor.G),int(255*anOldColor.B));
169   setColor(aColor);
170
171   myUseLabels->setChecked(thePrs->IsLabeled());
172   myNbLabels->setValue(thePrs->GetNbLabels());
173   mySelColor->setEnabled( !myUseMagnitude->isChecked() );
174 }
175
176 int VisuGUI_IsoSurfPane::storeToPrsObject (VISU::IsoSurfaces_i* thePrs)
177 {
178   thePrs->SetNbSurfaces(NbrIso->value());
179
180   if( myRangeGrp->checkedId() == ScalarBarRange )
181   {
182     thePrs->SetSubRange( myScalarPane->getMin(), myScalarPane->getMax() );
183     thePrs->SetSubRangeFixed( true );
184   }
185   else // CustomRange
186   {
187     thePrs->SetSubRange( MinIso->text().toDouble(), MaxIso->text().toDouble() );
188     thePrs->SetSubRangeFixed( false );
189   }
190
191   thePrs->ShowLabels(myUseLabels->isChecked(), myNbLabels->value());
192   thePrs->ShowColored(myUseMagnitude->isChecked());
193   if(!thePrs->IsColored()){
194     QColor aQColor = color();
195     SALOMEDS::Color aColor;
196     aColor.R = aQColor.red()/255.;
197     aColor.G = aQColor.green()/255.;
198     aColor.B = aQColor.blue()/255.;
199     thePrs->SetColor(aColor);
200   }
201   return 1;
202 }
203
204 void VisuGUI_IsoSurfPane::onRangeButtonClicked( int theId )
205 {
206   bool isCustomRange = theId == 1;
207   MinIso->setEnabled( isCustomRange );
208   MaxIso->setEnabled( isCustomRange );
209 }
210
211 void VisuGUI_IsoSurfPane::onCBUpdate()
212 {
213   myScalarPane->setRange(MinIso->text().toDouble(), MaxIso->text().toDouble(), true);
214 }
215
216 bool VisuGUI_IsoSurfPane::check()
217 {
218   if (MinIso->text().toDouble() >= MaxIso->text().toDouble()) {
219     MESSAGE(tr("MSG_MINMAX_VALUES").toLatin1().data());
220     SUIT_MessageBox::warning( this,tr("WRN_VISU"),
221                               tr("MSG_MINMAX_VALUES"),
222                               tr("BUT_OK"));
223     return false;
224   }
225   return true;
226 }
227
228 void VisuGUI_IsoSurfPane::setColor()
229 {
230   QColor cnew = QColorDialog::getColor( color(), this );
231   if ( cnew.isValid() )
232     setColor(cnew);
233 }
234
235 void VisuGUI_IsoSurfPane::setColor( const QColor& theColor)
236 {
237   /*  QPalette pal = mySelColor->palette();
238   pal.setColor(mySelColor->backgroundRole(), theColor);
239   mySelColor->setPalette(pal);*/
240   mySelColor->setColor( theColor );
241 }
242
243 QColor VisuGUI_IsoSurfPane::color() const
244 {
245   return mySelColor->color();
246 }
247
248
249 /*!
250   Constructor
251 */
252 VisuGUI_IsoSurfacesDlg::VisuGUI_IsoSurfacesDlg (SalomeApp_Module* theModule)
253   : VisuGUI_ScalarBarBaseDlg(theModule)
254 {
255   setWindowTitle(tr("DEFINE_ISOSURFACES"));
256   setSizeGripEnabled( TRUE );
257
258   QVBoxLayout* TopLayout = new QVBoxLayout(this);
259   TopLayout->setSpacing( 6 );
260   TopLayout->setMargin(11);
261
262   myTabBox = new QTabWidget(this);
263   myIsoPane = new  VisuGUI_IsoSurfPane(this, GetScalarPane());
264   if ( myIsoPane->layout() )
265     myIsoPane->layout()->setMargin( 5 );
266   myTabBox->addTab(myIsoPane, "Iso Surface");
267   myInputPane = new VisuGUI_InputPane(VISU::TISOSURFACES, theModule, this);
268   myTabBox->addTab(GetScalarPane(), "Scalar Bar");
269   myTabBox->addTab(myInputPane, "Input");
270
271   TopLayout->addWidget(myTabBox);
272
273   QGroupBox* GroupButtons = new QGroupBox( this );
274   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) );
275   //GroupButtons->setColumnLayout(0, Qt::Vertical );
276   //GroupButtons->layout()->setSpacing( 0 );
277   //GroupButtons->layout()->setMargin( 0 );
278   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons );
279   GroupButtonsLayout->setAlignment( Qt::AlignTop );
280   GroupButtonsLayout->setSpacing( 6 );
281   GroupButtonsLayout->setMargin( 11 );
282
283   QPushButton* buttonOk = new QPushButton( tr( "BUT_OK" ), GroupButtons );
284   buttonOk->setAutoDefault( TRUE );
285   buttonOk->setDefault( TRUE );
286   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
287   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
288   QPushButton* buttonCancel = new QPushButton( tr( "BUT_CANCEL" ) , GroupButtons );
289   buttonCancel->setAutoDefault( TRUE );
290   GroupButtonsLayout->addWidget( buttonCancel, 0, 2 );
291   QPushButton* buttonHelp = new QPushButton( tr( "BUT_HELP" ) , GroupButtons );
292   buttonHelp->setAutoDefault( TRUE );
293   GroupButtonsLayout->addWidget( buttonHelp, 0, 3 );
294
295   TopLayout->addWidget(GroupButtons);
296
297   // signals and slots connections
298   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
299   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
300   connect( buttonHelp,   SIGNAL( clicked() ), this, SLOT( onHelp() ) );
301 }
302
303 VisuGUI_IsoSurfacesDlg::~VisuGUI_IsoSurfacesDlg()
304 {}
305
306 void VisuGUI_IsoSurfacesDlg::accept()
307 {
308   if ( myIsoPane->check() )
309     VisuGUI_ScalarBarBaseDlg::accept();
310 }
311
312 void VisuGUI_IsoSurfacesDlg::initFromPrsObject( VISU::ColoredPrs3d_i* thePrs,
313                                                 bool theInit )
314 {
315   if( theInit )
316     myPrsCopy = VISU::TSameAsFactory<VISU::TISOSURFACES>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
317
318   VisuGUI_ScalarBarBaseDlg::initFromPrsObject(myPrsCopy, theInit);
319
320   myIsoPane->initFromPrsObject(myPrsCopy);
321
322   if( !theInit )
323     return;
324
325   myInputPane->initFromPrsObject( myPrsCopy );
326   myTabBox->setCurrentIndex( 0 );
327 }
328
329 int VisuGUI_IsoSurfacesDlg::storeToPrsObject(VISU::ColoredPrs3d_i* thePrs)
330 {
331   if(!myInputPane->check() || !GetScalarPane()->check())
332     return 0;
333   
334   int anIsOk = myInputPane->storeToPrsObject( myPrsCopy );
335   anIsOk &= GetScalarPane()->storeToPrsObject( myPrsCopy );
336   anIsOk &= myIsoPane->storeToPrsObject( myPrsCopy );
337
338   VISU::TSameAsFactory<VISU::TISOSURFACES>().Copy(myPrsCopy, thePrs);
339
340   return anIsOk;
341 }
342
343 QString VisuGUI_IsoSurfacesDlg::GetContextHelpFilePath()
344 {
345   return "iso_surfaces_page.html";
346 }