Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
flowlayout.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples 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 #include <QtGui>
42 #include <QRect>
43 #include <QStyle>
44 #include <QWidget>
45 #include <QWidgetItem>
46 
47 #include "flowlayout.h"
48 FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
49  : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
50 {
51  setContentsMargins(margin, margin, margin, margin);
52 }
53 
54 FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
55  : m_hSpace(hSpacing), m_vSpace(vSpacing)
56 {
57  setContentsMargins(margin, margin, margin, margin);
58 }
59 
60 FlowLayout::~FlowLayout()
61 {
62  QLayoutItem *item;
63  while ((item = takeAt(0)))
64  delete item;
65 }
66 
67 void FlowLayout::addItem(QLayoutItem *item)
68 {
69  itemList.append(item);
70 }
71 
72 int FlowLayout::horizontalSpacing() const
73 {
74  if (m_hSpace >= 0) {
75  return m_hSpace;
76  } else {
77  return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
78  }
79 }
80 
81 int FlowLayout::verticalSpacing() const
82 {
83  if (m_vSpace >= 0) {
84  return m_vSpace;
85  } else {
86  return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
87  }
88 }
89 
90 int FlowLayout::count() const
91 {
92  return itemList.size();
93 }
94 
95 QLayoutItem *FlowLayout::itemAt(int index) const
96 {
97  return itemList.value(index);
98 }
99 
100 QLayoutItem *FlowLayout::takeAt(int index)
101 {
102  if (index >= 0 && index < itemList.size())
103  return itemList.takeAt(index);
104  else
105  return 0;
106 }
107 
108 Qt::Orientations FlowLayout::expandingDirections() const
109 {
110  return 0;
111 }
112 
113 bool FlowLayout::hasHeightForWidth() const
114 {
115  return true;
116 }
117 
118 int FlowLayout::heightForWidth(int width) const
119 {
120  int height = doLayout(QRect(0, 0, width, 0), true);
121  return height;
122 }
123 
124 void FlowLayout::setGeometry(const QRect &rect)
125 {
126  QLayout::setGeometry(rect);
127  doLayout(rect, false);
128 }
129 
130 QSize FlowLayout::sizeHint() const
131 {
132  return minimumSize();
133 }
134 
135 QSize FlowLayout::minimumSize() const
136 {
137  QSize size;
138  QLayoutItem *item;
139  foreach (item, itemList)
140  size = size.expandedTo(item->minimumSize());
141 
142  size += QSize(2*margin(), 2*margin());
143  return size;
144 }
145 
146 int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
147 {
148  int left, top, right, bottom;
149  getContentsMargins(&left, &top, &right, &bottom);
150  QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
151  int x = effectiveRect.x();
152  int y = effectiveRect.y();
153  int lineHeight = 0;
154 
155  QLayoutItem *item;
156  foreach (item, itemList) {
157  QWidget *wid = item->widget();
158  int spaceX = horizontalSpacing();
159  if (spaceX == -1)
160  spaceX = wid->style()->layoutSpacing(
161  QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
162  int spaceY = verticalSpacing();
163  if (spaceY == -1)
164  spaceY = wid->style()->layoutSpacing(
165  QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
166  int nextX = x + item->sizeHint().width() + spaceX;
167  if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
168  x = effectiveRect.x();
169  y = y + lineHeight + spaceY;
170  nextX = x + item->sizeHint().width() + spaceX;
171  lineHeight = 0;
172  }
173 
174  if (!testOnly)
175  item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
176 
177  x = nextX;
178  lineHeight = qMax(lineHeight, item->sizeHint().height());
179  }
180  return y + lineHeight - rect.y() + bottom;
181 }
182 
183 int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
184 {
185  QObject *parent = this->parent();
186  if (!parent) {
187  return -1;
188  } else if (parent->isWidgetType()) {
189  QWidget *pw = static_cast<QWidget *>(parent);
190  return pw->style()->pixelMetric(pm, 0, pw);
191  } else {
192  return static_cast<QLayout *>(parent)->spacing();
193  }
194 }