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