Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/visu.git] / src / VISUGUI / VisuGUI_Panel.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_Panel.cxx
24 //  Author : Oleg Uvarov
25 //  Module : VISU
26 //
27 #include "VisuGUI_Panel.h"
28
29 #include "VisuGUI.h"
30 #include "VisuGUI_Tools.h"
31
32 #include "SUIT_ResourceMgr.h"
33
34 #include <QScrollArea>
35 #include <QHBoxLayout>
36 #include <QVBoxLayout>
37 #include <QApplication>
38 #include <QPushButton>
39
40 /*!
41   \class MainFrame
42   \internal
43   \brief Frame inserted in viewport with redefined sizeHint method 
44          in order to avoid unreasonable increasing of viewport size
45 */
46 class VisuGUI_Panel::MainFrame : public QFrame
47 {
48 public:
49   /*!
50     \brief Constructor.
51     \param theParent parent widget
52   */
53   MainFrame( QWidget* theParent = 0 )
54   : QFrame( theParent )
55   {
56   }
57   
58   /*!
59     \brief Gets frame size hint
60     \return frame size hint
61   */
62   virtual QSize sizeHint() const
63   {
64     return minimumSizeHint();
65   }
66 };
67
68 /*!
69   \class VisuGUI_Panel
70   \brief Base class for VISU interactive dockable panels.
71
72   Set of classes is derived from this class and are intended for representing widgets 
73   (GUI controls) for different operations. VisuGUI_Panel consists of main frame 
74   inserted in scroll view and four push buttons. So all widgets of derived sub-panels 
75   should be inherited from mainframe() instead of \93this\94 pointer.
76 */
77
78 /*!
79   \brief Constructor creates panels look and feel
80   \param theName name of the panel
81   \param theParent parent widget
82 */
83 VisuGUI_Panel::VisuGUI_Panel( const QString& theName, 
84                               const VisuGUI* theModule, 
85                               QWidget* theParent,
86                               const int theBtns  )
87   : QtxDockWidget( theName, theParent ),
88     myModule( theModule ),
89     myOK( 0 ),
90     myApply( 0 ),
91     myClose( 0 ),
92     myHelp( 0 )
93 {
94   QWidget* aGrp = new QWidget( this );
95   setWidget( aGrp );
96
97   // Create scroll view
98   myView = new QScrollArea( aGrp );
99
100   // Create main frame
101   myMainFrame = new MainFrame( myView );
102   myMainFrame->setFrameStyle( QFrame::Plain | QFrame::NoFrame );
103   
104   myView->setWidget( myMainFrame );
105   myView->setAlignment( Qt::AlignCenter );
106   myView->setWidgetResizable( true );
107   myView->setMinimumWidth( myMainFrame->sizeHint().width() + 22 );
108   
109   // Create buttons
110   QWidget* aBtnWg = new QWidget( aGrp );
111   QHBoxLayout* aBtnWgLayout = new QHBoxLayout( aBtnWg );
112
113   aBtnWgLayout->addStretch();
114
115   if( theBtns & OKBtn )
116   {
117     myOK = new QPushButton( tr( "BUT_OK" ), aBtnWg );
118     aBtnWgLayout->addWidget( myOK );
119     connect( myOK, SIGNAL( clicked() ), SLOT( onOK() ) );
120   }
121   if( theBtns & ApplyBtn )
122   {
123     myApply = new QPushButton( tr( "BUT_APPLY" ), aBtnWg );
124     aBtnWgLayout->addWidget( myApply );
125     connect( myApply, SIGNAL( clicked() ), SLOT( onApply() ) );
126   }
127   if( theBtns & CloseBtn )
128   {
129     myClose = new QPushButton( tr( "BUT_CLOSE" ), aBtnWg );
130     aBtnWgLayout->addWidget( myClose );
131     connect( myClose, SIGNAL( clicked() ), SLOT( onClose() ) );
132   }
133   if( theBtns & HelpBtn )
134   {
135     myHelp = new QPushButton( tr( "BUT_HELP" ), aBtnWg );
136     aBtnWgLayout->addWidget( myHelp );
137     connect( myHelp, SIGNAL( clicked() ), SLOT( onHelp() ) );
138   }
139
140   aBtnWgLayout->addStretch();
141
142   // fill layout
143   QVBoxLayout* aLay = new QVBoxLayout( aGrp );
144   aLay->setContentsMargins( 0, 0, 0, 0 );
145   aLay->addWidget( myView, 1 );
146   aLay->addWidget( aBtnWg );
147 }
148
149 /*!
150   \brief Destructor
151 */
152 VisuGUI_Panel::~VisuGUI_Panel()
153 {
154 }
155
156 /*!
157   \brief Verifies validity of input data
158
159   This virtual method should be redefined in derived classes. Usually operator 
160   corresponding to the sub-panel calls this method to check validity of input 
161   data when Apply/OK button is pressed.
162
163   \param theErrMsg Error message. 
164   
165         If data is invalid when panel can return message using this parameter given 
166         clear explanation what is wrong
167
168   \return TRUE if data is valid, FALSE otherwise 
169 */
170 bool VisuGUI_Panel::isValid( QString& /*theErrMsg*/ )
171 {
172   return true;
173 }
174 /*!
175   \brief Virtual methods should be redefined in derived classes and 
176          clears all GUI controls
177 */
178 void VisuGUI_Panel::clear()
179 {
180 }
181
182 /*!
183   \brief Virtual slot called when \93OK\94 button pressed emits corresponding signal.
184
185   This slot moves focus in OK button before emitting signal. Mainly it provides 
186   application with correct moving data from currently edited controls to internal 
187   structure. For example QTable moves data from cell editor to table item when 
188   focus is out.
189
190 */
191 void VisuGUI_Panel::onOK()
192 {
193   if ( myOK )
194   {
195     myOK->setFocus();
196     qApp->processEvents();
197   }
198 }
199
200 /*!
201   \brief Virtual slot called when \93Apply\94 button pressed emits corresponding signal.
202   \sa onOK
203 */
204 void VisuGUI_Panel::onApply()
205 {
206   if ( myApply )
207   {
208     myApply->setFocus();
209     qApp->processEvents();
210   }
211 }
212
213 /*!
214   \brief Virtual slot called when \93Close\94 button pressed emits corresponding signal.
215   \sa onOK
216 */
217 void VisuGUI_Panel::onClose()
218 {
219   if ( myClose )
220     myClose->setFocus();
221 }
222
223 /*!
224   \brief Virtual slot called when \93Help\94 button pressed emits corresponding signal.
225   \sa onOK
226 */
227 void VisuGUI_Panel::onHelp()
228 {
229   if ( myHelp )
230     myHelp->setFocus();
231 }
232
233 /*!
234   \brief Gets frame inserted in scroll view. All controls of derived 
235          panels should use it as parent
236   \return QFrame* object 
237 */
238 QFrame* VisuGUI_Panel::mainFrame()
239 {
240   return myMainFrame;
241 }