2007-09-16 11:08:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-10-12 10:45:12 +02:00
|
|
|
* it under the terms of the GNU Library/Lesser General Public License
|
|
|
|
* version 2, or (at your option) any later version, as published by the
|
|
|
|
* Free Software Foundation
|
2007-09-16 11:08:08 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
2007-10-12 10:45:12 +02:00
|
|
|
* You should have received a copy of the GNU Library/Lesser General Public
|
2007-09-16 11:08:08 +02:00
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "borderlayout.h"
|
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
|
|
|
class BorderLayout::Private {
|
|
|
|
public:
|
|
|
|
BorderLayout * q;
|
|
|
|
|
|
|
|
explicit Private(BorderLayout * parent = 0)
|
2008-02-29 18:50:57 +01:00
|
|
|
: q(parent)
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
|
|
|
sizes[LeftPositioned] = -1;
|
|
|
|
sizes[RightPositioned] = -1;
|
|
|
|
sizes[TopPositioned] = -1;
|
|
|
|
sizes[BottomPositioned] = -1;
|
|
|
|
sizes[CenterPositioned] = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Private()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
QMap< Position, LayoutItem * > itemPositions;
|
2007-09-16 11:08:08 +02:00
|
|
|
QMap< Position, qreal > sizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
BorderLayout::BorderLayout(LayoutItem * parent) :
|
|
|
|
Layout(parent), d(new Private(this))
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
if (parent) {
|
|
|
|
parent->setLayout(this);
|
|
|
|
}
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BorderLayout::~BorderLayout()
|
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
releaseManagedItems();
|
2007-09-16 11:08:08 +02:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
Qt::Orientations BorderLayout::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal | Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
void BorderLayout::relayout()
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
2007-11-20 00:45:56 +01:00
|
|
|
QRectF rect = geometry();
|
2008-02-29 18:50:57 +01:00
|
|
|
rect.setTopLeft(rect.topLeft() + QPointF(margin(LeftMargin), margin(TopMargin)));
|
|
|
|
rect.setBottomRight(rect.bottomRight() - QPointF(margin(RightMargin), margin(BottomMargin)));
|
2007-09-16 11:08:08 +02:00
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
QPointF origin = rect.topLeft();
|
2007-10-10 20:27:57 +02:00
|
|
|
qreal top, bottom, left, right;
|
|
|
|
top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : 0;
|
|
|
|
left = (d->sizes[LeftPositioned] >= 0) ? d->sizes[LeftPositioned] : 0;
|
2007-11-20 00:45:56 +01:00
|
|
|
bottom = rect.height() - ((d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned] : 0);
|
|
|
|
right = rect.width() - ((d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : 0);
|
2007-09-16 11:08:08 +02:00
|
|
|
|
|
|
|
if (d->itemPositions[TopPositioned] /*&& d->itemPositions[TopPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : d->itemPositions[TopPositioned]->sizeHint().height();
|
2007-09-16 11:08:08 +02:00
|
|
|
d->itemPositions[TopPositioned]->setGeometry(QRectF(origin, QSizeF(
|
2007-11-20 00:45:56 +01:00
|
|
|
rect.width(), top)));
|
2007-10-11 15:50:54 +02:00
|
|
|
top += spacing();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[BottomPositioned] /*&& d->itemPositions[BottomPositioned]->isVisible()*/) {
|
|
|
|
bottom = (d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned]
|
2008-02-29 18:50:57 +01:00
|
|
|
: d->itemPositions[BottomPositioned]->sizeHint().height();
|
2007-09-16 11:08:08 +02:00
|
|
|
d->itemPositions[BottomPositioned]->setGeometry(QRectF(origin + QPointF(0,
|
2007-11-20 00:45:56 +01:00
|
|
|
rect.height() - bottom), QSizeF(rect.width(),
|
2007-09-16 11:08:08 +02:00
|
|
|
bottom)));
|
2007-11-20 00:45:56 +01:00
|
|
|
bottom = rect.height() - bottom - spacing();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[LeftPositioned] /*&& d->itemPositions[LeftPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
left = (d->sizes[LeftPositioned] >= 0) ? d->sizes[LeftPositioned] : d->itemPositions[LeftPositioned]->sizeHint().width();
|
2007-09-16 11:08:08 +02:00
|
|
|
d->itemPositions[LeftPositioned]->setGeometry(QRectF(origin + QPointF(0, top),
|
|
|
|
QSizeF(left, bottom - top)));
|
2007-10-11 15:50:54 +02:00
|
|
|
left += spacing();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[RightPositioned] /*&& d->itemPositions[RightPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
right = (d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : d->itemPositions[RightPositioned]->sizeHint().width();
|
2007-09-16 11:08:08 +02:00
|
|
|
d->itemPositions[RightPositioned]->setGeometry(QRectF(origin + QPointF(
|
2007-11-20 00:45:56 +01:00
|
|
|
rect.width() - right, top), QSizeF(right, bottom - top)));
|
|
|
|
right = rect.width() - right - spacing();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[CenterPositioned] /*&& d->itemPositions[CenterPositioned]->isVisible()*/) {
|
|
|
|
d->itemPositions[CenterPositioned]->setGeometry(QRectF(
|
|
|
|
origin + QPointF(left, top), QSizeF(right - left, bottom - top)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void BorderLayout::releaseManagedItems()
|
|
|
|
{
|
|
|
|
foreach (Plasma::LayoutItem *item, d->itemPositions) {
|
|
|
|
item->unsetManagingLayout(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF BorderLayout::sizeHint() const
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
|
|
|
qreal hintHeight = 0.0;
|
|
|
|
qreal hintWidth = 0.0;
|
|
|
|
|
|
|
|
if (d->itemPositions[TopPositioned] /*&& d->itemPositions[TopPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
hintHeight += d->itemPositions[TopPositioned]->sizeHint().height();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[BottomPositioned] /*&& d->itemPositions[BottomPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
hintHeight += d->itemPositions[BottomPositioned]->sizeHint().height();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[LeftPositioned] /*&& d->itemPositions[LeftPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
hintWidth += d->itemPositions[LeftPositioned]->sizeHint().width();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[RightPositioned] /*&& d->itemPositions[RightPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
hintWidth += d->itemPositions[RightPositioned]->sizeHint().width();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->itemPositions[CenterPositioned] /*&& d->itemPositions[CenterPositioned]->isVisible()*/) {
|
2008-02-29 18:50:57 +01:00
|
|
|
hintHeight += d->itemPositions[CenterPositioned]->sizeHint().height();
|
|
|
|
hintWidth += d->itemPositions[CenterPositioned]->sizeHint().width();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
return QSizeF(hintWidth + 2 + margin(LeftMargin) + margin(RightMargin), hintHeight + 2 + margin(TopMargin) + margin(BottomMargin));
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void BorderLayout::addItem(Plasma::LayoutItem * item)
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
|
|
|
BorderLayout::addItem (item, CenterPositioned);
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void BorderLayout::addItem(Plasma::LayoutItem * item, Position position)
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
2007-11-19 23:04:30 +01:00
|
|
|
removeItem(item);
|
2007-09-16 11:08:08 +02:00
|
|
|
d->itemPositions[position] = item;
|
2008-02-29 18:50:57 +01:00
|
|
|
item->setManagingLayout(this);
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void BorderLayout::removeItem(LayoutItem * item)
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
QMutableMapIterator< Position, Plasma::LayoutItem * > i(d->itemPositions);
|
2007-09-16 11:08:08 +02:00
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value() == item) {
|
|
|
|
i.remove();
|
2008-02-29 18:50:57 +01:00
|
|
|
item->unsetManagingLayout(this);
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int BorderLayout::count() const
|
|
|
|
{
|
|
|
|
int count = 0;
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach (Plasma::LayoutItem * i, d->itemPositions) {
|
2007-09-16 11:08:08 +02:00
|
|
|
if (i) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
int BorderLayout::indexOf(LayoutItem * item) const
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
|
|
|
int count = 0;
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach (Plasma::LayoutItem * i, d->itemPositions) {
|
2007-09-16 11:08:08 +02:00
|
|
|
if (i) {
|
|
|
|
if (item == i) {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem * BorderLayout::itemAt(int index) const
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
|
|
|
int count = 0;
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach (Plasma::LayoutItem * i, d->itemPositions) {
|
2007-09-16 11:08:08 +02:00
|
|
|
if (i) {
|
|
|
|
if (index == count) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2007-09-20 23:44:12 +02:00
|
|
|
|
|
|
|
return 0;
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
Plasma::LayoutItem * BorderLayout::takeAt(int i)
|
2007-09-16 11:08:08 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
Plasma::LayoutItem * item = itemAt(i);
|
2007-09-16 11:08:08 +02:00
|
|
|
removeItem(item);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BorderLayout::setSize(qreal size, Position border)
|
|
|
|
{
|
|
|
|
d->sizes[border] = size;
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BorderLayout::setAutoSize(Position border)
|
|
|
|
{
|
|
|
|
d->sizes[border] = -1;
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-09-16 11:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal BorderLayout::size(Position border)
|
|
|
|
{
|
|
|
|
if (border == CenterPositioned) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return d->sizes[border];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|