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 examples 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 #include "DolphinQt/QtUtils/FlowLayout.h"
52 
53 #include <QtWidgets>
54 
FlowLayout(QWidget * parent,int margin,int h_spacing,int v_spacing)55 FlowLayout::FlowLayout(QWidget* parent, int margin, int h_spacing, int v_spacing)
56     : QLayout(parent), m_h_space(h_spacing), m_v_space(v_spacing)
57 {
58   setContentsMargins(margin, margin, margin, margin);
59 }
60 
FlowLayout(int margin,int h_spacing,int v_spacing)61 FlowLayout::FlowLayout(int margin, int h_spacing, int v_spacing)
62     : m_h_space(h_spacing), m_v_space(v_spacing)
63 {
64   setContentsMargins(margin, margin, margin, margin);
65 }
66 
~FlowLayout()67 FlowLayout::~FlowLayout()
68 {
69   QLayoutItem* item;
70   while ((item = takeAt(0)))
71     delete item;
72 }
73 
addItem(QLayoutItem * item)74 void FlowLayout::addItem(QLayoutItem* item)
75 {
76   m_item_list.append(item);
77 }
78 
horizontalSpacing() const79 int FlowLayout::horizontalSpacing() const
80 {
81   if (m_h_space >= 0)
82   {
83     return m_h_space;
84   }
85   else
86   {
87     return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
88   }
89 }
90 
verticalSpacing() const91 int FlowLayout::verticalSpacing() const
92 {
93   if (m_v_space >= 0)
94   {
95     return m_v_space;
96   }
97   else
98   {
99     return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
100   }
101 }
102 
count() const103 int FlowLayout::count() const
104 {
105   return m_item_list.size();
106 }
107 
itemAt(int index) const108 QLayoutItem* FlowLayout::itemAt(int index) const
109 {
110   return m_item_list.value(index);
111 }
112 
takeAt(int index)113 QLayoutItem* FlowLayout::takeAt(int index)
114 {
115   if (index >= 0 && index < m_item_list.size())
116     return m_item_list.takeAt(index);
117   else
118     return 0;
119 }
120 
expandingDirections() const121 Qt::Orientations FlowLayout::expandingDirections() const
122 {
123   return {};
124 }
125 
hasHeightForWidth() const126 bool FlowLayout::hasHeightForWidth() const
127 {
128   return true;
129 }
130 
heightForWidth(int width) const131 int FlowLayout::heightForWidth(int width) const
132 {
133   int height = doLayout(QRect(0, 0, width, 0), true);
134   return height;
135 }
136 
setGeometry(const QRect & rect)137 void FlowLayout::setGeometry(const QRect& rect)
138 {
139   QLayout::setGeometry(rect);
140   doLayout(rect, false);
141 }
142 
sizeHint() const143 QSize FlowLayout::sizeHint() const
144 {
145   return minimumSize();
146 }
147 
minimumSize() const148 QSize FlowLayout::minimumSize() const
149 {
150   QSize size;
151   for (const auto& item : m_item_list)
152     size = size.expandedTo(item->minimumSize());
153 
154   size += QSize(2 * margin(), 2 * margin());
155   return size;
156 }
157 
doLayout(const QRect & rect,bool testOnly) const158 int FlowLayout::doLayout(const QRect& rect, bool testOnly) const
159 {
160   int left, top, right, bottom;
161   getContentsMargins(&left, &top, &right, &bottom);
162   QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
163   int x = effectiveRect.x();
164   int y = effectiveRect.y();
165   int lineHeight = 0;
166 
167   for (const auto& item : m_item_list)
168   {
169     QWidget* wid = item->widget();
170     int spaceX = horizontalSpacing();
171     if (spaceX == -1)
172       spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton,
173                                            Qt::Horizontal);
174     int spaceY = verticalSpacing();
175     if (spaceY == -1)
176       spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton,
177                                            Qt::Vertical);
178     int nextX = x + item->sizeHint().width() + spaceX;
179     if (nextX - spaceX > effectiveRect.right() && lineHeight > 0)
180     {
181       x = effectiveRect.x();
182       y = y + lineHeight + spaceY;
183       nextX = x + item->sizeHint().width() + spaceX;
184       lineHeight = 0;
185     }
186 
187     if (!testOnly)
188       item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
189 
190     x = nextX;
191     lineHeight = qMax(lineHeight, item->sizeHint().height());
192   }
193   return y + lineHeight - rect.y() + bottom;
194 }
195 
smartSpacing(QStyle::PixelMetric pm) const196 int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
197 {
198   QObject* parent = this->parent();
199   if (!parent)
200   {
201     return -1;
202   }
203   else if (parent->isWidgetType())
204   {
205     QWidget* pw = static_cast<QWidget*>(parent);
206     return pw->style()->pixelMetric(pm, 0, pw);
207   }
208   else
209   {
210     return static_cast<QLayout*>(parent)->spacing();
211   }
212 }
213