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

liblancelot

  • sources
  • kde-4.14
  • workspace
  • kdeplasma-addons
  • libs
  • lancelot
  • layouts
FullBorderLayout.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser/Library General Public License version 2,
6  * or (at your option) any later version, as published by the Free
7  * Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser/Library General Public License for more details
13  *
14  * You should have received a copy of the GNU Lesser/Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "FullBorderLayout.h"
21 
22 #include <QGraphicsWidget>
23 
24 #include <lancelot/lancelot.h>
25 
26 namespace Lancelot {
27 
28 class FullBorderLayout::Private {
29 public:
30  FullBorderLayout * q;
31 
32  explicit Private(FullBorderLayout * parent = 0)
33  : q(parent)
34  {
35  sizes[FullBorderLayout::LeftBorder] = -1;
36  sizes[FullBorderLayout::RightBorder] = -1;
37  sizes[FullBorderLayout::TopBorder] = -1;
38  sizes[FullBorderLayout::BottomBorder] = -1;
39  }
40 
41  virtual ~Private()
42  {
43  }
44 
45  QMap < FullBorderLayout::Place, QGraphicsLayoutItem * > itemPositions;
46  QMap < FullBorderLayout::Border, qreal > sizes;
47 
48  void calculateBorderSizes(qreal & top, qreal & bottom, qreal & left, qreal & right) // const
49  {
50  // top
51  top = sizes[FullBorderLayout::TopBorder];
52  if (top < 0) {
53  top = 0;
54  if (itemPositions[FullBorderLayout::TopLeft]) {
55  top = qMax(top, itemPositions[FullBorderLayout::TopLeft]->preferredSize().height());
56  }
57  if (itemPositions[FullBorderLayout::Top]) {
58  top = qMax(top, itemPositions[FullBorderLayout::Top]->preferredSize().height());
59  }
60  if (itemPositions[FullBorderLayout::TopRight]) {
61  top = qMax(top, itemPositions[FullBorderLayout::TopRight]->preferredSize().height());
62  }
63  }
64 
65  // bottom
66  bottom = sizes[FullBorderLayout::BottomBorder];
67  if (bottom < 0) {
68  bottom = 0;
69  if (itemPositions[FullBorderLayout::BottomLeft]) {
70  bottom = qMax(bottom, itemPositions[FullBorderLayout::BottomLeft]->preferredSize().height());
71  }
72  if (itemPositions[FullBorderLayout::Bottom]) {
73  bottom = qMax(bottom, itemPositions[FullBorderLayout::Bottom]->preferredSize().height());
74  }
75  if (itemPositions[FullBorderLayout::BottomRight]) {
76  bottom = qMax(bottom, itemPositions[FullBorderLayout::BottomRight]->preferredSize().height());
77  }
78  }
79 
80  // left
81  left = sizes[FullBorderLayout::LeftBorder];
82  if (left < 0) {
83  left = 0;
84  if (itemPositions[FullBorderLayout::TopLeft]) {
85  left = qMax(left, itemPositions[FullBorderLayout::TopLeft]->preferredSize().width());
86  }
87  if (itemPositions[FullBorderLayout::Left]) {
88  left = qMax(left, itemPositions[FullBorderLayout::Left]->preferredSize().width());
89  }
90  if (itemPositions[FullBorderLayout::BottomLeft]) {
91  left = qMax(left, itemPositions[FullBorderLayout::BottomLeft]->preferredSize().width());
92  }
93  }
94 
95  // right
96  right = sizes[FullBorderLayout::RightBorder];
97  if (right < 0) {
98  right = 0;
99  if (itemPositions[FullBorderLayout::TopRight]) {
100  right = qMax(right, itemPositions[FullBorderLayout::TopRight]->preferredSize().width());
101  }
102  if (itemPositions[FullBorderLayout::Right]) {
103  right = qMax(right, itemPositions[FullBorderLayout::Right]->preferredSize().width());
104  }
105  if (itemPositions[FullBorderLayout::BottomRight]) {
106  right = qMax(right, itemPositions[FullBorderLayout::BottomRight]->preferredSize().width());
107  }
108  }
109  }
110 };
111 
112 FullBorderLayout::FullBorderLayout(QGraphicsLayoutItem * parent) :
113  QGraphicsLayout(parent), d(new Private(this))
114 {
115 }
116 
117 FullBorderLayout::~FullBorderLayout()
118 {
119  delete d;
120 }
121 
122 void FullBorderLayout::setGeometry(const QRectF & rect)
123 {
124  QGraphicsLayout::setGeometry(rect);
125 
126  QRectF effectiveRect = geometry();
127  qreal left = 0, top = 0, right = 0, bottom = 0;
128  getContentsMargins(&left, &top, &right, &bottom);
129  effectiveRect.adjust(+left, +top, -right, -bottom);
130 
131  qreal topBorder, bottomBorder, leftBorder, rightBorder;
132  d->calculateBorderSizes(topBorder, bottomBorder, leftBorder, rightBorder);
133 
134  QRectF itemRect;
135 
136  itemRect = effectiveRect;
137  itemRect.setSize(QSizeF(leftBorder, topBorder));
138 
139  if (d->itemPositions[TopLeft]) {
140  d->itemPositions[TopLeft]->setGeometry(itemRect);
141  }
142 
143  itemRect.setLeft(effectiveRect.left() + leftBorder);
144  itemRect.setWidth(effectiveRect.width() - leftBorder - rightBorder);
145 
146  if (d->itemPositions[Top]) {
147  d->itemPositions[Top]->setGeometry(itemRect);
148  }
149 
150  itemRect.setWidth(rightBorder);
151  itemRect.moveRight(effectiveRect.right());
152 
153  if (d->itemPositions[TopRight]) {
154  d->itemPositions[TopRight]->setGeometry(itemRect);
155  }
156 
157  itemRect.setTop(effectiveRect.top() + topBorder);
158  itemRect.setHeight(effectiveRect.height() - topBorder - bottomBorder);
159 
160  if (d->itemPositions[Right]) {
161  d->itemPositions[Right]->setGeometry(itemRect);
162  }
163 
164  itemRect.setHeight(bottomBorder);
165  itemRect.moveBottom(effectiveRect.bottom());
166 
167  if (d->itemPositions[BottomRight]) {
168  d->itemPositions[BottomRight]->setGeometry(itemRect);
169  }
170 
171  itemRect.setLeft(effectiveRect.left() + leftBorder);
172  itemRect.setWidth(effectiveRect.width() - leftBorder - rightBorder);
173 
174  if (d->itemPositions[Bottom]) {
175  d->itemPositions[Bottom]->setGeometry(itemRect);
176  }
177 
178  itemRect.setLeft(effectiveRect.left());
179  itemRect.setWidth(leftBorder);
180 
181  if (d->itemPositions[BottomLeft]) {
182  d->itemPositions[BottomLeft]->setGeometry(itemRect);
183  }
184 
185  itemRect.setTop(effectiveRect.top() + topBorder);
186  itemRect.setHeight(effectiveRect.height() - topBorder - bottomBorder);
187 
188  if (d->itemPositions[Left]) {
189  d->itemPositions[Left]->setGeometry(itemRect);
190  }
191 
192  itemRect = effectiveRect;
193  itemRect.adjust(
194  leftBorder, topBorder,
195  - rightBorder, - bottomBorder
196  );
197 
198  if (d->itemPositions[Center]) {
199  d->itemPositions[Center]->setGeometry(itemRect);
200  }
201 }
202 
203 QSizeF FullBorderLayout::sizeHint(Qt::SizeHint which,
204  const QSizeF & constraint) const
205 {
206  if (which == Qt::MaximumSize) {
207  return MAX_WIDGET_SIZE;
208  }
209 
210  qreal topMargin, bottomMargin, leftMargin, rightMargin;
211  d->calculateBorderSizes(topMargin, bottomMargin, leftMargin, rightMargin);
212 
213  qreal hintHeight = topMargin + bottomMargin;
214  qreal hintWidth = leftMargin + rightMargin;
215 
216  if (d->itemPositions[Center]) {
217  hintHeight += d->itemPositions[Center]
218  ->effectiveSizeHint(which, constraint).height();
219  hintWidth += d->itemPositions[Center]
220  ->effectiveSizeHint(which, constraint).width();
221  }
222 
223  QSizeF result = QSizeF(hintWidth, hintHeight);
224  if (constraint.isValid()) {
225  result = result.boundedTo(constraint);
226  }
227  return result;
228 }
229 
230 void FullBorderLayout::addItem(QGraphicsLayoutItem * item)
231 {
232  FullBorderLayout::addItem (item, Center);
233 }
234 
235 void FullBorderLayout::addItem(QGraphicsLayoutItem * item, Place position)
236 {
237  d->itemPositions[position] = item;
238  updateGeometry();
239 }
240 
241 int FullBorderLayout::count() const
242 {
243  int count = 0;
244  foreach (QGraphicsLayoutItem * i, d->itemPositions) {
245  if (i) {
246  ++count;
247  }
248  }
249  return count;
250 }
251 
252 QGraphicsLayoutItem * FullBorderLayout::itemAt(int index) const
253 {
254  int count = 0;
255  foreach (QGraphicsLayoutItem * i, d->itemPositions) {
256  if (i) {
257  if (index == count) {
258  return i;
259  }
260  count++;
261  }
262  }
263  return 0;
264 }
265 
266 void FullBorderLayout::setSize(qreal size, Border border)
267 {
268  d->sizes[border] = size;
269  updateGeometry();
270 }
271 
272 void FullBorderLayout::setAutoSize(Border border)
273 {
274  d->sizes[border] = -1;
275  updateGeometry();
276 }
277 
278 qreal FullBorderLayout::size(Border border) const
279 {
280  return d->sizes[border];
281 }
282 
283 void FullBorderLayout::removeAt(int index)
284 {
285  QMutableMapIterator < Place, QGraphicsLayoutItem * > i(d->itemPositions);
286  int count = 0;
287  while (i.hasNext()) {
288  i.next();
289  if (i.value()) {
290  if (index == count++) {
291  i.remove();
292  }
293  }
294  }
295  updateGeometry();
296 }
297 
298 } // namespace Lancelot
299 
QMutableMapIterator
Lancelot::FullBorderLayout::addItem
void addItem(QGraphicsLayoutItem *item)
Adds item in the center.
Definition: FullBorderLayout.cpp:230
QMutableMapIterator::value
T & value()
Lancelot::FullBorderLayout::TopLeft
Definition: FullBorderLayout.h:59
QGraphicsLayoutItem::geometry
QRectF geometry() const
Lancelot::FullBorderLayout::sizeHint
L_Override QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
Definition: FullBorderLayout.cpp:203
Lancelot::FullBorderLayout::removeAt
L_Override void removeAt(int index)
Definition: FullBorderLayout.cpp:283
Lancelot::FullBorderLayout::setGeometry
L_Override void setGeometry(const QRectF &rect)
Definition: FullBorderLayout.cpp:122
QMap
QSizeF::isValid
bool isValid() const
Lancelot::FullBorderLayout::RightBorder
Left border.
Definition: FullBorderLayout.h:47
QRectF::top
qreal top() const
MAX_WIDGET_SIZE
#define MAX_WIDGET_SIZE
Definition: lancelot.h:45
QRectF::left
qreal left() const
QRectF::adjust
void adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2)
QRectF::setLeft
void setLeft(qreal x)
QRectF::setHeight
void setHeight(qreal height)
QRectF::bottom
qreal bottom() const
Lancelot::FullBorderLayout::Right
Definition: FullBorderLayout.h:58
QRectF::moveRight
void moveRight(qreal x)
Lancelot::FullBorderLayout::~FullBorderLayout
virtual ~FullBorderLayout()
Destroys this Lancelot::FullBorderLayout.
Definition: FullBorderLayout.cpp:117
Lancelot::FullBorderLayout::BottomRight
Definition: FullBorderLayout.h:62
QGraphicsLayoutItem::preferredSize
QSizeF preferredSize() const
Lancelot::FullBorderLayout::setSize
void setSize(qreal size, Border border)
Deactivates the automatic sizing of a border widget, and sets it to the specified size...
Definition: FullBorderLayout.cpp:266
Lancelot::FullBorderLayout::Top
Definition: FullBorderLayout.h:55
FullBorderLayout.h
Lancelot::FullBorderLayout::Border
Border
Borders enum.
Definition: FullBorderLayout.h:43
Lancelot::FullBorderLayout::Place
Place
Positions supported by FullBorderLayout.
Definition: FullBorderLayout.h:53
QSizeF::boundedTo
QSizeF boundedTo(const QSizeF &otherSize) const
QGraphicsLayout::getContentsMargins
virtual void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
QGraphicsLayout::updateGeometry
virtual void updateGeometry()
Lancelot::FullBorderLayout::Left
Definition: FullBorderLayout.h:57
Lancelot::FullBorderLayout::count
L_Override int count() const
Definition: FullBorderLayout.cpp:241
Lancelot::FullBorderLayout::size
qreal size(Border border) const
Returns the size of the specified border widget.
Definition: FullBorderLayout.cpp:278
Lancelot::FullBorderLayout::Center
Definition: FullBorderLayout.h:54
QRectF::right
qreal right() const
QMutableMapIterator::hasNext
bool hasNext() const
QMutableMapIterator::next
Item next()
Lancelot::FullBorderLayout::TopRight
Definition: FullBorderLayout.h:60
Lancelot::FullBorderLayout::itemAt
L_Override QGraphicsLayoutItem * itemAt(int i) const
Definition: FullBorderLayout.cpp:252
Lancelot::FullBorderLayout::BottomBorder
Top border.
Definition: FullBorderLayout.h:45
QRectF::width
qreal width() const
QSizeF
QRectF
QRectF::setTop
void setTop(qreal y)
QRectF::setWidth
void setWidth(qreal width)
QMutableMapIterator::remove
void remove()
QRectF::moveBottom
void moveBottom(qreal y)
lancelot.h
Lancelot::FullBorderLayout::Bottom
Definition: FullBorderLayout.h:56
QGraphicsLayout
Lancelot::FullBorderLayout::FullBorderLayout
FullBorderLayout(QGraphicsLayoutItem *parent=0)
Creates a new Lancelot::FullBorderLayout.
Definition: FullBorderLayout.cpp:112
QRectF::height
qreal height() const
QRectF::setSize
void setSize(const QSizeF &size)
Lancelot::FullBorderLayout::LeftBorder
Bottom border.
Definition: FullBorderLayout.h:46
Lancelot::FullBorderLayout::TopBorder
Definition: FullBorderLayout.h:44
Lancelot::FullBorderLayout::setAutoSize
void setAutoSize(Border border)
Activates the automatic sizing of a border widget, according to it's sizeHint()
Definition: FullBorderLayout.cpp:272
Lancelot::FullBorderLayout::BottomLeft
Definition: FullBorderLayout.h:61
QGraphicsLayoutItem
QGraphicsLayoutItem::setGeometry
virtual void setGeometry(const QRectF &rect)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:43:01 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

liblancelot

Skip menu "liblancelot"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

workspace API Reference

Skip menu "workspace API Reference"
  • kdeplasma-addons
  •       GroupingDesktop
  •     liblancelot

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