-
Notifications
You must be signed in to change notification settings - Fork 9
/
osafe_statusbar.py
129 lines (99 loc) · 4.6 KB
/
osafe_statusbar.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
import os
import FreeCAD
from osafe_translate_utils import *
# Language path for InitGui.py
def getLanguagePath():
return os.path.join(os.path.dirname(__file__),"translations")
# Status bar buttons
def setStatusIcons(show=True):
"shows or hides the BIM icons in the status bar"
import FreeCADGui
from PySide import QtCore,QtGui
def addonMgr():
mw = FreeCADGui.getMainWindow()
if mw:
st = mw.statusBar()
statuswidget = st.findChild(QtGui.QToolBar,"OSAFEStatusWidget")
if statuswidget:
updatebutton = statuswidget.findChild(QtGui.QPushButton,"UpdateButton")
if updatebutton:
statuswidget.actions()[-1].setVisible(False)
FreeCADGui.runCommand("Std_AddonMgr")
class CheckWorker(QtCore.QThread):
updateAvailable = QtCore.Signal(bool)
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
try:
import git
except ImportError:
return
FreeCAD.Console.PrintLog("Checking for available updates of the OSAFE workbench\n")
osafe_dir = os.path.join(FreeCAD.getUserAppDataDir(),"Mod","OSAFE")
etabs_api_dir = os.path.join(FreeCAD.getUserAppDataDir(),"Mod","etabs_api")
for directory in (osafe_dir, etabs_api_dir):
if os.path.exists(directory) and os.path.exists(directory + os.sep + '.git'):
gitrepo = git.Git(directory)
try:
gitrepo.fetch()
if "git pull" in gitrepo.status():
self.updateAvailable.emit(True)
return
except:
# can fail for any number of reasons, ex. not being online
pass
self.updateAvailable.emit(False)
def checkUpdates():
FreeCAD.osafe_update_checker = CheckWorker()
FreeCAD.osafe_update_checker.updateAvailable.connect(showUpdateButton)
FreeCAD.osafe_update_checker.start()
def showUpdateButton(avail):
if avail:
FreeCAD.Console.PrintLog("An OSAFE update is available\n")
mw = FreeCADGui.getMainWindow()
if mw:
st = mw.statusBar()
statuswidget = st.findChild(QtGui.QToolBar,"OSAFEStatusWidget")
if statuswidget:
updatebutton = statuswidget.findChild(QtGui.QPushButton,"UpdateButton")
if updatebutton:
# updatebutton.show() # doesn't work for some reason
statuswidget.actions()[-1].setVisible(True)
else:
FreeCAD.Console.PrintLog("No OSAFE update available\n")
if hasattr(FreeCAD,"osafe_update_checker"):
del FreeCAD.osafe_update_checker
# main code
mw = FreeCADGui.getMainWindow()
if mw:
st = mw.statusBar()
statuswidget = st.findChild(QtGui.QToolBar,"OSAFEStatusWidget")
if show:
if statuswidget:
statuswidget.show()
else:
statuswidget = QtGui.QToolBar()
statuswidget.setObjectName("OSAFEStatusWidget")
# update notifier button (starts hidden)
updatebutton = QtGui.QPushButton()
bwidth = updatebutton.fontMetrics().boundingRect("AAAA").width()
updatebutton.setObjectName("UpdateButton")
updatebutton.setMaximumWidth(bwidth)
updatebutton.setIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__),"images","update.svg")))
updatebutton.setText("")
updatebutton.setToolTip(translate("osafe","An update to the OSAFE workbench is available. Click here to open the addons manager."))
updatebutton.setFlat(True)
QtCore.QObject.connect(updatebutton,QtCore.SIGNAL("pressed()"),addonMgr)
updatebutton.hide()
statuswidget.addWidget(updatebutton)
st.addPermanentWidget(statuswidget)
QtCore.QTimer.singleShot(2500, checkUpdates) # delay a bit the check for osafe WB update...
else:
if statuswidget:
statuswidget.hide()
else:
# when switching workbenches, the toolbar sometimes "jumps"
# out of the status bar to any other dock area...
statuswidget = mw.findChild(QtGui.QToolBar,"OSAFEStatusWidget")
if statuswidget:
statuswidget.hide()