1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
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:
CardLayout(QWidget * parent,int dist)51     CardLayout(QWidget *parent, int dist): QLayout(parent, 0, dist) {}
CardLayout(QLayout * parent,int dist)52     CardLayout(QLayout *parent, int dist): QLayout(parent, dist) {}
CardLayout(int 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]
count() const76 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]
itemAt(int idx) const84 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 
takeAt(int idx)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]
addItem(QLayoutItem * item)100 void CardLayout::addItem(QLayoutItem *item)
101 {
102     list.append(item);
103 }
104 //! [4]
105 
106 
107 //! [5]
~CardLayout()108 CardLayout::~CardLayout()
109 {
110      QLayoutItem *item;
111      while ((item = takeAt(0)))
112          delete item;
113 }
114 //! [5]
115 
116 
117 //! [6]
setGeometry(const QRect & r)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]
sizeHint() const139 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 
minimumSize() const154 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]
167