Debian Codegraph @

src/GUI/GettingStartedTipsController.cxx

raw symbols SCIP
1 #include "GettingStartedTipsController.hxx"
2
3 #include <algorithm>
4
5 #include <QSettings>
6 #include <QDebug>
7 #include <QQmlContext>
8 #include <QTimer>
9
10 #include <QtQml> // qmlContext
11
12 #include "GettingStartedTip.hxx"
13 #include "SettingsWrapper.hxx"
14 #include "TipBackgroundBox.hxx"
15
16 struct TipGeometryByArrowLocation
17 {
18 TipGeometryByArrowLocation(GettingStartedTip::Arrow a, const QRectF& g, Qt::Alignment al) :
19 arrow(a),
20 geometry(g),
21 verticalAlignment(al)
22 {
23 }
24
25 GettingStartedTip::Arrow arrow;
26 QRectF geometry;
27 // specify how vertical space is adjusted; is the top, bottom or center fixed
28 Qt::Alignment verticalAlignment = Qt::AlignVCenter;
29 };
30
31 const double tipBoxWidth = 300.0;
32 const double halfBoxWidth = tipBoxWidth * 0.5;
33 const double arrowSideOffset = TipBackgroundBox::arrowSideOffset();
34 const double rightSideOffset = -tipBoxWidth + arrowSideOffset;
35 const double dummyHeight = 200.0;
36 const double topHeightOffset = -TipBackgroundBox::arrowHeight();
37
38 static std::initializer_list<TipGeometryByArrowLocation> static_tipGeometries = {
39 {GettingStartedTip::Arrow::BottomRight, QRectF{rightSideOffset, 0.0, tipBoxWidth, dummyHeight}, Qt::AlignBottom},
40 {GettingStartedTip::Arrow::BottomCenter, QRectF{halfBoxWidth, 0.0, tipBoxWidth, dummyHeight}, Qt::AlignBottom},
41 {GettingStartedTip::Arrow::TopCenter, QRectF{-halfBoxWidth, 0.0, tipBoxWidth, dummyHeight}, Qt::AlignTop},
42 {GettingStartedTip::Arrow::TopRight, QRectF{rightSideOffset, 0.0, tipBoxWidth, dummyHeight}, Qt::AlignTop},
43 {GettingStartedTip::Arrow::TopLeft, QRectF{-arrowSideOffset, 0.0, tipBoxWidth, dummyHeight}, Qt::AlignTop},
44 {GettingStartedTip::Arrow::LeftCenter, QRectF{0.0, 0.0, tipBoxWidth, dummyHeight}, Qt::AlignVCenter},
45 {GettingStartedTip::Arrow::RightCenter, QRectF{-(tipBoxWidth + TipBackgroundBox::arrowHeight()), 0.0, tipBoxWidth, dummyHeight}, Qt::AlignVCenter},
46 {GettingStartedTip::Arrow::LeftTop, QRectF{0.0, topHeightOffset, tipBoxWidth, dummyHeight}, Qt::AlignTop},
47 {GettingStartedTip::Arrow::NoArrow, QRectF{-halfBoxWidth, 0.0, tipBoxWidth, dummyHeight}, Qt::AlignVCenter},
48
49 };
50
51 /**
52 * @brief The GettingStartedTipsController::ItemPositionObserver class
53 *
54 * This is a helper to observe the full position (and in the future, transform if required)
55 * of a QQuickItem, so we can update a signal when the on-screen position changes. This
56 * is necessary to re-transform the tooltip location if the item it's 'attached' to
57 * moves, or some ancestor does.
58 *
59 * At present this does not handle arbitrary scaling or rotation, but observing those
60 * signals would also be possible.
61 */
62 class GettingStartedTipsController::ItemPositionObserver : public QObject
63 {
64 Q_OBJECT
65 public:
66 ItemPositionObserver(QObject* pr) :
67 QObject(pr)
68 {
69 _notMovingTimeout = new QTimer(this);
70 _notMovingTimeout->setSingleShot(true);
71 _notMovingTimeout->setInterval(1000);
72
73 connect(this, SIGNAL(itemPositionChanged()),
74 _notMovingTimeout, SLOT(start()));
75
76 connect(_notMovingTimeout, &QTimer::timeout,
77 this, &ItemPositionObserver::itemNotMoving);
78 }
79
80 void setObservedItem(QQuickItem* obs)
81 {
82 if (obs == _observedItem)
83 return;
84
85 if (_observedItem) {
86 for (auto o = _observedItem; o; o = o->parentItem()) {
87 disconnect(o, nullptr, this, nullptr);
88 }
89 }
90
91 _observedItem = obs;
92 if (obs) {
93 startObserving(_observedItem);
94 }
95 }
96
97 bool hasRecentlyMoved() const
98 {
99 return _notMovingTimeout->isActive();
100 }
101 signals:
102 void itemPositionChanged();
103
104 void itemNotMoving();
105 private:
106 void startObserving(QQuickItem* obs)
107 {
108
109 connect(obs, &QQuickItem::xChanged, this, &ItemPositionObserver::itemPositionChanged);
110 connect(obs, &QQuickItem::yChanged, this, &ItemPositionObserver::itemPositionChanged);
111 connect(obs, &QQuickItem::widthChanged, this, &ItemPositionObserver::itemPositionChanged);
112 connect(obs, &QQuickItem::heightChanged, this, &ItemPositionObserver::itemPositionChanged);
113
114 // recurse up the item hierarchy
115 if (obs->parentItem()) {
116 startObserving(obs->parentItem());
117 }
118 }
119
120 QPointer<QQuickItem> _observedItem;
121 QTimer* _notMovingTimeout = nullptr;
122 };
123
124 // static used to ensure only one controller is active at a time
125 static QPointer<GettingStartedTipsController> static_activeController;
126
127 GettingStartedTipsController::GettingStartedTipsController(QObject *parent) : QObject(parent)
128 {
129 // observer for the tip item
130 _positionObserver = new ItemPositionObserver(this);
131 connect(_positionObserver, &ItemPositionObserver::itemPositionChanged,
132 this, &GettingStartedTipsController::tipPositionInVisualAreaChanged);
133
134 // observer for the visual area (which could also be scrolled)
135 _viewAreaObserver = new ItemPositionObserver(this);
136 connect(_viewAreaObserver, &ItemPositionObserver::itemPositionChanged,
137 this, &GettingStartedTipsController::tipPositionInVisualAreaChanged);
138
139 connect(_positionObserver, &ItemPositionObserver::itemNotMoving,
140 this, &GettingStartedTipsController::tipPositionInVisualAreaChanged);
141 connect(_viewAreaObserver, &ItemPositionObserver::itemNotMoving,
142 this, &GettingStartedTipsController::tipPositionInVisualAreaChanged);
143
144 auto qqParent = qobject_cast<QQuickItem*>(parent);
145 if (qqParent) {
146 setVisualArea(qqParent);
147 }
148 }
149
150 GettingStartedTipsController::~GettingStartedTipsController()
151 {
152 }
153
154 int GettingStartedTipsController::count() const
155 {
156 if (_oneShotTip) {
157 return 1;
158 }
159
160 return _tips.size();
161 }
162
163 int GettingStartedTipsController::index() const
164 {
165 if (_oneShotTip) {
166 return 0;
167 }
168
169 return _index;
170 }
171
172 GettingStartedTip *GettingStartedTipsController::tip() const
173 {
174 if (_oneShotTip) {
175 return _oneShotTip;
176 }
177
178 if (_tips.empty())
179 return nullptr;
180
181 if ((_index < 0) || (_index >= _tips.size()))
182 return nullptr;
183
184 return _tips.at(_index);
185 }
186
187 void GettingStartedTipsController::setVisualArea(QQuickItem *visualArea)
188 {
189 if (_visualArea == visualArea)
190 return;
191
192 _visualArea = visualArea;
193 _viewAreaObserver->setObservedItem(_visualArea);
194
195 emit tipPositionInVisualAreaChanged();
196 emit visualAreaChanged(_visualArea);
197 }
198
199 void GettingStartedTipsController::setActiveTipHeight(int activeTipHeight)
200 {
201 if (_activeTipHeight == activeTipHeight)
202 return;
203
204 _activeTipHeight = activeTipHeight;
205 emit activeTipHeightChanged(_activeTipHeight);
206 emit tipGeometryChanged();
207 }
208
209 void GettingStartedTipsController::showOneShotTip(GettingStartedTip *tip)
210 {
211 if (_scopeActive) {
212 return;
213 }
214
215 if (static_activeController != this) {
216 return;
217 }
218
219 auto settings = flightgear::getQSettings();
220 settings.beginGroup("GettingStarted-DontShow");
221 if (settings.value(tip->tipId()).toBool()) {
222 return;
223 }
224
225 // mark the tip as shown
226 settings.setValue(tip->tipId(), true);
227 _oneShotTip = tip;
228
229 connect(_oneShotTip, &QObject::destroyed, this, &GettingStartedTipsController::onOneShotDestroyed);
230
231 currentTipUpdated();
232 emit indexChanged(0);
233 emit countChanged(count());
234 }
235
236 void GettingStartedTipsController::tipsWereReset()
237 {
238 bool a = shouldShowScope();
239 if (a != _scopeActive) {
240 _scopeActive = a;
241 static_activeController = this; // we became active
242 emit activeChanged();
243 currentTipUpdated();
244 }
245 }
246
247 void GettingStartedTipsController::currentTipUpdated()
248 {
249 _positionObserver->setObservedItem(tip());
250
251 emit activeChanged();
252 emit tipChanged();
253 emit tipPositionInVisualAreaChanged();
254 emit tipGeometryChanged();
255 }
256
257 bool GettingStartedTipsController::addTip(GettingStartedTip *t)
258 {
259 if (_tips.contains(t)) {
260 qWarning() << Q_FUNC_INFO << "Duplicate tip" << t;
261 return false;
262 }
263
264 // this logic is important to suppress duplicate tips inside a ListView or Repeater;
265 // effectively, we only show a tip on the first registered instance.
266 Q_FOREACH(GettingStartedTip* tip, _tips) {
267 if (tip->tipId() == t->tipId()) {
268 return false;
269 }
270 }
271
272 _tips.append(t);
273 // order tips by nextTip ID, if defined
274 std::sort(_tips.begin(), _tips.end(), [](const GettingStartedTip* a, GettingStartedTip *b) {
275 return a->nextTip() == b->tipId();
276 });
277
278 currentTipUpdated();
279 emit countChanged(count());
280 return true;
281 }
282
283 void GettingStartedTipsController::removeTip(GettingStartedTip *t)
284 {
285 const bool removedActive = (tip() == t);
286 if (!_tips.removeOne(t)) {
287 qWarning() << Q_FUNC_INFO << "tip not found";
288 }
289
290 if (removedActive) {
291 _index = qMax(_index - 1, 0);
292 }
293
294 currentTipUpdated();
295 emit countChanged(count());
296 }
297
298 void GettingStartedTipsController::onOneShotDestroyed()
299 {
300 if (_oneShotTip == sender()) {
301 emit activeChanged();
302 currentTipUpdated();
303 }
304 }
305
306 bool GettingStartedTipsController::isActive() const
307 {
308 if (_oneShotTip)
309 return true;
310
311 return _scopeActive && !_tips.empty();
312 }
313
314 QPointF GettingStartedTipsController::tipPositionInVisualArea() const
315 {
316 auto t = tip();
317 if (!_visualArea || !t) {
318 return {};
319 }
320
321 return _visualArea->mapFromItem(t, QPointF{0,0});
322 }
323
324 QRectF GettingStartedTipsController::tipGeometry() const
325 {
326 auto t = tip();
327 if (!t)
328 return {};
329
330 const auto arrow = t->arrow();
331 auto it = std::find_if(static_tipGeometries.begin(), static_tipGeometries.end(),
332 [arrow](const TipGeometryByArrowLocation& tg)
333 {
334 return tg.arrow == arrow;
335 });
336
337 if (it == static_tipGeometries.end()) {
338 qWarning() << Q_FUNC_INFO << "Missing tip geometry" << arrow;
339 return {};
340 }
341
342 QRectF g = it->geometry;
343 if ((arrow == GettingStartedTip::Arrow::LeftCenter)
344 || (arrow == GettingStartedTip::Arrow::RightCenter)
345 || (arrow == GettingStartedTip::Arrow::LeftTop)
346 || (arrow == GettingStartedTip::Arrow::NoArrow))
347 {
348 g.setHeight(_activeTipHeight);
349 } else {
350 g.setHeight(_activeTipHeight + TipBackgroundBox::arrowHeight());
351 }
352
353 switch (it->verticalAlignment) {
354 case Qt::AlignBottom:
355 g.moveBottom(0);
356 break;
357
358 case Qt::AlignTop:
359 g.moveTop(0);
360 break;
361
362 case Qt::AlignVCenter:
363 g.moveTop(_activeTipHeight * -0.5);
364 break;
365 }
366
367 return g;
368 }
369
370 bool GettingStartedTipsController::tipPositionValid() const
371 {
372 if (!_visualArea || !isActive())
373 return false;
374
375 // hide tips when resizing the window or scrolling; it's visually distracting otherwise
376 if (_positionObserver->hasRecentlyMoved() || _viewAreaObserver->hasRecentlyMoved()) {
377 return false;
378 }
379
380 if (_oneShotTip)
381 return true;
382
383 return !_tips.empty();
384 }
385
386 int GettingStartedTipsController::activeTipHeight() const
387 {
388 return _activeTipHeight;
389 }
390
391 QRectF GettingStartedTipsController::contentGeometry() const
392 {
393 QRectF g(0.0, 0.0, tipBoxWidth, 200.0);
394
395 auto t = tip();
396 if (!t)
397 return g;
398
399 const auto arrow = t->arrow();
400 if ((arrow == GettingStartedTip::Arrow::TopCenter) ||
401 (arrow == GettingStartedTip::Arrow::TopLeft) ||
402 (arrow == GettingStartedTip::Arrow::TopRight))
403 {
404 g.moveTop(TipBackgroundBox::arrowHeight());
405 }
406
407 if (arrow == GettingStartedTip::Arrow::RightCenter) {
408 g.setWidth(tipBoxWidth - TipBackgroundBox::arrowHeight());
409 }
410
411 if (arrow == GettingStartedTip::Arrow::LeftCenter) {
412 g.setWidth(tipBoxWidth - TipBackgroundBox::arrowHeight());
413 g.moveLeft(TipBackgroundBox::arrowHeight());
414 }
415
416 return g;
417 }
418
419 void GettingStartedTipsController::close()
420 {
421 // one-shot tips handle this logic differently; we set the don't show
422 // when the tip first appears
423 if (_oneShotTip) {
424 disconnect(_oneShotTip, nullptr, this, nullptr);
425 _oneShotTip = nullptr;
426 static_activeController.clear();
427 } else {
428 if (_scopeActive) {
429 static_activeController.clear();
430 }
431
432 auto settings = flightgear::getQSettings();
433 settings.beginGroup("GettingStarted-DontShow");
434 settings.setValue(_scopeId, true);
435 _scopeActive = false;
436 }
437
438 emit activeChanged();
439 currentTipUpdated();
440 }
441
442 void GettingStartedTipsController::setIndex(int index)
443 {
444 if (_oneShotTip) {
445 return;
446 }
447
448 if (_index == index)
449 return;
450
451 _index = qBound(0, index, _tips.size() - 1);
452 _positionObserver->setObservedItem(tip());
453
454 emit indexChanged(_index);
455 currentTipUpdated();
456 }
457
458 void GettingStartedTipsController::setScopeId(QString scopeId)
459 {
460 if (_scopeId == scopeId)
461 return;
462
463 _scopeId = scopeId;
464 _scopeActive = shouldShowScope();
465 if (_scopeActive) {
466 static_activeController = this;
467 }
468 emit scopeIdChanged(_scopeId);
469 emit activeChanged();
470 }
471
472 bool GettingStartedTipsController::shouldShowScope() const
473 {
474 if (static_activeController && (static_activeController != this)) {
475 return false;
476 }
477
478 if (_scopeId.isEmpty())
479 return true;
480
481 auto settings = flightgear::getQSettings();
482 settings.beginGroup("GettingStarted-DontShow");
483 return settings.value(_scopeId).toBool() == false;
484 }
485
486
487 #include "GettingStartedTipsController.moc"