Developer Documentation
QtFlapBox.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS QtFlapBox - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 //== INCLUDES =================================================================
59 
60 #include "QtFlapBox.hh"
61 
62 #include <QScrollBar>
63 
64 //== NAMESPACES ===============================================================
65 
66 namespace ACG {
67 namespace QtWidgets {
68 
69 //== IMPLEMENTATION ==========================================================
70 
71 
72 QtFlapBox::QtFlapBox( QWidget * _parent, Qt::WindowFlags /* _f */ )
73  : QScrollArea( _parent )
74 {
75  setBackgroundRole(QPalette::Dark);
76  // setSizePolicy( QSizePolicy::Minimum, QSizePolicy::MinimumExpanding );
77  QSizePolicy sp = sizePolicy();
78  sp.setHorizontalPolicy( QSizePolicy::MinimumExpanding );
79  sp.setVerticalPolicy( QSizePolicy::Preferred );
80  sp.setHorizontalStretch( 1 );
81  sp.setVerticalStretch( 0 );
82  setSizePolicy( sp );
83 
84  // create a container that holds all the flaps
85  container = new QWidget;
86  container->setBackgroundRole( QPalette::Button );
87 
88 
89  // flaps are ordered vertically
90  boxlayout = new QVBoxLayout( container );
91  boxlayout->setMargin(0);
92  boxlayout->setSpacing(0);
93 
94  // the container will be scrollable
95  setWidget( container );
96 
97  relayout();
98 }
99 
100 
101 //-----------------------------------------------------------------------------
102 
103 
104 QtFlapBox::~QtFlapBox()
105 {
106 }
107 
108 
109 //-----------------------------------------------------------------------------
110 
111 
112 int
113 QtFlapBox::addItem( QWidget * _widget,
114  const QIcon & _icon,
115  const QString & _text )
116 {
117  return insertItem( -1, _widget, _icon, _text );
118 }
119 
120 
121 //-----------------------------------------------------------------------------
122 
123 
124 int
125 QtFlapBox::addItem( QWidget * _widget, const QString & _text )
126 {
127  return insertItem( -1, _widget, QIcon(), _text);
128 }
129 
130 
131 //-----------------------------------------------------------------------------
132 
133 
134 int
135 QtFlapBox::count() const
136 {
137  return flapList.count();
138 }
139 
140 
141 //-----------------------------------------------------------------------------
142 
143 
144 int
145 QtFlapBox::indexOf( QWidget * _widget ) const
146 {
147  Flap * f = flap( _widget );
148  return ( f ? flapList.indexOf( *f ) : -1 );
149 }
150 
151 
152 //-----------------------------------------------------------------------------
153 
154 
155 int
156 QtFlapBox::insertItem( int _index,
157  QWidget * _widget,
158  const QIcon & _icon,
159  const QString & _text )
160 {
161  if ( ! _widget )
162  return -1;
163 
164  connect( _widget, SIGNAL( destroyed( QObject * ) ),
165  this, SLOT( widgetDestroyed( QObject * ) ) );
166 
167  Flap f;
168  f.button = new QPushButton;//( container );
169  // f.button->setMinimumSize( 200, 20 );
170  connect( f.button, SIGNAL( clicked() ),
171  this, SLOT( buttonClicked() ) );
172 
173  f.widget = _widget;
174  f.widget->setParent( container );
175  f.widget->hide();
176 
177  f.setText( _text );
178  f.setIcon( _icon );
179 
180  if ( _index < 0 || _index >= flapList.count() )
181  {
182  _index = flapList.count();
183  flapList.append( f );
184  boxlayout->addWidget( f.button );
185  boxlayout->addWidget( f.widget );
186  relayout();
187  }
188  else
189  {
190  flapList.insert( _index, f );
191  relayout();
192  }
193 
194  f.button->show();
195 
196 // d->updateTabs();
197 // itemInserted(index);
198 
199  return _index;
200 
201 }
202 
203 
204 //-----------------------------------------------------------------------------
205 
206 
207 
208 int
209 QtFlapBox::insertItem( int _index,
210  QWidget * _widget,
211  const QString & _text )
212 {
213  return insertItem( _index, _widget, QIcon(), _text );
214 }
215 
216 
217 //-----------------------------------------------------------------------------
218 
219 
220 bool
221 QtFlapBox::isItemEnabled( int _index ) const
222 {
223  const Flap * f = flap( _index );
224  return f && f->button->isEnabled();
225 }
226 
227 
228 //-----------------------------------------------------------------------------
229 
230 
231 QIcon
232 QtFlapBox::itemIcon( int _index ) const
233 {
234  const Flap * f = flap( _index );
235  return ( f ? f->icon() : QIcon() );
236 }
237 
238 //-----------------------------------------------------------------------------
239 
240 
241 QString
242 QtFlapBox::itemText( int _index ) const
243 {
244  const Flap * f = flap( _index );
245  return ( f ? f->text() : QString() );
246 }
247 
248 
249 //-----------------------------------------------------------------------------
250 
251 
252 QString
253 QtFlapBox::itemToolTip( int _index ) const
254 {
255  const Flap * f = flap( _index );
256  return ( f ? f->toolTip() : QString() );
257 }
258 
259 
260 //-----------------------------------------------------------------------------
261 
262 
263 void
264 QtFlapBox::removeItem( int _index )
265 {
266  if ( QWidget * w = widget( _index ) )
267  {
268  disconnect( w, SIGNAL( destroyed( QObject * ) ),
269  this, SLOT( widgetDestroyed( QObject * ) ) );
270  w->setParent( this );
271  // destroy internal data
272  widgetDestroyed( w );
273  // itemRemoved( _index );
274  }
275 }
276 
277 
278 //-----------------------------------------------------------------------------
279 
280 
281 void
282 QtFlapBox::setItemEnabled( int _index, bool _enabled )
283 {
284  Flap * f = flap( _index );
285  if ( ! f )
286  return;
287 
288  f->button->setEnabled( _enabled );
289 }
290 
291 
292 //-----------------------------------------------------------------------------
293 
294 
295 void
296 QtFlapBox::setItemIcon( int _index, const QIcon & _icon )
297 {
298  Flap * f = flap( _index );
299  if ( f )
300  f->setIcon( _icon );
301 }
302 
303 
304 //-----------------------------------------------------------------------------
305 
306 
307 void
308 QtFlapBox::setItemText( int _index, const QString & _text )
309 {
310  Flap * f = flap( _index );
311  if ( f )
312  f->setText( _text );
313 }
314 
315 
316 //-----------------------------------------------------------------------------
317 
318 
319 void
320 QtFlapBox::setItemToolTip( int _index, const QString & _tip )
321 {
322  Flap * f = flap( _index );
323  if ( f )
324  f->setToolTip( _tip );
325 }
326 
327 
328 //-----------------------------------------------------------------------------
329 
330 
331 QWidget *
332 QtFlapBox::widget( int _index ) const
333 {
334  if ( _index < 0 || _index >= (int) flapList.size() )
335  return 0;
336 
337  return flapList.at( _index ).widget;
338 }
339 
340 
341 //-----------------------------------------------------------------------------
342 
343 
344 bool
345 QtFlapBox::isItemHidden( int _index ) const
346 {
347  const Flap * f = flap( _index );
348 
349  if ( ! f )
350  return false;
351 
352  return f->widget->isHidden();
353 }
354 
355 
356 //-----------------------------------------------------------------------------
357 
358 
359 void
360 QtFlapBox::setItemHidden( int _index, bool _hidden )
361 {
362  Flap * f = flap( _index );
363  if ( ! f )
364  return;
365 
366  f->widget->setHidden( _hidden );
367 }
368 
369 
370 //-----------------------------------------------------------------------------
371 
372 
373 QSize
374 QtFlapBox::sizeHint() const
375 {
376  int width_hint = 0;
377  int height_hint = 0;
378 
379  for ( FlapList::ConstIterator i = flapList.constBegin();
380  i != flapList.constEnd(); ++i)
381  {
382  QWidget * w = i->widget;
383  QSize ws = w->sizeHint();
384 
385  QPushButton * b = i->button;
386  QSize bs = b->sizeHint();
387 
388  if ( bs.width() > width_hint )
389  width_hint = bs.width();
390 
391  if ( ! w->isHidden() )
392  {
393  height_hint += ws.height();
394  if ( ws.width() > width_hint )
395  width_hint = ws.width();
396  }
397 
398  height_hint += bs.height();
399  }
400 
401 
402  QSize sz( width_hint + verticalScrollBar()->sizeHint().width()
403  + 2 * frameWidth(),
404  height_hint );
405 
406  return sz;
407 }
408 
409 
410 //-----------------------------------------------------------------------------
411 
412 
413 QtFlapBox::Flap *
414 QtFlapBox::flap( QWidget * _widget ) const
415 {
416  if ( ! _widget )
417  return 0;
418 
419  for ( FlapList::ConstIterator i = flapList.constBegin();
420  i != flapList.constEnd(); ++i )
421  if ( (*i).widget == _widget )
422  return (Flap*) &(*i);
423 
424  return 0;
425 }
426 
427 
428 //-----------------------------------------------------------------------------
429 
430 
431 const QtFlapBox::Flap *
432 QtFlapBox::flap( int _index ) const
433 {
434  if ( _index >= 0 && _index < flapList.size() )
435  return &flapList.at( _index );
436  return 0;
437 }
438 
439 
440 //-----------------------------------------------------------------------------
441 
442 
443 QtFlapBox::Flap *
444 QtFlapBox::flap( int _index )
445 {
446  if ( _index >= 0 && _index < flapList.size() )
447  return &flapList[ _index ];
448  return 0;
449 }
450 
451 
452 
453 //-----------------------------------------------------------------------------
454 
455 
456 void updateFlaps()
457 {
458 // QToolBoxButton *lastButton = currentPage ? currentPage->button : 0;
459 // bool after = false;
460 // for (PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i) {
461 // QToolBoxButton *tB = (*i).button;
462 // QWidget *tW = (*i).widget;
463 // if (after) {
464 // QPalette p = tB->palette();
465 // p.setColor(tB->backgroundRole(), tW->palette().color(tW->backgroundRole()));
466 // tB->setPalette(p);
467 // tB->update();
468 // } else if (tB->backgroundRole() != QPalette::Background) {
469 // tB->setBackgroundRole(QPalette::Background);
470 // tB->update();
471 // }
472 // after = (*i).button == lastButton;
473 // }
474 
475 }
476 
477 
478 //-----------------------------------------------------------------------------
479 
480 
481 void
482 QtFlapBox::relayout()
483 {
484  delete boxlayout;
485  boxlayout = new QVBoxLayout;
486  boxlayout->setMargin(0);
487  boxlayout->setSpacing(0);
488 
489  for ( FlapList::ConstIterator i = flapList.constBegin();
490  i != flapList.constEnd(); ++i )
491  {
492  boxlayout->addWidget( (*i).button );
493  boxlayout->addWidget( (*i).widget );
494  }
495 
496  container->setLayout( boxlayout );
497 
498  QSize area_size = size();
499  QSize cont_size = container->sizeHint();
500  QSize s;
501  s.setWidth( area_size.width() );
502  s.setHeight( cont_size.height() );
503 
504  container->resize( s );
505  // resize( sizeHint() );
506 
507  updateGeometry();
508 }
509 
510 
511 //-----------------------------------------------------------------------------
512 
513 
514 void
515 QtFlapBox::buttonClicked()
516 {
517  QPushButton * b = ::qobject_cast< QPushButton * >( sender() );
518 
519  for ( FlapList::ConstIterator i = flapList.constBegin();
520  i != flapList.constEnd(); ++i)
521  if ( i->button == b )
522  {
523  QWidget * w = i->widget;
524 
525  if ( w->isHidden() )
526  w->show();
527  else
528  w->hide();
529 
530  relayout();
531  break;
532  }
533 
534 }
535 
536 
537 //-----------------------------------------------------------------------------
538 
539 
540 void
541 QtFlapBox::widgetDestroyed( QObject * _object )
542 {
543  // no verification - vtbl corrupted already
544  QWidget * p = ( QWidget * ) _object;
545 
546  Flap * f = flap( p );
547 
548  if ( !p || !f )
549  return;
550 
551  boxlayout->removeWidget( f->widget );
552  boxlayout->removeWidget( f->button );
553  delete f->button;
554 
555  flapList.removeAll( *f );
556 }
557 
558 
559 //-----------------------------------------------------------------------------
560 
561 
562 void
563 QtFlapBox::resizeEvent( QResizeEvent * _event )
564 {
565  QSize container_size = container->size();
566 
567  container_size.setWidth( _event->size().width() );
568 
569  container->resize( container_size );
570 
571  QScrollArea::resizeEvent( _event );
572 }
573 
574 
575 //-----------------------------------------------------------------------------
576 
577 
578 
579 //=============================================================================
580 } // namespace QtWidgets
581 } // namespace ACG
582 //=============================================================================
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51