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