-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimTutorial.py
314 lines (271 loc) · 12.1 KB
/
BimTutorial.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# ***************************************************************************
# * *
# * Copyright (c) 2018 Yorik van Havre <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
from __future__ import print_function
"""This is the tutorial of the BIM workbench"""
import os
import FreeCAD
from BimTranslateUtils import *
html = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }</style></head><body>inserthere</body></html>"""
URL = "https://www.freecadweb.org/wiki/BIM_ingame_tutorial"
TESTINTERVAL = 1000 # interval between tests
class BIM_Tutorial:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_Tutorial.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_Tutorial", "BIM Tutorial"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Tutorial", "Starts or continues the BIM in-game tutorial"
),
}
def Activated(self):
import FreeCADGui
from PySide import QtCore, QtGui
# find existing tutorial
m = FreeCADGui.getMainWindow()
self.dock = m.findChild(QtGui.QDockWidget, "BIMTutorial")
if not self.dock:
# set the tutorial dialog up
self.form = FreeCADGui.PySideUic.loadUi(
os.path.join(os.path.dirname(__file__), "dialogTutorial.ui")
)
self.form.setObjectName("BIMTutorial")
self.form.progressBar.setValue(0)
self.form.labelGoal1.setText("")
self.form.labelGoal2.setText("")
self.form.labelIcon1.setText("")
self.form.labelIcon2.setText("")
self.form.buttonPrevious.setEnabled(False)
self.form.buttonNext.setEnabled(False)
self.form.buttonPrevious.clicked.connect(self.previous)
self.form.buttonNext.clicked.connect(self.next)
self.form.labelTasks.hide()
self.form.textEdit.setOpenExternalLinks(True)
self.dock = QtGui.QDockWidget()
self.dock.setWidget(self.form)
m.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dock)
# load icons
self.pixyes = QtGui.QPixmap(":/icons/button_valid.svg").scaled(16, 16)
self.pixno = QtGui.QPixmap(":/icons/button_right.svg").scaled(16, 16)
self.pixempty = QtGui.QPixmap()
# fire the loading after displaying the widget
from DraftGui import todo
# self.load()
# todo.delay(self.load,None)
QtCore.QTimer.singleShot(1000, self.load)
def load(self, arg=None):
import re, sys, codecs
if sys.version_info.major < 3:
import urllib2
else:
import urllib.request as urllib2
# initial loading
if not hasattr(self, "form") or not self.form or not hasattr(self, "dock"):
return
# load tutorial from wiki
offlineloc = os.path.join(
FreeCAD.getUserAppDataDir(), "BIM", "Tutorial", "Tutorial.html"
)
try:
u = urllib2.urlopen(URL)
html = u.read()
if sys.version_info.major >= 3:
html = html.decode("utf8")
html = html.replace("\n", " ")
html = html.replace('"/wiki/', '"https://www.freecadweb.org/wiki/')
html = re.sub(
'<div id="toc".*?</ul> </div>', "", html
) # remove table of contents
u.close()
except:
# unable to load tutorial. Look for offline version
if os.path.exists(offlineloc):
f = open(offlineloc)
html = f.read()
f.close()
else:
FreeCAD.Console.PrintError(
translate(
"BIM",
"Unable to access the tutorial. Verify that you are online (This is needed only once).",
)
+ "\n"
)
return
else:
if not os.path.exists(os.path.dirname(offlineloc)):
os.makedirs(os.path.dirname(offlineloc))
f = codecs.open(offlineloc, "w", "utf-8")
f.write(html)
f.close()
# setup title and progress bar
self.steps = len(re.findall("infotext", html)) - 1
# setup description texts and goals
self.descriptions = [""] + re.findall(
"<p><br /> </p><p><br /> </p> (.*?)<p><b>Tutorial step", html
)
self.goal1 = re.findall('goal1">(.*?)</div', html)
self.goal2 = re.findall('goal2">(.*?)</div', html)
self.test1 = re.findall('test1".*?>(.*?)</div', html)
self.test2 = re.findall('test2".*?>(.*?)</div', html)
# fix mediawiki encodes
self.test1 = [t.replace("<", "<").replace(">", ">") for t in self.test1]
self.test2 = [t.replace("<", "<").replace(">", ">") for t in self.test2]
# download images (QTextEdit cannot load online images)
self.form.textEdit.setHtml(
html.replace("inserthere", translate("BIM", "Downloading images..."))
)
nd = []
for descr in self.descriptions:
imagepaths = re.findall('<img.*?src="(.*?)"', descr)
if imagepaths:
store = os.path.join(FreeCAD.getUserAppDataDir(), "BIM", "Tutorial")
if not os.path.exists(store):
os.makedirs(store)
for path in imagepaths:
# name = re.findall("[\\w.-]+\\.(?i)(?:jpg|png|gif|bmp)",path)
name = re.findall("(?i)[\\w.-]+\\.(?:jpg|png|gif|bmp)", path)
if name:
name = name[-1]
storename = os.path.join(store, name)
if not os.path.exists(storename):
if path.startswith("/images"):
# relative path
fullpath = "https://www.freecadweb.org/wiki" + path
else:
fullpath = path
u = urllib2.urlopen(fullpath)
imagedata = u.read()
f = open(storename, "wb")
f.write(imagedata)
f.close()
u.close()
# descr = descr.replace(path,"file://"+storename.replace("\\","/"))
# fix for windows - seems to work everywhere else too...
descr = descr.replace(
path, "file:///" + storename.replace("\\", "/")
)
else:
print("unparsable image path:", path)
nd.append(descr)
self.descriptions = nd
# check where we are
self.step = FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Mod/BIM"
).GetInt("CurrentTutorialStep", 0)
if self.step:
self.update()
else:
self.next()
def next(self):
self.step += 1
self.update()
def previous(self):
self.step -= 1
self.update()
def update(self):
from PySide import QtCore, QtGui
if not hasattr(self, "form") or not self.form or not hasattr(self, "dock"):
return
# stay within bounds!
if self.step > self.steps:
self.step = self.steps
elif self.step < 1:
self.step = 1
t = self.descriptions[self.step]
# print(t)
# set contents
self.form.textEdit.setHtml(html.replace("inserthere", t))
self.form.labelGoal1.setText(self.goal1[self.step])
if self.goal1[self.step]:
self.form.labelIcon1.setPixmap(self.pixno)
else:
self.form.labelIcon1.setPixmap(self.pixempty)
self.form.labelGoal2.setText(self.goal2[self.step])
if self.goal2[self.step]:
self.form.labelIcon2.setPixmap(self.pixno)
else:
self.form.labelIcon2.setPixmap(self.pixempty)
if self.goal1[self.step] or self.goal2[self.step]:
self.form.labelTasks.show()
else:
self.form.labelTasks.hide()
self.dock.setWindowTitle(
translate("BIM", "BIM Tutorial - step")
+ " "
+ str(self.step)
+ " / "
+ str(self.steps)
)
self.form.progressBar.setValue(int((float(self.step) / self.steps) * 100))
# save the current step
FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM").SetInt(
"CurrentTutorialStep", self.step
)
if self.steps > self.step:
self.form.buttonNext.setEnabled(True)
else:
self.form.buttonNext.setEnabled(False)
if self.step == 1:
self.form.buttonPrevious.setEnabled(False)
else:
self.form.buttonPrevious.setEnabled(True)
# launch test watcher
self.done1 = False
self.done2 = False
if self.test1[self.step] or self.test2[self.step]:
QtCore.QTimer.singleShot(TESTINTERVAL, self.checkGoals)
def checkGoals(self):
from PySide import QtCore, QtGui
if not hasattr(self, "form"):
return
if self.goal1[self.step]:
if self.test1[self.step]:
if not self.done1:
try:
result = eval(self.test1[self.step])
except:
print("BIM Tutorial: unable to eval: " + self.test1[self.step])
result = False
self.done1 = True
if result:
self.form.labelIcon1.setPixmap(self.pixyes)
self.done1 = True
if self.goal2[self.step]:
if self.test2[self.step]:
if not self.done2:
try:
result = eval(self.test2[self.step])
except:
print("BIM Tutorial: unable to eval: " + self.test2[self.step])
result = False
self.done2 = True
if result:
self.form.labelIcon2.setPixmap(self.pixyes)
self.done2 = True
if (self.test1[self.step] or self.test2[self.step]) and (
(not self.done1) or (not self.done2)
):
QtCore.QTimer.singleShot(TESTINTERVAL, self.checkGoals)