]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxGroupBox.cxx
Salome HOME
Bug fix for SSPP10924
[modules/gui.git] / src / Qtx / QtxGroupBox.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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/
18 //
19 // File:      QtxGroupBox.cxx
20 // Author:    Sergey TELKOV
21
22 #include "QtxGroupBox.h"
23
24 #include <qhbox.h>
25 #include <qlayout.h>
26 #include <qtoolbutton.h>
27 #include <qapplication.h>
28 #include <qobjectlist.h>
29
30 QtxGroupBox::QtxGroupBox( QWidget* parent, const char* name )
31 : QGroupBox( parent, name ),
32 myContainer( 0 )
33 {
34 }
35
36 QtxGroupBox::QtxGroupBox( const QString& title, QWidget* parent, const char* name )
37 : QGroupBox( title, parent, name ),
38 myContainer( 0 )
39 {
40   initialize();
41 }
42
43 QtxGroupBox::QtxGroupBox( int strips, Orientation o, QWidget* parent, const char* name )
44 : QGroupBox( strips, o, parent, name ),
45 myContainer( 0 )
46 {
47   initialize();
48 }
49
50 QtxGroupBox::QtxGroupBox( int strips, Orientation o, const QString& title,
51                                           QWidget* parent, const char* name )
52 : QGroupBox( strips, o, title, parent, name ),
53 myContainer( 0 )
54 {
55   initialize();
56 }
57
58 QtxGroupBox::~QtxGroupBox()
59 {
60   delete myContainer;
61 }
62
63 void QtxGroupBox::initialize()
64 {
65   myContainer = new QHBox( this, 0, WStyle_Customize | WStyle_NoBorderEx | WStyle_Tool );
66
67   updateTitle();
68 }
69
70 #if QT_VER < 3
71
72 int QtxGroupBox::insideMargin() const
73 {
74   int m = 0;
75   if ( layout() )
76     m = layout()->margin();
77   return m;
78 }
79
80 int QtxGroupBox::insideSpacing() const
81 {
82   int s = 0;
83   if ( layout() )
84     s = layout()->spacing();
85   return s;
86 }
87
88 void QtxGroupBox::setInsideMargin( int m )
89 {
90   if ( layout() )
91     layout()->setMargin( m );
92 }
93
94 void QtxGroupBox::setInsideSpacing( int s )
95 {
96   if ( layout() )
97     layout()->setSpacing( s );
98 }
99
100 #endif
101
102 void QtxGroupBox::insertTitleWidget( QWidget* wid )
103 {
104   if ( !myContainer )
105     return;
106
107   wid->reparent( myContainer, QPoint( 0, 0 ), true );
108   wid->installEventFilter( this );
109
110   updateTitle();
111 }
112
113 void QtxGroupBox::removeTitleWidget( QWidget* wid )
114 {
115   if ( !myContainer || wid->parentWidget() != myContainer )
116     return;
117
118   wid->reparent( 0, QPoint( 0, 0 ), false );
119   wid->removeEventFilter( this );
120
121   updateTitle();
122 }
123
124 void QtxGroupBox::adjustInsideMargin()
125 {
126   QApplication::sendPostedEvents( myContainer, QEvent::ChildInserted );
127
128   myContainer->resize( myContainer->minimumSizeHint() );
129
130   setInsideMargin( myContainer->height() );
131 }
132
133 void QtxGroupBox::setAlignment( int align )
134 {
135   QGroupBox::setAlignment( align );
136
137   updateTitle();
138 }
139
140 void QtxGroupBox::setTitle( const QString& title )
141 {
142   QGroupBox::setTitle( title );
143
144   updateTitle();
145 }
146
147 void QtxGroupBox::setColumnLayout( int strips, Orientation o )
148 {
149   if ( myContainer )
150     myContainer->reparent( 0, QPoint( 0, 0 ), false );
151
152   QGroupBox::setColumnLayout( strips, o );
153
154   if ( myContainer )
155     myContainer->reparent( this, QPoint( 0, 0 ), false );
156
157   updateTitle();
158 }
159
160 void QtxGroupBox::show()
161 {
162   QGroupBox::show();
163
164   updateTitle();
165 }
166
167 void QtxGroupBox::update()
168 {
169   QGroupBox::update();
170
171   updateTitle();
172 }
173
174 QSize QtxGroupBox::sizeHint() const
175 {
176   QSize sz = QGroupBox::sizeHint();
177
178   int sw = titleSize().width();
179
180   if ( myContainer )
181   {
182     if ( alignment() == AlignCenter )
183       sw += 2 * ( myContainer->width() + 5 );
184     else
185       sw += 1 * ( myContainer->width() + 5 );
186   }
187
188   sw += frameRect().left();
189
190   return QSize( QMAX( sz.width(), sw ), sz.height() );
191 }
192
193 QSize QtxGroupBox::minimumSizeHint() const
194 {
195   QSize sz = QGroupBox::minimumSizeHint();
196
197   int sw = titleSize().width() + myContainer ? myContainer->width() + 5 : 0;
198
199   if ( myContainer )
200   {
201     if ( alignment() == AlignCenter )
202       sw += 2 * ( myContainer->width() + 5 );
203     else
204       sw += 1 * ( myContainer->width() + 5 );
205   }
206
207   sw += frameRect().left();
208
209   return QSize( QMAX( sz.width(), sw ), sz.height() );
210 }
211
212 bool QtxGroupBox::eventFilter( QObject* obj, QEvent* e )
213 {
214   QEvent::Type type = e->type();
215   if ( myContainer && obj->parent() == myContainer &&
216        ( type == QEvent::Show || type == QEvent::ShowToParent ||
217          type == QEvent::Hide || type == QEvent::HideToParent ) )
218     QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
219
220   return QGroupBox::eventFilter( obj, e );
221 }
222
223 void QtxGroupBox::resizeEvent( QResizeEvent* e )
224 {
225   QGroupBox::resizeEvent( e );
226
227   updateTitle();
228 }
229
230 void QtxGroupBox::childEvent( QChildEvent* e )
231 {
232   if ( e->type() == QEvent::ChildInserted && e->child() == myContainer )
233     return;
234
235   QGroupBox::childEvent( e );
236 }
237
238 void QtxGroupBox::customEvent( QCustomEvent* )
239 {
240   updateTitle();
241 }
242
243 void QtxGroupBox::frameChanged()
244 {
245   updateTitle();
246 }
247
248 QSize QtxGroupBox::titleSize() const
249 {
250   QSize sz( 0, 0 );
251
252   if ( layout() )
253   {
254     QSpacerItem* si = 0;
255     for ( QLayoutIterator it = layout()->iterator(); it.current() && !si; ++it )
256       si = it.current()->spacerItem();
257     if ( si )
258       sz = si->sizeHint();
259   }
260
261   int w = sz.width();
262   int h = sz.height() + insideMargin();
263
264   return QSize( w, h );
265 }
266
267 void QtxGroupBox::updateTitle()
268 {
269   if ( !myContainer )
270     return;
271
272   int align = alignment();
273   if ( align == AlignAuto )
274     align = QApplication::reverseLayout() ? AlignRight : AlignLeft;
275
276   if ( title().isEmpty() )
277     align = AlignRight;
278
279   QSize ts = titleSize();
280
281   int m = 5;
282
283   int w = frameRect().width() - ts.width();
284   if ( align == AlignCenter )
285     w = w / 2;
286
287   w -= m;
288
289   QApplication::sendPostedEvents( myContainer, QEvent::ChildInserted );
290   myContainer->resize( myContainer->minimumSizeHint() );
291
292   bool vis = false;
293   const QObjectList* list = myContainer->children();
294   if ( list )
295   {
296     for ( QObjectListIt it( *list ); it.current() && !vis; ++it )
297       vis = it.current()->isWidgetType() &&
298             ((QWidget*)it.current())->isVisibleTo( myContainer );
299   }
300
301   if ( myContainer->height() > ts.height() || myContainer->width() > w || !vis )
302     myContainer->hide();
303   else
304   {
305     int x = 0;
306     if ( align == AlignRight )
307       x = frameRect().left() + m;
308     else
309       x = frameRect().right() - myContainer->width() - m;
310
311     int y = frameRect().top() - ( myContainer->height() - frameWidth() ) / 2;
312
313     QPoint pos( x, QMAX( 0, y ) );
314     pos = mapToGlobal( pos );
315     if ( myContainer->parentWidget() )
316       pos = myContainer->parentWidget()->mapFromGlobal( pos );
317     myContainer->move( pos );
318     myContainer->show();
319   }
320
321   updateGeometry();
322 }