Salome HOME
Prevent exception at shape selection if no mesh was pre-selected
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_PrecisionDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH 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   : SMESHGUI_PrecisionDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_PrecisionDlg.h"
29 #include "QAD_Config.h"
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_VTKUtils.h"
32
33 #include <qgroupbox.h>
34 #include <qpushbutton.h>
35 #include <qcheckbox.h>
36 #include <qspinbox.h>
37 #include <qlayout.h>
38 #include <qlabel.h>
39
40 #define SPACING 5                  
41 #define MARGIN  10
42 #define DEFAULT_VAL 10
43 #define RANGE 128
44
45 /*
46   Class       : SMESHGUI_PrecisionDlg
47   Description : Dialog to specify precision of mesh quality controls
48 */
49
50 //=======================================================================
51 // name    : SMESHGUI_PrecisionDlg::SMESHGUI_PrecisionDlg
52 // Purpose : Constructor
53 //=======================================================================
54 SMESHGUI_PrecisionDlg::SMESHGUI_PrecisionDlg( QWidget* theParent )
55 : QDialog( theParent, "SMESHGUI_PrecisionDlg", true,
56            WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
57 {
58   setCaption( tr( "CAPTION" ) );
59
60   QVBoxLayout* aDlgLay = new QVBoxLayout( this, MARGIN, SPACING );
61
62   QFrame* aMainFrame = createMainFrame  ( this );
63   QFrame* aBtnFrame  = createButtonFrame( this );
64
65   aDlgLay->addWidget( aMainFrame );
66   aDlgLay->addWidget( aBtnFrame );
67
68   aDlgLay->setStretchFactor( aMainFrame, 1 );
69
70   setMinimumWidth( (int)( QFontMetrics( font() ).width( tr( "CAPTION" ) ) * 1.5 ) );
71
72   Init();
73
74 }
75
76 //=======================================================================
77 // name    : SMESHGUI_PrecisionDlg::~SMESHGUI_PrecisionDlg
78 // Purpose : Destructor
79 //=======================================================================
80 SMESHGUI_PrecisionDlg::~SMESHGUI_PrecisionDlg()
81 {
82 }
83
84 //=======================================================================
85 // name    : SMESHGUI_PrecisionDlg::createButtonFrame
86 // Purpose : Create frame containing buttons
87 //=======================================================================
88 QFrame* SMESHGUI_PrecisionDlg::createButtonFrame( QWidget* theParent )
89 {
90   QGroupBox* aGrp = new QGroupBox( 1, Qt::Vertical, theParent );
91   aGrp->setFrameStyle( QFrame::NoFrame );
92   aGrp->setInsideMargin( 0 );
93
94   myOKBtn = new QPushButton( tr( "SMESH_BUT_OK"    ), aGrp );
95
96   QLabel* aLbl = new QLabel( aGrp );
97   aLbl->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
98
99   myCancelBtn = new QPushButton( tr( "SMESH_BUT_CANCEL" ), aGrp );
100
101   connect( myOKBtn, SIGNAL( clicked() ), SLOT( onOk() ) );
102   connect( myCancelBtn, SIGNAL( clicked() ), SLOT( onClose() ) );
103
104   return aGrp;
105 }
106
107 //=======================================================================
108 // name    : SMESHGUI_PrecisionDlg:: createMainFrame
109 // Purpose : Create frame containing dialog's input fields
110 //=======================================================================
111 QFrame* SMESHGUI_PrecisionDlg::createMainFrame( QWidget* theParent )
112 {
113   QGroupBox* aGrp = new QGroupBox( 2, Qt::Horizontal, theParent );
114   new QLabel( tr( "PRECISION" ), aGrp );
115   mySpinBox = new QSpinBox( 0, RANGE, 1, aGrp );
116   myNotUseChk = new QCheckBox( tr( "NOT_USE" ), aGrp );
117
118   connect( myNotUseChk, SIGNAL( toggled( bool ) ), SLOT( onNotUse() ) );
119
120   return aGrp;
121 }
122
123 //=======================================================================
124 // name    : SMESHGUI_PrecisionDlg::Init
125 // Purpose : Initialize dialog fields
126 //=======================================================================
127 void SMESHGUI_PrecisionDlg::Init()
128 {
129   if ( QAD_CONFIG->hasSetting( "SMESH:ControlsPrecision" ) )
130   {
131     QString aStr = QAD_CONFIG->getSetting( "SMESH:ControlsPrecision" );
132     bool isOk = false;
133     int aVal = aStr.toInt( &isOk );
134     mySpinBox->setValue( isOk ? aVal : DEFAULT_VAL );
135     myNotUseChk->setChecked( !isOk );
136   }
137   else
138   {
139     mySpinBox->setValue( DEFAULT_VAL );
140     myNotUseChk->setChecked( true );
141   }
142
143   onNotUse();
144
145   SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI();
146   aSMESHGUI->SetActiveDialogBox( ( QDialog* )this ) ;
147   connect( aSMESHGUI, SIGNAL( SignalCloseAllDialogs() ), SLOT( onClose() ) );
148 }
149
150 //=======================================================================
151 // name    : SMESHGUI_PrecisionDlg::onOk
152 // Purpose : SLOT. Called when OK button pressed
153 //=======================================================================
154 void SMESHGUI_PrecisionDlg::onOk()
155 {
156   if ( myNotUseChk->isChecked() )
157   {
158     QAD_CONFIG->removeSettings( "SMESH:ControlsPrecision" );
159     SMESH::SetControlsPrecision( -1 );
160   }
161   else
162   {
163     mySpinBox->clearFocus();
164     int aVal = mySpinBox->value();
165     QAD_CONFIG->addSetting( "SMESH:ControlsPrecision", QString( "%1" ).arg( aVal ) );
166     SMESH::SetControlsPrecision( aVal );
167   }
168   
169   disconnect( SMESHGUI::GetSMESHGUI(), 0, this, 0 );
170   SMESHGUI::GetSMESHGUI()->ResetState() ;
171   accept();
172 }
173
174 //=======================================================================
175 // name    : SMESHGUI_PrecisionDlg::onClose
176 // Purpose : SLOT. Called when "Cancel" button pressed
177 //=======================================================================
178 void SMESHGUI_PrecisionDlg::onClose()
179 {
180   disconnect( SMESHGUI::GetSMESHGUI(), 0, this, 0 );
181   reject();
182 }
183
184 //=================================================================================
185 // function : SMESHGUI_PrecisionDlg::closeEvent()
186 // purpose  :
187 //=================================================================================
188 void SMESHGUI_PrecisionDlg::closeEvent( QCloseEvent* )
189 {
190   onClose();
191 }
192
193 //=======================================================================
194 // name    : SMESHGUI_PrecisionDlg::onClose
195 // Purpose : SLOT. Called when state of "Do not use" check box changed
196 //=======================================================================
197 void SMESHGUI_PrecisionDlg::onNotUse()
198 {
199   mySpinBox->setEnabled( !myNotUseChk->isChecked() );
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221