Debian Codegraph @

Dekko/components/StretchRow.cpp

raw symbols SCIP
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 "StretchRow.h"
19
20 #include <QElapsedTimer>
21 #include <QDebug>
22 #include <qmath.h>
23
24 StretchRow::StretchRow(QQuickItem * parent) : QQuickItem (parent)
25 , m_spacing (0)
26 { }
27
28 int StretchRow::getSpacing(void) const {
29 return m_spacing;
30 }
31
32 void StretchRow::setSpacing(int spacing) {
33 if (m_spacing != spacing) {
34 m_spacing = spacing;
35 emit spacingChanged (spacing);
36 }
37 }
38
39 void StretchRow::classBegin(void) {
40 connect(this, &StretchRow::childrenChanged, this, &StretchRow::polish);
41 connect(this, &StretchRow::visibleChanged, this, &StretchRow::polish);
42 connect(this, &StretchRow::widthChanged, this, &StretchRow::polish);
43 connect(this, &StretchRow::heightChanged, this, &StretchRow::polish);
44 connect(this, &StretchRow::spacingChanged, this, &StretchRow::polish);
45 }
46
47 void StretchRow::componentComplete (void) {
48 polish ();
49 }
50
51 void StretchRow::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->implicitHeight () > tmpH) {
62 tmpH = child->implicitHeight ();
63 }
64 if (child->implicitWidth () >= 0) {
65 tmpW += child->implicitWidth ();
66 }
67 else {
68 nbStretch++;
69 }
70 nbItems++;
71 }
72 }
73 /// resize layout
74 if (nbItems > 1) {
75 tmpW += (m_spacing * (nbItems -1));
76 }
77 setImplicitWidth (tmpW);
78 setImplicitHeight (tmpH);
79 const qreal layoutWidth = width ();
80 const qreal autoSize = (nbStretch > 0 ? (layoutWidth - implicitWidth ()) / qreal (nbStretch) : 0);
81 /// position children
82 int currX = 0;
83 for (QList<QQuickItem *>::const_iterator it = childrenList.constBegin (); it != childrenList.constEnd (); it++) {
84 QQuickItem * child = (* it);
85 if (child != Q_NULLPTR && !child->inherits ("QQuickRepeater") && child->isVisible ()) {
86 if (currX) {
87 currX += m_spacing;
88 }
89 child->setX (currX);
90 child->setWidth (child->implicitWidth () >= 0 ? child->implicitWidth () : autoSize);
91 currX += child->width ();
92 }
93 }
94 }
95
96 void StretchRow::itemChange (QQuickItem::ItemChange change, const QQuickItem::ItemChangeData & value) {
97 if (change == QQuickItem::ItemChildAddedChange) {
98 QQuickItem * child = value.item;
99 if (child != Q_NULLPTR) {
100 connect (child, &QQuickItem::visibleChanged,
101 this, &StretchRow::polish, Qt::UniqueConnection);
102 connect (child, &QQuickItem::implicitWidthChanged,
103 this, &StretchRow::polish, Qt::UniqueConnection);
104 connect (child, &QQuickItem::implicitHeightChanged,
105 this, &StretchRow::polish, Qt::UniqueConnection);
106 }
107 }
108 }
109