libplasma
borderlayout.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "borderlayout.h"
00021
00022 namespace Plasma {
00023
00024 class BorderLayout::Private {
00025 public:
00026 BorderLayout * q;
00027
00028 explicit Private(BorderLayout * parent = 0)
00029 : q(parent)
00030 {
00031 sizes[LeftPositioned] = -1;
00032 sizes[RightPositioned] = -1;
00033 sizes[TopPositioned] = -1;
00034 sizes[BottomPositioned] = -1;
00035 sizes[CenterPositioned] = -1;
00036
00037 }
00038
00039 virtual ~Private()
00040 {
00041 }
00042
00043 QMap< Position, LayoutItem * > itemPositions;
00044 QMap< Position, qreal > sizes;
00045 };
00046
00047
00048
00049 BorderLayout::BorderLayout(LayoutItem * parent) :
00050 Layout(parent), d(new Private(this))
00051 {
00052 if (parent) {
00053 parent->setLayout(this);
00054 }
00055 }
00056
00057 BorderLayout::~BorderLayout()
00058 {
00059 delete d;
00060 }
00061
00062 Qt::Orientations BorderLayout::expandingDirections() const
00063 {
00064 return Qt::Horizontal | Qt::Vertical;
00065 }
00066
00067 void BorderLayout::relayout()
00068 {
00069 QRectF rect = geometry();
00070 rect.setTopLeft(rect.topLeft() + QPointF(margin(LeftMargin), margin(TopMargin)));
00071 rect.setBottomRight(rect.bottomRight() - QPointF(margin(RightMargin), margin(BottomMargin)));
00072
00073 QPointF origin = rect.topLeft();
00074 qreal top, bottom, left, right;
00075 top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : 0;
00076 left = (d->sizes[LeftPositioned] >= 0) ? d->sizes[LeftPositioned] : 0;
00077 bottom = rect.height() - ((d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned] : 0);
00078 right = rect.width() - ((d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : 0);
00079
00080 if (d->itemPositions[TopPositioned] ) {
00081 top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : d->itemPositions[TopPositioned]->sizeHint().height();
00082 d->itemPositions[TopPositioned]->setGeometry(QRectF(origin, QSizeF(
00083 rect.width(), top)));
00084 top += spacing();
00085 }
00086
00087 if (d->itemPositions[BottomPositioned] ) {
00088 bottom = (d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned]
00089 : d->itemPositions[BottomPositioned]->sizeHint().height();
00090 d->itemPositions[BottomPositioned]->setGeometry(QRectF(origin + QPointF(0,
00091 rect.height() - bottom), QSizeF(rect.width(),
00092 bottom)));
00093 bottom = rect.height() - bottom - spacing();
00094 }
00095
00096 if (d->itemPositions[LeftPositioned] ) {
00097 left = (d->sizes[LeftPositioned] >= 0) ? d->sizes[LeftPositioned] : d->itemPositions[LeftPositioned]->sizeHint().width();
00098 d->itemPositions[LeftPositioned]->setGeometry(QRectF(origin + QPointF(0, top),
00099 QSizeF(left, bottom - top)));
00100 left += spacing();
00101 }
00102
00103 if (d->itemPositions[RightPositioned] ) {
00104 right = (d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : d->itemPositions[RightPositioned]->sizeHint().width();
00105 d->itemPositions[RightPositioned]->setGeometry(QRectF(origin + QPointF(
00106 rect.width() - right, top), QSizeF(right, bottom - top)));
00107 right = rect.width() - right - spacing();
00108 }
00109
00110 if (d->itemPositions[CenterPositioned] ) {
00111 d->itemPositions[CenterPositioned]->setGeometry(QRectF(
00112 origin + QPointF(left, top), QSizeF(right - left, bottom - top)));
00113 }
00114 }
00115
00116 QSizeF BorderLayout::sizeHint() const
00117 {
00118 qreal hintHeight = 0.0;
00119 qreal hintWidth = 0.0;
00120
00121 if (d->itemPositions[TopPositioned] ) {
00122 hintHeight += d->itemPositions[TopPositioned]->sizeHint().height();
00123 }
00124
00125 if (d->itemPositions[BottomPositioned] ) {
00126 hintHeight += d->itemPositions[BottomPositioned]->sizeHint().height();
00127 }
00128
00129 if (d->itemPositions[LeftPositioned] ) {
00130 hintWidth += d->itemPositions[LeftPositioned]->sizeHint().width();
00131 }
00132
00133 if (d->itemPositions[RightPositioned] ) {
00134 hintWidth += d->itemPositions[RightPositioned]->sizeHint().width();
00135 }
00136
00137 if (d->itemPositions[CenterPositioned] ) {
00138 hintHeight += d->itemPositions[CenterPositioned]->sizeHint().height();
00139 hintWidth += d->itemPositions[CenterPositioned]->sizeHint().width();
00140 }
00141
00142 return QSizeF(hintWidth + 2 + margin(LeftMargin) + margin(RightMargin), hintHeight + 2 + margin(TopMargin) + margin(BottomMargin));
00143 }
00144
00145 void BorderLayout::addItem(Plasma::LayoutItem * item)
00146 {
00147 BorderLayout::addItem (item, CenterPositioned);
00148 }
00149
00150 void BorderLayout::addItem(Plasma::LayoutItem * item, Position position)
00151 {
00152 removeItem(item);
00153 d->itemPositions[position] = item;
00154 item->setManagingLayout(this);
00155 updateGeometry();
00156 }
00157
00158 void BorderLayout::removeItem(LayoutItem * item)
00159 {
00160 QMutableMapIterator< Position, Plasma::LayoutItem * > i(d->itemPositions);
00161 while (i.hasNext()) {
00162 i.next();
00163 if (i.value() == item) {
00164 i.remove();
00165 item->unsetManagingLayout(this);
00166 }
00167 }
00168 updateGeometry();
00169 }
00170
00171 int BorderLayout::count() const
00172 {
00173 int count = 0;
00174 foreach (Plasma::LayoutItem * i, d->itemPositions) {
00175 if (i) {
00176 ++count;
00177 }
00178 }
00179 return count;
00180 }
00181
00182 int BorderLayout::indexOf(LayoutItem * item) const
00183 {
00184 int count = 0;
00185 foreach (Plasma::LayoutItem * i, d->itemPositions) {
00186 if (i) {
00187 if (item == i) {
00188 return count;
00189 }
00190 ++count;
00191 }
00192 }
00193 return -1;
00194 }
00195
00196 LayoutItem * BorderLayout::itemAt(int index) const
00197 {
00198 int count = 0;
00199 foreach (Plasma::LayoutItem * i, d->itemPositions) {
00200 if (i) {
00201 if (index == count) {
00202 return i;
00203 }
00204 count++;
00205
00206 }
00207 }
00208
00209 return 0;
00210 }
00211
00212 Plasma::LayoutItem * BorderLayout::takeAt(int i)
00213 {
00214 Plasma::LayoutItem * item = itemAt(i);
00215 removeItem(item);
00216 return item;
00217 }
00218
00219 void BorderLayout::setSize(qreal size, Position border)
00220 {
00221 d->sizes[border] = size;
00222 updateGeometry();
00223 }
00224
00225 void BorderLayout::setAutoSize(Position border)
00226 {
00227 d->sizes[border] = -1;
00228 updateGeometry();
00229 }
00230
00231 qreal BorderLayout::size(Position border)
00232 {
00233 if (border == CenterPositioned) {
00234 return -1;
00235 }
00236 return d->sizes[border];
00237 }
00238
00239 }