Fix flowLayout

This commit is contained in:
Markus Koch 2022-01-03 16:55:33 +01:00
parent 9ebd0da3f2
commit ed8a481b84
3 changed files with 289 additions and 244 deletions

View File

@ -1,156 +1,190 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). ** Copyright (C) 2016 The Qt Company Ltd.
** All rights reserved. ** Contact: https://www.qt.io/licensing/
** Contact: Nokia Corporation (qt-info@nokia.com) **
** ** This file is part of the examples of the Qt Toolkit.
** This file is part of the examples of the Qt Toolkit. **
** ** $QT_BEGIN_LICENSE:BSD$
** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage
** You may use this file under the terms of the BSD license as follows: ** Licensees holding valid commercial Qt licenses may use this file in
** ** accordance with the commercial license agreement provided with the
** "Redistribution and use in source and binary forms, with or without ** Software or, alternatively, in accordance with the terms contained in
** modification, are permitted provided that the following conditions are ** a written agreement between you and The Qt Company. For licensing terms
** met: ** and conditions see https://www.qt.io/terms-conditions. For further
** * Redistributions of source code must retain the above copyright ** information use the contact form at https://www.qt.io/contact-us.
** notice, this list of conditions and the following disclaimer. **
** * Redistributions in binary form must reproduce the above copyright ** BSD License Usage
** notice, this list of conditions and the following disclaimer in ** Alternatively, you may use this file under the terms of the BSD license
** the documentation and/or other materials provided with the ** as follows:
** distribution. **
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor ** "Redistribution and use in source and binary forms, with or without
** the names of its contributors may be used to endorse or promote ** modification, are permitted provided that the following conditions are
** products derived from this software without specific prior written ** met:
** permission. ** * Redistributions of source code must retain the above copyright
** ** notice, this list of conditions and the following disclaimer.
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** * Redistributions in binary form must reproduce the above copyright
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** notice, this list of conditions and the following disclaimer in
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** the documentation and/or other materials provided with the
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** distribution.
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** * Neither the name of The Qt Company Ltd nor the names of its
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** contributors may be used to endorse or promote products derived
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** from this software without specific prior written permission.
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY **
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT **
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** $QT_END_LICENSE$ ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
****************************************************************************/ ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui> //#include <QtWidget>
#include <QWidget>
#include "flowlayout.h" #include "flowlayout.h"
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) //! [1]
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing) : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
{ {
setContentsMargins(margin, margin, margin, margin); setContentsMargins(margin, margin, margin, margin);
} }
FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
: m_hSpace(hSpacing), m_vSpace(vSpacing) : m_hSpace(hSpacing), m_vSpace(vSpacing)
{ {
setContentsMargins(margin, margin, margin, margin); setContentsMargins(margin, margin, margin, margin);
} }
//! [1]
FlowLayout::~FlowLayout() //! [2]
{ FlowLayout::~FlowLayout()
{
QLayoutItem *item; QLayoutItem *item;
while ((item = takeAt(0))) while ((item = takeAt(0)))
delete item; delete item;
} }
//! [2]
void FlowLayout::addItem(QLayoutItem *item) void FlowLayout::addWidgetAt(QWidget *w, int pos)
{ {
addWidget(w);
this->itemList.move(this->itemList.count()-1, 0);
}
//! [3]
void FlowLayout::addItem(QLayoutItem *item)
{
itemList.append(item); itemList.append(item);
} }
//! [3]
int FlowLayout::horizontalSpacing() const //! [4]
{ int FlowLayout::horizontalSpacing() const
{
if (m_hSpace >= 0) { if (m_hSpace >= 0) {
return m_hSpace; return m_hSpace;
} else { } else {
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing); return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
} }
} }
int FlowLayout::verticalSpacing() const int FlowLayout::verticalSpacing() const
{ {
if (m_vSpace >= 0) { if (m_vSpace >= 0) {
return m_vSpace; return m_vSpace;
} else { } else {
return smartSpacing(QStyle::PM_LayoutVerticalSpacing); return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
} }
} }
//! [4]
int FlowLayout::count() const //! [5]
{ int FlowLayout::count() const
{
return itemList.size(); return itemList.size();
} }
QLayoutItem *FlowLayout::itemAt(int index) const QLayoutItem *FlowLayout::itemAt(int index) const
{ {
return itemList.value(index); return itemList.value(index);
} }
QLayoutItem *FlowLayout::takeAt(int index) QLayoutItem *FlowLayout::takeAt(int index)
{ {
if (index >= 0 && index < itemList.size()) if (index >= 0 && index < itemList.size())
return itemList.takeAt(index); return itemList.takeAt(index);
else return nullptr;
return 0; }
} //! [5]
Qt::Orientations FlowLayout::expandingDirections() const //! [6]
{ Qt::Orientations FlowLayout::expandingDirections() const
return 0; {
} return { };
}
//! [6]
bool FlowLayout::hasHeightForWidth() const //! [7]
{ bool FlowLayout::hasHeightForWidth() const
{
return true; return true;
} }
int FlowLayout::heightForWidth(int width) const int FlowLayout::heightForWidth(int width) const
{ {
int height = doLayout(QRect(0, 0, width, 0), true); int height = doLayout(QRect(0, 0, width, 0), true);
return height; return height;
} }
//! [7]
void FlowLayout::setGeometry(const QRect &rect) //! [8]
{ void FlowLayout::setGeometry(const QRect &rect)
{
QLayout::setGeometry(rect); QLayout::setGeometry(rect);
doLayout(rect, false); doLayout(rect, false);
} }
QSize FlowLayout::sizeHint() const QSize FlowLayout::sizeHint() const
{ {
return minimumSize(); return minimumSize();
} }
QSize FlowLayout::minimumSize() const QSize FlowLayout::minimumSize() const
{ {
QSize size; QSize size;
QLayoutItem *item; for (const QLayoutItem *item : itemList)
foreach (item, itemList)
size = size.expandedTo(item->minimumSize()); size = size.expandedTo(item->minimumSize());
size += QSize(2*margin(), 2*margin()); const QMargins margins = contentsMargins();
size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom());
return size; return size;
} }
//! [8]
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const //! [9]
{ int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
{
int left, top, right, bottom; int left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom); getContentsMargins(&left, &top, &right, &bottom);
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
int x = effectiveRect.x(); int x = effectiveRect.x();
int y = effectiveRect.y(); int y = effectiveRect.y();
int lineHeight = 0; int lineHeight = 0;
//! [9]
QLayoutItem *item; //! [10]
foreach (item, itemList) { for (QLayoutItem *item : itemList) {
QWidget *wid = item->widget(); const QWidget *wid = item->widget();
int spaceX = horizontalSpacing(); int spaceX = horizontalSpacing();
if (spaceX == -1) if (spaceX == -1)
spaceX = wid->style()->layoutSpacing( spaceX = wid->style()->layoutSpacing(
@ -159,6 +193,8 @@
if (spaceY == -1) if (spaceY == -1)
spaceY = wid->style()->layoutSpacing( spaceY = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical); QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
//! [10]
//! [11]
int nextX = x + item->sizeHint().width() + spaceX; int nextX = x + item->sizeHint().width() + spaceX;
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
x = effectiveRect.x(); x = effectiveRect.x();
@ -174,22 +210,19 @@
lineHeight = qMax(lineHeight, item->sizeHint().height()); lineHeight = qMax(lineHeight, item->sizeHint().height());
} }
return y + lineHeight - rect.y() + bottom; return y + lineHeight - rect.y() + bottom;
} }
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const //! [11]
{ //! [12]
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
{
QObject *parent = this->parent(); QObject *parent = this->parent();
if (!parent) { if (!parent) {
return -1; return -1;
} else if (parent->isWidgetType()) { } else if (parent->isWidgetType()) {
QWidget *pw = static_cast<QWidget *>(parent); QWidget *pw = static_cast<QWidget *>(parent);
return pw->style()->pixelMetric(pm, 0, pw); return pw->style()->pixelMetric(pm, nullptr, pw);
} else { } else {
return static_cast<QLayout *>(parent)->spacing(); return static_cast<QLayout *>(parent)->spacing();
} }
} }
void FlowLayout::insertWidgetAt(QWidget *widget, int index) //! [12]
{
addWidget(widget);
this->itemList.move(this->itemList.count()-1, 0);
//this->itemList.insert(index, new QWidgetItem(widget));
}

View File

@ -1,77 +1,89 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). ** Copyright (C) 2016 The Qt Company Ltd.
** All rights reserved. ** Contact: https://www.qt.io/licensing/
** Contact: Nokia Corporation (qt-info@nokia.com) **
** ** This file is part of the examples of the Qt Toolkit.
** This file is part of the examples of the Qt Toolkit. **
** ** $QT_BEGIN_LICENSE:BSD$
** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage
** You may use this file under the terms of the BSD license as follows: ** Licensees holding valid commercial Qt licenses may use this file in
** ** accordance with the commercial license agreement provided with the
** "Redistribution and use in source and binary forms, with or without ** Software or, alternatively, in accordance with the terms contained in
** modification, are permitted provided that the following conditions are ** a written agreement between you and The Qt Company. For licensing terms
** met: ** and conditions see https://www.qt.io/terms-conditions. For further
** * Redistributions of source code must retain the above copyright ** information use the contact form at https://www.qt.io/contact-us.
** notice, this list of conditions and the following disclaimer. **
** * Redistributions in binary form must reproduce the above copyright ** BSD License Usage
** notice, this list of conditions and the following disclaimer in ** Alternatively, you may use this file under the terms of the BSD license
** the documentation and/or other materials provided with the ** as follows:
** distribution. **
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor ** "Redistribution and use in source and binary forms, with or without
** the names of its contributors may be used to endorse or promote ** modification, are permitted provided that the following conditions are
** products derived from this software without specific prior written ** met:
** permission. ** * Redistributions of source code must retain the above copyright
** ** notice, this list of conditions and the following disclaimer.
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** * Redistributions in binary form must reproduce the above copyright
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** notice, this list of conditions and the following disclaimer in
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** the documentation and/or other materials provided with the
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** distribution.
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** * Neither the name of The Qt Company Ltd nor the names of its
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** contributors may be used to endorse or promote products derived
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** from this software without specific prior written permission.
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY **
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT **
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** $QT_END_LICENSE$ ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
****************************************************************************/ ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef FLOWLAYOUT_H #ifndef FLOWLAYOUT_H
#define FLOWLAYOUT_H #define FLOWLAYOUT_H
#include <QLayout> #include <QLayout>
#include <QRect> #include <QRect>
#include <QWidgetItem> #include <QStyle>
class FlowLayout : public QLayout //! [0]
{ class FlowLayout : public QLayout
public: {
FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1); public:
FlowLayout(int margin = -1, int hSpacing = -1, int vSpacing = -1); explicit FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1);
explicit FlowLayout(int margin = -1, int hSpacing = -1, int vSpacing = -1);
~FlowLayout(); ~FlowLayout();
void addItem(QLayoutItem *item); void addWidgetAt(QWidget *w, int pos);
void addItem(QLayoutItem *item) override;
int horizontalSpacing() const; int horizontalSpacing() const;
int verticalSpacing() const; int verticalSpacing() const;
Qt::Orientations expandingDirections() const; Qt::Orientations expandingDirections() const override;
bool hasHeightForWidth() const; bool hasHeightForWidth() const override;
int heightForWidth(int) const; int heightForWidth(int) const override;
int count() const; int count() const override;
QLayoutItem *itemAt(int index) const; QLayoutItem *itemAt(int index) const override;
QSize minimumSize() const; QSize minimumSize() const override;
void setGeometry(const QRect &rect); void setGeometry(const QRect &rect) override;
QSize sizeHint() const; QSize sizeHint() const override;
QLayoutItem *takeAt(int index); QLayoutItem *takeAt(int index) override;
void insertWidgetAt(QWidget *widget, int index);
private: private:
int doLayout(const QRect &rect, bool testOnly) const; int doLayout(const QRect &rect, bool testOnly) const;
int smartSpacing(QStyle::PixelMetric pm) const; int smartSpacing(QStyle::PixelMetric pm) const;
QList<QLayoutItem *> itemList; QList<QLayoutItem *> itemList;
int m_hSpace; int m_hSpace;
int m_vSpace; int m_vSpace;
}; };
//! [0]
#endif #endif // FLOWLAYOUT_H

View File

@ -173,7 +173,7 @@ void MainWindow::searchFinished(KonaSearch *ks) {
KonaPreviewImageView *temp = new KonaPreviewImageView(); KonaPreviewImageView *temp = new KonaPreviewImageView();
temp->setObjectName(QString::number(ki->getId())); temp->setObjectName(QString::number(ki->getId()));
if (insertAtBeginning) { if (insertAtBeginning) {
flowLayout->insertWidgetAt(temp, 0); // Add @ beginning flowLayout->addWidgetAt(temp, 0); // Add @ beginning
} }
else { else {
flowLayout->addWidget(temp); // Add @ end flowLayout->addWidget(temp); // Add @ end