~zsombi/ubuntu-ui-toolkit/listitemSelectModeBugs

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/plugin/ucbottomedgeregion.cpp

  • Committer: Zsombor Egri
  • Date: 2015-12-07 17:02:28 UTC
  • mfrom: (1662.2.80 staging)
  • Revision ID: zsombor.egri@canonical.com-20151207170228-coqv9fvqublui315
staging sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Author: Zsombor Egri <zsombor.egri@canonical.com>
 
17
 */
 
18
 
 
19
#include "ucbottomedge.h"
 
20
#include "ucbottomedge_p.h"
 
21
#include "ucbottomedgeregion.h"
 
22
#include "propertychange_p.h"
 
23
#include <QtQml/private/qqmlproperty_p.h>
 
24
 
 
25
/*!
 
26
 * \qmltype BottomEdgeRegion
 
27
 * \instantiates UCBottomEdgeRegion
 
28
 * \inherits QtObject
 
29
 * \inmodule Ubuntu.Components 1.3
 
30
 * \since Ubuntu.Components 1.3
 
31
 * \ingroup ubuntu
 
32
 * \brief Defines an active region within the BottomEdge component.
 
33
 *
 
34
 * Bottom edge regions are portions within the bottom edge area which can define
 
35
 * different content or action whenever the drag enters in the area. The area is
 
36
 * defined by \l from and \l to properties vertically, whereas horizontally is
 
37
 * stretched across bottom edge width. Custom content can be defined through
 
38
 * \l contentUrl or \l contentComponent properties, which will override the
 
39
 * \l BottomEdge::contentUrl and \l BottomEdge::contentComponent properties for the
 
40
 * time the gesture is in the section area.
 
41
 * \qml
 
42
 * import QtQuick 2.4
 
43
 * import Ubuntu.Components 1.3
 
44
 *
 
45
 * MainView {
 
46
 *     width: units.gu(40)
 
47
 *     height: units.gu(70)
 
48
 *
 
49
 *     Page {
 
50
 *         header: PageHeader {
 
51
 *             title: "BottomEdge regions"
 
52
 *         }
 
53
 *
 
54
 *         BottomEdge {
 
55
 *             id: bottomEdge
 
56
 *             height: parent.height - units.gu(20)
 
57
 *             hint: BottomEdgeHint {
 
58
 *                 text: "My bottom edge"
 
59
 *             }
 
60
 *             // a fake content till we reach the committable area
 
61
 *             contentComponent: Rectangle {
 
62
 *                 width: bottomEdge.width
 
63
 *                 height: bottomEdge.height
 
64
 *                 color: UbuntuColors.green
 
65
 *             }
 
66
 *             // override bottom edge sections to switch to real content
 
67
 *             BottomEdgeRegion {
 
68
 *                 from: 0.33
 
69
 *                 contentComponent: Page {
 
70
 *                     width: bottomEdge.width
 
71
 *                     height: bottomEdge.height
 
72
 *                     header: PageHeader {
 
73
 *                         title: "BottomEdge Content"
 
74
 *                     }
 
75
 *                 }
 
76
 *             }
 
77
 *         }
 
78
 *     }
 
79
 * }
 
80
 * \endqml
 
81
 *
 
82
 * Entering into the section area is signalled by the \l entered signal and when
 
83
 * drag leaves the area the \l exited signal is emitted. If the drag ends within
 
84
 * the section area, the \l dragEnded signal is emitted. In case the section's
 
85
 * \l to property is less than 1.0, the bottom edge content will only be exposed
 
86
 * to that value, and the \l BottomEdge::status will get the \e Committed value.
 
87
 * No further drag is possible after reaching \e Commited state.
 
88
 *
 
89
 * \note Whereas there is no restriction on making overlapping sections, beware that
 
90
 * overlapping sections changing the content through the \l contentUrl or \l contentComponent
 
91
 * properties will cause unpredictable results.
 
92
 */
 
93
 
 
94
UCBottomEdgeRegion::UCBottomEdgeRegion(QObject *parent)
 
95
    : QObject(parent)
 
96
    , m_bottomEdge(qobject_cast<UCBottomEdge*>(parent))
 
97
    , m_component(Q_NULLPTR)
 
98
    , m_urlBackup(Q_NULLPTR)
 
99
    , m_componentBackup(Q_NULLPTR)
 
100
    , m_from(0.0)
 
101
    , m_to(-1.0)
 
102
    , m_enabled(true)
 
103
{
 
104
}
 
105
 
 
106
void UCBottomEdgeRegion::attachToBottomEdge(UCBottomEdge *bottomEdge)
 
107
{
 
108
    QQml_setParent_noEvent(this, bottomEdge);
 
109
    m_bottomEdge = bottomEdge;
 
110
    // adjust to property value if not set yet
 
111
    if (m_to <= 0.0) {
 
112
        m_to = 1.0;
 
113
        Q_EMIT toChanged();
 
114
    }
 
115
}
 
116
 
 
117
bool UCBottomEdgeRegion::contains(qreal dragRatio)
 
118
{
 
119
    return (m_enabled && (m_from < m_to) && dragRatio >= m_from && dragRatio <= m_to);
 
120
}
 
121
 
 
122
void UCBottomEdgeRegion::enter()
 
123
{
 
124
    Q_EMIT entered();
 
125
    // backup url
 
126
    if (m_url.isValid()) {
 
127
        m_urlBackup = new PropertyChange(m_bottomEdge, "contentUrl");
 
128
        QQmlProperty property(this, "contentUrl", qmlContext(this));
 
129
        QQmlAbstractBinding *binding = QQmlPropertyPrivate::binding(property);
 
130
        if (binding) {
 
131
            PropertyChange::setBinding(m_urlBackup, binding);
 
132
        } else {
 
133
            PropertyChange::setValue(m_urlBackup, m_url);
 
134
        }
 
135
    }
 
136
    if (m_component) {
 
137
        m_componentBackup = new PropertyChange(m_bottomEdge, "contentComponent");
 
138
        QQmlProperty property(this, "contentComponent", qmlContext(this));
 
139
        QQmlAbstractBinding *binding = QQmlPropertyPrivate::binding(property);
 
140
        if (binding) {
 
141
            PropertyChange::setBinding(m_componentBackup, binding);
 
142
        } else {
 
143
            PropertyChange::setValue(m_componentBackup, QVariant::fromValue<QQmlComponent*>(m_component));
 
144
        }
 
145
    }
 
146
}
 
147
 
 
148
void UCBottomEdgeRegion::exit()
 
149
{
 
150
    if (m_componentBackup) {
 
151
        delete m_componentBackup;
 
152
        m_componentBackup = Q_NULLPTR;
 
153
    }
 
154
    if (m_urlBackup) {
 
155
        delete m_urlBackup;
 
156
        m_urlBackup = Q_NULLPTR;
 
157
    }
 
158
    Q_EMIT exited();
 
159
}
 
160
 
 
161
const QRectF UCBottomEdgeRegion::rect(const QRectF &bottomEdgeRect)
 
162
{
 
163
    QRectF regionRect(
 
164
                bottomEdgeRect.topLeft() + QPointF(0, bottomEdgeRect.height() * (1.0 - m_to)),
 
165
                QSizeF(bottomEdgeRect.width(), bottomEdgeRect.height() * (m_to - m_from)));
 
166
    return regionRect;
 
167
}
 
168
 
 
169
/*!
 
170
 * \qmlproperty bool BottomEdgeRegion::enabled
 
171
 * Enables the section. Disabled sections do not trigger nor change the BottomEdge
 
172
 * content. Defaults to false.
 
173
 */
 
174
void UCBottomEdgeRegion::setEnabled(bool enabled)
 
175
{
 
176
    if (enabled == m_enabled) {
 
177
        return;
 
178
    }
 
179
    m_enabled = enabled;
 
180
    if (m_bottomEdge) {
 
181
        UCBottomEdgePrivate::get(m_bottomEdge)->validateRegion(this);
 
182
    }
 
183
    Q_EMIT enabledChanged();
 
184
}
 
185
 
 
186
/*!
 
187
 * \qmlproperty real BottomEdgeRegion::from
 
188
 * Specifies the starting ratio of the bottom erge area. The value must be bigger
 
189
 * or equal to 0 but strictly smaller than \l to. Defaults to 0.0.
 
190
 */
 
191
void UCBottomEdgeRegion::setFrom(qreal from)
 
192
{
 
193
    if (from == m_from) {
 
194
        return;
 
195
    }
 
196
    m_from = from;
 
197
    if (m_bottomEdge) {
 
198
        UCBottomEdgePrivate::get(m_bottomEdge)->validateRegion(this);
 
199
    }
 
200
    Q_EMIT fromChanged();
 
201
}
 
202
 
 
203
/*!
 
204
 * \qmlproperty real BottomEdgeRegion::to
 
205
 * Specifies the ending ratio of the bottom edge area. The value must be bigger
 
206
 * than \l from and smaller or equal to 1.0.
 
207
 * \note If the end point is less than 1.0, ending the drag within the section
 
208
 * will result in exposing the bottom edge content only till the ration specified
 
209
 * by this property.
 
210
 */
 
211
void UCBottomEdgeRegion::setTo(qreal to)
 
212
{
 
213
    if (to == m_to) {
 
214
        return;
 
215
    }
 
216
    m_to = to;
 
217
    if (m_bottomEdge) {
 
218
        UCBottomEdgePrivate::get(m_bottomEdge)->validateRegion(this);
 
219
    }
 
220
    Q_EMIT toChanged();
 
221
}
 
222
 
 
223
/*!
 
224
 * \qmlproperty url BottomEdgeRegion::contentUrl
 
225
 * Specifies the url to the document defining the section specific content. This
 
226
 * propery will temporarily override the \l BottomEdge::contentUrl property value
 
227
 * when the drag gesture enters the section area. The orginal value will be restored
 
228
 * once the gesture leaves the section area.
 
229
 */
 
230
 
 
231
/*!
 
232
 * \qmlproperty Component BottomEdgeRegion::contentComponent
 
233
 * Specifies the component defining the section specific content. This propery
 
234
 * will temporarily override the \l BottomEdge::contentComponent property value
 
235
 * when the drag gesture enters the section area. The orginal value will be restored
 
236
 * once the gesture leaves the section area.
 
237
 */
 
238
 
 
239
/*!
 
240
 * \qmlsignal void BottomEdgeRegion::entered()
 
241
 * Signal triggered when the drag enters into the area defined by the bottom edge
 
242
 * section.
 
243
 */
 
244
 
 
245
/*!
 
246
 * \qmlsignal void BottomEdgeRegion::exited()
 
247
 * Signal triggered when the drag leaves the area defined by the bottom edge section.
 
248
 */
 
249
 
 
250
/*!
 
251
 * \qmlsignal void BottomEdgeRegion::dragEnded()
 
252
 * Signal triggered when the drag ends within the active bottom edge section area.
 
253
 */