diff options
Diffstat (limited to 'examples/graphicsview/flowlayout')
-rw-r--r-- | examples/graphicsview/flowlayout/flowlayout.cpp | 210 | ||||
-rw-r--r-- | examples/graphicsview/flowlayout/flowlayout.h | 77 | ||||
-rw-r--r-- | examples/graphicsview/flowlayout/flowlayout.pro | 12 | ||||
-rw-r--r-- | examples/graphicsview/flowlayout/main.cpp | 55 | ||||
-rw-r--r-- | examples/graphicsview/flowlayout/window.cpp | 63 | ||||
-rw-r--r-- | examples/graphicsview/flowlayout/window.h | 48 |
6 files changed, 465 insertions, 0 deletions
diff --git a/examples/graphicsview/flowlayout/flowlayout.cpp b/examples/graphicsview/flowlayout/flowlayout.cpp new file mode 100644 index 0000000..d36c37f --- /dev/null +++ b/examples/graphicsview/flowlayout/flowlayout.cpp @@ -0,0 +1,210 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "flowlayout.h" +#include <QtGui/qwidget.h> +#include <QtCore/qmath.h> + +FlowLayout::FlowLayout() +{ + m_spacing[0] = 6; + m_spacing[1] = 6; + QSizePolicy sp = sizePolicy(); + sp.setHeightForWidth(true); + setSizePolicy(sp); +} + +void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item) +{ + item->setParentLayoutItem(this); + if (uint(index) > uint(m_items.count())) + index = m_items.count(); + m_items.insert(index, item); + invalidate(); +} + +int FlowLayout::count() const +{ + return m_items.count(); +} + +QGraphicsLayoutItem *FlowLayout::itemAt(int index) const +{ + return m_items.value(index); +} + +void FlowLayout::removeAt(int index) +{ + m_items.removeAt(index); + invalidate(); +} + +qreal FlowLayout::spacing(Qt::Orientation o) const +{ + return m_spacing[int(o) - 1]; +} + +void FlowLayout::setSpacing(Qt::Orientations o, qreal spacing) +{ + if (o & Qt::Horizontal) + m_spacing[0] = spacing; + if (o & Qt::Vertical) + m_spacing[1] = spacing; +} + +void FlowLayout::setGeometry(const QRectF &geom) +{ + QGraphicsLayout::setGeometry(geom); + doLayout(geom, true); +} + +qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const +{ + QPointF tl = geom.topLeft(); + qreal maxw = geom.width(); + + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + maxw = maxw - left - right; + qreal x = 0; + qreal y = 0; + qreal maxRowHeight = 0; + QSizeF pref; + for (int i = 0; i < m_items.count(); ++i) { + QGraphicsLayoutItem *item = m_items.at(i); + pref = item->effectiveSizeHint(Qt::PreferredSize); + maxRowHeight = qMax(maxRowHeight, pref.height()); + + qreal next_x; + next_x = x + pref.width(); + if (next_x > maxw) { + if (x == 0) { + pref.setWidth(maxw); + } else { + x = 0; + next_x = pref.width(); + } + y += maxRowHeight + spacing(Qt::Vertical); + maxRowHeight = 0; + } + + if (applyNewGeometry) + item->setGeometry(QRectF(QPointF(left + x, top + y), pref)); + x = next_x + spacing(Qt::Horizontal); + } + maxRowHeight = qMax(maxRowHeight, pref.height()); + return top + y + maxRowHeight + bottom; +} + +QSizeF FlowLayout::minSize(const QSizeF &constraint) const +{ + QSizeF size(0, 0); + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + if (constraint.width() > 0) { // height for width + qreal height = doLayout(QRectF(QPointF(0,0), constraint), false); + size = QSizeF(constraint.width(), height); + } else { + QGraphicsLayoutItem *item; + foreach (item, m_items) + size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize)); + size += QSize(left + right, top + bottom); + } + return size; +} + +QSizeF FlowLayout::prefSize() const +{ + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + + QGraphicsLayoutItem *item; + qreal maxh = 0; + qreal totalWidth = 0; + foreach (item, m_items) { + if (totalWidth > 0) + totalWidth += spacing(Qt::Horizontal); + QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); + totalWidth += pref.width(); + maxh = qMax(maxh, pref.height()); + } + maxh += spacing(Qt::Vertical); + + const qreal goldenAspectRatio = 1.61803399; + qreal w = qSqrt(totalWidth * maxh * goldenAspectRatio) + left + right; + + return minSize(QSizeF(w, -1)); +} + +QSizeF FlowLayout::maxSize() const +{ + QGraphicsLayoutItem *item; + qreal totalWidth = 0; + qreal totalHeight = 0; + foreach (item, m_items) { + if (totalWidth > 0) + totalWidth += spacing(Qt::Horizontal); + if (totalHeight > 0) + totalHeight += spacing(Qt::Vertical); + QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); + totalWidth += pref.width(); + totalHeight += pref.height(); + } + + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + return QSizeF(left + totalWidth + right, top + totalHeight + bottom); +} + +QSizeF FlowLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +{ + switch (which) { + case Qt::PreferredSize: + return prefSize(); + case Qt::MinimumSize: + return minSize(constraint); + case Qt::MaximumSize: + return maxSize(); + default: + break; + } + return constraint; +} diff --git a/examples/graphicsview/flowlayout/flowlayout.h b/examples/graphicsview/flowlayout/flowlayout.h new file mode 100644 index 0000000..c1e5635 --- /dev/null +++ b/examples/graphicsview/flowlayout/flowlayout.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/qgraphicslayout.h> + +class FlowLayout : public QGraphicsLayout +{ +public: + FlowLayout(); + inline void addItem(QGraphicsLayoutItem *item); + void insertItem(int index, QGraphicsLayoutItem *item); + void setSpacing(Qt::Orientations o, qreal spacing); + qreal spacing(Qt::Orientation o) const; + + // inherited functions + void setGeometry(const QRectF &geom); + + int count() const; + QGraphicsLayoutItem *itemAt(int index) const; + void removeAt(int index); + +protected: + QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; + +private: + qreal doLayout(const QRectF &geom, bool applyNewGeometry) const; + QSizeF minSize(const QSizeF &constraint) const; + QSizeF prefSize() const; + QSizeF maxSize() const; + + QList<QGraphicsLayoutItem*> m_items; + qreal m_spacing[2]; +}; + + +inline void FlowLayout::addItem(QGraphicsLayoutItem *item) +{ + insertItem(-1, item); +} diff --git a/examples/graphicsview/flowlayout/flowlayout.pro b/examples/graphicsview/flowlayout/flowlayout.pro new file mode 100644 index 0000000..c029d6c --- /dev/null +++ b/examples/graphicsview/flowlayout/flowlayout.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ma 30. mar 12:46:15 2009 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +HEADERS += flowlayout.h window.h +SOURCES += flowlayout.cpp main.cpp window.cpp diff --git a/examples/graphicsview/flowlayout/main.cpp b/examples/graphicsview/flowlayout/main.cpp new file mode 100644 index 0000000..d1a40c7 --- /dev/null +++ b/examples/graphicsview/flowlayout/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include "window.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QGraphicsScene scene; + QGraphicsView *view = new QGraphicsView(&scene); + Window *w = new Window; + scene.addItem(w); + view->show(); + return app.exec(); +} diff --git a/examples/graphicsview/flowlayout/window.cpp b/examples/graphicsview/flowlayout/window.cpp new file mode 100644 index 0000000..dd53b56 --- /dev/null +++ b/examples/graphicsview/flowlayout/window.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/qgraphicsproxywidget.h> +#include <QtGui/qlabel.h> +#include "flowlayout.h" +#include "window.h" + +Window::Window() +: QGraphicsWidget(0, Qt::Window) +{ + FlowLayout *lay = new FlowLayout; + QLatin1String wiseWords("I am not bothered by the fact that I am unknown." + "I am bothered when I do not know others. (Confucius)"); + QString sentence(wiseWords); + QStringList words = sentence.split(QLatin1Char(' '), QString::SkipEmptyParts); + for (int i = 0; i < words.count(); ++i) { + QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this); + QLabel *label = new QLabel(words.at(i)); + label->setFrameStyle(QFrame::Box | QFrame::Plain); + proxy->setWidget(label); + lay->addItem(proxy); + } + setLayout(lay); +} diff --git a/examples/graphicsview/flowlayout/window.h b/examples/graphicsview/flowlayout/window.h new file mode 100644 index 0000000..6d315f5 --- /dev/null +++ b/examples/graphicsview/flowlayout/window.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/qgraphicswidget.h> + +class Window : public QGraphicsWidget { + Q_OBJECT +public: + Window(); +}; |