~kalikiana/ubuntu-ui-toolkit/appsettings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Zsombor Egri <zsombor.egri@canonical.com>
 */

#define foreach Q_FOREACH
#include <private/qqmlbinding_p.h>
#undef foreach

#include "ulconditionallayout.h"
#include "ulconditionallayout_p.h"
#include "ullayouts.h"
#include "ullayouts_p.h"

ULConditionalLayoutPrivate::ULConditionalLayoutPrivate(ULConditionalLayout *qq) :
    q_ptr(qq),
    when(0),
    component(0)
{
}

ULLayouts *ULConditionalLayoutPrivate::layouts()
{
    Q_Q(ULConditionalLayout);
    return qobject_cast<ULLayouts*>(q->parent());
}

/*!
 * \qmltype ConditionalLayout
 * \instantiates ULConditionalLayout
 * \inqmlmodule Ubuntu.Layouts 1.0
 * \ingroup ubuntu-layouts
 * \brief ConditionalLayout defines the layout of a given form factor.
 *
 * ConditionalLayout is a non-visual element defining the actual layout to be applied
 * when a given condition evaluates to true. The condition can be any valid boolean
 * JavaScript expression.
 *
 * As Layouts can be nested, it is recommended to choose properties of the Layouts item,
 * often width and height, when defining the condition. Other elements like Screen
 * orientation can also be used to define the layout activation condition.
 *
 * There must be only a single component defined in the ConditionalLayout which will be
 * instantiated upon activation.
 *
 * Each conditional layout must be named with a string, which identifies the layout
 * when activated.
 *
 * \qml
 * Layouts {
 *     id: layouts
 *     width: units.gu(40)
 *     height: units.gu(40)
 *     layouts: [
 *         ConditionalLayout {
 *             name: "flow"
 *             when: layouts.width > units.gu(60)
 *             Flow {
 *                 anchors.fill: parent
 *                 spacing: units.dp(3)
 *                 flow: Flow.LeftToRight
 *                 ItemLayout {
 *                     item: "item1"
 *                     width: units.gu(30)
 *                     height: units.gu(20)
 *                 }
 *                 ItemLayout {
 *                     item: "item2"
 *                     width: units.gu(30)
 *                     height: units.gu(20)
 *                 }
 *                 ItemLayout {
 *                     item: "item3"
 *                     width: units.gu(30)
 *                     height: units.gu(20)
 *                 }
 *             }
 *         }
 *     ]
 *
 *     Column {
 *         spacing: units.dp(2)
 *         Button {
 *             text: "Button #1"
 *             Layouts.item: "item1"
 *         }
 *         Button {
 *             text: "Button #2"
 *             Layouts.item: "item2"
 *         }
 *         Button {
 *             text: "Button #3"
 *             Layouts.item: "item3"
 *         }
 *     }
 * }
 * \endqml
 *
 * All Items to be managed by the Layouts engine must identify themselves by setting
 * the Layouts.item attached property to a unique name (string). These unique names
 * are then used by the ItemLayout component, so can apply the new layout to the
 * correct Item.
 *
 * \sa {ItemLayout}
 */

ULConditionalLayout::ULConditionalLayout(QObject *parent) :
    QObject(parent),
    d_ptr(new ULConditionalLayoutPrivate(this))
{
}

ULConditionalLayout::~ULConditionalLayout()
{
}

/*!
 * \qmlproperty string ConditionalLayout::name
 * This property defines the name of the layout. Within the definition of Layouts,
 * these names should be unique for each layout.
 * \sa Layouts
 */
QString ULConditionalLayout::layoutName() const
{
    Q_D(const ULConditionalLayout);
    return d->name;
}
void ULConditionalLayout::setLayoutName(const QString &name)
{
    Q_D(ULConditionalLayout);
    if (name == d->name) {
        return;
    }
    d->name = name;
}

/*!
 * \qmlproperty bool ConditionalLayout::when
 * This property defines the condition that when evaluating to true, chooses this
 * ConditionalLayout to become the active layout.
 * When two ConditionalLayouts \b when condition is evaluated to true, the first
 * one declared in the layouts list is chosen.
 */
QQmlBinding *ULConditionalLayout::when() const
{
    Q_D(const ULConditionalLayout);
    return d->when;
}
void ULConditionalLayout::setWhen(QQmlBinding *when)
{
    Q_D(ULConditionalLayout);
    d->when = when;

    // re-layout
    ULLayouts *layouts = qobject_cast<ULLayouts*>(parent());
    if (layouts) {
        layouts->d_ptr->updateLayout();
    }
}

/*!
 * \qmlproperty Component ConditionalLayout::layout
 * \default
 * Default property holding the definition component of the layout. The component
 * will be instantiated once the condition evaluates to true.
 */
QQmlComponent *ULConditionalLayout::layout() const
{
    Q_D(const ULConditionalLayout);
    return d->component;
}
void ULConditionalLayout::setLayout(QQmlComponent *component)
{
    Q_D(ULConditionalLayout);
    d->component = component;
}