Debian Codegraph @

src/imports/Layouts/plugin/propertychanges.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 "propertychanges_p.h"
20
21 #include <QtQml/QQmlInfo>
22 #include <QtQml/private/qqmlcontext_p.h>
23 #include <QtQuick/QQuickItem>
24 #include <QtQuick/private/qquickanchors_p.h>
25
26 #include "ullayouts_p.h"
27 #include "ullayouts.h"
28
29 /******************************************************************************
30 * PropertyAction
31 * Saves property state, applies target values or bindings and restores property
32 * saved values. Distinguishes Binding and Value types, which is relevant when
33 * the property is restored to its defaults.
34 */
35 PropertyAction::PropertyAction()
36 : type(Value)
37 , fromBinding(0)
38 , toValueSet(false)
39 , deleteFromBinding(false)
40 , deleteToBinding(false)
41 {
42 }
43
44 PropertyAction::PropertyAction(const PropertyAction &other)
45 : type(other.type)
46 , property(other.property)
47 , fromBinding(other.fromBinding)
48 , toBinding(other.toBinding)
49 , fromValue(other.fromValue)
50 , toValue(other.toValue)
51 , toValueSet(other.toValueSet)
52 , deleteFromBinding(other.deleteFromBinding)
53 , deleteToBinding(other.deleteToBinding)
54 {
55 }
56
57 PropertyAction::PropertyAction(QObject *item, const QString &name, Type type)
58 : type(type)
59 , property(item, name, qmlContext(item))
60 , fromBinding(QQmlPropertyPrivate::binding(property))
61 , fromValue(property.read())
62 , toValueSet(false)
63 , deleteFromBinding(false)
64 , deleteToBinding(false)
65 {
66 }
67
68 PropertyAction::PropertyAction(QObject *item, const QString &name, QQmlContext *context, const QVariant &value, Type type)
69 : type(type)
70 , property(item, name, context)
71 , fromBinding(QQmlPropertyPrivate::binding(property))
72 , fromValue(property.read())
73 , toValue(value)
74 , toValueSet(value.isValid())
75 , deleteFromBinding(false)
76 , deleteToBinding(false)
77 {
78 }
79
80 /*
81 * Set target value.
82 */
83 void PropertyAction::setValue(const QVariant &value)
84 {
85 toValue = value;
86 toValueSet = true;
87 }
88
89 /*
90 * Set target binding and whether it can be deleted upon reset.
91 */
92 void PropertyAction::setTargetBinding(QQmlAbstractBinding *binding, bool deletable)
93 {
94 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
95 toBinding = binding;
96 #else
97 toBinding = QQmlAbstractBinding::getPointer(binding);
98 #endif
99 deleteToBinding = deletable;
100 }
101
102
103 /*
104 * Apply property action by setting the target binding (toBinding) or by setting the
105 * target value if the value is set.
106 */
107 void PropertyAction::apply()
108 {
109 if (toBinding) {
110 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
111 QQmlAbstractBinding::Ptr binding(QQmlPropertyPrivate::binding(property));
112 QQmlPropertyPrivate::setBinding(property, toBinding.data());
113 #else
114 QQmlAbstractBinding *binding = QQmlPropertyPrivate::setBinding(property, toBinding.data());
115 #endif
116 if (binding != fromBinding || (binding == fromBinding && deleteFromBinding)) {
117 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
118 if (binding == fromBinding) {
119 fromBinding.reset();
120 fromBinding=0;
121 }
122 binding->removeFromObject();
123 #else
124 binding->destroy();
125 if (binding == fromBinding) {
126 fromBinding = 0;
127 }
128 #endif
129 }
130 } else if (toValueSet) {
131 if (!property.object()->setProperty(property.name().toLocal8Bit(), toValue)) {
132 qmlWarning(property.object()) << "Layouts: updating property \""
133 << property.name()
134 << "\" failed.";
135 }
136 }
137 }
138
139 /*
140 * Resets the hosted property and removes its bindings.
141 */
142 void PropertyAction::reset()
143 {
144 property.reset();
145 if (fromBinding) {
146 QQmlPropertyPrivate::setBinding(property, 0);
147 if (deleteFromBinding) {
148 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
149 fromBinding->removeFromObject();
150 fromBinding.reset();
151 #else
152 fromBinding->destroy();
153 fromBinding = 0;
154 #endif
155 deleteFromBinding = false;
156 }
157 }
158 }
159
160 /*
161 * Reverts the property to its defaults. Restores original bindings and in case
162 * of Value types restores their original values.
163 */
164 void PropertyAction::revert(bool reset)
165 {
166 if (reset) {
167 property.reset();
168 }
169 if (fromBinding) {
170 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
171 QQmlAbstractBinding::Ptr revertedBinding(QQmlPropertyPrivate::binding(property));
172 QQmlPropertyPrivate::setBinding(property, fromBinding.data());
173 #else
174 QQmlAbstractBinding *revertedBinding = QQmlPropertyPrivate::setBinding(property, fromBinding);
175 #endif
176
177 if (revertedBinding && (revertedBinding != fromBinding) && ((revertedBinding != toBinding.data()) || (revertedBinding == toBinding.data() && deleteToBinding))) {
178 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
179 revertedBinding->removeFromObject();
180 revertedBinding.reset();
181 #else
182 revertedBinding->destroy();
183 #endif
184 }
185 } else if (toBinding && QQmlPropertyPrivate::binding(property) == toBinding.data()) {
186 QQmlPropertyPrivate::setBinding(property, 0);
187 if (deleteToBinding) {
188 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
189 toBinding->removeFromObject();
190 toBinding.reset();
191 #else
192 toBinding.data()->destroy();
193 toBinding.clear();
194 #endif
195 deleteToBinding = false;
196 }
197 } else if (property.isValid() && fromValue.isValid() && (type == Value)) {
198 property.write(fromValue);
199 }
200 }
201
202 /******************************************************************************
203 * PropertyChange
204 * Default low priority change.
205 */
206 PropertyChange::PropertyChange(Priority priority)
207 : actionPriority(priority)
208 , resetOnRevert(true)
209 {
210 }
211
212 PropertyChange::PropertyChange(QQuickItem *target, const QString &property, const QVariant &value, Priority priority)
213 : actionPriority(priority)
214 , resetOnRevert(true)
215 , action(target, property, PropertyAction::Value)
216 {
217 if (value.isValid()) {
218 action.setValue(value);
219 }
220 }
221
222 PropertyChange::PropertyChange(QQuickItem *target, const QString &property, const QQmlScriptString &script, QQmlContext *scriptContext, Priority priority)
223 : actionPriority(priority)
224 , resetOnRevert(true)
225 , action(target, property, PropertyAction::Value)
226 {
227 if (!script.isEmpty()) {
228 bool ok = false;
229 qreal value = script.numberLiteral(&ok);
230 if (ok) {
231 action.setValue(value);
232 } else {
233 #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
234 QQmlBinding *binding = QQmlBinding::create(&QQmlPropertyPrivate::get(action.property)->core, script, target, scriptContext);
235 #else
236 QQmlBinding *binding = new QQmlBinding(script, target, scriptContext);
237 #endif
238 binding->setTarget(action.property);
239 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
240 action.toBinding = binding;
241 #else
242 action.toBinding = QQmlAbstractBinding::getPointer(binding);
243 #endif
244 action.deleteToBinding = true;
245 }
246 }
247 }
248
249 void PropertyChange::saveState()
250 {
251 }
252
253 void PropertyChange::apply()
254 {
255 action.apply();
256 }
257
258 void PropertyChange::revert()
259 {
260 action.revert(resetOnRevert);
261 }
262
263
264 /******************************************************************************
265 * PropertyBackup
266 */
267 PropertyBackup::PropertyBackup(QQuickItem *target, const QString &property)
268 : PropertyChange(target, property, QVariant(), High)
269 {
270 }
271
272
273 /******************************************************************************
274 * ParentChange
275 * Normal priority change, a PropertyChange reparenting an item to the target.
276 */
277 ParentChange::ParentChange(QQuickItem *item, QQuickItem *targetParent, bool topmostChild)
278 : PropertyChange(item, "parent", QVariant::fromValue(targetParent), Normal)
279 , newParent(targetParent)
280 , topmostChild(topmostChild)
281 {
282 }
283
284 void ParentChange::apply()
285 {
286 // get child items before reparenting
287 QList<QQuickItem*> items = newParent->childItems();
288
289 PropertyChange::apply();
290
291 if (topmostChild && (items.count() > 0)) {
292 // move the hosted item as topmost child
293 QQuickItem *item = static_cast<QQuickItem*>(action.property.object());
294 item->stackBefore(items[0]);
295 }
296 }
297
298 /******************************************************************************
299 * AnchorChange
300 * Low priority change for anchoring
301 */
302 AnchorChange::AnchorChange(QQuickItem *item, const QString &anchor, QQuickItem *target, const QString &targetAnchor)
303 : PropertyChange(item, "anchors." + anchor, QVariant())
304 , active(false)
305 {
306 QQuickAnchors *anchors = item->property("anchors").value<QQuickAnchors*>();
307 // check the special cases, like fill, centerIn
308 if (anchor != "fill" || (anchor == "fill" && !anchors->fill())) {
309 active = true;
310 if (targetAnchor.isEmpty()) {
311 action.setValue(QVariant::fromValue(target));
312 } else {
313 action.setValue(target->property(QString("anchors." + targetAnchor).toLocal8Bit()));
314 }
315 }
316 }
317
318 void AnchorChange::apply()
319 {
320 if (!active)
321 return;
322 PropertyChange::apply();
323 }
324
325 void AnchorChange::revert()
326 {
327 if (!active)
328 return;
329 PropertyChange::revert();
330 }
331
332 /******************************************************************************
333 * ItemStackBackup
334 * High priority change backing up the item's stack position.
335 */
336 ItemStackBackup::ItemStackBackup(QQuickItem *item)
337 : PropertyChange(High)
338 , target(item)
339 , prevItem(0)
340 {
341 }
342
343 void ItemStackBackup::saveState()
344 {
345 QQuickItem *rewindParent = target->parentItem();
346 if (!rewindParent) {
347 return;
348 }
349 // save original stack position, but detect layout objects!
350 QList<QQuickItem*> children = rewindParent->childItems();
351 int index = children.indexOf(target);
352 if (index > 0) {
353 prevItem = children.at(index - 1);
354 }
355 }
356
357 void ItemStackBackup::revert()
358 {
359 if (prevItem) {
360 target->stackAfter(prevItem);
361 }
362 }
363
364 /******************************************************************************
365 * AnchorBackup
366 * High priority change backing up item anchors and margins.
367 */
368 AnchorBackup::AnchorBackup(QQuickItem *item)
369 : PropertyChange(item, "anchors", QVariant(), High)
370 , anchorsObject(action.fromValue.value<QQuickAnchors*>())
371 , used(anchorsObject->usedAnchors())
372 {
373 if ((used & QQuickAnchors::LeftAnchor) == QQuickAnchors::LeftAnchor) {
374 actions << PropertyAction(item, "anchors.left")
375 << PropertyAction(item, "anchors.leftMargin", PropertyAction::Value);
376 }
377 if ((used & QQuickAnchors::RightAnchor) == QQuickAnchors::RightAnchor) {
378 actions << PropertyAction(item, "anchors.right")
379 << PropertyAction(item, "anchors.rightMargin", PropertyAction::Value);
380 }
381 if ((used & QQuickAnchors::TopAnchor) == QQuickAnchors::TopAnchor) {
382 actions << PropertyAction(item, "anchors.top")
383 << PropertyAction(item, "anchors.topMargin", PropertyAction::Value);
384 }
385 if ((used & QQuickAnchors::BottomAnchor) == QQuickAnchors::BottomAnchor) {
386 actions << PropertyAction(item, "anchors.bottom")
387 << PropertyAction(item, "anchors.bottomMargin", PropertyAction::Value);
388 }
389 if ((used & QQuickAnchors::HCenterAnchor) == QQuickAnchors::HCenterAnchor) {
390 actions << PropertyAction(item, "anchors.horizontalCenter")
391 << PropertyAction(item, "anchors.horizontalCenterOffset", PropertyAction::Value);
392 }
393 if ((used & QQuickAnchors::VCenterAnchor) == QQuickAnchors::VCenterAnchor) {
394 actions << PropertyAction(item, "anchors.verticalCenter")
395 << PropertyAction(item, "anchors.verticalCenterOffset", PropertyAction::Value);
396 }
397 if ((used & QQuickAnchors::BaselineAnchor) == QQuickAnchors::BaselineAnchor) {
398 actions << PropertyAction(item, "anchors.baseline")
399 << PropertyAction(item, "anchors.baselineOffset", PropertyAction::Value);
400 }
401
402 if (anchorsObject->fill()) {
403 actions << PropertyAction(item, "anchors.fill")
404 << PropertyAction(item, "anchors.margins", PropertyAction::Value);
405 }
406 if (anchorsObject->centerIn()) {
407 actions << PropertyAction(item, "anchors.centerIn")
408 << PropertyAction(item, "anchors.alignWhenCentered", PropertyAction::Value);
409 }
410 }
411
412 void AnchorBackup::saveState()
413 {
414 // no need to call superclass' saveState() as we don't touch the anchor property
415 // only its properties
416 }
417
418 void AnchorBackup::apply()
419 {
420 // reset all anchors
421 if (!used) {
422 return;
423 }
424
425 for (int i = 0; i < actions.count(); i++) {
426 actions[i].reset();
427 }
428 }
429
430 void AnchorBackup::revert()
431 {
432 // revert all anchors
433 if (!used) {
434 return;
435 }
436
437 for (int i = 0; i < actions.count(); i++) {
438 actions[i].revert(true);
439 }
440 }
441
442 /******************************************************************************
443 * ChangeList
444 */
445
446 ChangeList::~ChangeList()
447 {
448 clear();
449 }
450
451 void ChangeList::apply()
452 {
453 QList<PropertyChange*> list = unifiedChanges();
454 for (int i = 0; i < list.count(); i++) {
455 list[i]->apply();
456 }
457 }
458
459 void ChangeList::revert()
460 {
461 // reverse order of apply()
462 QList<PropertyChange*> list = unifiedChanges();
463 for (int i = list.count() - 1; i >= 0; i--) {
464 list[i]->revert();
465 }
466 }
467
468 void ChangeList::clear()
469 {
470 for (int priority = PropertyChange::High; priority < PropertyChange::MaxPriority; priority++) {
471 for (int change = 0; change < changes[priority].count(); change++) {
472 delete changes[priority][change];
473 }
474 changes[priority].clear();
475 }
476 }
477
478 ChangeList &ChangeList::addChange(PropertyChange *change)
479 {
480 if (change && (change->priority() < PropertyChange::MaxPriority)) {
481 change->saveState();
482 changes[change->priority()] << change;
483 }
484 return *this;
485 }
486
487 // creates two changes, one for reparenting and one for itemstack backup
488 ChangeList &ChangeList::addParentChange(QQuickItem *item, QQuickItem *newParent, bool topmostItem)
489 {
490 return addChange(new ParentChange(item, newParent, topmostItem))
491 .addChange(new ItemStackBackup(item));
492 }
493
494 QList<PropertyChange*> ChangeList::unifiedChanges()
495 {
496 QList<PropertyChange*> list;
497 for (int priority = PropertyChange::High; priority < PropertyChange::MaxPriority; priority++) {
498 list << changes[priority];
499 }
500 return list;
501 }