• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm/lib

  • sources
  • kde-4.12
  • kdepim
  • kalarm
  • lib
packedlayout.cpp
Go to the documentation of this file.
1 /*
2  * packedlayout.cpp - layout to pack items into rows
3  * Program: kalarm
4  * Copyright © 2007 by David Jarvie <djarvie@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 #include "kalarm.h" //krazy:exclude=includes (kalarm.h must be first)
21 #include "packedlayout.h"
22 
23 
24 PackedLayout::PackedLayout(QWidget* parent, Qt::Alignment alignment)
25  : QLayout(parent),
26  mAlignment(alignment),
27  mWidthCached(0)
28 {
29 }
30 
31 PackedLayout::PackedLayout(Qt::Alignment alignment)
32  : QLayout(),
33  mAlignment(alignment),
34  mWidthCached(0)
35 {
36 }
37 
38 PackedLayout::~PackedLayout()
39 {
40  while (!mItems.isEmpty())
41  delete mItems.takeFirst();
42 }
43 
44 /******************************************************************************
45  * Inserts a button into the group.
46  */
47 void PackedLayout::addItem(QLayoutItem* item)
48 {
49  mWidthCached = 0;
50  mItems.append(item);
51 }
52 
53 QLayoutItem* PackedLayout::itemAt(int index) const
54 {
55  return (index >= 0 && index < mItems.count()) ? mItems[index] : 0;
56 }
57 
58 QLayoutItem* PackedLayout::takeAt(int index)
59 {
60  if (index < 0 || index >= mItems.count())
61  return 0;
62  return mItems.takeAt(index);
63 }
64 
65 int PackedLayout::heightForWidth(int w) const
66 {
67  if (w != mWidthCached)
68  {
69  mHeightCached = arrange(QRect(0, 0, w, 0), false);
70  mWidthCached = w;
71  }
72  return mHeightCached;
73 }
74 
75 void PackedLayout::setGeometry(const QRect& rect)
76 {
77  QLayout::setGeometry(rect);
78  arrange(rect, true);
79 }
80 
81 // Return the maximum size of any widget.
82 QSize PackedLayout::minimumSize() const
83 {
84  QSize size;
85  for (int i = 0, end = mItems.count(); i < end; ++i)
86  size = size.expandedTo(mItems[i]->minimumSize());
87  int m = margin() * 2;
88  return QSize(size.width() + m, size.height() + m);
89 }
90 
91 // Arranges widgets and returns height required.
92 int PackedLayout::arrange(const QRect& rect, bool set) const
93 {
94  int x = rect.x();
95  int y = rect.y();
96  int yrow = 0;
97  QList<QRect> posn;
98  int end = mItems.count();
99  QList<QLayoutItem*> items;
100  for (int i = 0; i < end; ++i)
101  {
102  QLayoutItem* item = mItems[i];
103  if (item->isEmpty())
104  continue;
105  QSize size = item->sizeHint();
106  int right = x + size.width();
107  if (right > rect.right() && x > rect.x())
108  {
109  x = rect.x();
110  y = y + yrow + spacing();
111  right = x + size.width();
112  yrow = size.height();
113  }
114  else
115  yrow = qMax(yrow, size.height());
116  items.append(item);
117  posn.append(QRect(QPoint(x, y), size));
118  x = right + spacing();
119  }
120  if (set)
121  {
122  int count = items.count();
123  if (mAlignment == Qt::AlignLeft)
124  {
125  // Left aligned: no position adjustment needed
126  // Set the positions of all the layout items
127  for (int i = 0; i < count; ++i)
128  items[i]->setGeometry(posn[i]);
129  }
130  else
131  {
132  // Set the positions of all the layout items
133  for (int i = 0; i < count; )
134  {
135  // Adjust item positions a row at a time
136  y = posn[i].y();
137  int last; // after last item in this row
138  for (last = i + 1; last < count && posn[last].y() == y; ++last) ;
139  int n = last - i; // number of items in this row
140  int free = rect.right() - posn[last - 1].right();
141  switch (mAlignment)
142  {
143  case Qt::AlignJustify:
144  if (n == 1)
145  {
146  items[i]->setGeometry(posn[i]);
147  ++i;
148  }
149  else if (n > 1)
150  {
151  for (int j = 0; i < last; ++j, ++i)
152  items[i]->setGeometry(QRect(QPoint(posn[i].x() + (free * j)/(n - 1), y), posn[i].size()));
153  }
154  break;
155  case Qt::AlignHCenter:
156  free /= 2;
157  // fall through to AlignRight
158  case Qt::AlignRight:
159  for ( ; i < last; ++i)
160  items[i]->setGeometry(QRect(QPoint(posn[i].x() + free, y), posn[i].size()));
161  break;
162  default:
163  break;
164  }
165  }
166  }
167  }
168  return y + yrow - rect.y();
169 }
170 
171 // vim: et sw=4:
PackedLayout::itemAt
virtual QLayoutItem * itemAt(int index) const
Definition: packedlayout.cpp:53
PackedLayout::minimumSize
virtual QSize minimumSize() const
Definition: packedlayout.cpp:82
PackedLayout::takeAt
virtual QLayoutItem * takeAt(int index)
Definition: packedlayout.cpp:58
QWidget
PackedLayout::setGeometry
virtual void setGeometry(const QRect &r)
Definition: packedlayout.cpp:75
PackedLayout::~PackedLayout
~PackedLayout()
Definition: packedlayout.cpp:38
PackedLayout::PackedLayout
PackedLayout(QWidget *parent, Qt::Alignment alignment)
Constructor.
Definition: packedlayout.cpp:24
PackedLayout::heightForWidth
virtual int heightForWidth(int w) const
Definition: packedlayout.cpp:65
packedlayout.h
QLayout
PackedLayout::count
virtual int count() const
Definition: packedlayout.h:46
set
void set(quint32, const QString &, const QString &, const QString &, const QString &, time_t)
PackedLayout::addItem
virtual void addItem(QLayoutItem *item)
Definition: packedlayout.cpp:47
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm/lib

Skip menu "kalarm/lib"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal