source: trunk/examples/customlayout/card.cpp@ 20

Last change on this file since 20 was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
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
15class CardLayoutIterator :public QGLayoutIterator
16{
17public:
18 CardLayoutIterator( QPtrList<QLayoutItem> *l )
19 : idx( 0 ), list( l ) {}
20
21 QLayoutItem *current();
22 QLayoutItem *next();
23 QLayoutItem *takeCurrent();
24
25private:
26 int idx;
27 QPtrList<QLayoutItem> *list;
28};
29
30QLayoutItem *CardLayoutIterator::current()
31{
32 return idx < int( list->count() ) ? list->at( idx ) : 0;
33}
34
35QLayoutItem *CardLayoutIterator::next()
36{
37 idx++; return current();
38}
39
40QLayoutItem *CardLayoutIterator::takeCurrent()
41{
42 return idx < int( list->count() ) ?list->take( idx ) : 0;
43}
44
45
46
47QLayoutIterator CardLayout::iterator()
48{
49 return QLayoutIterator( new CardLayoutIterator( &list ) );
50}
51
52CardLayout::~CardLayout()
53{
54 deleteAllItems();
55}
56
57void CardLayout::addItem( QLayoutItem *item )
58{
59 list.append( item );
60}
61
62void 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
86QSize 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
101QSize 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}
Note: See TracBrowser for help on using the repository browser.