forked from SuzanneSoy/SearchBar
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRefreshTools.py
More file actions
141 lines (115 loc) · 4.87 KB
/
RefreshTools.py
File metadata and controls
141 lines (115 loc) · 4.87 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import os
import FreeCAD as App
import FreeCADGui as Gui
import StyleMapping_SearchBar
import FreeCADGui as Gui
from PySide.QtWidgets import QLabel, QProgressBar, QApplication
from PySide.QtCore import Qt, SIGNAL, Signal, QObject, QThread, QSize
from PySide.QtGui import QIcon, QPixmap, QAction, QGuiApplication
# Define the translation
translate = App.Qt.translate
# Define a QProgressBar as a counter dialog
progressBar = QProgressBar(minimum=0, value=0)
progressBar.setWindowFlags(Qt.WindowType.Dialog | Qt.WindowType.WindowStaysOnTopHint)
progressBar.setMinimumSize(300, 20)
# Get the stylesheet from the main window and use it for this form
progressBar.setStyleSheet("background-color: " + StyleMapping_SearchBar.ReturnStyleItem("Background_Color") + ";")
def loadAllWorkbenches():
import FreeCADGui as Gui
from PySide.QtWidgets import QLabel, QProgressBar, QApplication
from PySide.QtCore import Qt, SIGNAL, Signal, QObject, QThread, QSize
from PySide.QtGui import QIcon, QPixmap, QAction, QGuiApplication
activeWorkbench = Gui.activeWorkbench().name()
# # Define a QProgressBar as a counter dialog
# progressBar = QProgressBar(minimum=0, value=0)
# progressBar.setWindowFlags(Qt.WindowType.Dialog | Qt.WindowType.WindowStaysOnTopHint)
# progressBar.setMinimumSize(300, 20)
# # Get the stylesheet from the main window and use it for this form
# progressBar.setStyleSheet("background-color: " + StyleMapping_SearchBar.ReturnStyleItem("Background_Color") + ";")
# # Get the main window from FreeCAD
# mw = Gui.getMainWindow()
# # Center the widget
# cp = QGuiApplication.screenAt(mw.pos()).geometry().center()
# lbl.move(cp)
progressBar.show()
lst = Gui.listWorkbenches()
progressBar.setMaximum(len(lst)+1)
for i, wb in enumerate(lst):
msg = translate("SearchBar", "Loading workbench ") + wb + " (" + str(i + 1) + "/" + str(len(lst)) + ")"
print(msg)
progressBar.setFormat(msg)
progressBar.setValue(i)
Gui.updateGui() # Probably slower with this, because it redraws the entire GUI with all tool buttons changed etc. but allows the label to actually be updated, and it looks nice and gives a quick overview of all the workbenches…
try:
Gui.activateWorkbench(wb)
except Exception:
pass
# progressBar.close()
Gui.activateWorkbench(activeWorkbench)
return
def cachePath():
return os.path.join(App.getUserAppDataDir(), "Cache_SearchBarMod")
def gatherTools():
itemGroups = []
import SearchResults
for providerName, provider in SearchResults.resultProvidersCached.items():
itemGroups = itemGroups + provider()
return itemGroups
def writeCacheTools():
import Serialize_SearchBar
msg = translate("SearchBar", "Writing to cache ")
print(msg)
progressBar.setFormat(msg)
progressBar.setValue(progressBar.value()+1)
serializedItemGroups = Serialize_SearchBar.serialize(gatherTools())
# Todo: use wb and a specific encoding.
with open(cachePath(), "w") as cache:
cache.write(serializedItemGroups)
# I prefer to systematically deserialize, instead of taking the original version,
# this avoids possible inconsistencies between the original and the cache and
# makes sure cache-related bugs are noticed quickly.
import Serialize_SearchBar
itemGroups = Serialize_SearchBar.deserialize(serializedItemGroups)
progressBar.setValue(progressBar.value()+1)
print("SearchBox: Data file is created.")
progressBar.close()
return itemGroups
def readCacheTools():
# Todo: use rb and a specific encoding.
with open(cachePath(), "r") as cache:
serializedItemGroups = cache.read()
import Serialize_SearchBar
itemGroups = Serialize_SearchBar.deserialize(serializedItemGroups)
print("SearchBox: Tools are loaded.")
return itemGroups
def refreshToolbars(doLoadAllWorkbenches=True):
if doLoadAllWorkbenches:
loadAllWorkbenches()
return writeCacheTools()
else:
try:
return readCacheTools()
except:
return writeCacheTools()
def refreshToolsAction():
from PySide.QtWidgets import QApplication, QMessageBox
from PySide.QtCore import Qt
print("Refresh data file")
msgBox = QMessageBox()
msgBox.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)
# Get the main window from FreeCAD
mw = Gui.getMainWindow()
reply = msgBox.question(
mw,
translate("SearchBar", "Load all workbenches?"),
translate(
"SearchBar",
"""Load all workbenches? This can cause FreeCAD to become unstable. Please make sure you save your work first.\nIt is a advised to restart FreeCAD after this operation.""",
),
QMessageBox.Yes,
QMessageBox.No,
)
if reply == QMessageBox.Yes:
refreshToolbars()
else:
print("cancelled")