| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: flow.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 "flow.h"
|
|---|
| 14 |
|
|---|
| 15 | class SimpleFlowIterator :public QGLayoutIterator
|
|---|
| 16 | {
|
|---|
| 17 | public:
|
|---|
| 18 | SimpleFlowIterator( QPtrList<QLayoutItem> *l ) :idx(0), list(l) {}
|
|---|
| 19 | uint count() const;
|
|---|
| 20 | QLayoutItem *current();
|
|---|
| 21 | QLayoutItem *next();
|
|---|
| 22 | QLayoutItem *takeCurrent();
|
|---|
| 23 |
|
|---|
| 24 | private:
|
|---|
| 25 | int idx;
|
|---|
| 26 | QPtrList<QLayoutItem> *list;
|
|---|
| 27 |
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | uint SimpleFlowIterator::count() const
|
|---|
| 31 | {
|
|---|
| 32 | return list->count();
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | QLayoutItem *SimpleFlowIterator::current()
|
|---|
| 36 | {
|
|---|
| 37 | return idx < int(count()) ? list->at(idx) : 0;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | QLayoutItem *SimpleFlowIterator::next()
|
|---|
| 41 | {
|
|---|
| 42 | idx++; return current();
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | QLayoutItem *SimpleFlowIterator::takeCurrent()
|
|---|
| 46 | {
|
|---|
| 47 | return idx < int(count()) ? list->take( idx ) : 0;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | SimpleFlow::~SimpleFlow()
|
|---|
| 51 | {
|
|---|
| 52 | deleteAllItems();
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | int SimpleFlow::heightForWidth( int w ) const
|
|---|
| 57 | {
|
|---|
| 58 | if ( cached_width != w ) {
|
|---|
| 59 | //Not all C++ compilers support "mutable" yet:
|
|---|
| 60 | SimpleFlow * mthis = (SimpleFlow*)this;
|
|---|
| 61 | int h = mthis->doLayout( QRect(0,0,w,0), TRUE );
|
|---|
| 62 | mthis->cached_hfw = h;
|
|---|
| 63 | mthis->cached_width = w;
|
|---|
| 64 | return h;
|
|---|
| 65 | }
|
|---|
| 66 | return cached_hfw;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | void SimpleFlow::addItem( QLayoutItem *item)
|
|---|
| 70 | {
|
|---|
| 71 | list.append( item );
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | bool SimpleFlow::hasHeightForWidth() const
|
|---|
| 75 | {
|
|---|
| 76 | return TRUE;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | QSize SimpleFlow::sizeHint() const
|
|---|
| 80 | {
|
|---|
| 81 | return minimumSize();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | QSizePolicy::ExpandData SimpleFlow::expanding() const
|
|---|
| 85 | {
|
|---|
| 86 | return QSizePolicy::NoDirection;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | QLayoutIterator SimpleFlow::iterator()
|
|---|
| 90 | {
|
|---|
| 91 | return QLayoutIterator( new SimpleFlowIterator( &list ) );
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | void SimpleFlow::setGeometry( const QRect &r )
|
|---|
| 95 | {
|
|---|
| 96 | QLayout::setGeometry( r );
|
|---|
| 97 | doLayout( r );
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | int SimpleFlow::doLayout( const QRect &r, bool testonly )
|
|---|
| 101 | {
|
|---|
| 102 | int x = r.x();
|
|---|
| 103 | int y = r.y();
|
|---|
| 104 | int h = 0; //height of this line so far.
|
|---|
| 105 | QPtrListIterator<QLayoutItem> it(list);
|
|---|
| 106 | QLayoutItem *o;
|
|---|
| 107 | while ( (o=it.current()) != 0 ) {
|
|---|
| 108 | ++it;
|
|---|
| 109 | int nextX = x + o->sizeHint().width() + spacing();
|
|---|
| 110 | if ( nextX - spacing() > r.right() && h > 0 ) {
|
|---|
| 111 | x = r.x();
|
|---|
| 112 | y = y + h + spacing();
|
|---|
| 113 | nextX = x + o->sizeHint().width() + spacing();
|
|---|
| 114 | h = 0;
|
|---|
| 115 | }
|
|---|
| 116 | if ( !testonly )
|
|---|
| 117 | o->setGeometry( QRect( QPoint( x, y ), o->sizeHint() ) );
|
|---|
| 118 | x = nextX;
|
|---|
| 119 | h = QMAX( h, o->sizeHint().height() );
|
|---|
| 120 | }
|
|---|
| 121 | return y + h - r.y();
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | QSize SimpleFlow::minimumSize() const
|
|---|
| 125 | {
|
|---|
| 126 | QSize s(0,0);
|
|---|
| 127 | QPtrListIterator<QLayoutItem> it(list);
|
|---|
| 128 | QLayoutItem *o;
|
|---|
| 129 | while ( (o=it.current()) != 0 ) {
|
|---|
| 130 | ++it;
|
|---|
| 131 | s = s.expandedTo( o->minimumSize() );
|
|---|
| 132 | }
|
|---|
| 133 | return s;
|
|---|
| 134 | }
|
|---|