Debian Codegraph @

src/imports/Layouts/plugin/ulconditionallayout.cpp

raw
1 /*
2 * Copyright 2013 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 <[email protected]>
17 */
18
19 #include "ulconditionallayout_p.h"
20
21 #include "ullayouts_p.h"
22
23 ULConditionalLayoutPrivate::ULConditionalLayoutPrivate(ULConditionalLayout *qq) :
24 q_ptr(qq),
25 when(false),
26 component(0)
27 {
28 }
29
30 ULLayouts *ULConditionalLayoutPrivate::layouts()
31 {
32 Q_Q(ULConditionalLayout);
33 return qobject_cast<ULLayouts*>(q->parent());
34 }
35
36 /*!
37 * \qmltype ConditionalLayout
38 * \instantiates ULConditionalLayout
39 * \inqmlmodule Lomiri.Layouts 1.0
40 * \ingroup lomiri-layouts
41 * \brief ConditionalLayout defines the layout of a given form factor.
42 *
43 * ConditionalLayout is a non-visual element defining the actual layout to be applied
44 * when a given condition evaluates to true. The condition can be any valid boolean
45 * JavaScript expression.
46 *
47 * As Layouts can be nested, it is recommended to choose properties of the Layouts item,
48 * often width and height, when defining the condition. Other elements like Screen
49 * orientation can also be used to define the layout activation condition.
50 *
51 * There must be only a single component defined in the ConditionalLayout which will be
52 * instantiated upon activation.
53 *
54 * Each conditional layout must be named with a string, which identifies the layout
55 * when activated.
56 *
57 * \qml
58 * Layouts {
59 * id: layouts
60 * width: units.gu(40)
61 * height: units.gu(40)
62 * layouts: [
63 * ConditionalLayout {
64 * name: "flow"
65 * when: layouts.width > units.gu(60)
66 * Flow {
67 * anchors.fill: parent
68 * spacing: units.dp(3)
69 * flow: Flow.LeftToRight
70 * ItemLayout {
71 * item: "item1"
72 * width: units.gu(30)
73 * height: units.gu(20)
74 * }
75 * ItemLayout {
76 * item: "item2"
77 * width: units.gu(30)
78 * height: units.gu(20)
79 * }
80 * ItemLayout {
81 * item: "item3"
82 * width: units.gu(30)
83 * height: units.gu(20)
84 * }
85 * }
86 * }
87 * ]
88 *
89 * Column {
90 * spacing: units.dp(2)
91 * Button {
92 * text: "Button #1"
93 * Layouts.item: "item1"
94 * }
95 * Button {
96 * text: "Button #2"
97 * Layouts.item: "item2"
98 * }
99 * Button {
100 * text: "Button #3"
101 * Layouts.item: "item3"
102 * }
103 * }
104 * }
105 * \endqml
106 *
107 * All Items to be managed by the Layouts engine must identify themselves by setting
108 * the Layouts.item attached property to a unique name (string). These unique names
109 * are then used by the ItemLayout component, so can apply the new layout to the
110 * correct Item.
111 *
112 * \sa {ItemLayout}
113 */
114
115 ULConditionalLayout::ULConditionalLayout(QObject *parent) :
116 QObject(parent),
117 d_ptr(new ULConditionalLayoutPrivate(this))
118 {
119 }
120
121 ULConditionalLayout::~ULConditionalLayout()
122 {
123 }
124
125 /*!
126 * \qmlproperty string ConditionalLayout::name
127 * This property defines the name of the layout. Within the definition of Layouts,
128 * these names should be unique for each layout.
129 * \sa Layouts
130 */
131 QString ULConditionalLayout::layoutName() const
132 {
133 Q_D(const ULConditionalLayout);
134 return d->name;
135 }
136 void ULConditionalLayout::setLayoutName(const QString &name)
137 {
138 Q_D(ULConditionalLayout);
139 if (name == d->name) {
140 return;
141 }
142 d->name = name;
143 }
144
145 /*!
146 * \qmlproperty bool ConditionalLayout::when
147 * This property defines the condition that when evaluating to true, chooses this
148 * ConditionalLayout to become the active layout.
149 * When two ConditionalLayouts \b when condition is evaluated to true, the first
150 * one declared in the layouts list is chosen.
151 */
152 bool ULConditionalLayout::when() const
153 {
154 Q_D(const ULConditionalLayout);
155 return d->when;
156 }
157 void ULConditionalLayout::setWhen(bool when)
158 {
159 Q_D(ULConditionalLayout);
160 d->when = when;
161
162 // re-layout
163 ULLayouts *layouts = qobject_cast<ULLayouts*>(parent());
164 if (layouts) {
165 layouts->d_ptr->updateLayout();
166 }
167 }
168
169 /*!
170 * \qmlproperty Component ConditionalLayout::layout
171 * \default
172 * Default property holding the definition component of the layout. The component
173 * will be instantiated once the condition evaluates to true.
174 */
175 QQmlComponent *ULConditionalLayout::layout() const
176 {
177 Q_D(const ULConditionalLayout);
178 return d->component;
179 }
180 void ULConditionalLayout::setLayout(QQmlComponent *component)
181 {
182 Q_D(ULConditionalLayout);
183 d->component = component;
184 }