forked from oddtopus/dodo
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdodoDialogs.py
More file actions
307 lines (285 loc) · 12 KB
/
dodoDialogs.py
File metadata and controls
307 lines (285 loc) · 12 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
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
# SPDX-License-Identifier: LGPL-3.0-or-later
import csv
from os import listdir
from os.path import abspath, dirname, join
import FreeCAD
import FreeCADGui
from PySide.QtCore import *
from PySide.QtGui import *
from pFeatures import Flange
translate = FreeCAD.Qt.translate
try:
import quetzal_units as qu
except Exception:
qu = None # graceful fallback if module not yet present
class protoTypeDialog(object):
"prototype for dialogs.ui with callback function"
def __init__(self, dialog="anyFile.ui"):
dialogPath = join(dirname(abspath(__file__)), "dialogz", dialog)
self.form = FreeCADGui.PySideUic.loadUi(dialogPath)
### new shortcuts procedure
self.mw = FreeCADGui.getMainWindow()
for act in self.mw.findChildren(QAction):
if act.objectName() in ["actionX", "actionS"]:
self.mw.removeAction(act)
self.actionX = QAction(self.mw)
self.actionX.setObjectName("actionX") # define X action
self.actionX.setShortcut(QKeySequence("X"))
self.actionX.triggered.connect(self.accept)
self.mw.addAction(self.actionX)
self.actionS = QAction(self.mw)
self.actionS.setObjectName("actionS") # define S action
self.actionS.setShortcut(QKeySequence("S"))
self.actionS.triggered.connect(self.selectAction)
self.mw.addAction(self.actionS)
self.actionESC = QAction(self.mw)
FreeCAD.Console.PrintMessage(
translate("protoTypeDialog", '"%s" to select; "%s" to execute')
% (
self.actionS.shortcuts()[0].toString(),
self.actionX.shortcuts()[0].toString(),
)
+ "\r\n"
)
try:
self.view = FreeCADGui.activeDocument().activeView()
self.call = self.view.addEventCallback(
"SoMouseButtonEvent", self.action
) # SoKeyboardEvents replaced by QAction'
except:
FreeCAD.Console.PrintError(translate("protoTypeDialog", "No view available."))
def action(self, arg):
'Defines functions executed by the callback self.call when "SoMouseButtonEvent"'
# SoKeyboardEvents replaced by QAction':
CtrlAltShift = [arg["CtrlDown"], arg["AltDown"], arg["ShiftDown"]]
if arg["Button"] == "BUTTON1" and arg["State"] == "DOWN":
self.mouseActionB1(CtrlAltShift)
elif arg["Button"] == "BUTTON2" and arg["State"] == "DOWN":
self.mouseActionB2(CtrlAltShift)
elif arg["Button"] == "BUTTON3" and arg["State"] == "DOWN":
self.mouseActionB3(CtrlAltShift)
def selectAction(self):
"MUST be redefined in the child class"
print('"selectAction" performed')
pass
def mouseActionB1(self, CtrlAltShift):
"MUST be redefined in the child class"
pass
def mouseActionB2(self, CtrlAltShift):
"MUST be redefined in the child class"
pass
def mouseActionB3(self, CtrlAltShift):
"MUST be redefined in the child class"
pass
def reject(self):
"CAN be redefined to remove other attributes, such as arrow()s or label()s"
self.mw.removeAction(self.actionX)
self.mw.removeAction(self.actionS)
FreeCAD.Console.PrintMessage(
translate("protoTypeDialog", 'Actions "%s" and "%s" removed')
% (self.actionX.objectName(), self.actionS.objectName())
+ "\r\n"
)
try:
self.view.removeEventCallback("SoMouseButtonEvent", self.call)
except:
pass
FreeCADGui.Control.closeDialog()
if FreeCAD.ActiveDocument:
FreeCAD.ActiveDocument.recompute()
class protoPypeForm(QDialog):
"prototype dialog for insert pFeatures"
def __init__(
self,
winTitle="Title",
PType="Pipe",
PRating="SCH-STD",
icon="dodo.svg",
x=100,
y=350,
):
"""
__init__(self,winTitle='Title', PType='Pipe', PRating='SCH-STD')
winTitle: the window's title
PType: the pipeFeature type
PRating: the pipeFeature pressure rating class
It lookups in the directory ./tablez the file PType+"_"+PRating+".csv",
imports it's content in a list of dictionaries -> .pipeDictList and
shows a summary in the QListWidget -> .sizeList
Also create a property -> PRatingsList with the list of available PRatings for the
selected PType.
"""
super(protoPypeForm, self).__init__()
self.move(QPoint(x, y))
self.mw = FreeCADGui.getMainWindow()
self.PType = PType
self.PRating = PRating
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.setWindowTitle(winTitle)
iconPath = join(dirname(abspath(__file__)), "iconz", icon)
from PySide.QtGui import QIcon
Icon = QIcon()
Icon.addFile(iconPath)
self.setWindowIcon(Icon)
self.mainHL = QHBoxLayout()
self.setLayout(self.mainHL)
self.firstCol = QWidget()
self.firstCol.setLayout(QVBoxLayout())
self.mainHL.addWidget(self.firstCol)
self.currentRatingLab = QLabel(translate("protoPypeForm", "Rating: ") + self.PRating)
self.firstCol.layout().addWidget(self.currentRatingLab)
# DN / NPS toggle row
self._sizeSystemRow = QWidget()
self._sizeSystemRow.setLayout(QHBoxLayout())
self._sizeSystemRow.layout().setContentsMargins(0, 0, 0, 0)
self._sizeSystemRow.layout().setSpacing(4)
self._sizeSystemRow.layout().addWidget(
QLabel(translate("protoPypeForm", "Size:")))
self._btnDN = QPushButton("DN")
self._btnNPS = QPushButton("NPS")
self._btnDN.setCheckable(True)
self._btnNPS.setCheckable(True)
self._btnDN.setFlat(True)
self._btnNPS.setFlat(True)
_ss_active = "font-weight:bold; text-decoration:underline;"
_ss_inactive = ""
if qu and qu.get_size_system() == 1:
self._btnDN.setChecked(False)
self._btnNPS.setChecked(True)
self._btnDN.setStyleSheet(_ss_inactive)
self._btnNPS.setStyleSheet(_ss_active)
else:
self._btnDN.setChecked(True)
self._btnNPS.setChecked(False)
self._btnDN.setStyleSheet(_ss_active)
self._btnNPS.setStyleSheet(_ss_inactive)
self._sizeSystemRow.layout().addWidget(self._btnDN)
self._sizeSystemRow.layout().addWidget(self._btnNPS)
self._sizeSystemRow.layout().addStretch()
self.firstCol.layout().addWidget(self._sizeSystemRow)
self._btnDN.clicked.connect(lambda: self._setSizeSystem(0))
self._btnNPS.clicked.connect(lambda: self._setSizeSystem(1))
self.sizeList = QListWidget()
self.firstCol.layout().addWidget(self.sizeList)
self.pipeDictList = []
self.fileList = listdir(join(dirname(abspath(__file__)), "tablez"))
self.fillSizes()
self.PRatingsList = [
s.lstrip(PType + "_").rstrip(".csv")
for s in self.fileList
if s.startswith(PType) and s.endswith(".csv")
]
self.secondCol = QWidget()
self.secondCol.setLayout(QVBoxLayout())
self.combo = QComboBox()
self.combo.addItem(translate("protoPypeForm","<none>"))
try:
self.combo.addItems(
[
o.Label
for o in FreeCAD.activeDocument().Objects
if hasattr(o, "PType") and o.PType == "PypeLine"
]
)
except:
None
self.combostandart = QComboBox()
try:
asmeflag = False
dinflag = False
apiflag = False
asflag = False
bsflag = False
isoflag = False
for fstadart in self.fileList:
if fstadart.startswith("Flange_ASME") and asmeflag == False:
self.combostandart.addItem("ANSI/ASME")
asmeflag = True
elif fstadart.startswith("Flange_DIN") and dinflag == False:
self.combostandart.addItem("DIN")
dinflag = True
elif fstadart.startswith("Flange_API") and apiflag == False:
self.combostandart.addItem("API")
apiflag = True
elif fstadart.startswith("Flange_AS") and asflag == False:
self.combostandart.addItem("AS")
asflag = True
elif fstadart.startswith("Flange_BS") and bsflag == False:
self.combostandart.addItem("BS")
bsflag = True
elif fstadart.startswith("Flange_ISO") and isoflag == False:
self.combostandart.addItem("ISO")
isoflag = True
#TODO:Still doing some work here in order to sort standarts search
except Exception as e:
None
self.combo.currentIndexChanged.connect(self.setCurrentPL)
if FreeCAD.__activePypeLine__ and FreeCAD.__activePypeLine__ in [
self.combo.itemText(i) for i in range(self.combo.count())
]:
self.combo.setCurrentIndex(self.combo.findText(FreeCAD.__activePypeLine__))
self.secondCol.layout().addWidget(self.combostandart)
self.secondCol.layout().addWidget(self.combo)
self.ratingList = QListWidget()
self.ratingList.addItems(self.PRatingsList)
self.ratingList.itemClicked.connect(self.changeRating)
self.ratingList.setCurrentRow(0)
self.secondCol.layout().addWidget(self.ratingList)
self.btn1 = QPushButton(translate("protoPypeForm", "Insert"))
self.secondCol.layout().addWidget(self.btn1)
self.mainHL.addWidget(self.secondCol)
self.resize(max(350, int(self.mw.width() / 4)), max(350, int(self.mw.height() / 2)))
self.mainHL.setContentsMargins(0, 0, 0, 0)
def setCurrentPL(self, PLName=None):
if self.combo.currentText() not in ["<none>", "<new>"]:
FreeCAD.__activePypeLine__ = self.combo.currentText()
else:
FreeCAD.__activePypeLine__ = None
def fillSizes(self):
self.sizeList.clear()
for fileName in self.fileList:
if fileName == self.PType + "_" + self.PRating + ".csv":
f = open(join(dirname(abspath(__file__)), "tablez", fileName), "r")
reader = csv.DictReader(f, delimiter=";")
self.pipeDictList = [DNx for DNx in reader]
f.close()
for row in self.pipeDictList:
if qu:
s = qu.format_size_label(row)
else:
s = row["PSize"]
if "OD" in row.keys():
s += " - " + row["OD"]
if "thk" in row.keys():
s += "x" + row["thk"]
self.sizeList.addItem(s)
break
def changeRating(self, item):
self.PRating = item.text()
self.currentRatingLab.setText(translate("protoPypeForm", "Rating: ") + self.PRating)
self.fillSizes()
def _setSizeSystem(self, system):
"""Toggle the DN/NPS display on the size list without saving to prefs."""
_ss_active = "font-weight:bold; text-decoration:underline;"
_ss_inactive = ""
if system == 1:
self._btnDN.setChecked(False)
self._btnNPS.setChecked(True)
self._btnDN.setStyleSheet(_ss_inactive)
self._btnNPS.setStyleSheet(_ss_active)
else:
self._btnDN.setChecked(True)
self._btnNPS.setChecked(False)
self._btnDN.setStyleSheet(_ss_active)
self._btnNPS.setStyleSheet(_ss_inactive)
if qu:
# Temporarily override the preference for this session
qu.set_size_system(system)
self.fillSizes()
def findDN(self, DN):
result = None
for row in self.pipeDictList:
if row["PSize"] == DN:
result = row
break
return result