1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | //! [0]
|
---|
42 | #ifndef CARD_H
|
---|
43 | #define CARD_H
|
---|
44 |
|
---|
45 | #include <QtGui>
|
---|
46 | #include <QList>
|
---|
47 |
|
---|
48 | class CardLayout : public QLayout
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | CardLayout(QWidget *parent, int dist): QLayout(parent, 0, dist) {}
|
---|
52 | CardLayout(QLayout *parent, int dist): QLayout(parent, dist) {}
|
---|
53 | CardLayout(int dist): QLayout(dist) {}
|
---|
54 | ~CardLayout();
|
---|
55 |
|
---|
56 | void addItem(QLayoutItem *item);
|
---|
57 | QSize sizeHint() const;
|
---|
58 | QSize minimumSize() const;
|
---|
59 | QLayoutItem *count() const;
|
---|
60 | QLayoutItem *itemAt(int) const;
|
---|
61 | QLayoutItem *takeAt(int);
|
---|
62 | void setGeometry(const QRect &rect);
|
---|
63 |
|
---|
64 | private:
|
---|
65 | QList<QLayoutItem*> list;
|
---|
66 | };
|
---|
67 | #endif
|
---|
68 | //! [0]
|
---|
69 |
|
---|
70 |
|
---|
71 | //! [1]
|
---|
72 | //#include "card.h"
|
---|
73 | //! [1]
|
---|
74 |
|
---|
75 | //! [2]
|
---|
76 | QLayoutItem *CardLayout::count() const
|
---|
77 | {
|
---|
78 | // QList::size() returns the number of QLayoutItems in the list
|
---|
79 | return list.size();
|
---|
80 | }
|
---|
81 | //! [2]
|
---|
82 |
|
---|
83 | //! [3]
|
---|
84 | QLayoutItem *CardLayout::itemAt(int idx) const
|
---|
85 | {
|
---|
86 | // QList::value() performs index checking, and returns 0 if we are
|
---|
87 | // outside the valid range
|
---|
88 | return list.value(idx);
|
---|
89 | }
|
---|
90 |
|
---|
91 | QLayoutItem *CardLayout::takeAt(int idx)
|
---|
92 | {
|
---|
93 | // QList::take does not do index checking
|
---|
94 | return idx >= 0 && idx < list.size() ? list.takeAt(idx) : 0;
|
---|
95 | }
|
---|
96 | //! [3]
|
---|
97 |
|
---|
98 |
|
---|
99 | //! [4]
|
---|
100 | void CardLayout::addItem(QLayoutItem *item)
|
---|
101 | {
|
---|
102 | list.append(item);
|
---|
103 | }
|
---|
104 | //! [4]
|
---|
105 |
|
---|
106 |
|
---|
107 | //! [5]
|
---|
108 | CardLayout::~CardLayout()
|
---|
109 | {
|
---|
110 | QLayoutItem *item;
|
---|
111 | while ((item = takeAt(0)))
|
---|
112 | delete item;
|
---|
113 | }
|
---|
114 | //! [5]
|
---|
115 |
|
---|
116 |
|
---|
117 | //! [6]
|
---|
118 | void CardLayout::setGeometry(const QRect &r)
|
---|
119 | {
|
---|
120 | QLayout::setGeometry(r);
|
---|
121 |
|
---|
122 | if (list.size() == 0)
|
---|
123 | return;
|
---|
124 |
|
---|
125 | int w = r.width() - (list.count() - 1) * spacing();
|
---|
126 | int h = r.height() - (list.count() - 1) * spacing();
|
---|
127 | int i = 0;
|
---|
128 | while (i < list.size()) {
|
---|
129 | QLayoutItem *o = list.at(i);
|
---|
130 | QRect geom(r.x() + i * spacing(), r.y() + i * spacing(), w, h);
|
---|
131 | o->setGeometry(geom);
|
---|
132 | ++i;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | //! [6]
|
---|
136 |
|
---|
137 |
|
---|
138 | //! [7]
|
---|
139 | QSize CardLayout::sizeHint() const
|
---|
140 | {
|
---|
141 | QSize s(0,0);
|
---|
142 | int n = list.count();
|
---|
143 | if (n > 0)
|
---|
144 | s = QSize(100,70); //start with a nice default size
|
---|
145 | int i = 0;
|
---|
146 | while (i < n) {
|
---|
147 | QLayoutItem *o = list.at(i);
|
---|
148 | s = s.expandedTo(o->sizeHint());
|
---|
149 | ++i;
|
---|
150 | }
|
---|
151 | return s + n*QSize(spacing(), spacing());
|
---|
152 | }
|
---|
153 |
|
---|
154 | QSize CardLayout::minimumSize() const
|
---|
155 | {
|
---|
156 | QSize s(0,0);
|
---|
157 | int n = list.count();
|
---|
158 | int i = 0;
|
---|
159 | while (i < n) {
|
---|
160 | QLayoutItem *o = list.at(i);
|
---|
161 | s = s.expandedTo(o->minimumSize());
|
---|
162 | ++i;
|
---|
163 | }
|
---|
164 | return s + n*QSize(spacing(), spacing());
|
---|
165 | }
|
---|
166 | //! [7]
|
---|