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