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 BorderLayoutPrivate {
00025 public:
00026 BorderLayout * q;
00027
00028 explicit BorderLayoutPrivate(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 ~BorderLayoutPrivate()
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 BorderLayoutPrivate(this))
00051 {
00052 if (parent) {
00053 parent->setLayout(this);
00054 }
00055 }
00056
00057 BorderLayout::~BorderLayout()
00058 {
00059 releaseManagedItems();
00060 delete d;
00061 }
00062
00063 Qt::Orientations BorderLayout::expandingDirections() const
00064 {
00065 return Qt::Horizontal | Qt::Vertical;
00066 }
00067
00068 void BorderLayout::relayout()
00069 {
00070 QRectF rect = geometry();
00071 rect.setTopLeft(rect.topLeft() + QPointF(margin(LeftMargin), margin(TopMargin)));
00072 rect.setBottomRight(rect.bottomRight() - QPointF(margin(RightMargin), margin(BottomMargin)));
00073
00074 QPointF origin = rect.topLeft();
00075 qreal top, bottom, left, right;
00076 top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : 0;
00077 left = (d->sizes[LeftPositioned] >= 0) ? d->sizes[LeftPositioned] : 0;
00078 bottom = rect.height() - ((d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned] : 0);
00079 right = rect.width() - ((d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : 0);
00080
00081 if (d->itemPositions[TopPositioned] ) {
00082 top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : d->itemPositions[TopPositioned]->sizeHint().height();
00083 d->itemPositions[TopPositioned]->setGeometry(QRectF(origin, QSizeF(
00084 rect.width(), top)));
00085 top += spacing();
00086 }
00087
00088 if (d->itemPositions[BottomPositioned] ) {
00089 bottom = (d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned]
00090 : d->itemPositions[BottomPositioned]->sizeHint().height();
00091 d->itemPositions[BottomPositioned]->setGeometry(QRectF(origin + QPointF(0,
00092 rect.height() - bottom), QSizeF(rect.width(),
00093 bottom)));
00094 bottom = rect.height() - bottom - spacing();
00095 }
00096
00097 if (d->itemPositions[LeftPositioned] ) {
00098 left = (d->sizes[LeftPositioned] >= 0) ? d->sizes[LeftPositioned] : d->itemPositions[LeftPositioned]->sizeHint().width();
00099 d->itemPositions[LeftPositioned]->setGeometry(QRectF(origin + QPointF(0, top),
00100 QSizeF(left, bottom - top)));
00101 left += spacing();
00102 }
00103
00104 if (d->itemPositions[RightPositioned] ) {
00105 right = (d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : d->itemPositions[RightPositioned]->sizeHint().width();
00106 d->itemPositions[RightPositioned]->setGeometry(QRectF(origin + QPointF(
00107 rect.width() - right, top), QSizeF(right, bottom - top)));
00108 right = rect.width() - right - spacing();
00109 }
00110
00111 if (d->itemPositions[CenterPositioned] ) {
00112 d->itemPositions[CenterPositioned]->setGeometry(QRectF(
00113 origin + QPointF(left, top), QSizeF(right - left, bottom - top)));
00114 }
00115 }
00116
00117 void BorderLayout::releaseManagedItems()
00118 {
00119 foreach (Plasma::LayoutItem *item, d->itemPositions) {
00120 if (item) {
00121 item->unsetManagingLayout(this);
00122 }
00123 }
00124 }
00125
00126 QSizeF BorderLayout::sizeHint() const
00127 {
00128 qreal hintHeight = 0.0;
00129 qreal hintWidth = 0.0;
00130
00131 if (d->itemPositions[TopPositioned] ) {
00132 hintHeight += d->itemPositions[TopPositioned]->sizeHint().height();
00133 }
00134
00135 if (d->itemPositions[BottomPositioned] ) {
00136 hintHeight += d->itemPositions[BottomPositioned]->sizeHint().height();
00137 }
00138
00139 if (d->itemPositions[LeftPositioned] ) {
00140 hintWidth += d->itemPositions[LeftPositioned]->sizeHint().width();
00141 }
00142
00143 if (d->itemPositions[RightPositioned] ) {
00144 hintWidth += d->itemPositions[RightPositioned]->sizeHint().width();
00145 }
00146
00147 if (d->itemPositions[CenterPositioned] ) {
00148 hintHeight += d->itemPositions[CenterPositioned]->sizeHint().height();
00149 hintWidth += d->itemPositions[CenterPositioned]->sizeHint().width();
00150 }
00151
00152 return QSizeF(hintWidth + 2 + margin(LeftMargin) + margin(RightMargin), hintHeight + 2 + margin(TopMargin) + margin(BottomMargin));
00153 }
00154
00155 void BorderLayout::addItem(Plasma::LayoutItem * item)
00156 {
00157 BorderLayout::addItem (item, CenterPositioned);
00158 }
00159
00160 void BorderLayout::addItem(Plasma::LayoutItem * item, Position position)
00161 {
00162 removeItem(item);
00163 d->itemPositions[position] = item;
00164 item->setManagingLayout(this);
00165 updateGeometry();
00166 }
00167
00168 void BorderLayout::removeItem(LayoutItem * item)
00169 {
00170 QMutableMapIterator< Position, Plasma::LayoutItem * > i(d->itemPositions);
00171 while (i.hasNext()) {
00172 i.next();
00173 if (i.value() == item) {
00174 i.remove();
00175 item->unsetManagingLayout(this);
00176 }
00177 }
00178 updateGeometry();
00179 }
00180
00181 int BorderLayout::count() const
00182 {
00183 int count = 0;
00184 foreach (Plasma::LayoutItem * i, d->itemPositions) {
00185 if (i) {
00186 ++count;
00187 }
00188 }
00189 return count;
00190 }
00191
00192 int BorderLayout::indexOf(LayoutItem * item) const
00193 {
00194 int count = 0;
00195 foreach (Plasma::LayoutItem * i, d->itemPositions) {
00196 if (i) {
00197 if (item == i) {
00198 return count;
00199 }
00200 ++count;
00201 }
00202 }
00203 return -1;
00204 }
00205
00206 LayoutItem * BorderLayout::itemAt(int index) const
00207 {
00208 int count = 0;
00209 foreach (Plasma::LayoutItem * i, d->itemPositions) {
00210 if (i) {
00211 if (index == count) {
00212 return i;
00213 }
00214 count++;
00215
00216 }
00217 }
00218
00219 return 0;
00220 }
00221
00222 Plasma::LayoutItem * BorderLayout::takeAt(int i)
00223 {
00224 Plasma::LayoutItem * item = itemAt(i);
00225 removeItem(item);
00226 return item;
00227 }
00228
00229 void BorderLayout::setSize(qreal size, Position border)
00230 {
00231 d->sizes[border] = size;
00232 updateGeometry();
00233 }
00234
00235 void BorderLayout::setAutoSize(Position border)
00236 {
00237 d->sizes[border] = -1;
00238 updateGeometry();
00239 }
00240
00241 qreal BorderLayout::size(Position border)
00242 {
00243 if (border == CenterPositioned) {
00244 return -1;
00245 }
00246 return d->sizes[border];
00247 }
00248
00249 }