summaryrefslogtreecommitdiffstats
path: root/tests/manual/examples/widgets/graphicsview/flowlayout
diff options
context:
space:
mode:
Diffstat (limited to 'tests/manual/examples/widgets/graphicsview/flowlayout')
-rw-r--r--tests/manual/examples/widgets/graphicsview/flowlayout/CMakeLists.txt38
-rw-r--r--tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.cpp170
-rw-r--r--tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.h44
-rw-r--r--tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.pro10
-rw-r--r--tests/manual/examples/widgets/graphicsview/flowlayout/main.cpp24
-rw-r--r--tests/manual/examples/widgets/graphicsview/flowlayout/window.cpp24
-rw-r--r--tests/manual/examples/widgets/graphicsview/flowlayout/window.h16
7 files changed, 326 insertions, 0 deletions
diff --git a/tests/manual/examples/widgets/graphicsview/flowlayout/CMakeLists.txt b/tests/manual/examples/widgets/graphicsview/flowlayout/CMakeLists.txt
new file mode 100644
index 0000000000..66ad4707b5
--- /dev/null
+++ b/tests/manual/examples/widgets/graphicsview/flowlayout/CMakeLists.txt
@@ -0,0 +1,38 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+cmake_minimum_required(VERSION 3.16)
+project(flowlayout LANGUAGES CXX)
+
+if(NOT DEFINED INSTALL_EXAMPLESDIR)
+ set(INSTALL_EXAMPLESDIR "examples")
+endif()
+
+set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/graphicsview_flowlayout")
+
+find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
+
+qt_standard_project_setup()
+
+qt_add_executable(graphicsview_flowlayout
+ flowlayout.cpp flowlayout.h
+ main.cpp
+ window.cpp window.h
+)
+
+set_target_properties(graphicsview_flowlayout PROPERTIES
+ WIN32_EXECUTABLE TRUE
+ MACOSX_BUNDLE TRUE
+)
+
+target_link_libraries(graphicsview_flowlayout PRIVATE
+ Qt6::Core
+ Qt6::Gui
+ Qt6::Widgets
+)
+
+install(TARGETS graphicsview_flowlayout
+ RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
+ LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+)
diff --git a/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.cpp b/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.cpp
new file mode 100644
index 0000000000..ab5f4717b3
--- /dev/null
+++ b/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.cpp
@@ -0,0 +1,170 @@
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include "flowlayout.h"
+
+#include <QtMath>
+
+FlowLayout::FlowLayout(QGraphicsLayoutItem *parent) : QGraphicsLayout(parent)
+{
+ QSizePolicy sp = sizePolicy();
+ sp.setHeightForWidth(true);
+ setSizePolicy(sp);
+}
+
+void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item)
+{
+ item->setParentLayoutItem(this);
+ if (index > m_items.count() || index < 0)
+ 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
+{
+ qreal left, top, right, bottom;
+ getContentsMargins(&left, &top, &right, &bottom);
+ const qreal maxw = geom.width() - left - right;
+
+ qreal x = 0;
+ qreal y = 0;
+ qreal maxRowHeight = 0;
+ QSizeF pref;
+ for (QGraphicsLayoutItem *item : m_items) {
+ pref = item->effectiveSizeHint(Qt::PreferredSize);
+ maxRowHeight = qMax(maxRowHeight, pref.height());
+
+ qreal next_x;
+ next_x = x + pref.width();
+ if (next_x > maxw) {
+ if (qFuzzyIsNull(x)) {
+ 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
+ const qreal height = doLayout(QRectF(QPointF(0,0), constraint), false);
+ size = QSizeF(constraint.width(), height);
+ } else if (constraint.height() >= 0) { // width for height?
+ // not supported
+ } else {
+ for (const QGraphicsLayoutItem *item : std::as_const(m_items))
+ size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize));
+ size += QSizeF(left + right, top + bottom);
+ }
+ return size;
+}
+
+QSizeF FlowLayout::prefSize() const
+{
+ qreal left, right;
+ getContentsMargins(&left, nullptr, &right, nullptr);
+
+ qreal maxh = 0;
+ qreal totalWidth = 0;
+ for (const QGraphicsLayoutItem *item : std::as_const(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
+{
+ qreal totalWidth = 0;
+ qreal totalHeight = 0;
+ for (const QGraphicsLayoutItem *item : std::as_const(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
+{
+ QSizeF sh = constraint;
+ switch (which) {
+ case Qt::PreferredSize:
+ sh = prefSize();
+ break;
+ case Qt::MinimumSize:
+ sh = minSize(constraint);
+ break;
+ case Qt::MaximumSize:
+ sh = maxSize();
+ break;
+ default:
+ break;
+ }
+ return sh;
+}
diff --git a/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.h b/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.h
new file mode 100644
index 0000000000..028394827a
--- /dev/null
+++ b/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.h
@@ -0,0 +1,44 @@
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#ifndef FLOWLAYOUT_H
+#define FLOWLAYOUT_H
+
+#include <QGraphicsLayout>
+
+class FlowLayout : public QGraphicsLayout
+{
+public:
+ FlowLayout(QGraphicsLayoutItem *parent = nullptr);
+ 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) override;
+
+ int count() const override;
+ QGraphicsLayoutItem *itemAt(int index) const override;
+ void removeAt(int index) override;
+
+protected:
+ QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override;
+
+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] = {6, 6};
+};
+
+
+inline void FlowLayout::addItem(QGraphicsLayoutItem *item)
+{
+ insertItem(-1, item);
+}
+
+#endif // FLOWLAYOUT_H
diff --git a/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.pro b/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.pro
new file mode 100644
index 0000000000..7830e3a008
--- /dev/null
+++ b/tests/manual/examples/widgets/graphicsview/flowlayout/flowlayout.pro
@@ -0,0 +1,10 @@
+QT += widgets
+
+QMAKE_PROJECT_NAME = flowlayout_graphicsview
+
+HEADERS += flowlayout.h window.h
+SOURCES += flowlayout.cpp main.cpp window.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/widgets/graphicsview/flowlayout
+INSTALLS += target
diff --git a/tests/manual/examples/widgets/graphicsview/flowlayout/main.cpp b/tests/manual/examples/widgets/graphicsview/flowlayout/main.cpp
new file mode 100644
index 0000000000..118b404870
--- /dev/null
+++ b/tests/manual/examples/widgets/graphicsview/flowlayout/main.cpp
@@ -0,0 +1,24 @@
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+//! [1]
+#include "window.h"
+
+#include <QApplication>
+#include <QGraphicsView>
+
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ QGraphicsScene scene;
+ QGraphicsView view(&scene);
+ Window *w = new Window;
+ scene.addItem(w);
+
+ view.resize(400, 300);
+ view.show();
+
+ return app.exec();
+}
+//! [1]
diff --git a/tests/manual/examples/widgets/graphicsview/flowlayout/window.cpp b/tests/manual/examples/widgets/graphicsview/flowlayout/window.cpp
new file mode 100644
index 0000000000..ed9a5720d3
--- /dev/null
+++ b/tests/manual/examples/widgets/graphicsview/flowlayout/window.cpp
@@ -0,0 +1,24 @@
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include "window.h"
+#include "flowlayout.h"
+
+#include <QGraphicsProxyWidget>
+#include <QLabel>
+
+Window::Window(QGraphicsItem *parent) : QGraphicsWidget(parent, Qt::Window)
+{
+ FlowLayout *lay = new FlowLayout;
+ const QString sentence(QLatin1String("I am not bothered by the fact that I am unknown."
+ " I am bothered when I do not know others. (Confucius)"));
+ const QList<QStringView> words = QStringView{ sentence }.split(QLatin1Char(' '), Qt::SkipEmptyParts);
+ for (const QStringView &word : words) {
+ QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this);
+ QLabel *label = new QLabel(word.toString());
+ label->setFrameStyle(QFrame::Box | QFrame::Plain);
+ proxy->setWidget(label);
+ lay->addItem(proxy);
+ }
+ setLayout(lay);
+}
diff --git a/tests/manual/examples/widgets/graphicsview/flowlayout/window.h b/tests/manual/examples/widgets/graphicsview/flowlayout/window.h
new file mode 100644
index 0000000000..2fc5570406
--- /dev/null
+++ b/tests/manual/examples/widgets/graphicsview/flowlayout/window.h
@@ -0,0 +1,16 @@
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#ifndef WINDOW_H
+#define WINDOW_H
+
+#include <QGraphicsWidget>
+
+class Window : public QGraphicsWidget
+{
+ Q_OBJECT
+public:
+ Window(QGraphicsItem *parent = nullptr);
+};
+
+#endif // WINDOW_H