LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
flowlayout.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "flowlayout.h"
31 #include <QWidget>
32 
33 namespace LeechCraft
34 {
35 namespace Util
36 {
37  FlowLayout::FlowLayout (QWidget *parent,
38  int margin, int hspace, int vspace)
39  : QLayout (parent)
40  , HSpace_ (hspace)
41  , VSpace_ (vspace)
42  {
43  setContentsMargins (margin, margin, margin, margin);
44  }
45 
46  FlowLayout::FlowLayout (int margin, int hspace, int vspace)
47  : HSpace_ (hspace)
48  , VSpace_ (vspace)
49  {
50  setContentsMargins (margin, margin, margin, margin);
51  }
52 
54  {
55  QLayoutItem *item = 0;
56  while ((item = takeAt (0)))
57  delete item;
58  }
59 
60  void FlowLayout::addItem (QLayoutItem *item)
61  {
62  ItemList_ << item;
63  }
64 
66  {
67  return HSpace_ >= 0 ?
68  HSpace_ :
69  SmartSpacing (QStyle::PM_LayoutHorizontalSpacing);
70  }
71 
73  {
74  return VSpace_ >= 0 ?
75  VSpace_ :
76  SmartSpacing (QStyle::PM_LayoutVerticalSpacing);
77  }
78 
79  Qt::Orientations FlowLayout::expandingDirections () const
80  {
81  return 0;
82  }
83 
85  {
86  return true;
87  }
88 
89  int FlowLayout::heightForWidth (int width) const
90  {
91  return DoLayout (QRect (0, 0, width, 0), true);
92  }
93 
94  int FlowLayout::count () const
95  {
96  return ItemList_.size ();
97  }
98 
99  QLayoutItem* FlowLayout::itemAt (int idx) const
100  {
101  return ItemList_.value (idx);
102  }
103 
104  QLayoutItem* FlowLayout::takeAt (int idx)
105  {
106  if (idx >= 0 && idx < ItemList_.size ())
107  return ItemList_.takeAt (idx);
108  else
109  return 0;
110  }
111 
112  QSize FlowLayout::minimumSize () const
113  {
114  QSize size;
115  Q_FOREACH (const QLayoutItem *item, ItemList_)
116  size = size.expandedTo (item->minimumSize ());
117 
118  size += QSize (margin () * 2, margin () * 2);
119  return size;
120  }
121 
122  void FlowLayout::setGeometry (const QRect& rect)
123  {
124  QLayout::setGeometry (rect);
125  DoLayout (rect, false);
126  }
127 
128  QSize FlowLayout::sizeHint () const
129  {
130  return minimumSize ();
131  }
132 
133  int FlowLayout::DoLayout (const QRect& rect, bool testOnly) const
134  {
135  int left = 0, top = 0, right = 0, bottom = 0;
136  getContentsMargins (&left, &top, &right, &bottom);
137 
138  const QRect& effectiveRect = rect.adjusted (left, top, -right, -bottom);
139  int x = effectiveRect.x ();
140  int y = effectiveRect.y ();
141  int lineHeight = 0;
142 
143  Q_FOREACH (QLayoutItem *item, ItemList_)
144  {
145  QWidget *widget = item->widget ();
146 
147  int spaceX = horizontalSpacing ();
148  if (spaceX == -1)
149  spaceX = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
150  QSizePolicy::PushButton, Qt::Horizontal);
151  int spaceY = verticalSpacing ();
152  if (spaceY == -1)
153  spaceY = widget->style ()->layoutSpacing (QSizePolicy::PushButton,
154  QSizePolicy::PushButton, Qt::Vertical);
155 
156  const auto& sizeHint = item->sizeHint ();
157  const int hintWidth = sizeHint.width ();
158  int nextX = x + hintWidth + spaceX;
159  if (nextX - spaceX > effectiveRect.right () &&
160  lineHeight > 0)
161  {
162  x = effectiveRect.x ();
163  y += lineHeight + spaceY;
164  nextX = x + hintWidth + spaceX;
165  lineHeight = 0;
166  }
167 
168  if (!testOnly)
169  item->setGeometry (QRect (QPoint (x, y), sizeHint));
170 
171  x = nextX;
172  lineHeight = std::max (lineHeight, sizeHint.height ());
173  }
174 
175  return y + lineHeight - rect.y () + bottom;
176  }
177 
178  int FlowLayout::SmartSpacing (QStyle::PixelMetric pm) const
179  {
180  QObject *obj = parent ();
181  if (!obj)
182  return -1;
183  else if (obj->isWidgetType ())
184  {
185  QWidget *pw = static_cast<QWidget*> (obj);
186  return pw->style ()->pixelMetric (pm, 0, pw);
187  }
188  else
189  return static_cast<QLayout*> (obj)->spacing ();
190  }
191 }
192 }
FlowLayout(QWidget *, int=-1, int=-1, int=-1)
Definition: flowlayout.cpp:37
void setGeometry(const QRect &)
Definition: flowlayout.cpp:122
QLayoutItem * takeAt(int)
Definition: flowlayout.cpp:104
QLayoutItem * itemAt(int) const
Definition: flowlayout.cpp:99
Qt::Orientations expandingDirections() const
Definition: flowlayout.cpp:79
bool hasHeightForWidth() const
Definition: flowlayout.cpp:84
int heightForWidth(int) const
Definition: flowlayout.cpp:89
void addItem(QLayoutItem *)
Definition: flowlayout.cpp:60