]> git.lyx.org Git - features.git/commitdiff
* allow resizing of detached panel
authorEdwin Leuven <e.leuven@gmail.com>
Tue, 21 Nov 2006 09:06:12 +0000 (09:06 +0000)
committerEdwin Leuven <e.leuven@gmail.com>
Tue, 21 Nov 2006 09:06:12 +0000 (09:06 +0000)
* FlowLayout comes from:
http://doc.trolltech.com/4.1/layouts-flowlayout.html

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16004 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/qt4/iconpalette.C
src/frontends/qt4/iconpalette.h

index 867f9f210c1cf0608151cf58bd4e5b63ed232c6c..84e143edccf0ab7bdf064686b073967dfd1a4932 100644 (file)
@@ -27,10 +27,125 @@ using std::vector;
 namespace lyx {
 namespace frontend {
 
+FlowLayout::FlowLayout(QWidget *parent) : QLayout(parent)
+{
+       setMargin(0);
+       setSpacing(-1);
+}
+
+
+FlowLayout::~FlowLayout()
+{
+       QLayoutItem *item;
+       while ((item = takeAt(0)))
+               delete item;
+}
+
+
+void FlowLayout::addItem(QLayoutItem *item)
+{
+       itemList.append(item);
+}
+
+
+int FlowLayout::count() const
+{
+       return itemList.size();
+}
+
+
+QLayoutItem *FlowLayout::itemAt(int index) const
+{
+       return itemList.value(index);
+}
+
+
+QLayoutItem *FlowLayout::takeAt(int index)
+{
+       if (index >= 0 && index < itemList.size())
+               return itemList.takeAt(index);
+       else
+               return 0;
+}
+
+
+Qt::Orientations FlowLayout::expandingDirections() const
+{
+       return 0;
+}
+
+
+bool FlowLayout::hasHeightForWidth() const
+{
+       return true;
+}
+
+
+int FlowLayout::heightForWidth(int width) const
+{
+       int height = doLayout(QRect(0, 0, width, 0), true);
+       return height;
+}
+
+
+void FlowLayout::setGeometry(const QRect &rect)
+{
+       QLayout::setGeometry(rect);
+       doLayout(rect, false);
+}
+
+
+QSize FlowLayout::sizeHint() const
+{
+       int const ncols = 5;
+       int const rows = (itemList.size() - 1 )/ncols + 1;
+       int const cols = qMin(rows * itemList.size(), ncols);
+       return QSize(cols * minimumSize().width() + 1,
+               rows * minimumSize().height() + 1);
+}
+
+
+QSize FlowLayout::minimumSize() const
+{
+       QSize size;
+       int const n = itemList.size();
+    for (int i = 0; i < n; ++i) {
+               size = size.expandedTo(itemList.at(i)->minimumSize());
+       }
+       return size;
+}
+
+
+int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
+{
+       int x = rect.x();
+       int y = rect.y();
+       int lineHeight = 0;
+
+    for (int i = 0; i < itemList.size(); ++i) {
+               QLayoutItem * item = itemList.at(i);
+               int nextX = x + item->sizeHint().width() + spacing();
+               if (nextX - spacing() > rect.right() && lineHeight > 0) {
+                       x = rect.x();
+                       y = y + lineHeight + spacing();
+               nextX = x + item->sizeHint().width() + spacing();
+                       lineHeight = 0;
+               }
+
+               if (!testOnly)
+                       item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
+
+               x = nextX;
+               lineHeight = qMax(lineHeight, item->sizeHint().height());
+       }
+       return y + lineHeight - rect.y();
+}
+
+
 IconPalette::IconPalette(QWidget * parent, char const ** entries)
        : QWidget(parent)
 {
-       QGridLayout * layout_ = new QGridLayout(this);
+       FlowLayout * layout_ = new FlowLayout(this);
        layout_->setSpacing(0);
 
        int const button_size = 40;
@@ -41,9 +156,7 @@ IconPalette::IconPalette(QWidget * parent, char const ** entries)
                p->setToolTip(toqstr(string("\\") + entries[i]));
                connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
                buttons_.push_back(make_pair(p, entries[i]));
-               // put in a grid layout with 5 cols
-               int const row = i/5;
-               layout_->addWidget(p, row, i - 5*row);
+               layout_->addWidget(p);
        }
 
 }
index 49c02dfc0be8f9ccd9f8c28979c1c5265f0e02be..d8f3aaa21a286b356698b3d5a0168dbdddde475a 100644 (file)
@@ -13,6 +13,9 @@
 #define ICONPALETTE_H
 
 #include <QWidget>
+#include <QLayout>
+#include <QRect>
+#include <QWidgetItem>
 
 #include <string>
 #include <utility>
@@ -23,6 +26,29 @@ class QPushButton;
 namespace lyx {
 namespace frontend {
 
+class FlowLayout : public QLayout
+{
+public:
+       FlowLayout(QWidget *parent);
+       ~FlowLayout();
+
+       void addItem(QLayoutItem *item);
+       Qt::Orientations expandingDirections() const;
+       bool hasHeightForWidth() const;
+       int heightForWidth(int) const;
+       QSize minimumSize() const;
+       void setGeometry(const QRect &rect);
+       QSize sizeHint() const;
+       QLayoutItem * takeAt(int index);
+       QLayoutItem * itemAt(int index) const;
+       int count() const;
+
+private:
+       int doLayout(const QRect &rect, bool testOnly) const;
+       QList<QLayoutItem *> itemList;
+};
+
+
 /**
  * For holding an arbitrary set of icons.
  */