Maki
Formally verified low-code orchestration
Loading...
Searching...
No Matches
tour_guide.h
1#pragma once
2
3#include <QPointer>
4#include <QString>
5#include <QWidget>
6
7#include "logging.h"
8
9class QGraphicsView;
10class QGraphicsItem;
11class TourBubble;
12class TourOverlay;
13
14namespace tour
15{
16static const QString QUICK_TOUR_CAPABILTY = "Siren";
17static const QString QUICK_TOUR_EVENT = "start";
18static const QString QUICK_TOUR_NODE = "Capability Node";
19static const QString QUICK_TOUR_NAME = "MyTask";
20} // namespace tour
21
23{
24 static const int Valid = INT32_MAX;
25 static const int Invalid = -1;
26
27 enum class AdvanceOn
28 {
29 Button,
30 UserAction
31 };
32
33 int id = -1; // stable, never changes after creation
34
35 // Either target != nullptr (widget), or view+item != nullptr (scene item).
36 QPointer<QWidget> target;
37 QGraphicsView* view = nullptr;
38 QGraphicsItem* item = nullptr;
39
40 QString title;
41 QString text;
42
43 AdvanceOn advanceOn = AdvanceOn::Button;
44 int fallbackId = -1; // UserAction failure jumps here (-1 = stay)
45 bool allowBack = true;
46
47 bool isAlive() const;
48};
49
50class TourGuide : public QObject
51{
52 Q_OBJECT
53public:
54 explicit TourGuide(QWidget* topLevel, QObject* parent = nullptr);
55 ~TourGuide() override;
56
57 // --- building the pipeline (append) ------------------------------------
58 int addStep(QWidget* target, const QString& title, const QString& text, TourStep::AdvanceOn advanceOn = TourStep::AdvanceOn::Button, int fallbackId = -1,
59 int expectedId = -1);
60 int addItemStep(QGraphicsView* view, QGraphicsItem* item, const QString& title, const QString& text,
61 TourStep::AdvanceOn advanceOn = TourStep::AdvanceOn::Button, int fallbackId = -1, int expectedId = -1);
62
63 void removeStep(int stepId);
64
65 int insertStep(int beforeId, QWidget* target, const QString& title, const QString& text, TourStep::AdvanceOn advanceOn, int fallbackId, int expectedId);
66 int insertItemStep(int beforeId, QGraphicsView* view, QGraphicsItem* item, const QString& title, const QString& text, TourStep::AdvanceOn advanceOn,
67 int fallbackId, int expectedId);
68
69 template <typename Sender, typename Signal>
70 void connectTriggerSuccess(Sender* sender, Signal signal, std::function<int(const int currentId)> condition)
71 {
72 if (!sender)
73 {
74 LOG_ERROR("Tour trigger: null sender");
75 return;
76 }
77
78 if (!condition)
79 {
80 LOG_ERROR("Tour trigger: null condition");
81 return;
82 }
83
84 QObject::connect(sender, signal, this, [this, condition] {
85 LOG_INFO("Trigger success: {} with {}", m_active, m_currentId);
86 if (!m_active)
87 return;
88
89 auto stepId = condition(m_currentId);
90 if (stepId != TourStep::Invalid)
91 reportStepOutcome(true);
92 });
93 }
94
95 template <typename Sender, typename Signal>
96 void connectTriggerFailure(Sender* sender, Signal signal, std::function<int(const int currentId)> condition)
97 {
98 if (!condition)
99 {
100 LOG_ERROR("Tour trigger: null condition");
101 return;
102 }
103
104 QObject::connect(sender, signal, this, [this, condition] {
105 LOG_INFO("Trigger failure: {} with {}", m_active);
106 if (!m_active)
107 return;
108
109 auto stepId = condition(m_currentId);
110 if (stepId != TourStep::Invalid)
111 {
112 LOG_INFO("Trying to go to step: {}", stepId);
113 goTo(stepId);
114 }
115 });
116 }
117
118 void reportStepOutcome(bool success);
119
120 void start(int startId = -1);
121 void stop();
122 void cancel();
123
124 void goTo(int stepId);
125 void advance();
126 void back();
127
128 void setStepCount(int steps);
129 int currentStepId() const;
130 bool isActive() const;
131
132 bool eventFilter(QObject* watched, QEvent* event) override;
133
134signals:
135 void finished();
136 void cancelled();
137 void stepChanged(int stepId);
138
139private:
140 QWidget* m_window;
141 TourOverlay* m_overlay;
142 TourBubble* m_bubble;
143 QList<TourStep> m_steps;
144 int m_nextId = 0;
145 int m_currentId = -1; // current position tracked BY ID
146 bool m_active = false;
147 int mStepCount = -1;
148
149 int appendStep(QWidget* target, QGraphicsView* view, QGraphicsItem* item, const QString& title, const QString& text, TourStep::AdvanceOn advanceOn,
150 int fallbackId, int expectedId);
151 int insertStep(int beforeId, QGraphicsView* view, QGraphicsItem* item, QWidget* target, const QString& title, const QString& text,
152 TourStep::AdvanceOn advanceOn, int fallbackId, int expectedId);
153
154 int indexOf(int stepId) const;
155 void showStep(const TourStep& step);
156 void positionBubble();
157};
Definition tour_bubble.h:16
Definition tour_overlay.h:13
Definition tour_guide.h:23