| 1 | #include "GettingStartedTip.hxx" |
| 2 | |
| 3 | #include "GettingStartedScope.hxx" |
| 4 | #include "GettingStartedTipsController.hxx" |
| 5 | |
| 6 | static bool static_globalEnableTips = true; |
| 7 | |
| 8 | GettingStartedTip::GettingStartedTip(QQuickItem *parent) : |
| 9 | QQuickItem(parent) |
| 10 | { |
| 11 | _enabled = static_globalEnableTips; |
| 12 | |
| 13 | setImplicitHeight(1); |
| 14 | setImplicitWidth(1); |
| 15 | |
| 16 | if (_enabled) { |
| 17 | registerWithScope(); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | GettingStartedTip::~GettingStartedTip() |
| 22 | { |
| 23 | if (_enabled) { |
| 24 | unregisterFromScope(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | GettingStartedTipsController *GettingStartedTip::controller() const |
| 29 | { |
| 30 | return _controller.data(); |
| 31 | } |
| 32 | |
| 33 | void GettingStartedTip::componentComplete() |
| 34 | { |
| 35 | if (_enabled && !_controller) { |
| 36 | registerWithScope(); |
| 37 | } |
| 38 | |
| 39 | QQuickItem::componentComplete(); |
| 40 | } |
| 41 | |
| 42 | void GettingStartedTip::setGlobalTipsEnabled(bool enable) |
| 43 | { |
| 44 | static_globalEnableTips = enable; |
| 45 | } |
| 46 | |
| 47 | void GettingStartedTip::setEnabled(bool enabled) |
| 48 | { |
| 49 | if (_enabled == enabled) |
| 50 | return; |
| 51 | |
| 52 | _enabled = enabled & static_globalEnableTips; |
| 53 | if (enabled) { |
| 54 | registerWithScope(); |
| 55 | } else { |
| 56 | unregisterFromScope(); |
| 57 | } |
| 58 | |
| 59 | emit enabledChanged(); |
| 60 | } |
| 61 | |
| 62 | void GettingStartedTip::showOneShot() |
| 63 | { |
| 64 | auto ctl = findController(); |
| 65 | if (ctl) { |
| 66 | ctl->showOneShotTip(this); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void GettingStartedTip::setStandalone(bool standalone) |
| 71 | { |
| 72 | if (_standalone == standalone) |
| 73 | return; |
| 74 | |
| 75 | _standalone = standalone; |
| 76 | emit standaloneChanged(_standalone); |
| 77 | |
| 78 | // re-register with our scope, which will also unregister us if |
| 79 | // necessary. |
| 80 | if (_enabled) { |
| 81 | registerWithScope(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void GettingStartedTip::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) |
| 86 | { |
| 87 | const bool isParentChanged = (change == ItemParentHasChanged); |
| 88 | if (isParentChanged && _enabled) { |
| 89 | unregisterFromScope(); |
| 90 | } |
| 91 | |
| 92 | QQuickItem::itemChange(change, value); |
| 93 | |
| 94 | if (isParentChanged && _enabled) { |
| 95 | registerWithScope(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void GettingStartedTip::registerWithScope() |
| 100 | { |
| 101 | if (_controller) { |
| 102 | unregisterFromScope(); |
| 103 | } |
| 104 | |
| 105 | if (_standalone) { |
| 106 | return; // standalone tips don't register |
| 107 | } |
| 108 | |
| 109 | auto ctl = findController(); |
| 110 | if (!ctl) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | bool ok = ctl->addTip(this); |
| 115 | if (ok) { |
| 116 | _controller = ctl; |
| 117 | emit controllerChanged(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | GettingStartedTipsController* GettingStartedTip::findController() |
| 122 | { |
| 123 | QQuickItem* pr = const_cast<QQuickItem*>(parentItem()); |
| 124 | if (!pr) { |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
| 128 | while (pr) { |
| 129 | auto sa = qobject_cast<GettingStartedScopeAttached*>(qmlAttachedPropertiesObject<GettingStartedScope>(pr, false)); |
| 130 | if (sa && sa->controller()) { |
| 131 | return sa->controller(); |
| 132 | } |
| 133 | |
| 134 | pr = pr->parentItem(); |
| 135 | } |
| 136 | |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
| 140 | void GettingStartedTip::unregisterFromScope() |
| 141 | { |
| 142 | if (_controller) { |
| 143 | _controller->removeTip(this); |
| 144 | _controller.clear(); |
| 145 | emit controllerChanged(); |
| 146 | } |
| 147 | } |