forked from cgalvan/InteractiveTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.py
More file actions
122 lines (95 loc) · 3.74 KB
/
Copy pathtutorial.py
File metadata and controls
122 lines (95 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Copyright (c) Contributors to the Open 3D Engine Project.
For complete copyright and license terms please see the LICENSE at the root of this distribution.
SPDX-License-Identifier: Apache-2.0 OR MIT
"""
import json
import os
# Class that describes a step in the tutorial
class TutorialStep:
def __init__(self, title, content, highlight_pattern=None, highlight_parent=None, highlight_index=0):
self.title = title
self.content = content
# The highlight pattern is a widget/item that will be highlighted
# for this particular step (can be None)
# This pattern will be passed to `pyside_utils`
# to find the widget/item. See its documentation for supported patterns
self.highlight_pattern = highlight_pattern
self.highlight_parent = highlight_parent
self.highlight_index = highlight_index
self.prev_step = None
self.next_step = None
# Method that will be called when the step starts
# A step class can override this method if they need
# to setup any special handling/listeners
def on_step_start(self):
pass
# Method that will be called after a step has ended
# A step class can override this method if they need
# to perform any cleanup or other tasks
def on_step_end(self):
pass
def get_title(self):
return self.title
def get_content(self):
return self.content
def get_highlight_pattern(self):
return self.highlight_pattern
def get_highlight_parent(self):
return self.highlight_parent
def get_highlight_index(self):
return self.highlight_index
# Top-level entry point for describing a tutorial which is made up of a series of steps
class Tutorial:
def __init__(self):
self.steps = []
self.title = ""
@classmethod
def create_from_json_file(cls, file_path):
# If the specified file path isn't an absolute path,
# try to resolve it locally
if not os.path.isabs(file_path):
local_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(local_dir, file_path)
# Read the json data in from the json file
f = open(file_path)
data = json.load(f)
# Create the new tutorial
new_tutorial = cls()
new_tutorial.title = data["title"]
# Add the tutorial steps
steps = data["steps"]
for step in steps:
title = step["title"]
content = step["content"]
highlight_pattern = step.get("highlight_pattern", None)
highlight_parent = step.get("highlight_parent", None)
highlight_index = step.get("highlight_index", None)
new_step = TutorialStep(title, content, highlight_pattern, highlight_parent, highlight_index)
new_tutorial.add_step(new_step)
return new_tutorial
# Method that will be called when the tutorial starts
# A tutorial class can override this method if they need
# to setup any special handling/listeners
def on_tutorial_start(self):
pass
# Method that will be called after a tutorial has ended
# A tutorial class can override this method if they need
# to perform any cleanup or other tasks
def on_tutorial_end(self):
pass
def get_title(self):
return self.title
def add_step(self, step):
# Set the step references for the current last step on the list
if self.steps:
last_step = self.steps[-1]
last_step.next_step = step
step.prev_step = last_step
self.steps.append(step)
def get_first_step(self):
if self.steps:
return self.steps[0]
return None
def get_steps(self):
return self.steps