| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "StretchColumn.h" |
| 19 | |
| 20 | #include <QElapsedTimer> |
| 21 | #include <QDebug> |
| 22 | #include <qmath.h> |
| 23 | |
| 24 | StretchColumn::StretchColumn (QQuickItem * parent) : QQuickItem (parent) |
| 25 | , m_spacing (0) |
| 26 | { } |
| 27 | |
| 28 | int StretchColumn::getSpacing (void) const { |
| 29 | return m_spacing; |
| 30 | } |
| 31 | |
| 32 | void StretchColumn::setSpacing (int spacing) { |
| 33 | if (m_spacing != spacing) { |
| 34 | m_spacing = spacing; |
| 35 | emit spacingChanged (spacing); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void StretchColumn::classBegin (void) { |
| 40 | connect (this, &StretchColumn::childrenChanged, this, &StretchColumn::polish); |
| 41 | connect (this, &StretchColumn::visibleChanged, this, &StretchColumn::polish); |
| 42 | connect (this, &StretchColumn::widthChanged, this, &StretchColumn::polish); |
| 43 | connect (this, &StretchColumn::heightChanged, this, &StretchColumn::polish); |
| 44 | connect (this, &StretchColumn::spacingChanged, this, &StretchColumn::polish); |
| 45 | } |
| 46 | |
| 47 | void StretchColumn::componentComplete (void) { |
| 48 | polish (); |
| 49 | } |
| 50 | |
| 51 | void StretchColumn::updatePolish (void) { |
| 52 | const QList<QQuickItem *> childrenList = childItems (); |
| 53 | /// find items and stretchers |
| 54 | qreal tmpW = 0; |
| 55 | qreal tmpH = 0; |
| 56 | int nbItems = 0; |
| 57 | int nbStretch = 0; |
| 58 | for (QList<QQuickItem *>::const_iterator it = childrenList.constBegin (); it != childrenList.constEnd (); it++) { |
| 59 | QQuickItem * child = (* it); |
| 60 | if (child != Q_NULLPTR && !child->inherits ("QQuickRepeater") && child->isVisible ()) { |
| 61 | if (child->implicitWidth () > tmpW) { |
| 62 | tmpW = child->implicitWidth (); |
| 63 | } |
| 64 | if (child->implicitHeight () >= 0) { |
| 65 | tmpH += child->implicitHeight (); |
| 66 | } |
| 67 | else { |
| 68 | nbStretch++; |
| 69 | } |
| 70 | nbItems++; |
| 71 | } |
| 72 | } |
| 73 | /// resize layout |
| 74 | if (nbItems > 1) { |
| 75 | tmpH += (m_spacing * (nbItems -1)); |
| 76 | } |
| 77 | setImplicitWidth (tmpW); |
| 78 | setImplicitHeight (tmpH); |
| 79 | const qreal layoutWidth = width (); |
| 80 | const qreal layoutHeight = height (); |
| 81 | const qreal autoSize = (nbStretch > 0 ? (layoutHeight - tmpH) / qreal (nbStretch) : 0); |
| 82 | /// position children |
| 83 | int currY = 0; |
| 84 | for (QList<QQuickItem *>::const_iterator it = childrenList.constBegin (); it != childrenList.constEnd (); it++) { |
| 85 | QQuickItem * child = (* it); |
| 86 | if (child != Q_NULLPTR && !child->inherits ("QQuickRepeater") && child->isVisible ()) { |
| 87 | if (currY) { |
| 88 | currY += m_spacing; |
| 89 | } |
| 90 | child->setY (currY); |
| 91 | child->setWidth (layoutWidth); |
| 92 | child->setHeight (child->implicitHeight () >= 0 ? child->implicitHeight () : autoSize); |
| 93 | currY += child->height (); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void StretchColumn::itemChange (QQuickItem::ItemChange change, const QQuickItem::ItemChangeData & value) { |
| 99 | if (change == QQuickItem::ItemChildAddedChange) { |
| 100 | QQuickItem * child = value.item; |
| 101 | if (child != Q_NULLPTR) { |
| 102 | connect (child, &QQuickItem::visibleChanged, |
| 103 | this, &StretchColumn::polish, Qt::UniqueConnection); |
| 104 | connect (child, &QQuickItem::implicitWidthChanged, |
| 105 | this, &StretchColumn::polish, Qt::UniqueConnection); |
| 106 | connect (child, &QQuickItem::implicitHeightChanged, |
| 107 | this, &StretchColumn::polish, Qt::UniqueConnection); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |