| 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 |
#ifndef PROPERTYCHANGES_P_H |
| 20 |
#define PROPERTYCHANGES_P_H |
| 21 |
|
| 22 |
#include <QtCore/QVariant> |
| 23 |
#include <QtQml/QQmlListProperty> |
| 24 |
#include <QJSValue> |
| 25 |
#define foreach Q_FOREACH //workaround to fix private includes |
| 26 |
#include <QtQml/private/qqmlbinding_p.h> // for QmlBinding |
| 27 |
#include <QtQml/private/qqmljavascriptexpression_p.h> |
| 28 |
#include <QtQml/private/qqmlproperty_p.h> |
| 29 |
#undef foreach |
| 30 |
#include <QtQuick/private/qquickanchors_p_p.h> |
| 31 |
#include <QtQuick/private/qquickstate_p.h> |
| 32 |
|
| 33 |
class PropertyAction |
| 34 |
{ |
| 35 |
public: |
| 36 |
enum Type {Binding, Value}; |
| 37 |
PropertyAction(const PropertyAction &other); |
| 38 |
PropertyAction(); |
| 39 |
PropertyAction(QObject *item, const QString &name, Type type = Binding); |
| 40 |
PropertyAction(QObject *item, const QString &name, QQmlContext *context, const QVariant &value, Type type = Value); |
| 41 |
|
| 42 |
void setValue(const QVariant &value); |
| 43 |
void setTargetBinding(QQmlAbstractBinding *binding, bool deletable); |
| 44 |
void apply(); |
| 45 |
void reset(); |
| 46 |
void revert(bool reset = false); |
| 47 |
|
| 48 |
// members |
| 49 |
Type type; |
| 50 |
QQmlProperty property; |
| 51 |
|
| 52 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
| 53 |
QQmlAbstractBinding::Ptr fromBinding; |
| 54 |
QQmlAbstractBinding::Ptr toBinding; |
| 55 |
#else |
| 56 |
QQmlAbstractBinding *fromBinding; |
| 57 |
QWeakPointer<QQmlAbstractBinding> toBinding; |
| 58 |
#endif |
| 59 |
|
| 60 |
QVariant fromValue; |
| 61 |
QVariant toValue; |
| 62 |
|
| 63 |
bool toValueSet:1; |
| 64 |
bool deleteFromBinding:1; |
| 65 |
bool deleteToBinding:1; |
| 66 |
}; |
| 67 |
|
| 68 |
class QQuickItem; |
| 69 |
class PropertyChange |
| 70 |
{ |
| 71 |
public: |
| 72 |
enum Priority { |
| 73 |
High, |
| 74 |
Normal, |
| 75 |
Low, |
| 76 |
MaxPriority |
| 77 |
}; |
| 78 |
|
| 79 |
PropertyChange(Priority priority); |
| 80 |
PropertyChange(QQuickItem *target, const QString &property, const QVariant &value, Priority priority = Low); |
| 81 |
PropertyChange(QQuickItem *target, const QString &property, const QQmlScriptString &script, QQmlContext *scriptContext, Priority priority = Low); |
| 82 |
virtual ~PropertyChange() {} |
| 83 |
|
| 84 |
virtual void saveState(); |
| 85 |
virtual void apply(); |
| 86 |
virtual void revert(); |
| 87 |
|
| 88 |
inline Priority priority() { return actionPriority; } |
| 89 |
inline QQmlProperty property() const |
| 90 |
{ |
| 91 |
return action.property; |
| 92 |
} |
| 93 |
inline PropertyAction &actionObject() |
| 94 |
{ |
| 95 |
return action; |
| 96 |
} |
| 97 |
inline void resetWhenReverted(bool doReset) |
| 98 |
{ |
| 99 |
resetOnRevert = doReset; |
| 100 |
} |
| 101 |
|
| 102 |
protected: |
| 103 |
Priority actionPriority; |
| 104 |
bool resetOnRevert; |
| 105 |
PropertyAction action; |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
class PropertyBackup: public PropertyChange |
| 110 |
{ |
| 111 |
public: |
| 112 |
PropertyBackup(QQuickItem *target, const QString &property); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
class ParentChange : public PropertyChange |
| 117 |
{ |
| 118 |
public: |
| 119 |
ParentChange(QQuickItem *item, QQuickItem *targetParent, bool topmostChild); |
| 120 |
|
| 121 |
void apply() override; |
| 122 |
private: |
| 123 |
QQuickItem *newParent; |
| 124 |
bool topmostChild; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
class AnchorChange : public PropertyChange |
| 129 |
{ |
| 130 |
public: |
| 131 |
AnchorChange(QQuickItem *item, const QString &anchor, QQuickItem *target, const QString &targetAnchor = QString()); |
| 132 |
|
| 133 |
void apply() override; |
| 134 |
void revert() override; |
| 135 |
private: |
| 136 |
bool active; |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
class ItemStackBackup : public PropertyChange |
| 141 |
{ |
| 142 |
public: |
| 143 |
ItemStackBackup(QQuickItem *item); |
| 144 |
void apply() override {} |
| 145 |
void revert() override; |
| 146 |
|
| 147 |
protected: |
| 148 |
void saveState() override; |
| 149 |
QQuickItem *target; |
| 150 |
QQuickItem *prevItem; |
| 151 |
private: |
| 152 |
friend class ULLayouts; |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
class QQuickAnchors; |
| 157 |
class AnchorBackup : public PropertyChange |
| 158 |
{ |
| 159 |
public: |
| 160 |
AnchorBackup(QQuickItem *item); |
| 161 |
|
| 162 |
void apply() override; |
| 163 |
void revert() override; |
| 164 |
protected: |
| 165 |
void saveState() override; |
| 166 |
|
| 167 |
enum Anchor{ |
| 168 |
Left = 0, |
| 169 |
Right, |
| 170 |
Top, |
| 171 |
Bottom, |
| 172 |
HCenter, |
| 173 |
VCenter, |
| 174 |
Baseline, |
| 175 |
MaxAnchor |
| 176 |
}; |
| 177 |
enum Margins{ |
| 178 |
Margins = 0, |
| 179 |
LeftMargin, |
| 180 |
RightMargin, |
| 181 |
TopMargin, |
| 182 |
BottomMargin, |
| 183 |
HCenterOffset, |
| 184 |
VCenterOffset, |
| 185 |
BaselineOffset, |
| 186 |
MaxMargins |
| 187 |
}; |
| 188 |
|
| 189 |
inline QQuickAnchors::Anchor anchor(Anchor id) |
| 190 |
{ |
| 191 |
return (QQuickAnchors::Anchor)(1 << (int)id); |
| 192 |
} |
| 193 |
|
| 194 |
QQuickAnchors *anchorsObject; |
| 195 |
QQuickAnchors::Anchors used; |
| 196 |
QList<PropertyAction> actions; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
class ULConditionalLayoutAttached; |
| 201 |
class ChangeList |
| 202 |
{ |
| 203 |
public: |
| 204 |
ChangeList(){} |
| 205 |
~ChangeList(); |
| 206 |
|
| 207 |
void apply(); |
| 208 |
void revert(); |
| 209 |
void clear(); |
| 210 |
|
| 211 |
ChangeList &addChange(PropertyChange *change); |
| 212 |
ChangeList &addParentChange(QQuickItem *item, QQuickItem *newParent, bool topmostItem); |
| 213 |
|
| 214 |
private: |
| 215 |
QList<PropertyChange*> changes[PropertyChange::MaxPriority]; |
| 216 |
QList<PropertyChange*> unifiedChanges(); |
| 217 |
}; |
| 218 |
|
| 219 |
#endif // PROPERTYCHANGES_P_H |