~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/widgets/doc/snippets/code/doc_src_layout.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
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 Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its 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:
 
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
    int 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
int 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]