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