Geometrize  1.0
An application for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Public Member Functions | Private Member Functions | Private Attributes | List of all members
geometrize::layout::FlowLayout Class Reference

The FlowLayout class implements a layout that arranges components in a directional flow, like lines of text in a paragraph. The layout adapts to changing window sizes, a little like the Swing FlowLayout. It is based on the Qt Flow Layout example. More...

#include <flowlayout.h>

Inheritance diagram for geometrize::layout::FlowLayout:
Inheritance graph
[legend]
Collaboration diagram for geometrize::layout::FlowLayout:
Collaboration graph
[legend]

Public Member Functions

 FlowLayout (const int margin=-1, const int hSpacing=-1, const int vSpacing=-1)
 
virtual ~FlowLayout ()
 
int horizontalSpacing () const
 
int verticalSpacing () const
 
void addItem (QLayoutItem *item) override
 
Qt::Orientations expandingDirections () const override
 
bool hasHeightForWidth () const override
 
int heightForWidth (int width) const override
 
int count () const override
 
QLayoutItem * itemAt (int index) const override
 
QSize minimumSize () const override
 
void setGeometry (const QRect &rect) override
 
QSize sizeHint () const override
 
QLayoutItem * takeAt (int index) override
 

Private Member Functions

int doLayout (const QRect &rect, bool testOnly) const
 
int smartSpacing (QStyle::PixelMetric pm) const
 

Private Attributes

QList< QLayoutItem * > m_itemList
 
int m_hSpace
 
int m_vSpace
 

Detailed Description

The FlowLayout class implements a layout that arranges components in a directional flow, like lines of text in a paragraph. The layout adapts to changing window sizes, a little like the Swing FlowLayout. It is based on the Qt Flow Layout example.

Constructor & Destructor Documentation

geometrize::layout::FlowLayout::FlowLayout ( const int  margin = -1,
const int  hSpacing = -1,
const int  vSpacing = -1 
)
explicit
43  : m_hSpace(hSpacing), m_vSpace(vSpacing)
44 {
45  setContentsMargins(margin, margin, margin, margin);
46 }
int m_vSpace
Definition: flowlayout.h:78
int m_hSpace
Definition: flowlayout.h:77
geometrize::layout::FlowLayout::~FlowLayout ( )
virtual
49 {
50  while (QLayoutItem* item = takeAt(0)) {
51  delete item;
52  item = nullptr;
53  }
54 }
QLayoutItem * takeAt(int index) override
Definition: flowlayout.cpp:127

Here is the call graph for this function:

Member Function Documentation

void geometrize::layout::FlowLayout::addItem ( QLayoutItem *  item)
override
75 {
76  m_itemList.append(item);
77 }
QList< QLayoutItem * > m_itemList
Definition: flowlayout.h:76
int geometrize::layout::FlowLayout::count ( ) const
override
95 {
96  return m_itemList.size();
97 }
QList< QLayoutItem * > m_itemList
Definition: flowlayout.h:76
int geometrize::layout::FlowLayout::doLayout ( const QRect &  rect,
bool  testOnly 
) const
private
137 {
138  int left, top, right, bottom;
139  getContentsMargins(&left, &top, &right, &bottom);
140  QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
141  int x{effectiveRect.x()};
142  int y{effectiveRect.y()};
143  int lineHeight{0};
144 
145  QLayoutItem* item;
146  foreach (item, m_itemList) {
147  const QWidget* const wid{item->widget()};
148  if(wid->isHidden()) {
149  continue;
150  }
151 
152  int spaceX{horizontalSpacing()};
153  if (spaceX == -1) {
154  spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
155  }
156  int spaceY{verticalSpacing()};
157  if (spaceY == -1) {
158  spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
159  }
160  int nextX{x + item->sizeHint().width() + spaceX};
161  if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
162  x = effectiveRect.x();
163  y = y + lineHeight + spaceY;
164  nextX = x + item->sizeHint().width() + spaceX;
165  lineHeight = 0;
166  }
167 
168  if (!testOnly) {
169  item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
170  }
171 
172  x = nextX;
173  lineHeight = qMax(lineHeight, item->sizeHint().height());
174  }
175  return y + lineHeight - rect.y() + bottom;
176 }
int horizontalSpacing() const
Definition: flowlayout.cpp:56
QList< QLayoutItem * > m_itemList
Definition: flowlayout.h:76
int verticalSpacing() const
Definition: flowlayout.cpp:65

Here is the call graph for this function:

Here is the caller graph for this function:

Qt::Orientations geometrize::layout::FlowLayout::expandingDirections ( ) const
override
80 {
81  return 0;
82 }
bool geometrize::layout::FlowLayout::hasHeightForWidth ( ) const
override
85 {
86  return true;
87 }
int geometrize::layout::FlowLayout::heightForWidth ( int  width) const
override
90 {
91  return doLayout(QRect(0, 0, width, 0), true);
92 }
int doLayout(const QRect &rect, bool testOnly) const
Definition: flowlayout.cpp:136

Here is the call graph for this function:

int geometrize::layout::FlowLayout::horizontalSpacing ( ) const
57 {
58  if (m_hSpace >= 0) {
59  return m_hSpace;
60  } else {
61  return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
62  }
63 }
int smartSpacing(QStyle::PixelMetric pm) const
Definition: flowlayout.cpp:178
int m_hSpace
Definition: flowlayout.h:77

Here is the call graph for this function:

Here is the caller graph for this function:

QLayoutItem * geometrize::layout::FlowLayout::itemAt ( int  index) const
override
100 {
101  return m_itemList.value(index);
102 }
QList< QLayoutItem * > m_itemList
Definition: flowlayout.h:76
QSize geometrize::layout::FlowLayout::minimumSize ( ) const
override
105 {
106  QSize size;
107  QLayoutItem* item;
108  foreach (item, m_itemList) {
109  size = size.expandedTo(item->minimumSize());
110  }
111 
112  size += QSize(2 * margin(), 2 * margin());
113  return size;
114 }
QList< QLayoutItem * > m_itemList
Definition: flowlayout.h:76

Here is the caller graph for this function:

void geometrize::layout::FlowLayout::setGeometry ( const QRect &  rect)
override
117 {
118  QLayout::setGeometry(rect);
119  doLayout(rect, false);
120 }
int doLayout(const QRect &rect, bool testOnly) const
Definition: flowlayout.cpp:136

Here is the call graph for this function:

QSize geometrize::layout::FlowLayout::sizeHint ( ) const
override
123 {
124  return minimumSize();
125 }
QSize minimumSize() const override
Definition: flowlayout.cpp:104

Here is the call graph for this function:

int geometrize::layout::FlowLayout::smartSpacing ( QStyle::PixelMetric  pm) const
private
179 {
180  QObject* parent{this->parent()};
181  if (!parent) {
182  return -1;
183  } else if (parent->isWidgetType()) {
184  QWidget* pw{static_cast<QWidget*>(parent)};
185  return pw->style()->pixelMetric(pm, 0, pw);
186  } else {
187  return static_cast<QLayout*>(parent)->spacing();
188  }
189 }

Here is the caller graph for this function:

QLayoutItem * geometrize::layout::FlowLayout::takeAt ( int  index)
override
128 {
129  if (index >= 0 && index < m_itemList.size()) {
130  return m_itemList.takeAt(index);
131  } else {
132  return 0;
133  }
134 }
QList< QLayoutItem * > m_itemList
Definition: flowlayout.h:76

Here is the caller graph for this function:

int geometrize::layout::FlowLayout::verticalSpacing ( ) const
66 {
67  if (m_vSpace >= 0) {
68  return m_vSpace;
69  } else {
70  return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
71  }
72 }
int m_vSpace
Definition: flowlayout.h:78
int smartSpacing(QStyle::PixelMetric pm) const
Definition: flowlayout.cpp:178

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

int geometrize::layout::FlowLayout::m_hSpace
private
QList<QLayoutItem*> geometrize::layout::FlowLayout::m_itemList
private
int geometrize::layout::FlowLayout::m_vSpace
private

The documentation for this class was generated from the following files: