| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: card.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementing your own layout: flow example
|
|---|
| 5 | **
|
|---|
| 6 | ** Copyright (C) 1996 by Trolltech AS. All rights reserved.
|
|---|
| 7 | **
|
|---|
| 8 | ** This file is part of an example program for Qt. This example
|
|---|
| 9 | ** program may be used, distributed and modified without limitation.
|
|---|
| 10 | **
|
|---|
| 11 | *****************************************************************************/
|
|---|
| 12 |
|
|---|
| 13 | #include "card.h"
|
|---|
| 14 |
|
|---|
| 15 | class CardLayoutIterator :public QGLayoutIterator
|
|---|
| 16 | {
|
|---|
| 17 | public:
|
|---|
| 18 | CardLayoutIterator( QPtrList<QLayoutItem> *l )
|
|---|
| 19 | : idx( 0 ), list( l ) {}
|
|---|
| 20 |
|
|---|
| 21 | QLayoutItem *current();
|
|---|
| 22 | QLayoutItem *next();
|
|---|
| 23 | QLayoutItem *takeCurrent();
|
|---|
| 24 |
|
|---|
| 25 | private:
|
|---|
| 26 | int idx;
|
|---|
| 27 | QPtrList<QLayoutItem> *list;
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | QLayoutItem *CardLayoutIterator::current()
|
|---|
| 31 | {
|
|---|
| 32 | return idx < int( list->count() ) ? list->at( idx ) : 0;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | QLayoutItem *CardLayoutIterator::next()
|
|---|
| 36 | {
|
|---|
| 37 | idx++; return current();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | QLayoutItem *CardLayoutIterator::takeCurrent()
|
|---|
| 41 | {
|
|---|
| 42 | return idx < int( list->count() ) ?list->take( idx ) : 0;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | QLayoutIterator CardLayout::iterator()
|
|---|
| 48 | {
|
|---|
| 49 | return QLayoutIterator( new CardLayoutIterator( &list ) );
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | CardLayout::~CardLayout()
|
|---|
| 53 | {
|
|---|
| 54 | deleteAllItems();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void CardLayout::addItem( QLayoutItem *item )
|
|---|
| 58 | {
|
|---|
| 59 | list.append( item );
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void CardLayout::setGeometry( const QRect &rct )
|
|---|
| 63 | {
|
|---|
| 64 | QLayout::setGeometry( rct );
|
|---|
| 65 |
|
|---|
| 66 | QPtrListIterator<QLayoutItem> it( list );
|
|---|
| 67 | if ( it.count() == 0 )
|
|---|
| 68 | return;
|
|---|
| 69 |
|
|---|
| 70 | QLayoutItem *o;
|
|---|
| 71 |
|
|---|
| 72 | int i = 0;
|
|---|
| 73 |
|
|---|
| 74 | int w = rct.width() - ( list.count() - 1 ) * spacing();
|
|---|
| 75 | int h = rct.height() - ( list.count() - 1 ) * spacing();
|
|---|
| 76 |
|
|---|
| 77 | while ( ( o=it.current() ) != 0 ) {
|
|---|
| 78 | ++it;
|
|---|
| 79 | QRect geom( rct.x() + i * spacing(), rct.y() + i * spacing(),
|
|---|
| 80 | w, h );
|
|---|
| 81 | o->setGeometry( geom );
|
|---|
| 82 | ++i;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | QSize CardLayout::sizeHint() const
|
|---|
| 87 | {
|
|---|
| 88 | QSize s(0,0);
|
|---|
| 89 | int n = list.count();
|
|---|
| 90 | if ( n > 0 )
|
|---|
| 91 | s = QSize(100,70); //start with a nice default size
|
|---|
| 92 | QPtrListIterator<QLayoutItem> it(list);
|
|---|
| 93 | QLayoutItem *o;
|
|---|
| 94 | while ( (o=it.current()) != 0 ) {
|
|---|
| 95 | ++it;
|
|---|
| 96 | s = s.expandedTo( o->minimumSize() );
|
|---|
| 97 | }
|
|---|
| 98 | return s + n*QSize(spacing(),spacing());
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | QSize CardLayout::minimumSize() const
|
|---|
| 102 | {
|
|---|
| 103 | QSize s(0,0);
|
|---|
| 104 | int n = list.count();
|
|---|
| 105 | QPtrListIterator<QLayoutItem> it(list);
|
|---|
| 106 | QLayoutItem *o;
|
|---|
| 107 | while ( (o=it.current()) != 0 ) {
|
|---|
| 108 | ++it;
|
|---|
| 109 | s = s.expandedTo( o->minimumSize() );
|
|---|
| 110 | }
|
|---|
| 111 | return s + n*QSize(spacing(),spacing());
|
|---|
| 112 | }
|
|---|