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