[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    [Panel-devel] BorderLayout
From:       Ivan =?utf-8?q?=C4=8Cuki=C4=87?= <ivan.cukic+kde () gmail ! com>
Date:       2007-09-14 16:46:16
Message-ID: 200709141846.21345.ivan.cukic+kde () gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


Hi all,

This one I've made for my Lancelot plasmoid/application and thought that it 
could be useful to others.

It is inspired by Java's BorderLayout - there are 5 items that can be shown. 
One central and one for each border - left, right, top and bottom.

+--------------------------+
|           top            |
+---+------------------+---+
| l |                  | r |
| e |                  | i |
| f |      center      | g |
| t |                  | h |
|   |                  | t |
+---+------------------+---+
|          bottom          |
+--------------------------+

Border items are sized to match sizeHints - top and bottom obey height, left 
and right obey width, and the central component is stretched without 
consulting sizeHint.

There is a possibility to disable the usage of sizeHints for borders using the 
  void set*FixedSize()
methods.

The diffs are attached

Cheers.

-- 
You know, there are many people in the country today who,
through no fault of their own, are sane. Some of them were born sane.
Some of them became sane later in their lives...
   -- Monty Python's Flying Circus

["borderlayout.diff" (text/x-diff)]

Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 708682)
+++ CMakeLists.txt	(working copy)
@@ -32,6 +32,7 @@
     theme.cpp
     karambamanager.cpp
     widgets/boxlayout.cpp
+    widgets/borderlayout.cpp
     widgets/checkbox.cpp
     widgets/flowlayout.cpp
     widgets/flash.cpp
@@ -102,6 +103,7 @@
 
 install(FILES
     widgets/boxlayout.h
+    widgets/borderlayout.h
     widgets/hboxlayout.h
     widgets/vboxlayout.h
     widgets/flash.h
Index: widgets/borderlayout.h
===================================================================
--- widgets/borderlayout.h	(revision 0)
+++ widgets/borderlayout.h	(revision 0)
@@ -0,0 +1,82 @@
+/*
+ *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   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
+ *
+ *   You should have received a copy of the GNU Library General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef PLASMA_BORDER_LAYOUT
+#define PLASMA_BORDER_LAYOUT
+
+#include <QtCore/QMap>
+
+#include <plasma/plasma_export.h>
+#include <plasma/widgets/layout.h>
+
+namespace Plasma {
+
+class PLASMA_EXPORT BorderLayout : public Layout {
+public:
+    enum Position {LEFT, RIGHT, TOP, BOTTOM, CENTER};
+
+    // reimplemented
+    virtual Qt::Orientations expandingDirections() const;
+
+    explicit BorderLayout(LayoutItem * parent = 0);
+    virtual ~BorderLayout();
+
+    virtual QRectF geometry() const;
+    void setGeometry(const QRectF& geometry);
+    void invalidate();
+
+    QSizeF sizeHint() const;
+
+    void addItem(Plasma::LayoutItem * item);
+    void addItem(Plasma::LayoutItem * item, Position position);
+
+    void removeItem(Plasma::LayoutItem * item);
+
+    virtual int count() const;
+    virtual int indexOf(Plasma::LayoutItem * item) const;
+    virtual Plasma::LayoutItem * itemAt(int i) const;
+    virtual Plasma::LayoutItem * takeAt(int i);
+
+    void setAllFixedSize(qreal left, qreal top, qreal right, qreal bottom);
+    void setAllAutoSize();
+
+    void setLeftFixedSize(qreal size);
+    void setTopFixedSize(qreal size);
+    void setRightFixedSize(qreal size);
+    void setBottomFixedSize(qreal size);
+
+    qreal leftFixedSize();
+    qreal topFixedSize();
+    qreal rightFixedSize();
+    qreal bottomFixedSize();
+
+    void setLeftAutoSize();
+    void setTopAutoSize();
+    void setRightAutoSize();
+    void setBottomAutoSize();
+
+private:
+    QMap< Position, Plasma::LayoutItem * > m_itemPositions;
+    QList < Plasma::LayoutItem * > m_items;
+    QRectF m_geometry;
+    qreal m_top, m_bottom, m_left, m_right;
+};
+
+}
+
+#endif /* PLASMA_BORDER_LAYOUT */
Index: widgets/borderlayout.cpp
===================================================================
--- widgets/borderlayout.cpp	(revision 0)
+++ widgets/borderlayout.cpp	(revision 0)
@@ -0,0 +1,247 @@
+/*
+ *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   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
+ *
+ *   You should have received a copy of the GNU Library General Public
+ *   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 {
+
+BorderLayout::BorderLayout(LayoutItem * parent) :
+    Plasma::Layout(parent), m_top(0), m_bottom(0), m_left(0), m_right(0)
+{
+}
+
+BorderLayout::~BorderLayout()
+{
+}
+
+Qt::Orientations BorderLayout::expandingDirections() const
+{
+    return Qt::Horizontal | Qt::Vertical;
+}
+
+QRectF BorderLayout::geometry() const
+{
+    return m_geometry;
+}
+
+void BorderLayout::setGeometry(const QRectF& geometry)
+{
+    if (!geometry.isValid() || geometry.isEmpty()) {
+        return;
+    }
+
+    m_geometry = geometry;
+
+    invalidate();
+}
+
+void BorderLayout::invalidate()
+{
+
+    QPointF origin = m_geometry.topLeft();
+    qreal top = 0, bottom = 0, left = 0, right = 0;
+
+    if (m_itemPositions[TOP] /*&& m_itemPositions[TOP]->isVisible()*/) {
+        top = (m_top != 0) ? m_top : m_itemPositions[TOP]->sizeHint().height();
+        m_itemPositions[TOP]->setGeometry(QRectF(origin, QSizeF(
+                m_geometry.width(), top)));
+
+    }
+
+    if (m_itemPositions[BOTTOM] /*&& m_itemPositions[BOTTOM]->isVisible()*/) {
+        bottom = (m_bottom != 0) ? m_bottom
+                : m_itemPositions[BOTTOM]->sizeHint().height();
+        m_itemPositions[BOTTOM]->setGeometry(QRectF(origin + QPointF(0,
+                m_geometry.height() - bottom), QSizeF(m_geometry.width(),
+                bottom)));
+        bottom = m_geometry.height() - bottom;
+    }
+
+    if (m_itemPositions[LEFT] /*&& m_itemPositions[LEFT]->isVisible()*/) {
+        left = (m_left != 0) ? m_left : m_itemPositions[LEFT]->sizeHint().width();
+        m_itemPositions[LEFT]->setGeometry(QRectF(origin + QPointF(0, top),
+                QSizeF(left, bottom - top)));
+    }
+
+    if (m_itemPositions[RIGHT] /*&& m_itemPositions[RIGHT]->isVisible()*/) {
+        right = (m_right != 0) ? m_right : m_itemPositions[RIGHT]->sizeHint().width();
+        m_itemPositions[RIGHT]->setGeometry(QRectF(origin + QPointF(
+                m_geometry.width() - right, top), QSizeF(right, bottom - top)));
+        right = m_geometry.width() - right;
+    }
+
+    if (m_itemPositions[CENTER] /*&& m_itemPositions[CENTER]->isVisible()*/) {
+        m_itemPositions[CENTER]->setGeometry(QRectF(
+                origin + QPointF(left, top), QSizeF(right - left, bottom - top)));
+    }
+
+}
+
+QSizeF BorderLayout::sizeHint() const
+{
+    qreal hintHeight = 0.0;
+    qreal hintWidth = 0.0;
+
+    if (m_itemPositions[TOP] /*&& m_itemPositions[TOP]->isVisible()*/) {
+        hintHeight += m_itemPositions[TOP]->sizeHint().height();
+    }
+
+    if (m_itemPositions[BOTTOM] /*&& m_itemPositions[BOTTOM]->isVisible()*/) {
+        hintHeight += m_itemPositions[BOTTOM]->sizeHint().height();
+    }
+
+    if (m_itemPositions[LEFT] /*&& m_itemPositions[LEFT]->isVisible()*/) {
+        hintWidth += m_itemPositions[LEFT]->sizeHint().width();
+    }
+
+    if (m_itemPositions[RIGHT] /*&& m_itemPositions[RIGHT]->isVisible()*/) {
+        hintWidth += m_itemPositions[RIGHT]->sizeHint().width();
+    }
+
+    if (m_itemPositions[CENTER] /*&& m_itemPositions[CENTER]->isVisible()*/) {
+        hintHeight += m_itemPositions[CENTER]->sizeHint().height();
+        hintWidth += m_itemPositions[CENTER]->sizeHint().width();
+    }
+
+    return QSizeF(hintWidth, hintHeight);
+}
+
+void BorderLayout::addItem(Plasma::LayoutItem * item)
+{
+    BorderLayout::addItem (item, Position(CENTER));
+}
+
+void BorderLayout::addItem(Plasma::LayoutItem * item, Position position)
+{
+    m_itemPositions[position] = item;
+    m_items.append(item);
+}
+
+void BorderLayout::removeItem(Plasma::LayoutItem * item)
+{
+    m_items.removeAll(item);
+
+    QMutableMapIterator< Position, Plasma::LayoutItem * > i(m_itemPositions);
+    while (i.hasNext()) {
+        i.next();
+        if (i.value() == item)
+            i.remove();
+    }
+}
+
+int BorderLayout::count() const
+{
+    return m_items.size();
+}
+
+int BorderLayout::indexOf(Plasma::LayoutItem * item) const
+{
+    return m_items.indexOf(item);
+}
+
+Plasma::LayoutItem * BorderLayout::itemAt(int i) const
+{
+    return m_items[i];
+}
+
+Plasma::LayoutItem * BorderLayout::takeAt(int i)
+{
+    Plasma::LayoutItem * item = itemAt(i);
+    removeItem(item);
+    return item;
+}
+
+void BorderLayout::setLeftFixedSize(qreal size)
+{
+    m_left = size;
+}
+
+void BorderLayout::setTopFixedSize(qreal size)
+{
+    m_top = size;
+}
+
+void BorderLayout::setRightFixedSize(qreal size)
+{
+    m_right = size;
+}
+
+void BorderLayout::setBottomFixedSize(qreal size)
+{
+    m_bottom = size;
+}
+
+qreal BorderLayout::leftFixedSize()
+{
+    return m_left;
+}
+
+qreal BorderLayout::topFixedSize()
+{
+    return m_top;
+}
+
+qreal BorderLayout::rightFixedSize()
+{
+    return m_right;
+}
+
+qreal BorderLayout::bottomFixedSize()
+{
+    return m_bottom;
+}
+
+void BorderLayout::setLeftAutoSize()
+{
+    m_left = 0;
+}
+
+void BorderLayout::setTopAutoSize()
+{
+    m_top = 0;
+}
+
+void BorderLayout::setRightAutoSize()
+{
+    m_right = 0;
+}
+
+void BorderLayout::setBottomAutoSize()
+{
+    m_bottom = 0;
+}
+
+void BorderLayout::setAllFixedSize(qreal left, qreal top, qreal right,
+        qreal bottom)
+{
+    m_left = left;
+    m_top = top;
+    m_right = right;
+    m_bottom = bottom;
+}
+
+void BorderLayout::setAllAutoSize()
+{
+    m_left = 0;
+    m_top = 0;
+    m_right = 0;
+    m_bottom = 0;
+
+}
+
+}

["signature.asc" (application/pgp-signature)]

_______________________________________________
Panel-devel mailing list
Panel-devel@kde.org
https://mail.kde.org/mailman/listinfo/panel-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic