QtFlapBox.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "QtFlapBox.hh"
00054
00055 #include <QLayout>
00056 #include <QLabel>
00057 #include <QScrollBar>
00058 #include <QResizeEvent>
00059 #include <QSize>
00060 #include <iostream>
00061
00062
00063
00064 namespace ACG {
00065 namespace QtWidgets {
00066
00067
00068
00069
00070 QtFlapBox::QtFlapBox( QWidget * _parent, Qt::WindowFlags )
00071 : QScrollArea( _parent )
00072 {
00073 setBackgroundRole(QPalette::Dark);
00074
00075 QSizePolicy sp = sizePolicy();
00076 sp.setHorizontalPolicy( QSizePolicy::MinimumExpanding );
00077 sp.setVerticalPolicy( QSizePolicy::Preferred );
00078 sp.setHorizontalStretch( 1 );
00079 sp.setVerticalStretch( 0 );
00080 setSizePolicy( sp );
00081
00082
00083 container = new QWidget;
00084 container->setBackgroundRole( QPalette::Button );
00085
00086
00087
00088 boxlayout = new QVBoxLayout( container );
00089 boxlayout->setMargin(0);
00090 boxlayout->setSpacing(0);
00091
00092
00093 setWidget( container );
00094
00095 relayout();
00096 }
00097
00098
00099
00100
00101
00102 QtFlapBox::~QtFlapBox()
00103 {
00104 }
00105
00106
00107
00108
00109
00110 int
00111 QtFlapBox::addItem( QWidget * _widget,
00112 const QIcon & _icon,
00113 const QString & _text )
00114 {
00115 return insertItem( -1, _widget, _icon, _text );
00116 }
00117
00118
00119
00120
00121
00122 int
00123 QtFlapBox::addItem( QWidget * _widget, const QString & _text )
00124 {
00125 return insertItem( -1, _widget, QIcon(), _text);
00126 }
00127
00128
00129
00130
00131
00132 int
00133 QtFlapBox::count() const
00134 {
00135 return flapList.count();
00136 }
00137
00138
00139
00140
00141
00142 int
00143 QtFlapBox::indexOf( QWidget * _widget ) const
00144 {
00145 Flap * f = flap( _widget );
00146 return ( f ? flapList.indexOf( *f ) : -1 );
00147 }
00148
00149
00150
00151
00152
00153 int
00154 QtFlapBox::insertItem( int _index,
00155 QWidget * _widget,
00156 const QIcon & _icon,
00157 const QString & _text )
00158 {
00159 if ( ! _widget )
00160 return -1;
00161
00162 connect( _widget, SIGNAL( destroyed( QObject * ) ),
00163 this, SLOT( widgetDestroyed( QObject * ) ) );
00164
00165 Flap f;
00166 f.button = new QPushButton;
00167
00168 connect( f.button, SIGNAL( clicked() ),
00169 this, SLOT( buttonClicked() ) );
00170
00171 f.widget = _widget;
00172 f.widget->setParent( container );
00173 f.widget->hide();
00174
00175 f.setText( _text );
00176 f.setIcon( _icon );
00177
00178 if ( _index < 0 || _index >= flapList.count() )
00179 {
00180 _index = flapList.count();
00181 flapList.append( f );
00182 boxlayout->addWidget( f.button );
00183 boxlayout->addWidget( f.widget );
00184 relayout();
00185 }
00186 else
00187 {
00188 flapList.insert( _index, f );
00189 relayout();
00190 }
00191
00192 f.button->show();
00193
00194
00195
00196
00197 return _index;
00198
00199 }
00200
00201
00202
00203
00204
00205
00206 int
00207 QtFlapBox::insertItem( int _index,
00208 QWidget * _widget,
00209 const QString & _text )
00210 {
00211 return insertItem( _index, _widget, QIcon(), _text );
00212 }
00213
00214
00215
00216
00217
00218 bool
00219 QtFlapBox::isItemEnabled( int _index ) const
00220 {
00221 const Flap * f = flap( _index );
00222 return f && f->button->isEnabled();
00223 }
00224
00225
00226
00227
00228
00229 QIcon
00230 QtFlapBox::itemIcon( int _index ) const
00231 {
00232 const Flap * f = flap( _index );
00233 return ( f ? f->icon() : QIcon() );
00234 }
00235
00236
00237
00238
00239 QString
00240 QtFlapBox::itemText( int _index ) const
00241 {
00242 const Flap * f = flap( _index );
00243 return ( f ? f->text() : QString() );
00244 }
00245
00246
00247
00248
00249
00250 QString
00251 QtFlapBox::itemToolTip( int _index ) const
00252 {
00253 const Flap * f = flap( _index );
00254 return ( f ? f->toolTip() : QString() );
00255 }
00256
00257
00258
00259
00260
00261 void
00262 QtFlapBox::removeItem( int _index )
00263 {
00264 if ( QWidget * w = widget( _index ) )
00265 {
00266 disconnect( w, SIGNAL( destroyed( QObject * ) ),
00267 this, SLOT( widgetDestroyed( QObject * ) ) );
00268 w->setParent( this );
00269
00270 widgetDestroyed( w );
00271
00272 }
00273 }
00274
00275
00276
00277
00278
00279 void
00280 QtFlapBox::setItemEnabled( int _index, bool _enabled )
00281 {
00282 Flap * f = flap( _index );
00283 if ( ! f )
00284 return;
00285
00286 f->button->setEnabled( _enabled );
00287 }
00288
00289
00290
00291
00292
00293 void
00294 QtFlapBox::setItemIcon( int _index, const QIcon & _icon )
00295 {
00296 Flap * f = flap( _index );
00297 if ( f )
00298 f->setIcon( _icon );
00299 }
00300
00301
00302
00303
00304
00305 void
00306 QtFlapBox::setItemText( int _index, const QString & _text )
00307 {
00308 Flap * f = flap( _index );
00309 if ( f )
00310 f->setText( _text );
00311 }
00312
00313
00314
00315
00316
00317 void
00318 QtFlapBox::setItemToolTip( int _index, const QString & _tip )
00319 {
00320 Flap * f = flap( _index );
00321 if ( f )
00322 f->setToolTip( _tip );
00323 }
00324
00325
00326
00327
00328
00329 QWidget *
00330 QtFlapBox::widget( int _index ) const
00331 {
00332 if ( _index < 0 || _index >= (int) flapList.size() )
00333 return 0;
00334
00335 return flapList.at( _index ).widget;
00336 }
00337
00338
00339
00340
00341
00342 bool
00343 QtFlapBox::isItemHidden( int _index ) const
00344 {
00345 const Flap * f = flap( _index );
00346
00347 if ( ! f )
00348 return false;
00349
00350 return f->widget->isHidden();
00351 }
00352
00353
00354
00355
00356
00357 void
00358 QtFlapBox::setItemHidden( int _index, bool _hidden )
00359 {
00360 Flap * f = flap( _index );
00361 if ( ! f )
00362 return;
00363
00364 f->widget->setHidden( _hidden );
00365 }
00366
00367
00368
00369
00370
00371 QSize
00372 QtFlapBox::sizeHint() const
00373 {
00374 int width_hint = 0;
00375 int height_hint = 0;
00376
00377 for ( FlapList::ConstIterator i = flapList.constBegin();
00378 i != flapList.constEnd(); ++i)
00379 {
00380 QWidget * w = i->widget;
00381 QSize ws = w->sizeHint();
00382
00383 QPushButton * b = i->button;
00384 QSize bs = b->sizeHint();
00385
00386 if ( bs.width() > width_hint )
00387 width_hint = bs.width();
00388
00389 if ( ! w->isHidden() )
00390 {
00391 height_hint += ws.height();
00392 if ( ws.width() > width_hint )
00393 width_hint = ws.width();
00394 }
00395
00396 height_hint += bs.height();
00397 }
00398
00399
00400 QSize sz( width_hint + verticalScrollBar()->sizeHint().width()
00401 + 2 * frameWidth(),
00402 height_hint );
00403
00404 return sz;
00405 }
00406
00407
00408
00409
00410
00411 QtFlapBox::Flap *
00412 QtFlapBox::flap( QWidget * _widget ) const
00413 {
00414 if ( ! _widget )
00415 return 0;
00416
00417 for ( FlapList::ConstIterator i = flapList.constBegin();
00418 i != flapList.constEnd(); ++i )
00419 if ( (*i).widget == _widget )
00420 return (Flap*) &(*i);
00421
00422 return 0;
00423 }
00424
00425
00426
00427
00428
00429 const QtFlapBox::Flap *
00430 QtFlapBox::flap( int _index ) const
00431 {
00432 if ( _index >= 0 && _index < flapList.size() )
00433 return &flapList.at( _index );
00434 return 0;
00435 }
00436
00437
00438
00439
00440
00441 QtFlapBox::Flap *
00442 QtFlapBox::flap( int _index )
00443 {
00444 if ( _index >= 0 && _index < flapList.size() )
00445 return &flapList[ _index ];
00446 return 0;
00447 }
00448
00449
00450
00451
00452
00453
00454 void updateFlaps()
00455 {
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 }
00474
00475
00476
00477
00478
00479 void
00480 QtFlapBox::relayout()
00481 {
00482 delete boxlayout;
00483 boxlayout = new QVBoxLayout;
00484 boxlayout->setMargin(0);
00485 boxlayout->setSpacing(0);
00486
00487 for ( FlapList::ConstIterator i = flapList.constBegin();
00488 i != flapList.constEnd(); ++i )
00489 {
00490 boxlayout->addWidget( (*i).button );
00491 boxlayout->addWidget( (*i).widget );
00492 }
00493
00494 container->setLayout( boxlayout );
00495
00496 QSize area_size = size();
00497 QSize cont_size = container->sizeHint();
00498 QSize s;
00499 s.setWidth( area_size.width() );
00500 s.setHeight( cont_size.height() );
00501
00502 container->resize( s );
00503
00504
00505 updateGeometry();
00506 }
00507
00508
00509
00510
00511
00512 void
00513 QtFlapBox::buttonClicked()
00514 {
00515 QPushButton * b = ::qobject_cast< QPushButton * >( sender() );
00516
00517 for ( FlapList::ConstIterator i = flapList.constBegin();
00518 i != flapList.constEnd(); ++i)
00519 if ( i->button == b )
00520 {
00521 QWidget * w = i->widget;
00522
00523 if ( w->isHidden() )
00524 w->show();
00525 else
00526 w->hide();
00527
00528 relayout();
00529 break;
00530 }
00531
00532 }
00533
00534
00535
00536
00537
00538 void
00539 QtFlapBox::widgetDestroyed( QObject * _object )
00540 {
00541
00542 QWidget * p = ( QWidget * ) _object;
00543
00544 Flap * f = flap( p );
00545
00546 if ( !p || !f )
00547 return;
00548
00549 boxlayout->removeWidget( f->widget );
00550 boxlayout->removeWidget( f->button );
00551 delete f->button;
00552
00553 flapList.removeAll( *f );
00554 }
00555
00556
00557
00558
00559
00560 void
00561 QtFlapBox::resizeEvent( QResizeEvent * _event )
00562 {
00563 QSize container_size = container->size();
00564
00565 container_size.setWidth( _event->size().width() );
00566
00567 container->resize( container_size );
00568
00569 QScrollArea::resizeEvent( _event );
00570 }
00571
00572
00573
00574
00575
00576
00577
00578 }
00579 }
00580